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