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.project;
20  
21  //import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.log4j.Logger;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  import org.apache.struts.action.ActionMessage;
36  import org.apache.struts.action.ActionMessages;
37  import org.itracker.model.Project;
38  import org.itracker.model.ProjectScript;
39  import org.itracker.model.WorkflowScript;
40  import org.itracker.services.ConfigurationService;
41  import org.itracker.services.ProjectService;
42  import org.itracker.services.util.UserUtilities;
43  import org.itracker.web.actions.base.ItrackerBaseAction;
44  import org.itracker.web.forms.ProjectScriptForm;
45  import org.itracker.web.util.Constants;
46  
47  import bsh.ParseException;
48  
49  
50  public class EditProjectScriptAction extends ItrackerBaseAction {
51  	private static final Logger log = Logger.getLogger(EditProjectScriptAction.class);
52  	
53      public EditProjectScriptAction() {
54      }
55      
56      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57  
58      	ActionMessages errors = new ActionMessages();
59      	
60          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
61              return mapping.findForward("unauthorized");
62          }
63          
64          if(! isTokenValid(request)) {
65              log.debug("Invalid request token while editing workflow script.");
66  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
67  			"itracker.web.error.transaction"));
68  			saveErrors(request, errors);
69              return mapping.findForward("listworkflow");
70          }
71          resetToken(request);
72          ProjectScriptForm projectScriptForm = (ProjectScriptForm) form;
73          
74          try {
75              ConfigurationService configurationService = getITrackerServices().getConfigurationService();
76              ProjectService projectService = getITrackerServices().getProjectService();
77              String action = request.getParameter("action");
78              if ( action == null  )
79                  action = (String) projectScriptForm.getAction();
80              Integer projectId = (Integer) projectScriptForm.getProjectId();
81              Project project = projectService.getProject(projectId);
82              HashMap<String,String> fieldIds = (HashMap<String,String>) projectScriptForm.getFieldId();
83              HashMap<String,String> priorities = (HashMap<String,String>) projectScriptForm.getPriority();
84              HashMap<String,String> scriptItems = (HashMap<String,String>) projectScriptForm.getScriptItems();
85              HashMap<String,String> ids = (HashMap<String,String>) projectScriptForm.getId();
86              for ( Iterator<String> siIterator = scriptItems.keySet().iterator(); siIterator.hasNext(); ) {
87                  String key = (String) siIterator.next();
88                  if(key != null) {
89                      String scriptItemsvalue = (String) scriptItems.get(key);
90                      if(scriptItemsvalue != null && ! scriptItemsvalue.trim().equals("")&& scriptItemsvalue.trim().equals("on")) {
91                          Integer wfsIds = Integer.valueOf(key);
92                          Integer fieldId = Integer.valueOf((String) fieldIds.get(key));
93                          Integer priority = Integer.valueOf((String) priorities.get(key));
94                          WorkflowScript workflowScript = configurationService.getWorkflowScript(wfsIds);
95                          ProjectScript projectScript = new ProjectScript();
96                          Integer id = Integer.valueOf((String) ids.get(key));
97                          projectScript.setId(id);
98                          ProjectScript chkprojectScript = projectService.getProjectScript(id);
99                         
100                         projectScript.setFieldId(fieldId);
101                         projectScript.setPriority(priority);
102                         projectScript.setProject(project);
103                         projectScript.setScript(workflowScript);
104                         if("create".equals(action) || chkprojectScript == null ) {
105                             projectScript = projectService.addProjectScript(projectId, projectScript);
106                         } else {
107                             projectScript = projectService.updateProjectScript(projectScript);
108                         }
109                         if (projectScript == null) {
110                             throw new Exception("Error creating/updating project script.");
111                         }
112                     }
113                 }
114             }
115             
116             
117             HttpSession session = request.getSession(true);
118             session.removeAttribute(Constants.PROJECT_SCRIPT_KEY);
119             request.setAttribute("action",action);
120             saveToken(request);
121             return new ActionForward(
122                     mapping.findForward("editproject").getPath()
123                     + "?id=" + project.getId() +"&action=update");
124         } catch(ParseException pe) {
125             log.debug("Error parseing script.  Redisplaying form for correction.", pe);
126             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidscriptdata", pe.getMessage()));
127             saveErrors(request, errors);
128             saveToken(request);
129             return mapping.getInputForward();
130         } catch(Exception e) {
131             log.error("Exception processing form data", e);
132             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
133         }
134         
135         if(! errors.isEmpty()) {
136         	saveErrors(request, errors);
137         }
138         return mapping.findForward("error");
139     }
140     
141 }