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