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 import org.apache.struts.validator.*; 35 36 import cowsultants.itracker.ejb.client.exceptions.*; 37 import cowsultants.itracker.ejb.client.interfaces.*; 38 import cowsultants.itracker.ejb.client.models.*; 39 import cowsultants.itracker.ejb.client.resources.*; 40 import cowsultants.itracker.ejb.client.util.*; 41 import cowsultants.itracker.web.util.*; 42 43 44 57 public class ImportDataVerifyAction extends ITrackerAction { 58 private static final int UPDATE_STATUS = 1; 59 private static final int UPDATE_SEVERITY = 2; 60 private static final int UPDATE_RESOLUTION = 3; 61 62 public ImportDataVerifyAction () { 63 } 64 65 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 66 ActionErrors errors = new ActionErrors(); 67 68 if(! isLoggedIn(request, response)) { 69 return mapping.findForward("login"); 70 } 71 72 if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) { 73 return mapping.findForward("unauthorized"); 74 } 75 76 try { 77 FormFile file = (FormFile) PropertyUtils.getSimpleProperty(form, "importFile"); 78 String xmlData = new String (file.getFileData()); 79 80 ImportDataModel model = new ImportDataModel(); 81 GenericModel[] importData = ImportExportUtilities.importIssues(xmlData); 82 boolean[] existingModel = new boolean[importData.length]; 83 84 model.setReuseUsers((Boolean ) PropertyUtils.getSimpleProperty(form, "optionreuseusers")); 85 model.setReuseProjects((Boolean ) PropertyUtils.getSimpleProperty(form, "optionreuseprojects")); 86 model.setReuseConfig((Boolean ) PropertyUtils.getSimpleProperty(form, "optionreuseconfig")); 87 model.setCreatePasswords((Boolean ) PropertyUtils.getSimpleProperty(form, "optioncreatepasswords")); 88 model.setData(importData, existingModel); 89 90 InitialContext ic = new InitialContext(); 91 checkConfig(model, ic); 92 Logger.logDebug(model.toString()); 93 checkUsers(model, ic); 94 Logger.logDebug(model.toString()); 95 checkProjects(model, ic); 96 Logger.logDebug(model.toString()); 97 checkIssues(model, ic); 98 Logger.logDebug(model.toString()); 99 100 HttpSession session = request.getSession(true); 101 session.setAttribute(Constants.IMPORT_DATA_KEY, model); 102 } catch(ImportExportException iee) { 103 if(iee.getType() == ImportExportException.TYPE_INVALID_LOGINS) { 104 Logger.logError("Invalid logins found while verifying import data."); 105 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.importexport.invalidlogins", iee.getMessage())); 106 } else if(iee.getType() == ImportExportException.TYPE_INVALID_STATUS) { 107 Logger.logError("Invalid status found while verifying import data."); 108 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.importexport.invalidstatus", iee.getMessage())); 109 } else { 110 Logger.logError("Exception while verifying import data.", iee); 111 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); 112 } 113 } catch(Exception e) { 114 Logger.logError("Exception while verifying import data.", e); 115 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); 116 } 117 118 if(! errors.isEmpty()) { 119 saveErrors(request, errors); 120 return mapping.getInputForward(); 121 } 122 123 return mapping.findForward("importdataverify"); 124 } 125 126 private void checkConfig(ImportDataModel model, InitialContext ic) throws ImportExportException { 127 try { 128 int maxSeverityValue = 1; 129 int maxResolutionValue = 1; 130 131 Object scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 132 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 133 SystemConfiguration sc = scHome.create(); 134 135 ConfigurationModel[] statuses = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_STATUS, ImportExportUtilities.EXPORT_LOCALE); 136 ConfigurationModel[] severities = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_SEVERITY, ImportExportUtilities.EXPORT_LOCALE); 137 ConfigurationModel[] resolutions = sc.getConfigurationItemsByType(SystemConfigurationUtilities.TYPE_RESOLUTION, ImportExportUtilities.EXPORT_LOCALE); 138 CustomFieldModel[] fields = sc.getCustomFields(ImportExportUtilities.EXPORT_LOCALE); 139 140 for(int i = 0; i < severities.length; i++) { 141 maxSeverityValue = Math.max(maxSeverityValue, Integer.parseInt(severities[i].getValue())); 142 } 143 for(int i = 0; i < resolutions.length; i++) { 144 maxResolutionValue = Math.max(maxResolutionValue, Integer.parseInt(resolutions[i].getValue())); 145 } 146 147 GenericModel[] importData = model.getData(); 148 for(int i = 0; i < importData.length; i++) { 149 if(importData[i] instanceof ConfigurationModel) { 150 ConfigurationModel configItem = (ConfigurationModel) importData[i]; 156 if(configItem.getType() == SystemConfigurationUtilities.TYPE_STATUS) { 157 boolean found = false; 158 for(int j = 0; j < statuses.length; j++) { 159 if(model.getReuseConfig() && statuses[j].getName().equalsIgnoreCase(configItem.getName())) { 160 updateIssues(importData, UPDATE_STATUS, configItem.getValue(), statuses[j].getValue()); 162 model.setExistingModel(i, true); 163 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_STATUSES, ImportExportUtilities.IMPORT_STAT_REUSED); 164 found = true; 165 break; 166 } else if(statuses[j].getValue().equalsIgnoreCase(configItem.getValue())) { 167 throw new ImportExportException(configItem.getValue(), ImportExportException.TYPE_INVALID_STATUS); 169 } 170 } 171 if(! found) { 172 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_STATUSES, ImportExportUtilities.IMPORT_STAT_NEW); 173 } 174 } else if(configItem.getType() == SystemConfigurationUtilities.TYPE_SEVERITY) { 175 boolean found = false; 176 if(model.getReuseConfig()) { 177 for(int j = 0; j < severities.length; j++) { 178 if(severities[j].getName().equalsIgnoreCase(configItem.getName())) { 179 updateIssues(importData, UPDATE_SEVERITY, configItem.getValue(), severities[j].getValue()); 181 model.setExistingModel(i, true); 182 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_SEVERITIES, ImportExportUtilities.IMPORT_STAT_REUSED); 183 found = true; 184 break; 185 } 186 } 187 } 188 if(! found) { 189 updateIssues(importData, UPDATE_SEVERITY, configItem.getValue(), Integer.toString(++maxSeverityValue)); 190 configItem.setValue(Integer.toString(maxSeverityValue)); 191 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_SEVERITIES, ImportExportUtilities.IMPORT_STAT_NEW); 192 } 193 } else if(configItem.getType() == SystemConfigurationUtilities.TYPE_RESOLUTION) { 194 boolean found = false; 195 if(model.getReuseConfig()) { 196 for(int j = 0; j < resolutions.length; j++) { 197 if(resolutions[j].getName().equalsIgnoreCase(configItem.getName())) { 198 updateIssues(importData, UPDATE_RESOLUTION, configItem.getValue(), resolutions[j].getValue()); 200 model.setExistingModel(i, true); 201 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_RESOLUTIONS, ImportExportUtilities.IMPORT_STAT_REUSED); 202 found = true; 203 break; 204 } 205 } 206 } 207 if(! found) { 208 updateIssues(importData, UPDATE_RESOLUTION, configItem.getValue(), Integer.toString(++maxResolutionValue)); 209 configItem.setValue(Integer.toString(maxResolutionValue)); 210 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_RESOLUTIONS, ImportExportUtilities.IMPORT_STAT_NEW); 211 } 212 } 213 } else if(importData[i] instanceof CustomFieldModel) { 214 boolean found = false; 215 CustomFieldModel field = (CustomFieldModel) importData[i]; 216 if(model.getReuseFields()) { 217 for(int j = 0; j < fields.length; j++) { 218 if(fields[j].getFieldType() == field.getFieldType() && fields[j].getName().equalsIgnoreCase(field.getName())) { 219 field.setId(fields[j].getId()); 222 model.setExistingModel(i, true); 223 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_FIELDS, ImportExportUtilities.IMPORT_STAT_REUSED); 224 found = true; 225 break; 226 } 227 } 228 } 229 if(! found) { 230 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_FIELDS, ImportExportUtilities.IMPORT_STAT_NEW); 231 } 232 } 233 } 234 } catch(ImportExportException iee) { 235 throw iee; 236 } catch(Exception e) { 237 Logger.logError("Error verifiying import data.", e); 238 throw new ImportExportException(e.getMessage()); 239 } 240 } 241 242 private void checkUsers(ImportDataModel model, InitialContext ic) throws ImportExportException { 243 String invalidLogins = null; 244 245 try { 246 247 Object uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME); 248 UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class); 249 UserHandler uh = uhHome.create(); 250 251 GenericModel[] importData = model.getData(); 252 253 for(int i = 0; i < importData.length; i++) { 254 if(importData[i] instanceof UserModel) { 255 UserModel user = (UserModel) importData[i]; 256 UserModel existingUser = uh.getUserByLogin(user.getLogin()); 257 if(existingUser != null) { 258 if(model.getReuseUsers()) { 259 user.setId(existingUser.getId()); 260 model.setExistingModel(i, true); 261 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_USERS, ImportExportUtilities.IMPORT_STAT_REUSED); 262 Logger.logDebug("Reusing existing user " + user.getLogin() + "(" + user.getId() + ") during import."); 263 } else { 264 Logger.logDebug("Existing user " + existingUser.getLogin() + "(" + existingUser.getId() + ") during import. Adding to invalid login list."); 265 invalidLogins = (invalidLogins == null ? existingUser.getLogin() : invalidLogins + ", " + existingUser.getLogin()); 266 } 267 } else { 268 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_USERS, ImportExportUtilities.IMPORT_STAT_NEW); 269 } 270 } 271 } 272 } catch(Exception e) { 273 Logger.logError("Error verifiying import data.", e); 274 throw new ImportExportException(e.getMessage()); 275 } 276 277 if(invalidLogins != null) { 278 throw new ImportExportException(invalidLogins, ImportExportException.TYPE_INVALID_LOGINS); 279 } 280 } 281 282 private void checkProjects(ImportDataModel model, InitialContext ic) throws ImportExportException { 283 try { 284 Object phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME); 285 ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class); 286 ProjectHandler ph = phHome.create(); 287 288 ProjectModel[] existingProjects = ph.getAllProjects(); 289 if(existingProjects.length == 0) { 290 return; 291 } 292 293 GenericModel[] importData = model.getData(); 294 295 for(int i = 0; i < importData.length; i++) { 296 if(importData[i] instanceof ProjectModel) { 297 if(! model.getReuseProjects()) { 298 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_PROJECTS, ImportExportUtilities.IMPORT_STAT_NEW); 299 continue; 300 } 301 302 ProjectModel project = (ProjectModel) importData[i]; 303 boolean found = false; 304 for(int j = 0; j < existingProjects.length; j++) { 305 Logger.logDebug("Project Name: " + project.getName() + " Existing Project: " + existingProjects[j].getName()); 306 Logger.logDebug("Project Name: " + ITrackerResources.escapeUnicodeString(project.getName(), false) + " Existing Project: " + ITrackerResources.escapeUnicodeString(existingProjects[j].getName(), false)); 307 if(project.getName() != null && project.getName().equalsIgnoreCase(existingProjects[j].getName())) { 308 project.setId(existingProjects[j].getId()); 309 model.setExistingModel(i, true); 310 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_PROJECTS, ImportExportUtilities.IMPORT_STAT_REUSED); 311 found = true; 312 Logger.logDebug("Reusing existing project " + project.getName() + "(" + project.getId() + ") during import."); 313 break; 314 } 315 } 316 if(! found) { 317 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_PROJECTS, ImportExportUtilities.IMPORT_STAT_NEW); 318 } 319 } 320 } 321 } catch(Exception e) { 322 Logger.logError("Error verifiying import data.", e); 323 throw new ImportExportException(e.getMessage()); 324 } 325 } 326 327 private void checkIssues(ImportDataModel model, InitialContext ic) throws ImportExportException { 328 GenericModel[] importData = model.getData(); 329 330 for(int i = 0; i < importData.length; i++) { 331 if(importData[i] instanceof IssueModel) { 332 model.addVerifyStatistic(ImportExportUtilities.IMPORT_STAT_ISSUES, ImportExportUtilities.IMPORT_STAT_NEW); 333 } 334 } 335 } 336 337 private void updateIssues(GenericModel[] models, int updateType, String currentValue, String newValue) throws ImportExportException { 338 if(models == null || currentValue == null || newValue == null) { 339 return; 340 } 341 342 try { 343 for(int i = 0; i < models.length; i++) { 344 if(models[i] instanceof IssueModel) { 345 IssueModel issue = (IssueModel) models[i]; 346 if(updateType == UPDATE_STATUS && issue.getStatus() == Integer.parseInt(currentValue)) { 347 issue.setStatus(Integer.parseInt(newValue)); 348 } else if(updateType == UPDATE_SEVERITY && issue.getSeverity() == Integer.parseInt(currentValue)) { 349 issue.setSeverity(Integer.parseInt(newValue)); 350 } else if(updateType == UPDATE_RESOLUTION && currentValue.equalsIgnoreCase(issue.getResolution())) { 351 issue.setResolution(newValue); 352 } 353 } 354 } 355 } catch(Exception e) { 356 Logger.logDebug("Unable to update configuration data in issues.", e); 357 throw new ImportExportException("Unable to update configuration data in issues: " + e.getMessage()); 358 } 359 } 360 } 361 | Popular Tags |