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