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