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 org.apache.commons.beanutils.PropertyUtils;
22  import org.apache.log4j.Logger;
23  import org.apache.struts.action.*;
24  import org.itracker.model.WorkflowScript;
25  import org.itracker.model.util.UserUtilities;
26  import org.itracker.services.ConfigurationService;
27  import org.itracker.web.actions.base.ItrackerBaseAction;
28  import org.itracker.web.forms.WorkflowScriptForm;
29  import org.itracker.web.util.Constants;
30  import org.itracker.web.util.LoginUtilities;
31  import org.itracker.web.util.ServletContextUtils;
32  import org.springframework.dao.DataAccessException;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import javax.servlet.http.HttpSession;
38  import java.io.IOException;
39  
40  
41  public class EditWorkflowScriptAction extends ItrackerBaseAction {
42      private static final Logger log = Logger.getLogger(EditWorkflowScriptAction.class);
43  
44  
45      public ActionForward execute(ActionMapping mapping, final ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
46          ActionMessages errors = new ActionMessages();
47  
48          if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
49              return mapping.findForward("unauthorized");
50          }
51  
52          if (!isTokenValid(request)) {
53              log.debug("Invalid request token while editing workflow script.");
54              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
55                      "itracker.web.error.transaction"));
56              saveErrors(request, errors);
57              return mapping.findForward("listworkflow");
58          }
59          resetToken(request);
60  
61          try {
62              ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
63  
64              WorkflowScript workflowScript = new WorkflowScript();
65              WorkflowScriptForm./../../org/itracker/web/forms/WorkflowScriptForm.html#WorkflowScriptForm">WorkflowScriptForm form = (WorkflowScriptForm)actionForm;
66              workflowScript.setId(form.getId());
67              workflowScript.setName(form.getName());
68              workflowScript.setEvent(form.getEvent());
69              workflowScript.setScript(form.getScript());
70              workflowScript.setLanguage(WorkflowScript.ScriptLanguage.valueOf(form.getLanguage()));
71  
72              String action = form.getAction();
73  
74  
75              if ("create".equals(action)) {
76                  workflowScript = configurationService.createWorkflowScript(workflowScript);
77              } else if ("update".equals(action)) {
78                  workflowScript = configurationService.updateWorkflowScript(workflowScript);
79              }
80  
81              if (log.isDebugEnabled()) {
82                  log.debug("updated workflowscript was: " + workflowScript);
83              }
84              if (workflowScript == null) {
85                  throw new Exception("Error creating/updating workflow script.");
86              }
87              HttpSession session = request.getSession(true);
88              session.removeAttribute(Constants.WORKFLOW_SCRIPT_KEY);
89              request.setAttribute("action", action);
90              saveToken(request);
91              return new ActionForward(mapping.findForward("listworkflow").getPath() + "?id=" + workflowScript.getId() + "&action=update");
92          } catch (DataAccessException dae) {
93              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system.message",
94                      dae.getRootCause().getMessage(), "Data"));
95              saveErrors(request, errors);
96              return toInputForward(request, mapping);
97          } catch (Exception e) {
98              log.error("Exception processing form data", e);
99              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
100         }
101 
102         if (!errors.isEmpty()) {
103             saveErrors(request, errors);
104         }
105         return mapping.findForward("error");
106     }
107 
108 
109 
110     private ActionForward toInputForward(HttpServletRequest request, ActionMapping mapping) {
111         saveToken(request);
112         return mapping.getInputForward();
113     }
114 
115 }
116