KickJava   Java API By Example, From Geeks To Geeks.

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


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.user.UserManager;
23 import org.outerj.daisy.repository.variant.VariantManager;
24 import org.apache.xmlbeans.XmlObject;
25 import org.outerx.daisy.x10.DocumentDocument;
26 import org.outerx.daisy.x10.DocumentVariantDeletedDocument;
27
28 import java.util.*;
29 import java.text.DateFormat JavaDoc;
30
31 public class DocumentVariantDeletedTemplateFactory implements MailTemplateFactory {
32     public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception JavaDoc {
33         DocumentVariantDeletedDocument documentVariantDeletedDocument = (DocumentVariantDeletedDocument)eventDescription;
34         DocumentVariantDeletedDocument.DocumentVariantDeleted documentVariantDeletedXml = documentVariantDeletedDocument.getDocumentVariantDeleted();
35         DocumentDocument.Document documentXml = documentVariantDeletedXml.getDeletedDocumentVariant().getDocument();
36
37         VariantManager variantManager = repository.getVariantManager();
38         UserManager userManager = repository.getUserManager();
39
40         DocumentVariantDeletedMailTemplate template = new DocumentVariantDeletedMailTemplate();
41         template.documentId = documentXml.getId();
42         template.branch = TemplateUtil.getBranchName(documentXml.getBranchId(), variantManager);
43         template.language = TemplateUtil.getLanguageName(documentXml.getLanguageId(), variantManager);
44         template.documentName = documentXml.getName();
45         template.deleter = TemplateUtil.getUserName(documentVariantDeletedXml.getDeleterId(), userManager);
46         template.deletedOn = documentVariantDeletedXml.getDeletedTime().getTime();
47
48         return template;
49     }
50
51     static class DocumentVariantDeletedMailTemplate implements MailTemplate
52     {
53         private Map cachedByLocale = new HashMap();
54         long documentId;
55         String JavaDoc branch;
56         String JavaDoc language;
57         String JavaDoc documentName;
58         String JavaDoc deleter;
59         Date deletedOn;
60
61         private ResourceBundle getBundle(Locale locale) {
62             return ResourceBundle.getBundle("org/outerj/daisy/emailnotifier/serverimpl/formatters/messages", locale);
63         }
64
65         public String JavaDoc getSubject(Locale locale) {
66             ResourceBundle bundle = getBundle(locale);
67             return bundle.getString("deleted.subject") + " " + documentName;
68         }
69
70         public String JavaDoc getMessage(Locale locale) {
71             String JavaDoc message = (String JavaDoc)cachedByLocale.get(locale);
72             if (message == null) {
73                 DateFormat JavaDoc dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
74
75                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
76                 ResourceBundle bundle = getBundle(locale);
77                 buffer.append(bundle.getString("deleted.intro")).append('\n');
78                 buffer.append(bundle.getString("common.id")).append(": ").append(documentId).append("\n");
79                 buffer.append(bundle.getString("common.branch")).append(": ").append(branch).append("\n");
80                 buffer.append(bundle.getString("common.language")).append(": ").append(language).append("\n");
81                 buffer.append(bundle.getString("common.name")).append(": ").append(documentName).append("\n");
82                 buffer.append("\n");
83                 buffer.append(bundle.getString("deleted.deleter")).append(": ").append(deleter).append("\n");
84                 buffer.append(bundle.getString("deleted.deleted-on")).append(": ").append(dateFormat.format(deletedOn)).append("\n");
85
86                 message = buffer.toString();
87                 cachedByLocale.put(locale, message);
88             }
89             return message;
90         }
91     }
92 }
93
Popular Tags