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