001
014
015 package com.liferay.portal.freemarker;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
020 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portlet.journal.NoSuchTemplateException;
026 import com.liferay.portlet.journal.model.JournalTemplate;
027 import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
028
029 import java.io.IOException;
030 import java.io.InputStreamReader;
031 import java.io.Reader;
032
033
036 public class JournalTemplateLoader extends FreeMarkerTemplateLoader {
037
038 public Object findTemplateSource(String name) throws IOException {
039 try {
040 int pos = name.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
041
042 if (pos != -1) {
043 int x = name.indexOf(StringPool.SLASH, pos);
044 int y = name.indexOf(StringPool.SLASH, x + 1);
045 int z = name.indexOf(StringPool.SLASH, y + 1);
046
047 long companyId = GetterUtil.getLong(name.substring(x + 1, y));
048 long groupId = GetterUtil.getLong(name.substring(y + 1, z));
049 String templateId = name.substring(z + 1);
050
051 if (_log.isDebugEnabled()) {
052 _log.debug(
053 "Loading {companyId=" + companyId + ",groupId=" +
054 groupId + ",templateId=" + templateId + "}");
055 }
056
057 JournalTemplate template =
058 JournalTemplateLocalServiceUtil.getTemplate(
059 groupId, templateId);
060
061 return template;
062 }
063 }
064 catch (NoSuchTemplateException nste) {
065 return null;
066 }
067 catch (PortalException pe) {
068 throw new IOException("Template {" + name + "} not found");
069 }
070 catch (SystemException se) {
071 throw new IOException("Template {" + name + "} not found");
072 }
073
074 return null;
075 }
076
077 public long getLastModified(Object templateSource) {
078 if (templateSource instanceof JournalTemplate) {
079 JournalTemplate template = (JournalTemplate)templateSource;
080
081 return template.getModifiedDate().getTime();
082 }
083
084 return -1;
085 }
086
087 public Reader getReader(Object templateSource, String encoding)
088 throws IOException {
089
090 if (templateSource instanceof JournalTemplate) {
091 JournalTemplate template = (JournalTemplate)templateSource;
092
093 String xsl = template.getXsl();
094
095 return new UnsyncBufferedReader(
096 new InputStreamReader(
097 new UnsyncByteArrayInputStream(xsl.getBytes()), encoding));
098 }
099
100 return null;
101 }
102
103 private static Log _log = LogFactoryUtil.getLog(
104 JournalTemplateLoader.class);
105
106 }