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().getName());
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(getForward(
075                            renderRequest, "portlet.workflow_definition_links.view"));
076            }
077    
078            protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
079                    throws Exception {
080    
081                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
082    
083                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
084                            WebKeys.THEME_DISPLAY);
085    
086                    Enumeration<String> enu = actionRequest.getParameterNames();
087    
088                    while (enu.hasMoreElements()) {
089                            String name = enu.nextElement();
090    
091                            if (!name.startsWith(_PREFIX)) {
092                                    continue;
093                            }
094    
095                            String className = name.substring(_PREFIX.length(), name.length());
096                            String workflowDefinition = ParamUtil.getString(
097                                    actionRequest, name);
098    
099                            WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
100                                    themeDisplay.getUserId(), themeDisplay.getCompanyId(), groupId,
101                                    className, 0, 0, workflowDefinition);
102                    }
103            }
104    
105            private static final String _PREFIX = "workflowDefinitionName@";
106    
107    }