1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.workflowdefinitions.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.upload.UploadPortletRequest;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.LocaleUtil;
22  import com.liferay.portal.kernel.util.LocalizationUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.kernel.workflow.WorkflowDefinition;
27  import com.liferay.portal.kernel.workflow.WorkflowDefinitionFileException;
28  import com.liferay.portal.kernel.workflow.WorkflowDefinitionManagerUtil;
29  import com.liferay.portal.kernel.workflow.WorkflowException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.io.File;
36  import java.io.FileInputStream;
37  import java.io.FileNotFoundException;
38  
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditWorkflowDefinitionAction.java.html"><b><i>View Source</i></b>
54   * </a>
55   *
56   * @author Bruno Farache
57   */
58  public class EditWorkflowDefinitionAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateWorkflowDefinition(actionRequest);
70              }
71              else if (cmd.equals(Constants.DEACTIVATE) ||
72                       cmd.equals(Constants.DELETE) ||
73                       cmd.equals(Constants.RESTORE)) {
74  
75                  deleteWorkflowDefinition(actionRequest);
76              }
77  
78              sendRedirect(actionRequest, actionResponse);
79          }
80          catch (Exception e) {
81              if (e instanceof FileNotFoundException ||
82                  e instanceof WorkflowDefinitionFileException) {
83  
84                  SessionErrors.add(actionRequest, e.getClass().getName());
85              }
86              else if (e instanceof WorkflowException) {
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88  
89                  setForward(actionRequest, "portlet.workflow_definitions.error");
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
99              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().getName());
108 
109                 return mapping.findForward(
110                     "portlet.workflow_definitions.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(getForward(
118             renderRequest,
119             "portlet.workflow_definitions.edit_workflow_definition"));
120     }
121 
122     protected void deleteWorkflowDefinition(ActionRequest actionRequest)
123         throws Exception {
124 
125         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
126 
127         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128             WebKeys.THEME_DISPLAY);
129 
130         String name = ParamUtil.getString(actionRequest, "name");
131         int version = ParamUtil.getInteger(actionRequest, "version");
132 
133         if (cmd.equals(Constants.DEACTIVATE) || cmd.equals(Constants.RESTORE)) {
134             boolean active = !cmd.equals(Constants.DEACTIVATE);
135 
136             WorkflowDefinitionManagerUtil.updateActive(
137                 themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
138                 version, active);
139         }
140         else {
141             WorkflowDefinitionManagerUtil.undeployWorkflowDefinition(
142                 themeDisplay.getCompanyId(), themeDisplay.getUserId(), name,
143                 version);
144         }
145     }
146 
147     protected boolean isCheckMethodOnProcessAction() {
148         return _CHECK_METHOD_ON_PROCESS_ACTION;
149     }
150 
151     protected void updateWorkflowDefinition(ActionRequest actionRequest)
152         throws Exception {
153 
154         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
155             actionRequest);
156 
157         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
158             WebKeys.THEME_DISPLAY);
159 
160         Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(
161             actionRequest, "title");
162 
163         File file = uploadRequest.getFile("file");
164 
165         WorkflowDefinition workflowDefinition = null;
166 
167         if (!file.exists()) {
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), new FileInputStream(file));
184         }
185 
186         actionRequest.setAttribute(
187             WebKeys.WORKFLOW_DEFINITION, workflowDefinition);
188     }
189 
190     protected String getTitle(Map<Locale, String> titleMap) {
191         if (titleMap == null){
192             return null;
193         }
194 
195         String value = StringPool.BLANK;
196 
197         Locale[] locales = LanguageUtil.getAvailableLocales();
198 
199         for (Locale locale : locales) {
200             String languageId = LocaleUtil.toLanguageId(locale);
201             String title = titleMap.get(locale);
202 
203             if (Validator.isNotNull(title)) {
204                 value = LocalizationUtil.updateLocalization(
205                     value, "Title", title, languageId);
206             }
207             else {
208                 value = LocalizationUtil.removeLocalization(
209                     value, "Title", languageId);
210             }
211         }
212 
213         return value;
214     }
215 
216     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
217 
218 }