1 16 package org.outerj.daisy.emailnotifier.serverimpl.formatters; 17 18 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplateFactory; 19 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplate; 20 import org.outerj.daisy.emailnotifier.serverimpl.DocumentURLProvider; 21 import org.outerj.daisy.repository.*; 22 import org.outerj.daisy.repository.schema.RepositorySchema; 23 import org.outerj.daisy.repository.schema.DocumentType; 24 import org.outerj.daisy.repository.schema.PartType; 25 import org.outerj.daisy.repository.schema.FieldType; 26 import org.outerj.daisy.repository.variant.VariantManager; 27 import org.outerj.daisy.repository.user.UserManager; 28 import org.outerj.daisy.xmlutil.XmlEncodingDetector; 29 import org.outerj.daisy.docdiff.DocDiffOutputHelper; 30 import org.outerj.daisy.docdiff.DiffGenerator; 31 import org.outerj.daisy.docdiff.DocDiffOutput; 32 import org.outerj.daisy.diff.TextDiffOutput; 33 import org.outerj.daisy.diff.Diff; 34 import org.apache.xmlbeans.XmlObject; 35 import org.outerx.daisy.x10.*; 36 37 import java.util.*; 38 import java.text.DateFormat ; 39 import java.io.StringWriter ; 40 41 public class DocumentVariantUpdatedTemplateFactory implements MailTemplateFactory { 42 private static final long INCLUDE_LIMIT = 200000; 43 44 public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception { 45 DocumentVariantUpdatedDocument documentVariantUpdatedDocument = (DocumentVariantUpdatedDocument)eventDescription; 46 DocumentDocument.Document oldDocumentXml = documentVariantUpdatedDocument.getDocumentVariantUpdated().getOldDocumentVariant().getDocument(); 47 DocumentDocument.Document newDocumentXml = documentVariantUpdatedDocument.getDocumentVariantUpdated().getNewDocumentVariant().getDocument(); 48 Document document = repository.getDocument(newDocumentXml.getId(), newDocumentXml.getBranchId(), newDocumentXml.getLanguageId(), false); 49 50 DocumentVariantUpdatedMailTemplate template = new DocumentVariantUpdatedMailTemplate(); 51 template.repository = repository; 52 RepositorySchema repositorySchema = repository.getRepositorySchema(); 53 UserManager userManager = repository.getUserManager(); 54 VariantManager variantManager = repository.getVariantManager(); 55 56 template.url = urlProvider.getURL(document); 57 template.docId = newDocumentXml.getId(); 58 template.branch = TemplateUtil.getBranchName(newDocumentXml.getBranchId(), variantManager); 59 template.language = TemplateUtil.getLanguageName(newDocumentXml.getLanguageId(), variantManager); 60 template.docName = newDocumentXml.getName(); 61 if (!newDocumentXml.getName().equals(oldDocumentXml.getName())) { 62 template.oldDocName = oldDocumentXml.getName(); 63 } 64 65 template.documentType = repositorySchema.getDocumentTypeById(newDocumentXml.getTypeId(), false); 66 if (oldDocumentXml.getTypeId() != newDocumentXml.getTypeId()) { 67 template.oldDocumentType = repositorySchema.getDocumentTypeById(oldDocumentXml.getTypeId(), false); 68 } 69 70 template.modified = newDocumentXml.getVariantLastModified().getTime(); 71 template.modifier = TemplateUtil.getUserName(newDocumentXml.getVariantLastModifier(), userManager); 72 73 if (oldDocumentXml.getLastVersionId() != newDocumentXml.getLastVersionId()) { 74 template.versionCreated = true; 75 template.newVersionState = newDocumentXml.getNewVersionState().toString(); 76 Version version1 = document.getVersion(oldDocumentXml.getLastVersionId()); 77 Version version2 = document.getVersion(newDocumentXml.getLastVersionId()); 78 79 DocDiffOutputHelper outputHelper = new DocDiffOutputHelper(document, document, version1, version2, repository, null); 80 MyDocDiffOutput diffOutput = new MyDocDiffOutput(template, outputHelper); 81 DiffGenerator diffGenerator = new DiffGenerator(version1, version2, diffOutput); 82 diffGenerator.generateDiff(); 83 } 84 85 DocumentDocument.Document.CustomFields.CustomField[] oldCustomFields = oldDocumentXml.getCustomFields().getCustomFieldArray(); 89 DocumentDocument.Document.CustomFields.CustomField[] newCustomFields = newDocumentXml.getCustomFields().getCustomFieldArray(); 90 91 for (int i = 0; i < oldCustomFields.length; i++) { 92 String name = oldCustomFields[i].getName(); 93 boolean found = false; 94 for (int j = 0; j < newCustomFields.length; j++) { 95 if (newCustomFields[j].getName().equals(name)) { 96 found = true; 97 break; 98 } 99 } 100 if (!found) { 101 CustomFieldInfo customFieldInfo = new CustomFieldInfo(); 102 customFieldInfo.name = name; 103 template.addRemovedCustomField(customFieldInfo); 104 } 105 } 106 107 for (int i = 0; i < newCustomFields.length; i++) { 108 String name = newCustomFields[i].getName(); 109 boolean found = false; 110 int j; 111 for (j = 0; j < oldCustomFields.length; j++) { 112 if (oldCustomFields[j].getName().equals(name)) { 113 found = true; 114 break; 115 } 116 } 117 if (!found) { 118 CustomFieldInfo customFieldInfo = new CustomFieldInfo(); 119 customFieldInfo.name = name; 120 customFieldInfo.value = newCustomFields[i].getValue(); 121 template.addAddedCustomField(customFieldInfo); 122 } else { 123 if (!newCustomFields[i].getValue().equals(oldCustomFields[j].getValue())) { 124 CustomFieldInfo customFieldInfo = new CustomFieldInfo(); 125 customFieldInfo.name = name; 126 customFieldInfo.value = newCustomFields[i].getValue(); 127 customFieldInfo.oldValue = oldCustomFields[j].getValue(); 128 template.addUpdatedCustomField(customFieldInfo); 129 } 130 } 131 } 132 133 long[] oldCollections = oldDocumentXml.getCollectionIds().getCollectionIdArray(); 137 long[] newCollections = newDocumentXml.getCollectionIds().getCollectionIdArray(); 138 CollectionManager collectionManager = repository.getCollectionManager(); 139 140 for (int i = 0; i < oldCollections.length; i++) { 141 long collectionId = oldCollections[i]; 142 boolean found = false; 143 for (int j = 0; j < newCollections.length; j++) { 144 if (newCollections[j] == collectionId) { 145 found = true; 146 break; 147 } 148 } 149 if (!found) { 150 CollectionInfo collectionInfo = new CollectionInfo(); 151 collectionInfo.name = getCollectionName(collectionId, collectionManager); 152 template.addRemovedCollection(collectionInfo); 153 } 154 } 155 156 for (int i = 0; i < newCollections.length; i++) { 157 long collectionId = newCollections[i]; 158 boolean found = false; 159 for (int j = 0; j < oldCollections.length; j++) { 160 if (oldCollections[j] == collectionId) { 161 found = true; 162 break; 163 } 164 } 165 if (!found) { 166 CollectionInfo collectionInfo = new CollectionInfo(); 167 collectionInfo.name = getCollectionName(collectionId, collectionManager); 168 template.addAddedCollection(collectionInfo); 169 } 170 } 171 172 return template; 173 } 174 175 private static String getCollectionName(long collectionId, CollectionManager collectionManager) throws RepositoryException { 176 try { 177 return collectionManager.getCollection(collectionId, false).getName(); 178 } catch (CollectionNotFoundException e) { 179 return String.valueOf(collectionId); 180 } 181 } 182 183 static class DocumentVariantUpdatedMailTemplate implements MailTemplate { 184 private Map cachedByLocale = new HashMap(); 185 public Repository repository; 186 public long docId; 187 public String docName; 188 public String branch; 189 public String language; 190 public String oldDocName; 191 public DocumentType documentType; 192 public DocumentType oldDocumentType; 193 public Date modified; 194 public String modifier; 195 public String url; 196 197 public boolean versionCreated; 198 public String newVersionState; 199 public List parts; 200 public List fields; 201 public List addedLinks; 202 public List removedLinks; 203 public List addedCustomFields; 204 public List removedCustomFields; 205 public List updatedCustomFields; 206 public List addedCollections; 207 public List removedCollections; 208 209 private ResourceBundle getBundle(Locale locale) { 210 return ResourceBundle.getBundle("org/outerj/daisy/emailnotifier/serverimpl/formatters/messages", locale); 211 } 212 213 public String getSubject(Locale locale) { 214 ResourceBundle bundle = getBundle(locale); 215 return bundle.getString("updated.subject") + " " + docName; 216 } 217 218 public String getMessage(Locale locale) { 219 String message = (String )cachedByLocale.get(locale); 220 if (message == null) { 221 ResourceBundle bundle = getBundle(locale); 222 StringBuffer buffer = new StringBuffer (); 223 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale); 224 225 226 buffer.append(bundle.getString("updated.intro")).append('\n'); 227 228 if (url != null) { 229 buffer.append('\n').append(url).append('\n'); 230 } 231 232 buffer.append('\n').append(bundle.getString("common.id")).append(": ").append(docId); 233 buffer.append('\n').append(bundle.getString("common.branch")).append(": ").append(branch); 234 buffer.append('\n').append(bundle.getString("common.language")).append(": ").append(language); 235 buffer.append('\n').append(bundle.getString("common.name")).append(": ").append(docName); 236 if (oldDocName == null) { 237 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 238 } else { 239 buffer.append(" (").append(bundle.getString("updated.previously")).append(' ').append(oldDocName).append(')'); 240 } 241 buffer.append('\n').append(bundle.getString("common.document-type")).append(": ").append(documentType.getLabel(locale)); 242 if (oldDocumentType == null) { 243 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(")"); 244 } else { 245 buffer.append(" (").append(bundle.getString("updated.previously")).append(": ").append(oldDocumentType.getLabel(locale)).append(")"); 246 } 247 buffer.append('\n').append(bundle.getString("updated.updated-on")).append(": ").append(dateFormat.format(modified)); 248 buffer.append('\n').append(bundle.getString("updated.updated-by")).append(": ").append(modifier); 249 250 251 if (versionCreated) { 252 buffer.append("\n\n").append(bundle.getString("updated.version-created")).append(' ').append(bundle.getString("state." + newVersionState)); 253 254 if (parts != null) { 255 String partsTitle = bundle.getString("common.parts-title"); 256 buffer.append("\n\n").append(partsTitle); 257 buffer.append('\n').append(TemplateUtil.repeatChar('=', partsTitle.length())); 258 259 Iterator it = parts.iterator(); 260 while (it.hasNext()) { 261 Object object = it.next(); 262 if (object instanceof UpdatedPartInfo) { 263 UpdatedPartInfo partInfo = (UpdatedPartInfo)object; 264 String label = partInfo.partType.getLabel(locale); 265 buffer.append("\n\n").append(label); 266 buffer.append("\n").append(TemplateUtil.repeatChar('-', label.length())); 267 buffer.append('\n').append(bundle.getString("updated.part-updated")); 268 269 buffer.append("\n").append(bundle.getString("common.part.mime-type")).append(": ").append(partInfo.mimeType); 270 if (partInfo.oldMimeType == null) { 271 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 272 } else { 273 buffer.append(" (").append(bundle.getString("updated.previous-version")).append(": ").append(partInfo.oldMimeType).append(")"); 274 } 275 276 buffer.append("\n").append(bundle.getString("common.part.filename")).append(": ").append(partInfo.fileName); 277 if (partInfo.oldFileName == null) { 278 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 279 } else { 280 buffer.append(" (").append(bundle.getString("updated.previous-version")).append(": ").append(partInfo.oldFileName).append(")"); 281 } 282 283 buffer.append('\n').append(bundle.getString("common.part.size")).append(": ").append(partInfo.size).append(" ").append(bundle.getString("common.bytes")); 284 if (partInfo.oldSize == -1) { 285 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(")"); 286 } else { 287 buffer.append(" (").append(bundle.getString("updated.previous-version")).append(": ").append(partInfo.oldSize).append(" ").append(bundle.getString("common.bytes")).append(")"); 288 } 289 290 if (partInfo.oldContent != null) { 291 StringWriter writer = new StringWriter (); 292 TextDiffOutput textDiffOutput = new TextDiffOutput(writer, false, locale); 293 try { 294 Diff.diff(partInfo.oldContent, partInfo.newContent, textDiffOutput, 3); 295 } catch (Exception e) { 296 throw new RuntimeException (e); 297 } 298 299 buffer.append('\n').append(bundle.getString("updated.content-diff")).append(":\n"); 300 buffer.append(writer.getBuffer()); 301 } 302 } else if (object instanceof AddedPartInfo) { 303 AddedPartInfo partInfo = (AddedPartInfo)object; 304 String label = partInfo.partType.getLabel(locale); 305 buffer.append("\n\n").append(label); 306 buffer.append("\n").append(TemplateUtil.repeatChar('-', label.length())); 307 buffer.append('\n').append(bundle.getString("updated.part-added")); 308 buffer.append('\n').append(bundle.getString("common.part.mime-type")).append(": ").append(partInfo.mimeType); 309 buffer.append('\n').append(bundle.getString("common.part.filename")).append(": ").append(partInfo.fileName); 310 buffer.append('\n').append(bundle.getString("common.part.size")).append(": ").append(partInfo.size).append(" ").append(bundle.getString("common.bytes")); 311 if (partInfo.content != null) { 312 buffer.append('\n').append(bundle.getString("common.part.content")).append(":\n"); 313 buffer.append(partInfo.content); 314 buffer.append("\n"); 315 316 } 317 } else if (object instanceof RemovedPartInfo) { 318 RemovedPartInfo partInfo = (RemovedPartInfo)object; 319 String label = partInfo.partType.getLabel(locale); 320 buffer.append("\n\n"); 321 buffer.append(label); 322 buffer.append("\n").append(TemplateUtil.repeatChar('-', label.length())); 323 buffer.append('\n').append(bundle.getString("updated.part-removed")).append('\n'); 324 332 } else if (object instanceof MightBeUpdatedPartInfo) { 333 MightBeUpdatedPartInfo partInfo = (MightBeUpdatedPartInfo)object; 334 String label = partInfo.partType.getLabel(locale); 335 buffer.append("\n\n"); 336 buffer.append(label); 337 buffer.append("\n").append(bundle.getString("updated.no-diff-too-big")).append("\n"); 338 } 339 } 340 } 341 342 if (fields != null) { 343 String fieldsTitle = bundle.getString("common.fields-title"); 344 buffer.append("\n\n").append(fieldsTitle); 345 buffer.append("\n").append(TemplateUtil.repeatChar('=', fieldsTitle.length())); 346 347 Iterator it = fields.iterator(); 348 while (it.hasNext()) { 349 Object object = it.next(); 350 if (object instanceof AddedFieldInfo) { 351 AddedFieldInfo fieldInfo = (AddedFieldInfo)object; 352 String label = fieldInfo.fieldType.getLabel(locale); 353 String value = FieldHelper.getFormattedValue(fieldInfo.value, fieldInfo.fieldType.getValueType(), locale, repository); 354 buffer.append("\n").append(label).append(": ").append(value).append(" (").append(bundle.getString("updated.new-field")).append(')'); 355 } else if (object instanceof RemovedFieldInfo) { 356 RemovedFieldInfo fieldInfo = (RemovedFieldInfo)object; 357 String label = fieldInfo.fieldType.getLabel(locale); 358 buffer.append("\n").append(label).append(": (").append(bundle.getString("updated.field-removed")).append(")"); 359 } else if (object instanceof UpdatedFieldInfo) { 360 UpdatedFieldInfo fieldInfo = (UpdatedFieldInfo)object; 361 String label = fieldInfo.fieldType.getLabel(locale); 362 String value1 = FieldHelper.getFormattedValue(fieldInfo.value, fieldInfo.fieldType.getValueType(), locale, repository); 363 String value2 = FieldHelper.getFormattedValue(fieldInfo.oldValue, fieldInfo.fieldType.getValueType(), locale, repository); 364 buffer.append("\n").append(label).append(": ").append(value1).append(" (").append(bundle.getString("updated.previous-version")).append(": ").append(value2).append(")"); 365 } 366 } 367 } 368 369 if (removedLinks != null || addedLinks != null) { 370 String linksTitle = bundle.getString("common.links-title"); 371 buffer.append("\n\n").append(linksTitle); 372 buffer.append("\n").append(TemplateUtil.repeatChar('=', linksTitle.length())); 373 374 if (removedLinks != null) { 375 Iterator it = removedLinks.iterator(); 376 while (it.hasNext()) { 377 LinkInfo linkInfo = (LinkInfo)it.next(); 378 buffer.append('\n').append(bundle.getString("updated.removed-link")); 379 buffer.append("\n ").append(bundle.getString("common.link.title")).append(": ").append(linkInfo.title); 380 buffer.append("\n ").append(bundle.getString("common.link.target")).append(": ").append(linkInfo.target); 381 } 382 } 383 384 if (addedLinks != null) { 385 Iterator it = addedLinks.iterator(); 386 while (it.hasNext()) { 387 LinkInfo linkInfo = (LinkInfo)it.next(); 388 buffer.append('\n').append(bundle.getString("updated.added-link")); 389 buffer.append("\n ").append(bundle.getString("common.link.title")).append(": ").append(linkInfo.title); 390 buffer.append("\n ").append(bundle.getString("common.link.target")).append(": ").append(linkInfo.target); 391 } 392 } 393 } 394 } else { 395 buffer.append("\n\n").append(bundle.getString("updated.no-new-version")); 396 } 397 398 399 if (removedCustomFields != null || addedCustomFields != null || updatedCustomFields != null) { 400 String customFieldsTitle = bundle.getString("common.customfields-title"); 401 buffer.append("\n\n").append(customFieldsTitle); 402 buffer.append('\n').append(TemplateUtil.repeatChar('=', customFieldsTitle.length())); 403 404 if (removedCustomFields != null) { 405 Iterator it = removedCustomFields.iterator(); 406 while (it.hasNext()) { 407 CustomFieldInfo customFieldInfo = (CustomFieldInfo)it.next(); 408 buffer.append("\n").append(customFieldInfo.name).append(": (").append(bundle.getString("updated.removed")).append(")"); 409 } 410 } 411 if (addedCustomFields != null) { 412 Iterator it = addedCustomFields.iterator(); 413 while (it.hasNext()) { 414 CustomFieldInfo customFieldInfo = (CustomFieldInfo)it.next(); 415 buffer.append("\n").append(customFieldInfo.name).append(": ").append(customFieldInfo.value).append(" (").append(bundle.getString("updated.new")).append(")"); 416 } 417 } 418 if (updatedCustomFields != null) { 419 Iterator it = updatedCustomFields.iterator(); 420 while (it.hasNext()) { 421 CustomFieldInfo customFieldInfo = (CustomFieldInfo)it.next(); 422 buffer.append("\n").append(customFieldInfo.name).append(": ").append(customFieldInfo.value).append(" (").append(bundle.getString("updated.previously")).append(": ").append(customFieldInfo.oldValue).append(")"); 423 } 424 } 425 } 426 427 428 if (removedCollections != null || addedCollections != null) { 429 String collectionsTitle = bundle.getString("common.collections-title"); 430 buffer.append("\n\n").append(collectionsTitle); 431 buffer.append('\n').append(TemplateUtil.repeatChar('=', collectionsTitle.length())); 432 433 if (removedCollections != null) { 434 Iterator it = removedCollections.iterator(); 435 while (it.hasNext()) { 436 CollectionInfo collectionInfo = (CollectionInfo)it.next(); 437 buffer.append('\n').append(bundle.getString("updated.removed-from-collection")).append(' ').append(collectionInfo.name); 438 } 439 } 440 if (addedCollections != null) { 441 Iterator it = addedCollections.iterator(); 442 while (it.hasNext()) { 443 CollectionInfo collectionInfo = (CollectionInfo)it.next(); 444 buffer.append('\n').append(bundle.getString("updated.added-to-collection")).append(' ').append(collectionInfo.name); 445 } 446 } 447 } 448 449 message = buffer.toString(); 450 cachedByLocale.put(locale, message); 451 } 452 return message; 453 } 454 455 public void addRemovedPart(RemovedPartInfo removedPartInfo) { 456 if (parts == null) 457 parts = new ArrayList(); 458 parts.add(removedPartInfo); 459 } 460 461 public void addAddedPart(AddedPartInfo addedPartInfo) { 462 if (parts == null) 463 parts = new ArrayList(); 464 parts.add(addedPartInfo); 465 } 466 467 public void addUpdatedPart(UpdatedPartInfo updatedPartInfo) { 468 if (parts == null) 469 parts = new ArrayList(); 470 parts.add(updatedPartInfo); 471 } 472 473 public void addMightBeUpdatedPart(MightBeUpdatedPartInfo partInfo) { 474 if (parts == null) 475 parts = new ArrayList(); 476 parts.add(partInfo); 477 } 478 479 public void addUnchangedPart(UnchangedPartInfo unchangedPartInfo) { 480 485 } 486 487 public void addAddedField(AddedFieldInfo addedFieldInfo) { 488 if (fields == null) 489 fields = new ArrayList(); 490 fields.add(addedFieldInfo); 491 } 492 493 public void addRemovedField(RemovedFieldInfo removedFieldInfo) { 494 if (fields == null) 495 fields = new ArrayList(); 496 fields.add(removedFieldInfo); 497 } 498 499 public void addUpdatedField(UpdatedFieldInfo updatedFieldInfo) { 500 if (fields == null) 501 fields = new ArrayList(); 502 fields.add(updatedFieldInfo); 503 } 504 505 public void addAddedLink(LinkInfo linkInfo) { 506 if (addedLinks == null) 507 addedLinks = new ArrayList(); 508 addedLinks.add(linkInfo); 509 } 510 511 public void addRemovedLink(LinkInfo linkInfo) { 512 if (removedLinks == null) 513 removedLinks = new ArrayList(); 514 removedLinks.add(linkInfo); 515 } 516 517 public void addAddedCustomField(CustomFieldInfo customFieldInfo) { 518 if (addedCustomFields == null) 519 addedCustomFields = new ArrayList(); 520 addedCustomFields.add(customFieldInfo); 521 } 522 523 public void addRemovedCustomField(CustomFieldInfo customFieldInfo) { 524 if (removedCustomFields == null) 525 removedCustomFields = new ArrayList(); 526 removedCustomFields.add(customFieldInfo); 527 } 528 529 public void addUpdatedCustomField(CustomFieldInfo customFieldInfo) { 530 if (updatedCustomFields == null) 531 updatedCustomFields = new ArrayList(); 532 updatedCustomFields.add(customFieldInfo); 533 } 534 535 public void addAddedCollection(CollectionInfo collectionInfo) { 536 if (addedCollections == null) 537 addedCollections = new ArrayList(); 538 addedCollections.add(collectionInfo); 539 } 540 541 public void addRemovedCollection(CollectionInfo collectionInfo) { 542 if (removedCollections == null) 543 removedCollections = new ArrayList(); 544 removedCollections.add(collectionInfo); 545 } 546 } 547 548 static class RemovedPartInfo { 549 public PartType partType; 550 } 551 552 static class AddedPartInfo { 553 public PartType partType; 554 public String mimeType; 555 public String fileName; 556 public long size; 557 public String content; 558 } 559 560 static class UpdatedPartInfo { 561 public PartType partType; 562 public String mimeType; 563 public String oldMimeType; 564 public String fileName; 565 public String oldFileName; 566 public long size; 567 public long oldSize = -1; 568 public String oldContent; 569 public String newContent; 570 } 571 572 static class UnchangedPartInfo { 573 public PartType partType; 574 } 575 576 static class MightBeUpdatedPartInfo { 577 public PartType partType; 578 } 579 580 static class AddedFieldInfo { 581 public FieldType fieldType; 582 public Object value; 583 } 584 585 static class UpdatedFieldInfo { 586 public FieldType fieldType; 587 public Object value; 588 public Object oldValue; 589 } 590 591 static class RemovedFieldInfo { 592 public FieldType fieldType; 593 } 594 595 static class LinkInfo { 596 public String title; 597 public String target; 598 } 599 600 static class CustomFieldInfo { 601 public String name; 602 public String value; 603 public String oldValue; 604 } 605 606 static class CollectionInfo { 607 public String name; 608 } 609 610 static class MyDocDiffOutput implements DocDiffOutput { 611 private DocumentVariantUpdatedMailTemplate template; 612 private DocDiffOutputHelper outputHelper; 613 614 public MyDocDiffOutput(DocumentVariantUpdatedMailTemplate template, DocDiffOutputHelper outputHelper) { 615 this.outputHelper = outputHelper; 616 this.template = template; 617 } 618 619 public void begin() throws Exception { 620 } 621 622 public void end() throws Exception { 623 } 624 625 public void beginPartChanges() throws Exception { 626 } 627 628 public void partRemoved(Part removedPart) throws Exception { 629 RemovedPartInfo removedPartInfo = new RemovedPartInfo(); 630 removedPartInfo.partType = outputHelper.getPartType(removedPart.getTypeId()); 631 template.addRemovedPart(removedPartInfo); 632 } 633 634 public void partAdded(Part addedPart) throws Exception { 635 AddedPartInfo partInfo = new AddedPartInfo(); 636 partInfo.partType = outputHelper.getPartType(addedPart.getTypeId()); 637 String mimeType = addedPart.getMimeType(); 638 partInfo.mimeType = mimeType; 639 partInfo.fileName = addedPart.getFileName(); 640 partInfo.size = addedPart.getSize(); 641 642 if (mimeType.startsWith("text/") && addedPart.getSize() < INCLUDE_LIMIT) { 643 byte[] data = addedPart.getData(); 644 String encoding = null; 645 if (mimeType.equals("text/xml")) { 646 encoding = XmlEncodingDetector.detectEncoding(data); 647 } 648 partInfo.content = encoding != null ? new String (addedPart.getData(), encoding) : new String (addedPart.getData()); 649 } 650 651 template.addAddedPart(partInfo); 652 } 653 654 public void partUnchanged(Part unchangedPart) throws Exception { 655 UnchangedPartInfo partInfo = new UnchangedPartInfo(); 656 partInfo.partType = outputHelper.getPartType(unchangedPart.getTypeId()); 657 template.addUnchangedPart(partInfo); 658 } 659 660 public void partUpdated(Part version1Part, Part version2Part, String part1Data, String part2Data) throws Exception { 661 UpdatedPartInfo partInfo = new UpdatedPartInfo(); 662 partInfo.partType = outputHelper.getPartType(version1Part.getTypeId()); 663 664 partInfo.mimeType = version2Part.getMimeType(); 665 if (!version1Part.getMimeType().equals(version2Part.getMimeType())) { 666 partInfo.oldMimeType = version1Part.getMimeType(); 667 } 668 669 String version1FileName = nullToEmpty(version1Part.getFileName()); 670 String version2FileName = nullToEmpty(version2Part.getFileName()); 671 partInfo.fileName = version2FileName; 672 if (!version1FileName.equals(version2FileName)) { 673 partInfo.oldFileName = version1FileName; 674 } 675 676 partInfo.size = version2Part.getSize(); 677 if (version1Part.getSize() != version2Part.getSize()) { 678 partInfo.oldSize = version1Part.getSize(); 679 } 680 681 if (part1Data != null) { 682 partInfo.oldContent = part1Data; 683 partInfo.newContent = part2Data; 684 } 685 686 template.addUpdatedPart(partInfo); 687 } 688 689 private String nullToEmpty(String value) { 690 if (value == null) 691 return ""; 692 return value; 693 } 694 695 public void partMightBeUpdated(Part version2Part) throws Exception { 696 MightBeUpdatedPartInfo partInfo = new MightBeUpdatedPartInfo(); 697 partInfo.partType = outputHelper.getPartType(version2Part.getTypeId()); 698 template.addMightBeUpdatedPart(partInfo); 699 } 700 701 public void endPartChanges() throws Exception { 702 } 703 704 public void beginFieldChanges() throws Exception { 705 } 706 707 public void endFieldChanges() throws Exception { 708 } 709 710 public void fieldAdded(Field addedField) throws Exception { 711 AddedFieldInfo fieldInfo = new AddedFieldInfo(); 712 fieldInfo.fieldType = outputHelper.getFieldType(addedField.getTypeId()); 713 fieldInfo.value = addedField.getValue(); 714 template.addAddedField(fieldInfo); 715 } 716 717 public void fieldRemoved(Field removedField) throws Exception { 718 RemovedFieldInfo fieldInfo = new RemovedFieldInfo(); 719 fieldInfo.fieldType = outputHelper.getFieldType(removedField.getTypeId()); 720 template.addRemovedField(fieldInfo); 721 } 722 723 public void fieldUpdated(Field version1Field, Field version2Field) throws Exception { 724 UpdatedFieldInfo fieldInfo = new UpdatedFieldInfo(); 725 fieldInfo.fieldType = outputHelper.getFieldType(version1Field.getTypeId()); 726 fieldInfo.value = version2Field.getValue(); 727 fieldInfo.oldValue = version1Field.getValue(); 728 template.addUpdatedField(fieldInfo); 729 } 730 731 public void beginLinkChanges() throws Exception { 732 } 733 734 public void linkRemoved(Link link) throws Exception { 735 LinkInfo linkInfo = new LinkInfo(); 736 linkInfo.title = link.getTitle(); 737 linkInfo.target = link.getTarget(); 738 template.addRemovedLink(linkInfo); 739 } 740 741 public void linkAdded(Link link) throws Exception { 742 LinkInfo linkInfo = new LinkInfo(); 743 linkInfo.title = link.getTitle(); 744 linkInfo.target = link.getTarget(); 745 template.addAddedLink(linkInfo); 746 } 747 748 public void endLinkChanges() throws Exception { 749 } 750 } 751 } 752 | Popular Tags |