/**
 * 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.DDLRecord;
import com.liferay.dynamic.data.lists.service.DDLRecordLocalService;
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.DynamicQueryUADAnonymizer;

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

/**
 * Provides the base implementation for the ddl record UAD anonymizer.
 *
 * <p>
 * This implementation exists only as a container for the default methods
 * generated by ServiceBuilder. All custom service methods should be put in
 * {@link DDLRecordUADAnonymizer}.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @generated
 */
public abstract class BaseDDLRecordUADAnonymizer
	extends DynamicQueryUADAnonymizer<DDLRecord> {

	@Override
	public void autoAnonymize(
			DDLRecord ddlRecord, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(ddlRecord, anonymousUser);
		}

		if (ddlRecord.getVersionUserId() == userId) {
			ddlRecord.setVersionUserId(anonymousUser.getUserId());
			ddlRecord.setVersionUserName(anonymousUser.getFullName());
		}

		ddlRecordLocalService.updateDDLRecord(ddlRecord);
	}

	@Override
	public void delete(DDLRecord ddlRecord) throws PortalException {
		ddlRecordLocalService.deleteRecord(ddlRecord);
	}

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

	protected void autoAnonymizeAssetEntry(
		DDLRecord ddlRecord, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(ddlRecord);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

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

	protected AssetEntry fetchAssetEntry(DDLRecord ddlRecord) {
		return assetEntryLocalService.fetchEntry(
			DDLRecord.class.getName(), ddlRecord.getRecordId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected DDLRecordLocalService ddlRecordLocalService;

}