001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.workflowdefinitionlinks.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.workflow.WorkflowException;
020    import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    
025    import java.util.Enumeration;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Jorge Ferrer
039     * @author Bruno Farache
040     * @author Juan Fernández
041     */
042    public class EditWorkflowDefinitionLinkAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    try {
051                            updateWorkflowDefinitionLinks(actionRequest);
052    
053                            sendRedirect(actionRequest, actionResponse);
054                    }
055                    catch (Exception e) {
056                            if (e instanceof WorkflowException) {
057                                    SessionErrors.add(actionRequest, e.getClass());
058    
059                                    setForward(
060                                            actionRequest, "portlet.workflow_definition_links.error");
061                            }
062                            else {
063                                    throw e;
064                            }
065                    }
066            }
067    
068            @Override
069            public ActionForward render(
070                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
071                            RenderRequest renderRequest, RenderResponse renderResponse)
072                    throws Exception {
073    
074                    return mapping.findForward(
075                            getForward(
076                                    renderRequest, "portlet.workflow_definition_links.view"));
077            }
078    
079            protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
080                    throws Exception {
081    
082                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
083    
084                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
085                            WebKeys.THEME_DISPLAY);
086    
087                    Enumeration<String> enu = actionRequest.getParameterNames();
088    
089                    while (enu.hasMoreElements()) {
090                            String name = enu.nextElement();
091    
092                            if (!name.startsWith(_PREFIX)) {
093                                    continue;
094                            }
095    
096                            String className = name.substring(_PREFIX.length());
097                            String workflowDefinition = ParamUtil.getString(
098                                    actionRequest, name);
099    
100                            WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
101                                    themeDisplay.getUserId(), themeDisplay.getCompanyId(), groupId,
102                                    className, 0, 0, workflowDefinition);
103                    }
104            }
105    
106            private static final String _PREFIX = "workflowDefinitionName@";
107    
108    }