com.liferay.portlet.workflowdefinitionlinks.action.EditWorkflowDefinitionLinkAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.workflowdefinitionlinks.action.EditWorkflowDefinitionLinkAction.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.workflowdefinitionlinks.action;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.workflow.WorkflowException;
import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;

import java.util.Enumeration;

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 Bruno Farache
 * @author Juan Fernndez
 */
public class EditWorkflowDefinitionLinkAction extends PortletAction {

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

        try {
            updateWorkflowDefinitionLinks(actionRequest);

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

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

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

        return mapping.findForward(getForward(renderRequest, "portlet.workflow_definition_links.view"));
    }

    protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest) throws Exception {

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

        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        Enumeration<String> enu = actionRequest.getParameterNames();

        while (enu.hasMoreElements()) {
            String name = enu.nextElement();

            if (!name.startsWith(_PREFIX)) {
                continue;
            }

            String className = name.substring(_PREFIX.length(), name.length());
            String workflowDefinition = ParamUtil.getString(actionRequest, name);

            WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(themeDisplay.getUserId(),
                    themeDisplay.getCompanyId(), groupId, className, 0, 0, workflowDefinition);
        }
    }

    private static final String _PREFIX = "workflowDefinitionName@";

}