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

import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.microblogs.model.MicroblogsEntry;
import com.liferay.microblogs.service.MicroblogsEntryLocalService;
import com.liferay.microblogs.uad.constants.MicroblogsUADConstants;
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 MicroblogsEntryUADAnonymizer
	extends BaseMicroblogsEntryUADAnonymizer {

	@Override
	public void autoAnonymize(
			MicroblogsEntry microblogsEntry, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(microblogsEntry, anonymousUser);
		}

		if (microblogsEntry.getCreatorClassPK() == userId) {
			microblogsEntry.setCreatorClassPK(anonymousUser.getUserId());
		}

		microblogsEntryLocalService.updateMicroblogsEntry(microblogsEntry);
	}

	@Override
	public void delete(MicroblogsEntry microblogsEntry) throws PortalException {
		microblogsEntryLocalService.deleteMicroblogsEntry(microblogsEntry);
	}

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

	protected void autoAnonymizeAssetEntry(
		MicroblogsEntry microblogsEntry, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(microblogsEntry);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

	@Override
	protected String[] doGetUserIdFieldNames() {
		return MicroblogsUADConstants.USER_ID_FIELD_NAMES_MICROBLOGS_ENTRY;
	}

	protected AssetEntry fetchAssetEntry(MicroblogsEntry microblogsEntry) {
		return assetEntryLocalService.fetchEntry(
			MicroblogsEntry.class.getName(),
			microblogsEntry.getMicroblogsEntryId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected MicroblogsEntryLocalService microblogsEntryLocalService;

}