1 16 package org.outerj.daisy.emailnotifier.serverimpl.formatters; 17 18 import org.outerj.daisy.repository.*; 19 import org.outerj.daisy.repository.user.UserManager; 20 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplate; 21 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplateFactory; 22 import org.outerj.daisy.emailnotifier.serverimpl.DocumentURLProvider; 23 import org.outerx.daisy.x10.DocumentUpdatedDocument; 24 import org.outerx.daisy.x10.DocumentDocument; 25 import org.apache.xmlbeans.XmlObject; 26 27 import java.util.*; 28 import java.text.DateFormat ; 29 30 public class DocumentUpdatedTemplateFactory implements MailTemplateFactory { 31 32 public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception { 33 DocumentUpdatedDocument documentUpdatedDocument = (DocumentUpdatedDocument)eventDescription; 34 DocumentDocument.Document oldDocumentXml = documentUpdatedDocument.getDocumentUpdated().getOldDocument().getDocument(); 35 DocumentDocument.Document newDocumentXml = documentUpdatedDocument.getDocumentUpdated().getNewDocument().getDocument(); 36 Document document = repository.getDocument(newDocumentXml.getId(), false); 37 38 DocumentUpdatedMailTemplate template = new DocumentUpdatedMailTemplate(); 39 UserManager userManager = repository.getUserManager(); 40 41 template.url = urlProvider.getURL(document); 42 template.docId = newDocumentXml.getId(); 43 44 template.modified = newDocumentXml.getLastModified().getTime(); 45 template.modifier = TemplateUtil.getUserName(newDocumentXml.getLastModifier(), userManager); 46 template._private = newDocumentXml.getPrivate(); 47 if (newDocumentXml.getPrivate() != oldDocumentXml.getPrivate()) { 48 template.oldPrivate = oldDocumentXml.getPrivate() ? Boolean.TRUE : Boolean.FALSE; 49 } 50 template.retired = newDocumentXml.getRetired(); 51 if (newDocumentXml.getRetired() != oldDocumentXml.getRetired()) { 52 template.oldRetired = oldDocumentXml.getRetired() ? Boolean.TRUE : Boolean.FALSE; 53 } 54 template.owner = TemplateUtil.getUserName(newDocumentXml.getOwner(), userManager); 55 if (newDocumentXml.getOwner() != oldDocumentXml.getOwner()) { 56 template.oldOwner = TemplateUtil.getUserName(oldDocumentXml.getOwner(), userManager); 57 } 58 59 return template; 60 } 61 62 static class DocumentUpdatedMailTemplate implements MailTemplate { 63 private Map cachedByLocale = new HashMap(); 64 public long docId; 65 public Date modified; 66 public String modifier; 67 public boolean _private; 68 public Boolean oldPrivate; 69 public boolean retired; 70 public Boolean oldRetired; 71 public String owner; 72 public String oldOwner; 73 public String url; 74 75 private ResourceBundle getBundle(Locale locale) { 76 return ResourceBundle.getBundle("org/outerj/daisy/emailnotifier/serverimpl/formatters/messages", locale); 77 } 78 79 public String getSubject(Locale locale) { 80 ResourceBundle bundle = getBundle(locale); 81 return bundle.getString("updated.subject") + " " + docId; 82 } 83 84 public String getMessage(Locale locale) { 85 String message = (String )cachedByLocale.get(locale); 86 if (message == null) { 87 ResourceBundle bundle = getBundle(locale); 88 StringBuffer buffer = new StringBuffer (); 89 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale); 90 91 92 buffer.append(bundle.getString("updated.intro")).append('\n'); 93 94 if (url != null) { 95 buffer.append('\n').append(url).append('\n'); 96 } 97 98 buffer.append('\n').append(bundle.getString("common.id")).append(": ").append(docId); 99 buffer.append('\n').append(bundle.getString("updated.updated-on")).append(": ").append(dateFormat.format(modified)); 100 buffer.append('\n').append(bundle.getString("updated.updated-by")).append(": ").append(modifier); 101 buffer.append('\n').append(bundle.getString("updated.owner")).append(": ").append(owner); 102 if (oldOwner == null) { 103 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 104 } else { 105 buffer.append(" (").append(bundle.getString("updated.previously")).append(": ").append(oldOwner).append(")"); 106 } 107 buffer.append('\n').append(bundle.getString("common.private")).append(": ").append(bundle.getString("common." + _private)); 108 if (oldPrivate == null) { 109 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 110 } else { 111 buffer.append(" (").append(bundle.getString("updated.previously")).append(": ").append(bundle.getString("common." + oldPrivate)).append(")"); 112 } 113 buffer.append('\n').append(bundle.getString("common.retired")).append(": ").append(bundle.getString("common." + retired)); 114 if (oldRetired == null) { 115 buffer.append(" (").append(bundle.getString("updated.unchanged")).append(')'); 116 } else { 117 buffer.append(" (").append(bundle.getString("updated.previously")).append(": ").append(bundle.getString("common." + oldRetired)).append(")"); 118 } 119 120 message = buffer.toString(); 121 cachedByLocale.put(locale, message); 122 } 123 return message; 124 } 125 126 } 127 128 } 129 | Popular Tags |