/**
 * Copyright (c) 2000-2013 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.portlet.workflowtasks.notifications;

import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.notifications.BaseUserNotificationHandler;
import com.liferay.portal.kernel.portlet.LiferayPortletURL;
import com.liferay.portal.kernel.portlet.LiferayWindowState;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.MapUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.workflow.WorkflowException;
import com.liferay.portal.kernel.workflow.WorkflowTask;
import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
import com.liferay.portal.model.UserNotificationEvent;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PortletKeys;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.liferay.portlet.workflowtasks.action.WorkflowTaskPermissionChecker;

import javax.portlet.PortletRequest;
import javax.portlet.WindowState;

/**
 * @author Jonathan Lee
 */
public class WorkflowTasksUserNotificationHandler
	extends BaseUserNotificationHandler {

	public WorkflowTasksUserNotificationHandler() {
		setPortletId(PortletKeys.MY_WORKFLOW_TASKS);
	}

	@Override
	protected String getBody(
			UserNotificationEvent userNotificationEvent,
			ServiceContext serviceContext)
		throws Exception {

		JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
			userNotificationEvent.getPayload());

		if (!isWorkflowTaskVisible(
				jsonObject.getLong("workflowTaskId"), serviceContext)) {

			UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
				userNotificationEvent.getUserNotificationEventId());

			return StringPool.BLANK;
		}

		return HtmlUtil.escape(jsonObject.getString("notificationMessage"));
	}

	protected String getKaleoProcessLink(
			long workflowTaskId, ServiceContext serviceContext)
		throws Exception {

		LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
			serviceContext.getRequest(), PortletKeys.KALEO_FORMS,
			serviceContext.getPlid(), PortletRequest.RENDER_PHASE);

		String currentURL = portletURL.toString();

		portletURL.setParameter("tabs2", "edit-workflow-task");
		portletURL.setParameter("backURL", currentURL);
		portletURL.setParameter(
			"workflowTaskId", String.valueOf(workflowTaskId));
		portletURL.setWindowState(LiferayWindowState.POP_UP);

		return portletURL.toString();
	}

	@Override
	protected String getLink(
			UserNotificationEvent userNotificationEvent,
			ServiceContext serviceContext)
		throws Exception {

		JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
			userNotificationEvent.getPayload());

		String entryClassName = jsonObject.getString("entryClassName");

		long workflowTaskId = jsonObject.getLong("workflowTaskId");

		if (Validator.equals(entryClassName, _KALEO_PROCESS_CLASS_NAME)) {
			return getKaleoProcessLink(workflowTaskId, serviceContext);
		}

		if (workflowTaskId <= 0) {
			return StringPool.BLANK;
		}

		LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
			serviceContext.getRequest(), PortletKeys.MY_WORKFLOW_TASKS,
			PortalUtil.getControlPanelPlid(serviceContext.getCompanyId()),
			PortletRequest.RENDER_PHASE);

		portletURL.setControlPanelCategory("my");
		portletURL.setParameter(
			"struts_action", "/my_workflow_tasks/edit_workflow_task");
		portletURL.setParameter(
			"workflowTaskId", jsonObject.getString("workflowTaskId"));
		portletURL.setWindowState(WindowState.MAXIMIZED);

		return portletURL.toString();
	}

	protected boolean isWorkflowTaskVisible(
		long workflowTaskId, ServiceContext serviceContext) {

		if (workflowTaskId <= 0) {
			return true;
		}

		WorkflowTask workflowTask = null;

		try {
			workflowTask = WorkflowTaskManagerUtil.getWorkflowTask(
				serviceContext.getCompanyId(), workflowTaskId);
		}
		catch (WorkflowException we) {
		}

		if (workflowTask == null) {
			return false;
		}

		ThemeDisplay themeDisplay = serviceContext.getThemeDisplay();

		long groupId = MapUtil.getLong(
			workflowTask.getOptionalAttributes(), "groupId",
			themeDisplay.getSiteGroupId());

		return WorkflowTaskPermissionChecker.hasPermission(
			groupId, workflowTask, themeDisplay.getPermissionChecker());
	}

	private static final String _KALEO_PROCESS_CLASS_NAME =
		"com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess";

}