001    /**
002     * Copyright (c) 2000-2011 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.workflowdefinitions.action;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StreamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowDefinition;
028    import com.liferay.portal.kernel.workflow.WorkflowDefinitionFileException;
029    import com.liferay.portal.kernel.workflow.WorkflowDefinitionManagerUtil;
030    import com.liferay.portal.kernel.workflow.WorkflowException;
031    import com.liferay.portal.struts.PortletAction;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.WebKeys;
035    
036    import java.io.InputStream;
037    
038    import java.util.Locale;
039    import java.util.Map;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.RenderRequest;
045    import javax.portlet.RenderResponse;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Bruno Farache
053     */
054    public class EditWorkflowDefinitionAction extends PortletAction {
055    
056            @Override
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
063    
064                    try {
065                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
066                                    updateWorkflowDefinition(actionRequest);
067                            }
068                            else if (cmd.equals(Constants.DEACTIVATE) ||
069                                             cmd.equals(Constants.DELETE) ||
070                                             cmd.equals(Constants.RESTORE)) {
071    
072                                    deleteWorkflowDefinition(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof WorkflowDefinitionFileException) {
079                                    SessionErrors.add(actionRequest, e.getClass().getName());
080                            }
081                            else if (e instanceof WorkflowException) {
082                                    SessionErrors.add(actionRequest, e.getClass().getName());
083    
084                                    setForward(actionRequest, "portlet.workflow_definitions.error");
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
095                            RenderRequest renderRequest, RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            ActionUtil.getWorkflowDefinition(renderRequest);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof WorkflowException) {
103                                    SessionErrors.add(renderRequest, e.getClass().getName());
104    
105                                    return mapping.findForward(
106                                            "portlet.workflow_definitions.error");
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112    
113                    return mapping.findForward(getForward(
114                            renderRequest,
115                            "portlet.workflow_definitions.edit_workflow_definition"));
116            }
117    
118            protected void deleteWorkflowDefinition(ActionRequest actionRequest)
119                    throws Exception {
120    
121                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    String name = ParamUtil.getString(actionRequest, "name");
127                    int version = ParamUtil.getInteger(actionRequest, "version");
128    
129                    if (cmd.equals(Constants.DEACTIVATE) || cmd.equals(Constants.RESTORE)) {
130                            boolean active = !cmd.equals(Constants.DEACTIVATE);
131    
132                            WorkflowDefinitionManagerUtil.updateActive(
133                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
134                                    version, active);
135                    }
136                    else {
137                            WorkflowDefinitionManagerUtil.undeployWorkflowDefinition(
138                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
139                                    version);
140                    }
141            }
142    
143            @Override
144            protected boolean isCheckMethodOnProcessAction() {
145                    return _CHECK_METHOD_ON_PROCESS_ACTION;
146            }
147    
148            protected void updateWorkflowDefinition(ActionRequest actionRequest)
149                    throws Exception {
150    
151                    UploadPortletRequest uploadPortletRequest =
152                            PortalUtil.getUploadPortletRequest(actionRequest);
153    
154                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
155                            WebKeys.THEME_DISPLAY);
156    
157                    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
158                            actionRequest, "title");
159    
160                    InputStream inputStream = null;
161    
162                    try {
163                            inputStream = uploadPortletRequest.getFileAsStream("file");
164    
165                            WorkflowDefinition workflowDefinition = null;
166    
167                            if (inputStream == null) {
168                                    String name = ParamUtil.getString(actionRequest, "name");
169                                    int version = ParamUtil.getInteger(actionRequest, "version");
170    
171                                    workflowDefinition =
172                                            WorkflowDefinitionManagerUtil.getWorkflowDefinition(
173                                                    themeDisplay.getCompanyId(), name, version);
174    
175                                    WorkflowDefinitionManagerUtil.updateTitle(
176                                            themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
177                                            version, getTitle(titleMap));
178                            }
179                            else {
180                                    workflowDefinition =
181                                            WorkflowDefinitionManagerUtil.deployWorkflowDefinition(
182                                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(),
183                                                    getTitle(titleMap), inputStream);
184                            }
185    
186                            actionRequest.setAttribute(
187                                    WebKeys.WORKFLOW_DEFINITION, workflowDefinition);
188                    }
189                    finally {
190                            StreamUtil.cleanUp(inputStream);
191                    }
192    
193            }
194    
195            protected String getTitle(Map<Locale, String> titleMap) {
196                    if (titleMap == null) {
197                            return null;
198                    }
199    
200                    String value = StringPool.BLANK;
201    
202                    Locale[] locales = LanguageUtil.getAvailableLocales();
203    
204                    for (Locale locale : locales) {
205                            String languageId = LocaleUtil.toLanguageId(locale);
206                            String title = titleMap.get(locale);
207    
208                            if (Validator.isNotNull(title)) {
209                                    value = LocalizationUtil.updateLocalization(
210                                            value, "Title", title, languageId);
211                            }
212                            else {
213                                    value = LocalizationUtil.removeLocalization(
214                                            value, "Title", languageId);
215                            }
216                    }
217    
218                    return value;
219            }
220    
221            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
222    
223    }