KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > emailnotifier > serverimpl > formatters > DocumentUpdatedTemplateFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29
30 public class DocumentUpdatedTemplateFactory implements MailTemplateFactory {
31
32     public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception JavaDoc {
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 JavaDoc modifier;
67         public boolean _private;
68         public Boolean JavaDoc oldPrivate;
69         public boolean retired;
70         public Boolean JavaDoc oldRetired;
71         public String JavaDoc owner;
72         public String JavaDoc oldOwner;
73         public String JavaDoc 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 JavaDoc getSubject(Locale locale) {
80             ResourceBundle bundle = getBundle(locale);
81             return bundle.getString("updated.subject") + " " + docId;
82         }
83
84         public String JavaDoc getMessage(Locale locale) {
85             String JavaDoc message = (String JavaDoc)cachedByLocale.get(locale);
86             if (message == null) {
87                 ResourceBundle bundle = getBundle(locale);
88                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
89                 DateFormat JavaDoc 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