KickJava   Java API By Example, From Geeks To Geeks.

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


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