KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.Repository;
22 import org.outerj.daisy.repository.Document;
23 import org.outerj.daisy.repository.variant.VariantManager;
24 import org.apache.xmlbeans.XmlObject;
25 import org.outerx.daisy.x10.CommentCreatedDocument;
26 import org.outerx.daisy.x10.CommentDocument;
27
28 import java.util.*;
29 import java.text.DateFormat JavaDoc;
30
31 public class CommentCreatedTemplateFactory implements MailTemplateFactory {
32     public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception JavaDoc {
33         CommentCreatedDocument commentCreatedDocument = (CommentCreatedDocument)eventDescription;
34         CommentDocument.Comment commentXml = commentCreatedDocument.getCommentCreated().getNewComment().getComment();
35
36         Document document = repository.getDocument(commentXml.getDocumentId(), commentXml.getBranchId(), commentXml.getLanguageId(), false);
37         VariantManager variantManager = repository.getVariantManager();
38
39         CommentCreatedMailTemplate template = new CommentCreatedMailTemplate();
40         template.docId = String.valueOf(document.getId());
41         template.docName = document.getName();
42         template.branch = TemplateUtil.getBranchName(commentXml.getBranchId(), variantManager);
43         template.language = TemplateUtil.getLanguageName(commentXml.getLanguageId(), variantManager);
44         template.url = urlProvider.getURL(document);
45         template.creator = repository.getUserManager().getUserDisplayName(commentXml.getCreatedBy());
46         template.createdOn = commentXml.getCreatedOn().getTime();
47         template.commentText = commentXml.getContent();
48         template.commentVisibility = commentXml.getVisibility().toString();
49
50         return template;
51     }
52
53     static class CommentCreatedMailTemplate implements MailTemplate {
54         private Map cachedByLocale = new HashMap();
55         private String JavaDoc docName;
56         private String JavaDoc docId;
57         private String JavaDoc branch;
58         private String JavaDoc language;
59         private String JavaDoc commentText;
60         private String JavaDoc creator;
61         private Date createdOn;
62         private String JavaDoc commentVisibility;
63         private String JavaDoc url;
64
65         private ResourceBundle getBundle(Locale locale) {
66             return ResourceBundle.getBundle("org/outerj/daisy/emailnotifier/serverimpl/formatters/messages", locale);
67         }
68
69         public String JavaDoc getSubject(Locale locale) {
70             ResourceBundle bundle = getBundle(locale);
71             return bundle.getString("commentcreated.subject") + " " + docName;
72         }
73
74         public String JavaDoc getMessage(Locale locale) {
75             String JavaDoc message = (String JavaDoc)cachedByLocale.get(locale);
76             if (message == null) {
77                 ResourceBundle bundle = getBundle(locale);
78                 DateFormat JavaDoc dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
79
80                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81                 buffer.append(bundle.getString("commentcreated.intro"));
82                 buffer.append("\n");
83                 if (url != null) {
84                     buffer.append('\n').append(url).append('\n');
85                 }
86                 buffer.append('\n').append(bundle.getString("common.id")).append(": ").append(docId);
87                 buffer.append('\n').append(bundle.getString("common.name")).append(": ").append(docName);
88                 buffer.append('\n').append(bundle.getString("common.branch")).append(": ").append(branch);
89                 buffer.append('\n').append(bundle.getString("common.language")).append(": ").append(language);
90                 buffer.append("\n\n").append(bundle.getString("commentcreated.createdby")).append(": ").append(creator);
91                 buffer.append("\n").append(bundle.getString("commentcreated.createdon")).append(": ").append(dateFormat.format(createdOn));
92                 buffer.append("\n").append(bundle.getString("commentcreated.visibility")).append(": ").append(bundle.getString("commentcreated.visibility-" + commentVisibility));
93                 buffer.append("\n\n");
94                 buffer.append(commentText);
95
96                 message = buffer.toString();
97                 cachedByLocale.put(locale, message);
98             }
99             return message;
100         }
101     }
102 }
103
Popular Tags