001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
047                            PortletConfig portletConfig, ActionRequest actionRequest,
048                            ActionResponse actionResponse)
049                    throws Exception {
050    
051                    try {
052                            updateWorkflowDefinitionLinks(actionRequest);
053    
054                            sendRedirect(actionRequest, actionResponse);
055                    }
056                    catch (Exception e) {
057                            if (e instanceof WorkflowException) {
058                                    SessionErrors.add(actionRequest, e.getClass());
059    
060                                    setForward(
061                                            actionRequest, "portlet.workflow_definition_links.error");
062                            }
063                            else {
064                                    throw e;
065                            }
066                    }
067            }
068    
069            @Override
070            public ActionForward render(
071                            ActionMapping actionMapping, ActionForm actionForm,
072                            PortletConfig portletConfig, RenderRequest renderRequest,
073                            RenderResponse renderResponse)
074                    throws Exception {
075    
076                    return actionMapping.findForward(
077                            getForward(
078                                    renderRequest, "portlet.workflow_definition_links.view"));
079            }
080    
081            protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
082                    throws Exception {
083    
084                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
085    
086                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
087                            WebKeys.THEME_DISPLAY);
088    
089                    Enumeration<String> enu = actionRequest.getParameterNames();
090    
091                    while (enu.hasMoreElements()) {
092                            String name = enu.nextElement();
093    
094                            if (!name.startsWith(_PREFIX)) {
095                                    continue;
096                            }
097    
098                            String className = name.substring(_PREFIX.length());
099                            String workflowDefinition = ParamUtil.getString(
100                                    actionRequest, name);
101    
102                            WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
103                                    themeDisplay.getUserId(), themeDisplay.getCompanyId(), groupId,
104                                    className, 0, 0, workflowDefinition);
105                    }
106            }
107    
108            private static final String _PREFIX = "workflowDefinitionName@";
109    
110    }