/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription 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.portal.backgroundtask.messaging;

import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.Order;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
import com.liferay.portal.kernel.dao.orm.Property;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.messaging.BaseMessageListener;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.model.BackgroundTask;
import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
import com.liferay.portal.service.persistence.BackgroundTaskActionableDynamicQuery;
import com.liferay.portal.util.PropsValues;

import java.util.Date;
import java.util.List;

/**
 * @author Gergely Mathe
 */
public class BackgroundTaskCleanUpMessageListener extends BaseMessageListener {

	protected void addCommonCriterions(DynamicQuery dynamicQuery) {
		Property completedProperty = PropertyFactoryUtil.forName("completed");

		dynamicQuery.add(completedProperty.eq(Boolean.TRUE));
	}

	@Override
	protected void doReceive(Message message)
		throws PortalException, SystemException {

		if (PropsValues.BACKGROUND_TASK_CLEAN_UP_KEEP_COUNT == -1) {
			return;
		}

		final Date lastCreateDate;

		if (PropsValues.BACKGROUND_TASK_CLEAN_UP_KEEP_COUNT == 0) {
			lastCreateDate = new Date();
		}
		else {
			DynamicQuery dynamicQuery =
				BackgroundTaskLocalServiceUtil.dynamicQuery();

			addCommonCriterions(dynamicQuery);

			Order order = OrderFactoryUtil.desc("createDate");

			dynamicQuery.addOrder(order);

			dynamicQuery.setLimit(
				QueryUtil.ALL_POS,
				PropsValues.BACKGROUND_TASK_CLEAN_UP_KEEP_COUNT);

			dynamicQuery.setProjection(
				ProjectionFactoryUtil.property("createDate"));

			List<Date> createDates =
				BackgroundTaskLocalServiceUtil.dynamicQuery(dynamicQuery);

			if (ListUtil.isEmpty(createDates)) {
				return;
			}

			lastCreateDate = createDates.get(createDates.size() - 1);
		}

		ActionableDynamicQuery actionableDynamicQuery =
			new BackgroundTaskActionableDynamicQuery() {

			final Property createDate = PropertyFactoryUtil.forName(
				"createDate");

			@Override
			public void addCriteria(DynamicQuery dynamicQuery) {
				addCommonCriterions(dynamicQuery);

				dynamicQuery.add(createDate.lt(lastCreateDate));
			}

			@Override
			public void performAction(Object object)
				throws PortalException, SystemException {

				BackgroundTask backgroundTask = (BackgroundTask)object;

				BackgroundTaskLocalServiceUtil.deleteBackgroundTask(
					backgroundTask);
			}

		};

		actionableDynamicQuery.performActions();
	}

}