/**
 * 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.JournalArticle;
import com.liferay.journal.service.JournalArticleLocalService;
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.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 JournalArticleUADAnonymizer
	extends BaseJournalArticleUADAnonymizer {

	@Override
	public void autoAnonymize(
			JournalArticle journalArticle, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(journalArticle, anonymousUser);
		}

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

		journalArticleLocalService.updateJournalArticle(journalArticle);
	}

	@Override
	public void delete(JournalArticle journalArticle) throws PortalException {
		journalArticleLocalService.deleteArticle(journalArticle);
	}

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

	protected void autoAnonymizeAssetEntry(
		JournalArticle journalArticle, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(journalArticle);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

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

	@Override
	protected AssetEntry fetchAssetEntry(JournalArticle journalArticle) {
		return assetEntryLocalService.fetchEntry(
			JournalArticle.class.getName(),
			journalArticle.getResourcePrimKey());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected JournalArticleLocalService journalArticleLocalService;

}