KickJava   Java API By Example, From Geeks To Geeks.

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


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.variant.VariantManager;
20 import org.outerj.daisy.repository.user.UserManager;
21 import org.outerj.daisy.repository.schema.RepositorySchema;
22 import org.outerj.daisy.repository.schema.PartType;
23 import org.outerj.daisy.repository.schema.FieldType;
24 import org.outerj.daisy.repository.schema.DocumentType;
25 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplate;
26 import org.outerj.daisy.emailnotifier.serverimpl.MailTemplateFactory;
27 import org.outerj.daisy.emailnotifier.serverimpl.DocumentURLProvider;
28 import org.outerj.daisy.xmlutil.XmlEncodingDetector;
29 import org.outerx.daisy.x10.DocumentDocument;
30 import org.outerx.daisy.x10.LinksDocument;
31 import org.outerx.daisy.x10.DocumentVariantCreatedDocument;
32 import org.apache.xmlbeans.XmlObject;
33
34 import java.text.DateFormat JavaDoc;
35 import java.util.*;
36
37 public class DocumentVariantCreatedTemplateFactory implements MailTemplateFactory {
38
39     private static final long INCLUDE_LIMIT = 200000;
40
41     public MailTemplate createMailTemplate(XmlObject eventDescription, Repository repository, DocumentURLProvider urlProvider) throws Exception JavaDoc {
42         DocumentVariantCreatedDocument documentVariantCreatedDocument = (DocumentVariantCreatedDocument)eventDescription;
43         DocumentVariantCreatedDocument.DocumentVariantCreated documentVariantCreatedXml = documentVariantCreatedDocument.getDocumentVariantCreated();
44         DocumentDocument.Document documentXml = documentVariantCreatedXml.getNewDocumentVariant().getDocument();
45
46         Document document = repository.getDocument(documentXml.getId(), documentXml.getBranchId(), documentXml.getLanguageId(), false);
47         Version version = document.getVersion(1);
48         UserManager userManager = repository.getUserManager();
49         VariantManager variantManager = repository.getVariantManager();
50         RepositorySchema repositorySchema = repository.getRepositorySchema();
51         DocumentType documentType = repositorySchema.getDocumentTypeById(documentXml.getTypeId(), false);
52
53         DocumentVariantCreatedMailTemplate template = new DocumentVariantCreatedMailTemplate();
54         template.repository = repository;
55         template.docId = documentXml.getId();
56         template.branch = TemplateUtil.getBranchName(documentXml.getBranchId(), variantManager);
57         template.language = TemplateUtil.getLanguageName(documentXml.getLanguageId(), variantManager);
58         template.docName = documentXml.getName();
59         template.docType = documentType;
60         template.created = documentXml.getVariantLastModified().getTime();
61         template.creator = TemplateUtil.getUserName(documentXml.getVariantLastModifier(), userManager);
62         template.state = documentXml.getNewVersionState().toString();
63         template.url = urlProvider.getURL(document);
64
65         Part[] parts = version.getPartsInOrder().getArray();
66         for (int i = 0; i < parts.length; i++) {
67             PartInfo partInfo = new PartInfo();
68             partInfo.partType = repositorySchema.getPartTypeById(parts[i].getTypeId(), false);
69             String JavaDoc mimeType = parts[i].getMimeType();
70             partInfo.mimeType = mimeType;
71             partInfo.size = parts[i].getSize();
72
73             if (mimeType.startsWith("text/") && parts[i].getSize() < INCLUDE_LIMIT) {
74                 byte[] data = parts[i].getData();
75                 String JavaDoc encoding = null;
76                 if (mimeType.equals("text/xml")) {
77                     encoding = XmlEncodingDetector.detectEncoding(data);
78                 }
79                 partInfo.content = encoding != null ? new String JavaDoc(data, encoding) : new String JavaDoc(data);
80             }
81             template.addPart(partInfo);
82         }
83
84         LinksDocument.Links.Link[] links = documentXml.getLinks().getLinkArray();
85         for (int i = 0; i < links.length; i++) {
86             LinkInfo link = new LinkInfo();
87             link.title = links[i].getTitle();
88             link.target = links[i].getTarget();
89             template.addLink(link);
90         }
91
92         DocumentDocument.Document.CustomFields.CustomField[] customFields = documentXml.getCustomFields().getCustomFieldArray();
93         for (int i = 0; i < customFields.length; i++) {
94             CustomFieldInfo customFieldInfo = new CustomFieldInfo();
95             customFieldInfo.name = customFields[i].getName();
96             customFieldInfo.value = customFields[i].getValue();
97             template.addCustomField(customFieldInfo);
98         }
99
100         Field[] fields = version.getFieldsInOrder().getArray();
101         for (int i = 0; i < fields.length; i++) {
102             FieldInfo fieldInfo = new FieldInfo();
103             fieldInfo.fieldType = repositorySchema.getFieldTypeById(fields[i].getTypeId(), false);
104             fieldInfo.value = fields[i].getValue();
105             fieldInfo.valueType = fields[i].getValueType();
106             template.addField(fieldInfo);
107         }
108
109
110         long[] collectionIds = documentXml.getCollectionIds().getCollectionIdArray();
111         CollectionManager collectionManager = repository.getCollectionManager();
112         for (int i = 0; i < collectionIds.length; i++) {
113             CollectionInfo collectionInfo = new CollectionInfo();
114             collectionInfo.name = collectionManager.getCollection(collectionIds[i], false).getName();
115             template.addCollection(collectionInfo);
116         }
117
118         return template;
119     }
120
121     static class DocumentVariantCreatedMailTemplate implements MailTemplate
122     {
123         private Map cachedByLocale = new HashMap();
124         private Repository repository;
125         public String JavaDoc subject;
126         public long docId;
127         public String JavaDoc branch;
128         public String JavaDoc language;
129         public String JavaDoc docName;
130         public DocumentType docType;
131         public Date created;
132         public String JavaDoc creator;
133         public String JavaDoc state;
134         public List parts;
135         public List links;
136         public List fields;
137         public List customFields;
138         public List collections;
139         public String JavaDoc url;
140
141         private ResourceBundle getBundle(Locale locale) {
142             return ResourceBundle.getBundle("org/outerj/daisy/emailnotifier/serverimpl/formatters/messages", locale);
143         }
144
145         public String JavaDoc getSubject(Locale locale) {
146             ResourceBundle bundle = getBundle(locale);
147             return bundle.getString("created.subject") + " " + docName;
148         }
149
150         public String JavaDoc getMessage(Locale locale) {
151             String JavaDoc message = (String JavaDoc)cachedByLocale.get(locale);
152             if (message == null) {
153                 ResourceBundle bundle = getBundle(locale);
154                 DateFormat JavaDoc dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
155
156                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
157                 buffer.append(bundle.getString("created.intro")).append('\n');
158                 if (url != null) {
159                     buffer.append('\n').append(url).append('\n');
160                 }
161                 buffer.append('\n').append(bundle.getString("common.id")).append(": ").append(docId);
162                 buffer.append('\n').append(bundle.getString("common.branch")).append(": ").append(branch);
163                 buffer.append('\n').append(bundle.getString("common.language")).append(": ").append(language);
164                 buffer.append('\n').append(bundle.getString("common.name")).append(": ").append(docName);
165                 buffer.append('\n').append(bundle.getString("common.document-type")).append(": ").append(docType.getLabel(locale));
166                 buffer.append('\n').append(bundle.getString("created.created")).append(": ").append(dateFormat.format(created));
167                 buffer.append('\n').append(bundle.getString("created.creator")).append(": ").append(creator);
168                 buffer.append('\n').append(bundle.getString("created.state")).append(": ").append(bundle.getString("state." + state));
169
170                 if (parts != null) {
171                     String JavaDoc title = bundle.getString("common.parts-title");
172                     buffer.append("\n\n").append(title);
173                     buffer.append('\n').append(TemplateUtil.repeatChar('=', title.length()));
174                     Iterator partIt = parts.iterator();
175                     while (partIt.hasNext()) {
176                         PartInfo partInfo = (PartInfo)partIt.next();
177                         String JavaDoc label = partInfo.partType.getLabel(locale);
178                         buffer.append("\n\n");
179                         buffer.append(label);
180                         buffer.append("\n");
181                         buffer.append(TemplateUtil.repeatChar('-', label.length()));
182                         buffer.append('\n').append(bundle.getString("common.part.mime-type")).append(": ").append(partInfo.mimeType);
183                         buffer.append('\n').append(bundle.getString("common.part.size")).append(": ").append(partInfo.size).append(' ').append(bundle.getString("common.bytes"));
184                         if (partInfo.content != null) {
185                             buffer.append('\n').append(bundle.getString("common.part.content")).append(":\n");
186                             buffer.append(partInfo.content);
187                         }
188                     }
189                 }
190
191                 if (links != null) {
192                     String JavaDoc title = bundle.getString("common.links-title");
193                     buffer.append("\n\n").append(title);
194                     buffer.append('\n').append(TemplateUtil.repeatChar('=', title.length()));
195
196                     Iterator linkIt = links.iterator();
197                     while (linkIt.hasNext()) {
198                         LinkInfo link = (LinkInfo)linkIt.next();
199                         buffer.append("\n\n").append(bundle.getString("common.link.title")).append(": ").append(link.title);
200                         buffer.append("\n").append(bundle.getString("common.link.target")).append(": ").append(link.target);
201                     }
202                 }
203
204                 if (fields != null) {
205                     String JavaDoc title = bundle.getString("common.fields-title");
206                     buffer.append("\n\n").append(title);
207                     buffer.append('\n').append(TemplateUtil.repeatChar('=', title.length()));
208
209                     Iterator fieldIt = fields.iterator();
210                     while (fieldIt.hasNext()) {
211                         FieldInfo fieldInfo = (FieldInfo)fieldIt.next();
212                         buffer.append("\n").append(fieldInfo.fieldType.getLabel(locale)).append(": ");
213                         buffer.append(FieldHelper.getFormattedValue(fieldInfo.value, fieldInfo.valueType, locale, repository));
214
215                     }
216                 }
217
218                 if (customFields != null) {
219                     String JavaDoc title = bundle.getString("common.customfields-title");
220                     buffer.append("\n\n").append(title);
221                     buffer.append('\n').append(TemplateUtil.repeatChar('=', title.length()));
222
223                     Iterator customFieldIt = customFields.iterator();
224                     while (customFieldIt.hasNext()) {
225                         CustomFieldInfo customFieldInfo = (CustomFieldInfo)customFieldIt.next();
226                         buffer.append("\n").append(customFieldInfo.name).append(": ").append(customFieldInfo.value);
227                     }
228                 }
229
230                 if (collections != null) {
231                     String JavaDoc title = bundle.getString("common.collections-title");
232                     buffer.append("\n\n").append(title);
233                     buffer.append('\n').append(TemplateUtil.repeatChar('=', title.length()));
234                     buffer.append('\n').append(bundle.getString("created.collections-intro")).append(' ');
235
236                     Iterator collectionIt = collections.iterator();
237                     boolean first = true;
238                     while (collectionIt.hasNext()) {
239                         CollectionInfo collectionInfo = (CollectionInfo)collectionIt.next();
240                         if (!first)
241                             buffer.append(", ");
242                         buffer.append(collectionInfo.name);
243                         first = false;
244                     }
245                 }
246
247                 message = buffer.toString();
248                 cachedByLocale.put(locale, message);
249             }
250             return message;
251         }
252
253         public void addPart(PartInfo partInfo) {
254             if (parts == null)
255                 parts = new ArrayList();
256             parts.add(partInfo);
257         }
258
259         public void addLink(LinkInfo linkInfo) {
260             if (links == null)
261                 links = new ArrayList();
262             links.add(linkInfo);
263         }
264
265         public void addField(FieldInfo fieldInfo) {
266             if (fields == null)
267                 fields = new ArrayList();
268             fields.add(fieldInfo);
269         }
270
271         public void addCustomField(CustomFieldInfo customFieldInfo) {
272             if (customFields == null)
273                 customFields = new ArrayList();
274             customFields.add(customFieldInfo);
275         }
276
277         public void addCollection(CollectionInfo collectionInfo) {
278             if (collections == null)
279                 collections = new ArrayList();
280             collections.add(collectionInfo);
281         }
282     }
283
284     static class PartInfo {
285         public PartType partType;
286         public String JavaDoc mimeType;
287         public long size;
288         public String JavaDoc content;
289     }
290
291     static class LinkInfo {
292         public String JavaDoc title;
293         public String JavaDoc target;
294     }
295
296     static class FieldInfo {
297         public FieldType fieldType;
298         public Object JavaDoc value;
299         public ValueType valueType;
300     }
301
302     static class CustomFieldInfo {
303         public String JavaDoc name;
304         public String JavaDoc value;
305     }
306
307     static class CollectionInfo {
308         public String JavaDoc name;
309     }
310
311 }
312
Popular Tags