1 18 19 package cowsultants.itracker.ejb.client.util; 20 21 import java.text.*; 22 import java.util.*; 23 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 28 import cowsultants.itracker.ejb.client.exceptions.*; 29 import cowsultants.itracker.ejb.client.models.*; 30 import cowsultants.itracker.ejb.client.resources.ITrackerResources; 31 32 36 public class ImportHandler extends DefaultHandler implements ImportExportTags { 37 private Vector models; 38 private StringBuffer tagBuffer; 39 private SAXException endException; 40 41 private GenericModel parentModel; 42 private GenericModel childModel; 43 private Vector itemList; 44 private String tempStorage; 45 46 public ImportHandler() { 47 models = new Vector(); 48 endException = null; 49 } 50 51 public GenericModel[] getModels() { 52 GenericModel[] modelsArray = new GenericModel[models.size()]; 53 models.copyInto(modelsArray); 54 return modelsArray; 55 } 56 57 public void startDocument() { 58 Logger.logDebug("Started import xml parsing."); 59 } 60 61 public void endDocument() { 62 Logger.logDebug("Completed import xml parsing."); 63 } 64 65 public void startElement(String uri, String name, String qName, Attributes atts) throws SAXException { 66 Logger.logDebug("Parsing import tag " + qName); 67 68 if(endException != null) { 69 throw endException; 70 } 71 72 tempStorage = ""; 73 try { 74 if(TAG_COMPONENT.equals(qName)) { 75 String id = atts.getValue(ATTR_SYSTEMID); 76 if(id == null) { 77 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for component."); 78 } 79 80 childModel = new ComponentModel(); 81 childModel.setId(new Integer (id)); 82 } else if(TAG_COMPONENTS.equals(qName)) { 83 itemList = new Vector(); 84 } else if(TAG_CONFIGURATION.equals(qName)) { 85 parentModel = new SystemConfigurationModel(); 86 } else if(TAG_CUSTOM_FIELD.equals(qName)) { 87 String id = atts.getValue(ATTR_SYSTEMID); 88 if(id == null) { 89 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for issue."); 90 } 91 92 childModel = new CustomFieldModel(); 93 childModel.setId(new Integer (id)); 94 } else if(TAG_CUSTOM_FIELD_OPTION.equals(qName)) { 95 tempStorage = ITrackerResources.unescapeUnicodeString(atts.getValue(ATTR_VALUE)); 96 } else if(TAG_CUSTOM_FIELDS.equals(qName)) { 97 itemList = new Vector(); 98 } else if(TAG_HISTORY_ENTRY.equals(qName)) { 99 String creatorId = atts.getValue(ATTR_CREATOR_ID); 100 String date = atts.getValue(ATTR_DATE); 101 String status = atts.getValue(ATTR_STATUS); 102 if(creatorId == null) { 103 throw new SAXException ("Attribute creatorId was null for issue history."); 104 } else if(date == null) { 105 throw new SAXException ("Attribute date was null for issue history."); 106 } 107 108 childModel = new IssueHistoryModel(); 109 ((IssueHistoryModel) childModel).setUser((UserModel) findModel(creatorId)); 110 ((IssueHistoryModel) childModel).setStatus(status != null && ! status.equals("") ? Integer.parseInt(status) : IssueUtilities.HISTORY_STATUS_AVAILABLE); 111 ((IssueHistoryModel) childModel).setCreateDate(getDateValue(date, qName)); 112 tagBuffer = new StringBuffer (); 113 } else if(TAG_ISSUE.equals(qName)) { 114 String id = atts.getValue(ATTR_SYSTEMID); 115 if(id == null) { 116 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for issue."); 117 } 118 119 parentModel = new IssueModel(); 120 parentModel.setId(new Integer (id)); 121 } else if(TAG_ISSUE_ATTACHMENT.equals(qName)) { 122 childModel = new IssueAttachmentModel(); 123 } else if(TAG_ISSUE_ATTACHMENTS.equals(qName)) { 124 itemList = new Vector(); 125 } else if(TAG_ISSUE_COMPONENTS.equals(qName)) { 126 itemList = new Vector(); 127 } else if(TAG_ISSUE_FIELD.equals(qName)) { 128 String id = atts.getValue(ATTR_ID); 129 if(id == null) { 130 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for issue field."); 131 } 132 childModel = new IssueFieldModel((CustomFieldModel) findModel(id)); 133 } else if(TAG_ISSUE_FIELDS.equals(qName)) { 134 itemList = new Vector(); 135 } else if(TAG_ISSUE_HISTORY.equals(qName)) { 136 itemList = new Vector(); 137 } else if(TAG_ISSUE_VERSIONS.equals(qName)) { 138 itemList = new Vector(); 139 } else if(TAG_PROJECT.equals(qName)) { 140 String id = atts.getValue(ATTR_SYSTEMID); 141 if(id == null) { 142 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for project."); 143 } 144 145 parentModel = new ProjectModel(); 146 parentModel.setId(new Integer (id)); 147 } else if(TAG_PROJECT_FIELDS.equals(qName)) { 148 itemList = new Vector(); 149 } else if(TAG_PROJECT_OWNERS.equals(qName)) { 150 itemList = new Vector(); 151 } else if(TAG_RESOLUTION.equals(qName)) { 152 String value = atts.getValue(ATTR_VALUE); 153 String order = atts.getValue(ATTR_ORDER); 154 155 childModel = new ConfigurationModel(SystemConfigurationUtilities.TYPE_RESOLUTION, value, Integer.parseInt(order)); 156 tagBuffer = new StringBuffer (); 157 } else if(TAG_SEVERITY.equals(qName)) { 158 String value = atts.getValue(ATTR_VALUE); 159 String order = atts.getValue(ATTR_ORDER); 160 161 childModel = new ConfigurationModel(SystemConfigurationUtilities.TYPE_SEVERITY, value, Integer.parseInt(order)); 162 tagBuffer = new StringBuffer (); 163 } else if(TAG_STATUS.equals(qName)) { 164 String value = atts.getValue(ATTR_VALUE); 165 String order = atts.getValue(ATTR_ORDER); 166 167 childModel = new ConfigurationModel(SystemConfigurationUtilities.TYPE_STATUS, value, Integer.parseInt(order)); 168 tagBuffer = new StringBuffer (); 169 } else if(TAG_USER.equals(qName)) { 170 String id = atts.getValue(ATTR_SYSTEMID); 171 if(id == null) { 172 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for user."); 173 } 174 175 parentModel = new UserModel(); 176 parentModel.setId(new Integer (id)); 177 } else if(TAG_VERSION.equals(qName)) { 178 String id = atts.getValue(ATTR_SYSTEMID); 179 if(id == null) { 180 throw new SAXException ("Attribute " + ATTR_SYSTEMID + " was null for version."); 181 } 182 183 childModel = new VersionModel(); 184 childModel.setId(new Integer (id)); 185 } else if(TAG_VERSIONS.equals(qName)) { 186 itemList = new Vector(); 187 } else { 188 tagBuffer = new StringBuffer (); 189 } 190 } catch(NumberFormatException nfe) { 191 throw new SAXException ("Attribute in " + qName + " did not contain a numeric value."); 192 } 193 } 194 195 public void endElement(String uri, String name, String qName) { 196 Logger.logDebug("Completing import tag " + qName); 197 198 201 try { 202 if(TAG_ISSUE.equals(qName) || TAG_PROJECT.equals(qName) || TAG_USER.equals(qName) || TAG_CONFIGURATION.equals(qName)) { 203 models.add(parentModel.clone()); 204 parentModel = null; 205 childModel = null; 206 itemList = null; 207 } else if(TAG_RESOLUTION.equals(qName) || TAG_SEVERITY.equals(qName) || TAG_STATUS.equals(qName)) { 208 ((ConfigurationModel) childModel).setName(getBuffer()); 209 models.add(childModel.clone()); 210 ((SystemConfigurationModel) parentModel).addConfiguration((ConfigurationModel) childModel); 211 childModel = null; 212 } else if(TAG_COMPONENT.equals(qName) || TAG_VERSION.equals(qName) || TAG_CUSTOM_FIELD.equals(qName)) { 213 models.add(childModel.clone()); 216 itemList.add(childModel.clone()); 217 childModel = null; 218 } else if(TAG_HISTORY_ENTRY.equals(qName)) { 219 ((IssueHistoryModel) childModel).setDescription(getBuffer()); 220 itemList.add(childModel.clone()); 221 childModel = null; 222 } else if(TAG_ISSUE_ATTACHMENT.equals(qName)) { 223 itemList.add(childModel.clone()); 224 childModel = null; 225 } else if(TAG_ISSUE_FIELD.equals(qName)) { 226 itemList.add(childModel.clone()); 227 childModel = null; 228 } else if(TAG_COMPONENTS.equals(qName)) { 229 ComponentModel[] itemListArray = new ComponentModel[itemList.size()]; 230 for(int i = 0; i < itemList.size(); i++) { 231 itemListArray[i] = (ComponentModel) itemList.elementAt(i); 232 } 233 ((ProjectModel) parentModel).setComponents(itemListArray); 234 } else if(TAG_COMPONENT_DESCRIPTION.equals(qName)) { 235 ((ComponentModel) childModel).setDescription(getBuffer()); 236 } else if(TAG_COMPONENT_ID.equals(qName)) { 237 if(itemList == null) { 238 itemList = new Vector(); 239 } 240 itemList.add((ComponentModel) findModel(getBuffer())); 241 } else if(TAG_COMPONENT_NAME.equals(qName)) { 242 ((ComponentModel) childModel).setName(getBuffer()); 243 } else if(TAG_CONFIGURATION_VERSION.equals(qName)) { 244 ((SystemConfigurationModel) parentModel).setVersion(getBuffer()); 245 } else if(TAG_CREATE_DATE.equals(qName)) { 246 ((IssueModel) parentModel).setCreateDate(getDateValue(getBuffer(), qName)); 247 } else if(TAG_CREATOR.equals(qName)) { 248 ((IssueModel) parentModel).setCreator((UserModel) findModel(getBuffer())); 249 } else if(TAG_CUSTOM_FIELDS.equals(qName)) { 250 CustomFieldModel[] itemListArray = new CustomFieldModel[itemList.size()]; 251 for(int i = 0; i < itemList.size(); i++) { 252 itemListArray[i] = (CustomFieldModel) itemList.elementAt(i); 253 } 254 ((SystemConfigurationModel) parentModel).setCustomFields(itemListArray); 255 } else if(TAG_CUSTOM_FIELD_DATEFORMAT.equals(qName)) { 256 ((CustomFieldModel) childModel).setDateFormat(getBuffer()); 257 } else if(TAG_CUSTOM_FIELD_LABEL.equals(qName)) { 258 ((CustomFieldModel) childModel).setName(getBuffer()); 259 } else if(TAG_CUSTOM_FIELD_OPTION.equals(qName)) { 260 ((CustomFieldModel) childModel).addOption(tempStorage, getBuffer()); 261 } else if(TAG_CUSTOM_FIELD_REQUIRED.equals(qName)) { 262 ((CustomFieldModel) childModel).setRequired(("true".equalsIgnoreCase(getBuffer()) ? true : false)); 263 } else if(TAG_CUSTOM_FIELD_SORTOPTIONS.equals(qName)) { 264 ((CustomFieldModel) childModel).setSortOptionsByName(("true".equalsIgnoreCase(getBuffer()) ? true : false)); 265 } else if(TAG_CUSTOM_FIELD_TYPE.equals(qName)) { 266 ((CustomFieldModel) childModel).setFieldType(getBufferAsInt()); 267 } else if(TAG_EMAIL.equals(qName)) { 268 ((UserModel) parentModel).setEmail(getBuffer()); 269 } else if(TAG_FIRST_NAME.equals(qName)) { 270 ((UserModel) parentModel).setFirstName(getBuffer()); 271 } else if(TAG_ISSUE_ATTACHMENTS.equals(qName)) { 272 IssueAttachmentModel[] itemListArray = new IssueAttachmentModel[itemList.size()]; 273 for(int i = 0; i < itemList.size(); i++) { 274 itemListArray[i] = (IssueAttachmentModel) itemList.elementAt(i); 275 } 276 ((IssueModel) parentModel).setAttachments(itemListArray); 277 } else if(TAG_ISSUE_ATTACHMENT_CREATOR.equals(qName)) { 278 ((IssueAttachmentModel) childModel).setUser((UserModel) findModel(getBuffer())); 279 } else if(TAG_ISSUE_ATTACHMENT_DESCRIPTION.equals(qName)) { 280 ((IssueAttachmentModel) childModel).setDescription(getBuffer()); 281 } else if(TAG_ISSUE_ATTACHMENT_FILENAME.equals(qName)) { 282 ((IssueAttachmentModel) childModel).setFileName(getBuffer()); 283 } else if(TAG_ISSUE_ATTACHMENT_ORIGFILE.equals(qName)) { 284 ((IssueAttachmentModel) childModel).setOriginalFileName(getBuffer()); 285 } else if(TAG_ISSUE_ATTACHMENT_SIZE.equals(qName)) { 286 ((IssueAttachmentModel) childModel).setSize(getBufferAsLong()); 287 } else if(TAG_ISSUE_ATTACHMENT_TYPE.equals(qName)) { 288 ((IssueAttachmentModel) childModel).setType(getBuffer()); 289 } else if(TAG_ISSUE_COMPONENTS.equals(qName)) { 290 ComponentModel[] itemListArray = new ComponentModel[itemList.size()]; 291 for(int i = 0; i < itemList.size(); i++) { 292 itemListArray[i] = (ComponentModel) itemList.elementAt(i); 293 } 294 ((IssueModel) parentModel).setComponents(itemListArray); 295 } else if(TAG_ISSUE_DESCRIPTION.equals(qName)) { 296 ((IssueModel) parentModel).setDescription(getBuffer()); 297 } else if(TAG_ISSUE_FIELDS.equals(qName)) { 298 IssueFieldModel[] itemListArray = new IssueFieldModel[itemList.size()]; 299 for(int i = 0; i < itemList.size(); i++) { 300 itemListArray[i] = (IssueFieldModel) itemList.elementAt(i); 301 } 302 ((IssueModel) parentModel).setFields(itemListArray); 303 } else if(TAG_ISSUE_HISTORY.equals(qName)) { 304 IssueHistoryModel[] itemListArray = new IssueHistoryModel[itemList.size()]; 305 for(int i = 0; i < itemList.size(); i++) { 306 itemListArray[i] = (IssueHistoryModel) itemList.elementAt(i); 307 } 308 ((IssueModel) parentModel).setHistory(itemListArray); 309 } else if(TAG_ISSUE_PROJECT.equals(qName)) { 310 ((IssueModel) parentModel).setProject((ProjectModel) findModel(getBuffer())); 311 } else if(TAG_ISSUE_RESOLUTION.equals(qName)) { 312 ((IssueModel) parentModel).setResolution(getBuffer()); 313 } else if(TAG_ISSUE_SEVERITY.equals(qName)) { 314 ((IssueModel) parentModel).setSeverity(getBufferAsInt()); 315 } else if(TAG_ISSUE_STATUS.equals(qName)) { 316 ((IssueModel) parentModel).setStatus(getBufferAsInt()); 317 } else if(TAG_ISSUE_VERSIONS.equals(qName)) { 318 VersionModel[] itemListArray = new VersionModel[itemList.size()]; 319 for(int i = 0; i < itemList.size(); i++) { 320 itemListArray[i] = (VersionModel) itemList.elementAt(i); 321 } 322 ((IssueModel) parentModel).setVersions(itemListArray); 323 } else if(TAG_LAST_MODIFIED.equals(qName)) { 324 ((IssueModel) parentModel).setLastModifiedDate(getDateValue(getBuffer(), qName)); 325 } else if(TAG_LAST_NAME.equals(qName)) { 326 ((UserModel) parentModel).setLastName(getBuffer()); 327 } else if(TAG_LOGIN.equals(qName)) { 328 ((UserModel) parentModel).setLogin(getBuffer()); 329 } else if(TAG_OWNER.equals(qName)) { 330 ((IssueModel) parentModel).setOwner((UserModel) findModel(getBuffer())); 331 } else if(TAG_PROJECT_NAME.equals(qName)) { 332 ((ProjectModel) parentModel).setName(getBuffer()); 333 } else if(TAG_PROJECT_DESCRIPTION.equals(qName)) { 334 ((ProjectModel) parentModel).setDescription(getBuffer()); 335 } else if(TAG_PROJECT_FIELDS.equals(qName)) { 336 CustomFieldModel[] itemListArray = new CustomFieldModel[itemList.size()]; 337 for(int i = 0; i < itemList.size(); i++) { 338 itemListArray[i] = (CustomFieldModel) itemList.elementAt(i); 339 } 340 ((ProjectModel) parentModel).setCustomFields(itemListArray); 341 } else if(TAG_PROJECT_FIELD_ID.equals(qName)) { 342 itemList.add((CustomFieldModel) findModel(getBuffer())); 343 } else if(TAG_PROJECT_OPTIONS.equals(qName)) { 344 ((ProjectModel) parentModel).setOptions(getBufferAsInt()); 345 } else if(TAG_PROJECT_OWNERS.equals(qName)) { 346 UserModel[] itemListArray = new UserModel[itemList.size()]; 347 for(int i = 0; i < itemList.size(); i++) { 348 itemListArray[i] = (UserModel) itemList.elementAt(i); 349 } 350 ((ProjectModel) parentModel).setOwners(itemListArray); 351 } else if(TAG_PROJECT_OWNER_ID.equals(qName)) { 352 itemList.add((UserModel) findModel(getBuffer())); 353 } else if(TAG_PROJECT_STATUS.equals(qName)) { 354 ((ProjectModel) parentModel).setStatus(ProjectUtilities.STATUS_LOCKED); 356 357 String currBuffer = getBuffer(); 358 HashMap projectStatuses = ProjectUtilities.getStatusNames(EXPORT_LOCALE); 359 for(Iterator iter = projectStatuses.keySet().iterator(); iter.hasNext(); ) { 360 String key = (String ) iter.next(); 361 String keyValue = (String ) projectStatuses.get(key); 362 if(keyValue != null && keyValue.equalsIgnoreCase(currBuffer)) { 363 ((ProjectModel) parentModel).setStatus(Integer.parseInt(key)); 364 break; 365 } 366 } 367 } else if(TAG_SUPER_USER.equals(qName)) { 368 ((UserModel) parentModel).setSuperUser(("true".equalsIgnoreCase(getBuffer()) ? true : false)); 369 } else if(TAG_TARGET_VERSION_ID.equals(qName)) { 370 ((IssueModel) parentModel).setTargetVersion((VersionModel) findModel(getBuffer())); 371 } else if(TAG_USER_STATUS.equals(qName)) { 372 ((UserModel) parentModel).setStatus(UserUtilities.STATUS_LOCKED); 374 375 String currBuffer = getBuffer(); 376 HashMap userStatuses = UserUtilities.getStatusNames(EXPORT_LOCALE); 377 for(Iterator iter = userStatuses.keySet().iterator(); iter.hasNext(); ) { 378 String key = (String ) iter.next(); 379 String keyValue = (String ) userStatuses.get(key); 380 if(keyValue != null && keyValue.equalsIgnoreCase(currBuffer)) { 381 ((UserModel) parentModel).setStatus(Integer.parseInt(key)); 382 break; 383 } 384 } 385 } else if(TAG_VERSIONS.equals(qName)) { 386 VersionModel[] itemListArray = new VersionModel[itemList.size()]; 387 for(int i = 0; i < itemList.size(); i++) { 388 itemListArray[i] = (VersionModel) itemList.elementAt(i); 389 } 390 ((ProjectModel) parentModel).setVersions(itemListArray); 391 } else if(TAG_VERSION_DESCRIPTION.equals(qName)) { 392 ((VersionModel) childModel).setDescription(getBuffer()); 393 } else if(TAG_VERSION_ID.equals(qName)) { 394 if(itemList == null) { 395 itemList = new Vector(); 396 } 397 itemList.add((VersionModel) findModel(getBuffer())); 398 } else if(TAG_VERSION_NUMBER.equals(qName)) { 399 ((VersionModel) childModel).setVersionInfo(getBuffer()); 400 } 401 } catch(Exception e) { 402 Logger.logDebug("Error importing data.", e); 403 endException = new SAXException ("Error processing tag " + qName + ": " + e.getMessage()); 404 } 405 tagBuffer = null; 406 } 407 408 public void characters(char[] ch, int start, int length) { 409 Logger.logDebug("Read " + ch.length + " Start: " + start + " Length: " + length); 410 Logger.logDebug("String: " + new String (ch, start, length)); 411 if(tagBuffer != null) { 412 tagBuffer.append(ITrackerResources.unescapeUnicodeString(new String (ch, start, length))); 413 } 414 } 415 416 private String getBuffer() { 417 if(tagBuffer == null) { 418 return ""; 419 } else { 420 return tagBuffer.toString(); 421 } 422 } 423 424 private int getBufferAsInt() throws SAXException { 425 if(tagBuffer == null) { 426 return -1; 427 } else { 428 try { 429 return Integer.parseInt(tagBuffer.toString()); 430 } catch(NumberFormatException nfe) { 431 throw new SAXException ("Could not convert string buffer to int value."); 432 } 433 } 434 } 435 436 private long getBufferAsLong() throws SAXException { 437 if(tagBuffer == null) { 438 return -1; 439 } else { 440 try { 441 return Long.parseLong(tagBuffer.toString()); 442 } catch(NumberFormatException nfe) { 443 throw new SAXException ("Could not convert string buffer to long value."); 444 } 445 } 446 } 447 448 private GenericModel findModel(String modelTypeId) { 449 if(modelTypeId != null && ! modelTypeId.equals("")) { 450 for(int i = 0; i < models.size(); i++) { 451 GenericModel model = (GenericModel) models.elementAt(i); 452 if(getModelTypeIdString(model).equalsIgnoreCase(modelTypeId)) { 453 return model; 454 } 455 } 456 } 457 Logger.logDebug("Unable to find model id " + modelTypeId + " during import."); 458 return null; 459 } 460 461 private String getModelTypeIdString(GenericModel model) { 462 String idString = "UNKNOWN"; 463 464 if(model != null && model.getId() != null) { 465 String type = ""; 466 if(model instanceof ComponentModel) { 467 type = TAG_COMPONENT; 468 } else if(model instanceof CustomFieldModel) { 469 type = TAG_CUSTOM_FIELD; 470 } else if(model instanceof IssueModel) { 471 type = TAG_ISSUE; 472 } else if(model instanceof ProjectModel) { 473 type = TAG_PROJECT; 474 } else if(model instanceof UserModel) { 475 type = TAG_USER; 476 } else if(model instanceof VersionModel) { 477 type = TAG_VERSION; 478 } 479 480 idString = type + model.getId(); 481 } 482 483 return idString; 484 } 485 486 private Date getDateValue(String dateString, String qName) throws SAXException { 487 if(dateString == null || "".equals(dateString)) { 488 return new Date(); 489 } 490 491 try { 492 return DATE_FORMATTER.parse(dateString); 493 } catch(Exception e) { 494 throw new SAXException ("Value in " + qName + " did not contain a valid date value."); 495 } 496 } 497 } 498 | Popular Tags |