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.CustomField;
27  import org.itracker.model.CustomFieldValue;
28  import org.itracker.model.util.SystemConfigurationUtilities;
29  import org.itracker.model.util.UserUtilities;
30  import org.itracker.services.ConfigurationService;
31  import org.itracker.web.actions.base.ItrackerBaseAction;
32  import org.itracker.web.util.LoginUtilities;
33  import org.itracker.web.util.ServletContextUtils;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import java.io.IOException;
39  import java.util.Collections;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  public class OrderCustomFieldValueAction extends ItrackerBaseAction {
44      private static final Logger log = Logger
45              .getLogger(OrderCustomFieldValueAction.class);
46  
47      public ActionForward execute(ActionMapping mapping, ActionForm form,
48                                   HttpServletRequest request, HttpServletResponse response)
49              throws ServletException, IOException {
50          ActionMessages errors = new ActionMessages();
51  
52          if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request,
53                  response)) {
54              return mapping.findForward("unauthorized");
55          }
56  
57          try {
58              ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
59                      .getConfigurationService();
60  
61              Integer customFieldValueId = (Integer) PropertyUtils
62                      .getSimpleProperty(form, "id");
63              String action = (String) PropertyUtils.getSimpleProperty(form,
64                      "action");
65              if (customFieldValueId == null
66                      || customFieldValueId.intValue() <= 0) {
67                  throw new SystemConfigurationException(
68                          "Invalid custom field value id.");
69              }
70  
71              CustomFieldValue customFieldValue = configurationService
72                      .getCustomFieldValue(customFieldValueId);
73              if (customFieldValue == null) {
74                  throw new SystemConfigurationException(
75                          "Invalid custom field value id.");
76              }
77  
78              CustomField customField = configurationService
79                      .getCustomField(customFieldValue.getCustomField().getId());
80              if (customField == null) {
81                  throw new SystemConfigurationException(
82                          "Invalid custom field id.");
83              }
84              List<CustomFieldValue> customFieldvalues = customField.getOptions();
85  
86  
87              Collections.sort(customFieldvalues,
88                      CustomFieldValue.SORT_ORDER_COMPARATOR);
89              Iterator<CustomFieldValue> valuesIt = customFieldvalues.iterator();
90              CustomFieldValue curCustomFieldValue, prevCustomFieldValue = null;
91              int i = 0;
92              while (valuesIt.hasNext()) {
93                  curCustomFieldValue = valuesIt.next();
94                  curCustomFieldValue.setSortOrder(i);
95  
96                  if (curCustomFieldValue.getId() == customFieldValueId) {
97  
98                      if ("up".equals(action)) {
99                          if (prevCustomFieldValue != null) {
100                             curCustomFieldValue.setSortOrder(i - 1);
101                             prevCustomFieldValue.setSortOrder(i);
102                         }
103                     } else {
104                         CustomFieldValue value = valuesIt.next();
105                         value.setSortOrder(i++);
106                         curCustomFieldValue.setSortOrder(i);
107                         curCustomFieldValue = value;
108                     }
109 
110                 }
111                 prevCustomFieldValue = curCustomFieldValue;
112                 i++;
113             }
114 
115             configurationService.updateCustomField(customField);
116 
117             configurationService
118                     .resetConfigurationCache(Configuration.Type.customfield);
119             request.setAttribute("action", action);
120             return new ActionForward(mapping.findForward("editcustomfield")
121                     .getPath()
122                     + "?id=" + customField.getId() + "&action=update");
123         } catch (SystemConfigurationException nfe) {
124             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
125                     "itracker.web.error.invalidcustomfieldvalue"));
126             log.debug("Invalid custom field value id "
127                     + request.getParameter("id") + " specified.");
128         } catch (NumberFormatException nfe) {
129             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
130                     "itracker.web.error.invalidcustomfieldvalue"));
131             log.debug("Invalid custom field value id "
132                     + request.getParameter("id") + " specified.");
133         } catch (Exception e) {
134             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
135                     "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 }