1
14
15 package com.liferay.portal.freemarker;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
20 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
21 import com.liferay.portal.kernel.log.Log;
22 import com.liferay.portal.kernel.log.LogFactoryUtil;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portlet.journal.NoSuchTemplateException;
26 import com.liferay.portlet.journal.model.JournalTemplate;
27 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
28
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32
33
38 public class JournalTemplateLoader extends FreeMarkerTemplateLoader {
39
40 public Object findTemplateSource(String name) throws IOException {
41 try {
42 int pos = name.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
43
44 if (pos != -1) {
45 int x = name.indexOf(StringPool.SLASH, pos);
46 int y = name.indexOf(StringPool.SLASH, x + 1);
47 int z = name.indexOf(StringPool.SLASH, y + 1);
48
49 long companyId = GetterUtil.getLong(name.substring(x + 1, y));
50 long groupId = GetterUtil.getLong(name.substring(y + 1, z));
51 String templateId = name.substring(z + 1);
52
53 if (_log.isDebugEnabled()) {
54 _log.debug(
55 "Loading {companyId=" + companyId + ",groupId=" +
56 groupId + ",templateId=" + templateId + "}");
57 }
58
59 JournalTemplate template =
60 JournalTemplateLocalServiceUtil.getTemplate(
61 groupId, templateId);
62
63 return template;
64 }
65 }
66 catch (NoSuchTemplateException nste) {
67 return null;
68 }
69 catch (PortalException pe) {
70 throw new IOException("Template {" + name + "} not found");
71 }
72 catch (SystemException se) {
73 throw new IOException("Template {" + name + "} not found");
74 }
75
76 return null;
77 }
78
79 public long getLastModified(Object templateSource) {
80 if (templateSource instanceof JournalTemplate) {
81 JournalTemplate template = (JournalTemplate)templateSource;
82
83 return template.getModifiedDate().getTime();
84 }
85
86 return -1;
87 }
88
89 public Reader getReader(Object templateSource, String encoding)
90 throws IOException {
91
92 if (templateSource instanceof JournalTemplate) {
93 JournalTemplate template = (JournalTemplate)templateSource;
94
95 String xsl = template.getXsl();
96
97 return new UnsyncBufferedReader(
98 new InputStreamReader(
99 new UnsyncByteArrayInputStream(xsl.getBytes()), encoding));
100 }
101
102 return null;
103 }
104
105 private static Log _log = LogFactoryUtil.getLog(
106 JournalTemplateLoader.class);
107
108 }