/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.dynamic.data.lists.uad.anonymizer;

import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.dynamic.data.lists.model.DDLRecordVersion;
import com.liferay.dynamic.data.lists.service.DDLRecordVersionLocalService;
import com.liferay.dynamic.data.lists.uad.constants.DDLUADConstants;
import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.User;
import com.liferay.user.associated.data.anonymizer.UADAnonymizer;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Brian Wing Shun Chan
 */
@Component(service = UADAnonymizer.class)
public class DDLRecordVersionUADAnonymizer
	extends BaseDDLRecordVersionUADAnonymizer {

	@Override
	public void autoAnonymize(
			DDLRecordVersion ddlRecordVersion, long userId, User anonymousUser)
		throws PortalException {

		if (ddlRecordVersion.getUserId() == userId) {
			ddlRecordVersion.setUserId(anonymousUser.getUserId());
			ddlRecordVersion.setUserName(anonymousUser.getFullName());

			autoAnonymizeAssetEntry(ddlRecordVersion, anonymousUser);
		}

		if (ddlRecordVersion.getStatusByUserId() == userId) {
			ddlRecordVersion.setStatusByUserId(anonymousUser.getUserId());
			ddlRecordVersion.setStatusByUserName(anonymousUser.getFullName());
		}

		ddlRecordVersionLocalService.updateDDLRecordVersion(ddlRecordVersion);
	}

	@Override
	public void delete(DDLRecordVersion ddlRecordVersion)
		throws PortalException {

		ddlRecordVersionLocalService.deleteDDLRecordVersion(ddlRecordVersion);
	}

	@Override
	public Class<DDLRecordVersion> getTypeClass() {
		return DDLRecordVersion.class;
	}

	protected void autoAnonymizeAssetEntry(
		DDLRecordVersion ddlRecordVersion, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(ddlRecordVersion);

		if (assetEntry != null) {
			assetEntry.setUserId(anonymousUser.getUserId());
			assetEntry.setUserName(anonymousUser.getFullName());

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

	@Override
	protected ActionableDynamicQuery doGetActionableDynamicQuery() {
		return ddlRecordVersionLocalService.getActionableDynamicQuery();
	}

	@Override
	protected String[] doGetUserIdFieldNames() {
		return DDLUADConstants.USER_ID_FIELD_NAMES_DDL_RECORD_VERSION;
	}

	protected AssetEntry fetchAssetEntry(DDLRecordVersion ddlRecordVersion) {
		return assetEntryLocalService.fetchEntry(
			DDLRecordVersion.class.getName(),
			ddlRecordVersion.getRecordVersionId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected DDLRecordVersionLocalService ddlRecordVersionLocalService;

}