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.ByteArrayInputStream;
22  import java.io.IOException;
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.services.ConfigurationService;
38  import org.itracker.services.util.UserUtilities;
39  import org.itracker.web.actions.base.ItrackerBaseAction;
40  import org.itracker.web.util.Constants;
41  
42  import bsh.ParseException;
43  
44  
45  public class EditWorkflowScriptAction extends ItrackerBaseAction {
46  	private static final Logger log = Logger.getLogger(EditWorkflowScriptAction.class);
47  	
48     
49      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50          ActionMessages errors = new ActionMessages();
51  
52          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
53              return mapping.findForward("unauthorized");
54          }
55  
56          if(! isTokenValid(request)) {
57              log.debug("Invalid request token while editing workflow script.");
58  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
59  			"itracker.web.error.transaction"));
60  			saveErrors(request, errors);
61              return mapping.findForward("listworkflow");
62          }
63          resetToken(request);
64  
65          WorkflowScript workflowScript = null;
66          
67          try {
68              ConfigurationService configurationService = getITrackerServices().getConfigurationService();
69  
70              String scriptData = (String) PropertyUtils.getSimpleProperty(form, "script");
71              if ( scriptData != null && scriptData.trim().length() > 0 ) {
72                  //ByteArrayInputStream sbis = new ByteArrayInputStream(scriptData.getBytes());
73                  //Parser parser = new Parser(sbis);
74  //                try {
75  //                    while(!parser.Line()) {
76                          // do nothing, if script is syntactically correct
77                          // no exception is thrown
78  //                    }
79  //                } catch(Throwable t) {
80  //                    throw new ParseException(t.getMessage());
81  //                }
82              }
83  
84              log.info("Kimba:  using this module action 1" );
85              workflowScript = new WorkflowScript();
86              workflowScript.setId((Integer) PropertyUtils.getSimpleProperty(form, "id"));
87              workflowScript.setName((String) PropertyUtils.getSimpleProperty(form, "name"));
88              workflowScript.setEvent(((Integer) PropertyUtils.getSimpleProperty(form, "event")).intValue());
89              workflowScript.setScript(scriptData);
90  
91              String action = (String) PropertyUtils.getSimpleProperty(form, "action");
92              log.info("Kimba:  using this module action 2"+action );
93              if("create".equals(action)) {
94                  workflowScript = configurationService.createWorkflowScript(workflowScript);
95              } else if ("update".equals(action)) {
96                  workflowScript = configurationService.updateWorkflowScript(workflowScript);
97              }
98  
99              if (workflowScript == null) {
100                 throw new Exception("Error creating/updating workflow script.");
101             }
102 
103             HttpSession session = request.getSession(true);
104             session.removeAttribute(Constants.WORKFLOW_SCRIPT_KEY);
105             request.setAttribute("action",action);
106             saveToken(request);
107 //            return mapping.findForward("listworkflow");
108             return new ActionForward(mapping.findForward("listworkflow").getPath() + "?id=" + workflowScript.getId() +"&action=update");
109         } catch(ParseException pe) {
110             log.debug("Error parseing script.  Redisplaying form for correction.", pe);
111             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidscriptdata", pe.getMessage()));
112             saveErrors(request, errors);
113             saveToken(request);
114             return mapping.getInputForward();
115         } catch(Exception e) {
116             log.error("Exception processing form data", e);
117             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
118         }
119 
120         if(! errors.isEmpty()) {
121         	saveErrors(request, errors);
122         }
123         return mapping.findForward("error");
124     }
125 
126 }
127