com.liferay.portlet.workflowtasks.action.EditWorkflowTaskAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.workflowtasks.action.EditWorkflowTaskAction.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.workflowtasks.action;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.workflow.WorkflowException;
import com.liferay.portal.kernel.workflow.WorkflowTaskDueDateException;
import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;

import java.util.Calendar;
import java.util.Date;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Jorge Ferrer
 * @author Marcellus Tavares
 * @author Brian Wing Shun Chan
 */
public class EditWorkflowTaskAction extends PortletAction {

    @Override
    public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        try {
            if (cmd.equals(Constants.ASSIGN)) {
                assignTask(actionRequest);
            } else if (cmd.equals(Constants.SAVE)) {
                completeTask(actionRequest);
            } else if (cmd.equals(Constants.UPDATE)) {
                updateTask(actionRequest);
            }

            sendRedirect(actionRequest, actionResponse);
        } catch (Exception e) {
            if (e instanceof WorkflowTaskDueDateException) {
                SessionErrors.add(actionRequest, e.getClass().getName());
            } else if (e instanceof PrincipalException || e instanceof WorkflowException) {

                SessionErrors.add(actionRequest, e.getClass().getName());

                setForward(actionRequest, "portlet.workflow_tasks.error");
            } else {
                throw e;
            }
        }
    }

    @Override
    public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {

        try {
            ActionUtil.getWorkflowTask(renderRequest);
        } catch (Exception e) {
            if (e instanceof WorkflowException) {

                SessionErrors.add(renderRequest, e.getClass().getName());

                return mapping.findForward("portlet.workflow_tasks.error");
            } else {
                throw e;
            }
        }

        return mapping.findForward(getForward(renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
    }

    protected void assignTask(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long workflowTaskId = ParamUtil.getLong(actionRequest, "workflowTaskId");

        long assigneeUserId = ParamUtil.getLong(actionRequest, "assigneeUserId");
        String comment = ParamUtil.getString(actionRequest, "comment");

        WorkflowTaskManagerUtil.assignWorkflowTaskToUser(themeDisplay.getCompanyId(), themeDisplay.getUserId(),
                workflowTaskId, assigneeUserId, comment, null, null);
    }

    protected void completeTask(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long workflowTaskId = ParamUtil.getLong(actionRequest, "workflowTaskId");

        String transitionName = ParamUtil.getString(actionRequest, "transitionName");
        String comment = ParamUtil.getString(actionRequest, "comment");

        WorkflowTaskManagerUtil.completeWorkflowTask(themeDisplay.getCompanyId(), themeDisplay.getUserId(),
                workflowTaskId, transitionName, comment, null);
    }

    @Override
    protected boolean isCheckMethodOnProcessAction() {
        return _CHECK_METHOD_ON_PROCESS_ACTION;
    }

    protected void updateTask(ActionRequest actionRequest) throws Exception {
        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long workflowTaskId = ParamUtil.getLong(actionRequest, "workflowTaskId");

        String comment = ParamUtil.getString(actionRequest, "comment");

        int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
        int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
        int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
        int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
        int dueDateMinute = ParamUtil.getInteger(actionRequest, "dueDateMinute");
        int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");

        if (dueDateAmPm == Calendar.PM) {
            dueDateHour += 12;
        }

        Date dueDate = PortalUtil.getDate(dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
                new WorkflowTaskDueDateException());

        WorkflowTaskManagerUtil.updateDueDate(themeDisplay.getCompanyId(), themeDisplay.getUserId(), workflowTaskId,
                comment, dueDate);
    }

    private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;

}