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