KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > EditConfigurationAction


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 cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.resources.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.util.*;
41
42
43 public class EditConfigurationAction extends ITrackerAction {
44
45     public EditConfigurationAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         if(! isLoggedIn(request, response)) {
52             return mapping.findForward("login");
53         }
54         if(! isTokenValid(request)) {
55             Logger.logDebug("Invalid request token while editing configuration.");
56             return mapping.findForward("listconfiguration");
57         }
58         resetToken(request);
59         HttpSession session = request.getSession(true);
60
61         try {
62             InitialContext ic = new InitialContext();
63
64             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
65             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
66             SystemConfiguration sc = scHome.create();
67
68             String JavaDoc action = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "action");
69             String JavaDoc formValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "value");
70             HashMap translations = (HashMap) PropertyUtils.getSimpleProperty(form, "translations");
71
72             if(action == null) {
73                 return mapping.findForward("listconfiguration");
74             }
75
76             ConfigurationModel configItem = null;
77             if("createresolution".equals(action)) {
78                 int value = 0;
79                 int order = 0;
80
81                 try {
82                     ConfigurationModel[] resolutions = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION);
83                     for(int i = 0; i < resolutions.length; i++) {
84                         value = Math.max(value, Integer.parseInt(resolutions[i].getValue()));
85                         order = resolutions[i].getOrder();
86                     }
87                     if(value > 0) {
88                         String JavaDoc version = sc.getProperty("version");
89                         configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_RESOLUTION, Integer.toString(++value), version, ++order);
90                     }
91                 } catch(NumberFormatException JavaDoc nfe) {
92                     Logger.logDebug("Found invalid value or order for a resolution.", nfe);
93                     throw new SystemConfigurationException("Found invalid value or order for a resolution.");
94                 }
95             } else if("createseverity".equals(action)) {
96                 int value = 0;
97                 int order = 0;
98
99                 try {
100                     ConfigurationModel[] severities = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY);
101                     for(int i = 0; i < severities.length; i++) {
102                         value = Math.max(value, Integer.parseInt(severities[i].getValue()));
103                         order = severities[i].getOrder();
104                     }
105                     if(value > 0) {
106                         String JavaDoc version = sc.getProperty("version");
107                         configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_SEVERITY, Integer.toString(++value), version, ++order);
108                     }
109                 } catch(NumberFormatException JavaDoc nfe) {
110                     Logger.logDebug("Found invalid value or order for a severity.", nfe);
111                     throw new SystemConfigurationException("Found invalid value or order for a severity.");
112                 }
113             } else if("createstatus".equals(action)) {
114                 try {
115                     int value = Integer.parseInt(formValue);
116                     ConfigurationModel[] statuses = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
117                     for(int i = 0; i < statuses.length; i++) {
118                         if(value == Integer.parseInt(statuses[i].getValue())) {
119                             throw new SystemConfigurationException("Supplied status value already equals existing status.", "itracker.web.error.existingstatus");
120                         }
121                     }
122
123                     String JavaDoc version = sc.getProperty("version");
124                     configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_STATUS, formValue, version, value);
125                 } catch(NumberFormatException JavaDoc nfe) {
126                     throw new SystemConfigurationException("Invalid value " + formValue + " for status.", "itracker.web.error.invalidstatus");
127                 }
128             } else if("update".equals(action)) {
129                 Integer JavaDoc id = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "id");
130                 configItem = sc.getConfigurationItem(id);
131                 if(configItem == null) {
132                     throw new SystemConfigurationException("Invalid configuration item id " + id);
133                 }
134                 if(configItem.getType() == SystemConfigurationUtilities.TYPE_STATUS && formValue != null && ! formValue.equals("")) {
135                     if(! configItem.getValue().equalsIgnoreCase(formValue)) {
136                         try {
137                             int currStatus = Integer.parseInt(configItem.getValue());
138                             int newStatus = Integer.parseInt(formValue);
139
140                             ConfigurationModel[] statuses = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS);
141                             for(int i = 0; i < statuses.length; i++) {
142                                 if(newStatus == Integer.parseInt(statuses[i].getValue())) {
143                                     throw new SystemConfigurationException("Supplied status value already equals existing status.", "itracker.web.error.existingstatus");
144                                 }
145                             }
146
147                             Logger.logDebug("Changing issue status values from " + configItem.getValue() + " to " + formValue);
148
149                             UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
150                             Integer JavaDoc currUserId = (currUser == null ? new Integer JavaDoc(-1) : currUser.getId());
151
152                             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
153                             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
154                             IssueHandler ih = ihHome.create();
155                             IssueModel[] issues = ih.getIssuesWithStatus(currStatus);
156                             for(int i = 0; i < issues.length; i++) {
157                                 if(issues[i] != null) {
158                                     issues[i].setStatus(newStatus);
159                                     issues[i] = ih.updateIssue(issues[i], currUserId);
160                                     IssueActivityModel activity = new IssueActivityModel(IssueUtilities.ACTIVITY_SYSTEM_UPDATE,
161                                         ITrackerResources.getString("itracker.activity.system.status"), issues[i].getId(), currUserId);
162                                     ih.addIssueActivity(activity);
163                                 }
164                             }
165                         } catch(NumberFormatException JavaDoc nfe) {
166                             throw new SystemConfigurationException("Invalid value " + formValue + " for updated status.", "itracker.web.error.invalidstatus");
167                         }
168                     }
169                 }
170             } else {
171                 throw new SystemConfigurationException("Invalid action " + action + " while editing configuration item.");
172             }
173
174             if(configItem == null) {
175                 throw new SystemConfigurationException("Unable to create new configuration item model.");
176             }
177             if("update".equals(action)) {
178                 configItem = sc.updateConfigurationItem(configItem);
179             } else {
180                 configItem = sc.createConfigurationItem(configItem);
181             }
182
183             if(configItem == null) {
184                 throw new SystemConfigurationException("Unable to create new configuration item.");
185             }
186
187             String JavaDoc key = SystemConfigurationUtilities.getLanguageKey(configItem);
188             Logger.logDebug("Processing translations for configuration item " + configItem.getId() + " with key " + key);
189             if(translations != null && key != null && ! key.equals("")) {
190                 for(Iterator iter = translations.keySet().iterator(); iter.hasNext(); ) {
191                     String JavaDoc locale = (String JavaDoc) iter.next();
192                     if(locale != null) {
193                         String JavaDoc translation = (String JavaDoc) translations.get(locale);
194                         if(translation != null && ! translation.equals("")) {
195                             Logger.logDebug("Adding new translation for locale " + locale + " for " + configItem);
196                             sc.updateLanguageItem(new LanguageModel(locale, key, translation));
197                         }
198                     }
199                 }
200                 String JavaDoc baseValue = (String JavaDoc) translations.get(ITrackerResources.BASE_LOCALE);
201                 sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, key, baseValue));
202                 ITrackerResources.clearKeyFromBundles(key, true);
203             }
204
205             // Now reset the cached versions in IssueUtilities
206
sc.resetConfigurationCache(configItem.getType());
207
208             return mapping.findForward("listconfiguration");
209         } catch(SystemConfigurationException sce) {
210             Logger.logError("Exception processing form data: " + sce.getMessage(), sce);
211             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(sce.getKey()));
212         } catch(Exception JavaDoc e) {
213             Logger.logError("Exception processing form data", e);
214             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
215         }
216
217         if(! errors.isEmpty()) {
218             saveErrors(request, errors);
219             saveToken(request);
220             return mapping.getInputForward();
221         }
222
223         return mapping.findForward("error");
224     }
225 }
226   
Popular Tags