View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.actions.admin.workflow;
20  
21  import java.io.IOException;
22  import java.util.Arrays;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpSession;
28  
29  import org.apache.commons.beanutils.PropertyUtils;
30  import org.apache.log4j.Logger;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.apache.struts.action.ActionMessage;
35  import org.apache.struts.action.ActionMessages;
36  import org.itracker.model.WorkflowScript;
37  import org.itracker.model.NameValuePair;
38  import org.itracker.services.ConfigurationService;
39  import org.itracker.services.exceptions.SystemConfigurationException;
40  import org.itracker.services.util.UserUtilities;
41  import org.itracker.services.util.WorkflowUtilities;
42  import org.itracker.web.actions.base.ItrackerBaseAction;
43  import org.itracker.web.forms.WorkflowScriptForm;
44  import org.itracker.web.util.Constants;
45  
46  public class EditWorkflowScriptFormAction extends ItrackerBaseAction {
47  	private static final Logger log = Logger.getLogger(EditWorkflowScriptFormAction.class);
48  	
49  
50      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
51          ActionMessages errors = new ActionMessages();
52          
53          String pageTitleKey = "";
54          String pageTitleArg = "";
55  
56          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
57              return mapping.findForward("unauthorized");
58          }
59  
60          boolean isUpdate = false;
61  
62          try {
63              WorkflowScriptForm workflowScriptForm = (WorkflowScriptForm) form;
64              
65              if(workflowScriptForm == null) {
66                  workflowScriptForm = new WorkflowScriptForm();
67              }
68              String action = (String) request.getParameter("action");
69              action = (String) PropertyUtils.getSimpleProperty(workflowScriptForm, "action");
70  
71              if(action != null && action.equals("update")) {
72                  isUpdate = true;
73                  pageTitleKey = "itracker.web.admin.editworkflowscript.title.update";
74              } else {
75                  pageTitleKey = "itracker.web.admin.editworkflowscript.title.create";
76              }
77  
78              WorkflowScript workflowScript = new WorkflowScript();
79              if ("update".equals(action)) {
80                  ConfigurationService configurationService = getITrackerServices().getConfigurationService();
81                  
82  
83                  Integer id = (Integer) PropertyUtils.getSimpleProperty(workflowScriptForm, "id");
84                  workflowScript = configurationService.getWorkflowScript(id);
85  
86                  if(workflowScript == null) {
87                      throw new SystemConfigurationException("Invalid workflow script id " + id);
88                  }
89  
90                  workflowScriptForm.setAction("update");
91                  workflowScriptForm.setId(workflowScript.getId());
92                  workflowScriptForm.setName(workflowScript.getName());
93                  workflowScriptForm.setEvent(workflowScript.getEvent());
94                  workflowScriptForm.setScript(workflowScript.getScript());
95                 
96                  pageTitleArg = workflowScript.getName();
97                    
98              }
99  
100             if (workflowScript == null) {
101                 return mapping.findForward("unauthorized");    
102             }
103 
104             if(errors.isEmpty()) {
105 		        HttpSession session = request.getSession(true);
106                 request.setAttribute("workflowScriptForm", workflowScriptForm);
107                 session.setAttribute(Constants.WORKFLOW_SCRIPT_KEY, workflowScript);
108                 request.setAttribute("action",action);
109                 saveToken(request);
110                 
111                 request.setAttribute("pageTitleKey",pageTitleKey); 
112                 request.setAttribute("pageTitleArg",pageTitleArg);
113 
114                 NameValuePair[] eventTypes = WorkflowUtilities.getEvents( getLocale(request) );
115                 request.setAttribute("nameValuePair", Arrays.asList(eventTypes) );
116 
117                 return mapping.getInputForward();
118             }
119         } catch(SystemConfigurationException sce) {
120         	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidworkflowscript"));
121         } catch(Exception e) {
122             log.error("Exception while creating edit workflowScript form.", e);
123             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
124         }
125 
126         if(! errors.isEmpty()) {
127         	saveErrors(request, errors);
128         }
129 
130         request.setAttribute("pageTitleKey",pageTitleKey); 
131         request.setAttribute("pageTitleArg",pageTitleArg); 
132         request.setAttribute("isUpdate", isUpdate); 
133         return mapping.findForward("error");
134     }
135 
136 }
137