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.configuration;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
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.Configuration;
37  import org.itracker.services.ConfigurationService;
38  import org.itracker.services.exceptions.SystemConfigurationException;
39  import org.itracker.services.util.SystemConfigurationUtilities;
40  import org.itracker.services.util.UserUtilities;
41  import org.itracker.web.actions.base.ItrackerBaseAction;
42  
43  
44  
45  public class OrderConfigurationItemAction extends ItrackerBaseAction {
46  	private static final Logger log = Logger.getLogger(OrderConfigurationItemAction.class);
47  	
48      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49          ActionMessages errors = new ActionMessages();
50  
51          
52          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
53              return mapping.findForward("unauthorized");
54          }
55          
56          try {
57              ConfigurationService configurationService = getITrackerServices().getConfigurationService();
58              
59              Integer configId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
60              String action = (String) PropertyUtils.getSimpleProperty(form, "action");
61              if(configId == null || configId.intValue() <= 0) {
62                  throw new SystemConfigurationException("Invalid configuration id.");
63              }
64              
65              Configuration configItem = configurationService.getConfigurationItem(configId);
66              if(configItem == null) {
67                  throw new SystemConfigurationException("Invalid configuration id.");
68              }
69              
70              int configType = configItem.getType();
71              List<Configuration> configItems = configurationService.getConfigurationItemsByType(configType);
72              List<Configuration> newConfigItems = new ArrayList<Configuration>();
73              
74              for(int i = 0; i < configItems.size(); i++) {
75                  newConfigItems.add(configItems.get(i));
76              }
77              for(int i = 0; i < configItems.size(); i++) {
78                  if ( configItems.get(i) != null ) {
79                      Configuration firstConfiguration = new Configuration();
80                      Configuration secondConfiguration = new Configuration();
81                      Configuration curConfiguration = (Configuration) configItems.get(i);
82                      int todo_i = -1;
83                      if ( curConfiguration.getId().equals(configId) ) {
84                          if ("up".equals(action) ){
85                              todo_i = i - 1;
86                          } else {
87                              todo_i = i + 1;
88                          }
89                          Configuration todoConfiguration = (Configuration) configItems.get(todo_i);
90  
91                          firstConfiguration.setId(todoConfiguration.getId());
92                          firstConfiguration.setCreateDate(todoConfiguration.getCreateDate());
93                          firstConfiguration.setLastModifiedDate(todoConfiguration.getLastModifiedDate());
94                          firstConfiguration.setName(todoConfiguration.getName());
95                          firstConfiguration.setOrder(curConfiguration.getOrder());
96                          firstConfiguration.setType(todoConfiguration.getType());
97                          firstConfiguration.setValue(todoConfiguration.getValue());
98                          firstConfiguration.setVersion(todoConfiguration.getVersion());
99  
100 
101                         secondConfiguration.setId(curConfiguration.getId());
102                         secondConfiguration.setCreateDate(curConfiguration.getCreateDate());
103                         secondConfiguration.setLastModifiedDate(curConfiguration.getLastModifiedDate());
104                         secondConfiguration.setName(curConfiguration.getName());
105                         secondConfiguration.setOrder(todoConfiguration.getOrder());
106                         secondConfiguration.setType(curConfiguration.getType());
107                         secondConfiguration.setValue(curConfiguration.getValue());
108                         secondConfiguration.setVersion(curConfiguration.getVersion());
109 
110                         newConfigItems.set(todo_i,firstConfiguration);
111                         newConfigItems.set(i,secondConfiguration);
112                     }
113                 }
114             }
115             
116             newConfigItems = configurationService.updateConfigurationItems(newConfigItems,configType);
117             
118             // Only resolutions and severities can be reordered at this point.  Statuses
119             // and some basic workflow depend on the actual value of the status, so
120             // the order must equal the value of the status for it to work correctly.
121             if(configType == SystemConfigurationUtilities.TYPE_RESOLUTION) {
122                 configurationService.resetConfigurationCache(SystemConfigurationUtilities.TYPE_RESOLUTION);
123             } else if(configType == SystemConfigurationUtilities.TYPE_SEVERITY) {
124                 configurationService.resetConfigurationCache(SystemConfigurationUtilities.TYPE_SEVERITY);
125             }
126             
127             return mapping.findForward("listconfiguration");
128         } catch(SystemConfigurationException nfe) {
129             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
130             log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
131         } catch(NumberFormatException nfe) {
132             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
133             log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
134         } catch(Exception e) {
135             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
136             log.error("System Error.", e);
137         }
138         if(! errors.isEmpty()) {
139             saveErrors(request, errors);
140         }
141         return mapping.findForward("error");
142     }
143     
144 }