001    /**
002     * Copyright (c) 2000-2010 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.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.workflow.WorkflowDefinition;
027    import com.liferay.portal.kernel.workflow.WorkflowDefinitionFileException;
028    import com.liferay.portal.kernel.workflow.WorkflowDefinitionManagerUtil;
029    import com.liferay.portal.kernel.workflow.WorkflowException;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    
035    import java.io.File;
036    import java.io.FileInputStream;
037    import java.io.FileNotFoundException;
038    
039    import java.util.Locale;
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Bruno Farache
054     */
055    public class EditWorkflowDefinitionAction extends PortletAction {
056    
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 FileNotFoundException ||
079                                    e instanceof WorkflowDefinitionFileException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass().getName());
082                            }
083                            else if (e instanceof WorkflowException) {
084                                    SessionErrors.add(actionRequest, e.getClass().getName());
085    
086                                    setForward(actionRequest, "portlet.workflow_definitions.error");
087                            }
088                            else {
089                                    throw e;
090                            }
091                    }
092            }
093    
094            public ActionForward render(
095                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
096                            RenderRequest renderRequest, RenderResponse renderResponse)
097                    throws Exception {
098    
099                    try {
100                            ActionUtil.getWorkflowDefinition(renderRequest);
101                    }
102                    catch (Exception e) {
103                            if (e instanceof WorkflowException) {
104                                    SessionErrors.add(renderRequest, e.getClass().getName());
105    
106                                    return mapping.findForward(
107                                            "portlet.workflow_definitions.error");
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113    
114                    return mapping.findForward(getForward(
115                            renderRequest,
116                            "portlet.workflow_definitions.edit_workflow_definition"));
117            }
118    
119            protected void deleteWorkflowDefinition(ActionRequest actionRequest)
120                    throws Exception {
121    
122                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
123    
124                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
125                            WebKeys.THEME_DISPLAY);
126    
127                    String name = ParamUtil.getString(actionRequest, "name");
128                    int version = ParamUtil.getInteger(actionRequest, "version");
129    
130                    if (cmd.equals(Constants.DEACTIVATE) || cmd.equals(Constants.RESTORE)) {
131                            boolean active = !cmd.equals(Constants.DEACTIVATE);
132    
133                            WorkflowDefinitionManagerUtil.updateActive(
134                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
135                                    version, active);
136                    }
137                    else {
138                            WorkflowDefinitionManagerUtil.undeployWorkflowDefinition(
139                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
140                                    version);
141                    }
142            }
143    
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 uploadRequest = PortalUtil.getUploadPortletRequest(
152                            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                    File file = uploadRequest.getFile("file");
161    
162                    WorkflowDefinition workflowDefinition = null;
163    
164                    if (!file.exists()) {
165                            String name = ParamUtil.getString(actionRequest, "name");
166                            int version = ParamUtil.getInteger(actionRequest, "version");
167    
168                            workflowDefinition =
169                                    WorkflowDefinitionManagerUtil.getWorkflowDefinition(
170                                            themeDisplay.getCompanyId(), name, version);
171    
172                            WorkflowDefinitionManagerUtil.updateTitle(
173                                    themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
174                                    version, getTitle(titleMap));
175                    }
176                    else {
177                            workflowDefinition =
178                                    WorkflowDefinitionManagerUtil.deployWorkflowDefinition(
179                                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
180                                            getTitle(titleMap), new FileInputStream(file));
181                    }
182    
183                    actionRequest.setAttribute(
184                            WebKeys.WORKFLOW_DEFINITION, workflowDefinition);
185            }
186    
187            protected String getTitle(Map<Locale, String> titleMap) {
188                    if (titleMap == null){
189                            return null;
190                    }
191    
192                    String value = StringPool.BLANK;
193    
194                    Locale[] locales = LanguageUtil.getAvailableLocales();
195    
196                    for (Locale locale : locales) {
197                            String languageId = LocaleUtil.toLanguageId(locale);
198                            String title = titleMap.get(locale);
199    
200                            if (Validator.isNotNull(title)) {
201                                    value = LocalizationUtil.updateLocalization(
202                                            value, "Title", title, languageId);
203                            }
204                            else {
205                                    value = LocalizationUtil.removeLocalization(
206                                            value, "Title", languageId);
207                            }
208                    }
209    
210                    return value;
211            }
212    
213            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
214    
215    }