/**
 * 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.JournalFolder;
import com.liferay.journal.service.JournalFolderLocalService;
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 JournalFolderUADAnonymizer extends BaseJournalFolderUADAnonymizer {

	@Override
	public void autoAnonymize(
			JournalFolder journalFolder, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(journalFolder, anonymousUser);
		}

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

		journalFolderLocalService.updateJournalFolder(journalFolder);
	}

	@Override
	public void delete(JournalFolder journalFolder) throws PortalException {
		journalFolderLocalService.deleteFolder(journalFolder);
	}

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

	protected void autoAnonymizeAssetEntry(
		JournalFolder journalFolder, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(journalFolder);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

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

	protected AssetEntry fetchAssetEntry(JournalFolder journalFolder) {
		return assetEntryLocalService.fetchEntry(
			JournalFolder.class.getName(), journalFolder.getFolderId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected JournalFolderLocalService journalFolderLocalService;

}