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

import com.liferay.asset.kernel.model.AssetEntry;
import com.liferay.asset.kernel.service.AssetEntryLocalService;
import com.liferay.calendar.model.CalendarBooking;
import com.liferay.calendar.service.CalendarBookingLocalService;
import com.liferay.calendar.uad.constants.CalendarUADConstants;
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 Eduardo Lundgren
 */
@Component(service = UADAnonymizer.class)
public class CalendarBookingUADAnonymizer
	extends BaseCalendarBookingUADAnonymizer {

	@Override
	public void autoAnonymize(
			CalendarBooking calendarBooking, long userId, User anonymousUser)
		throws PortalException {

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

			autoAnonymizeAssetEntry(calendarBooking, anonymousUser);
		}

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

		calendarBookingLocalService.updateCalendarBooking(calendarBooking);
	}

	@Override
	public void delete(CalendarBooking calendarBooking) throws PortalException {
		calendarBookingLocalService.deleteCalendarBooking(calendarBooking);
	}

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

	protected void autoAnonymizeAssetEntry(
		CalendarBooking calendarBooking, User anonymousUser) {

		AssetEntry assetEntry = fetchAssetEntry(calendarBooking);

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

			assetEntryLocalService.updateAssetEntry(assetEntry);
		}
	}

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

	@Override
	protected String[] doGetUserIdFieldNames() {
		return CalendarUADConstants.USER_ID_FIELD_NAMES_CALENDAR_BOOKING;
	}

	protected AssetEntry fetchAssetEntry(CalendarBooking calendarBooking) {
		return assetEntryLocalService.fetchEntry(
			CalendarBooking.class.getName(),
			calendarBooking.getCalendarBookingId());
	}

	@Reference
	protected AssetEntryLocalService assetEntryLocalService;

	@Reference
	protected CalendarBookingLocalService calendarBookingLocalService;

}