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