1 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 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 action = (String ) PropertyUtils.getSimpleProperty(form, "action"); 69 String formValue = (String ) 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 version = sc.getProperty("version"); 89 configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_RESOLUTION, Integer.toString(++value), version, ++order); 90 } 91 } catch(NumberFormatException 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 version = sc.getProperty("version"); 107 configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_SEVERITY, Integer.toString(++value), version, ++order); 108 } 109 } catch(NumberFormatException 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 version = sc.getProperty("version"); 124 configItem = new ConfigurationModel(SystemConfigurationUtilities.TYPE_STATUS, formValue, version, value); 125 } catch(NumberFormatException nfe) { 126 throw new SystemConfigurationException("Invalid value " + formValue + " for status.", "itracker.web.error.invalidstatus"); 127 } 128 } else if("update".equals(action)) { 129 Integer id = (Integer ) 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 currUserId = (currUser == null ? new Integer (-1) : currUser.getId()); 151 152 Object 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 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 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 locale = (String ) iter.next(); 192 if(locale != null) { 193 String translation = (String ) 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 baseValue = (String ) translations.get(ITrackerResources.BASE_LOCALE); 201 sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, key, baseValue)); 202 ITrackerResources.clearKeyFromBundles(key, true); 203 } 204 205 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 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 |