/**
 * 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.journal.uad.anonymizer;

import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.journal.model.JournalFeed;
import com.liferay.journal.service.JournalFeedLocalService;
import com.liferay.journal.uad.constants.JournalUADConstants;
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 journal feed 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 JournalFeedUADAnonymizer}.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @generated
 */
public abstract class BaseJournalFeedUADAnonymizer
	extends DynamicQueryUADAnonymizer<JournalFeed> {

	@Override
	public void autoAnonymize(
			JournalFeed journalFeed, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(journalFeed, anonymousUser);
		}

		journalFeedLocalService.updateJournalFeed(journalFeed);
	}

	@Override
	public void delete(JournalFeed journalFeed) throws PortalException {
		journalFeedLocalService.deleteFeed(journalFeed);
	}

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

	protected void autoAnonymizeAssetEntry(
		JournalFeed journalFeed, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(journalFeed);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

	@Override
	protected String[] doGetUserIdFieldNames() {
		return JournalUADConstants.USER_ID_FIELD_NAMES_JOURNAL_FEED;
	}

	protected AssetEntry fetchAssetEntry(JournalFeed journalFeed) {
		return assetEntryLocalService.fetchEntry(
			JournalFeed.class.getName(), journalFeed.getId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected JournalFeedLocalService journalFeedLocalService;

}