001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.CharPool;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portlet.journal.NoSuchTemplateException;
027    import com.liferay.portlet.journal.model.JournalTemplate;
028    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
029    
030    import java.io.IOException;
031    import java.io.InputStreamReader;
032    import java.io.Reader;
033    
034    /**
035     * @author Mika Koivisto
036     */
037    public class JournalTemplateLoader extends FreeMarkerTemplateLoader {
038    
039            public Object findTemplateSource(String name) throws IOException {
040                    try {
041                            int pos = name.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
042    
043                            if (pos != -1) {
044                                    int x = name.indexOf(CharPool.SLASH, pos);
045                                    int y = name.indexOf(CharPool.SLASH, x + 1);
046                                    int z = name.indexOf(CharPool.SLASH, y + 1);
047    
048                                    long companyId = GetterUtil.getLong(name.substring(x + 1, y));
049                                    long groupId = GetterUtil.getLong(name.substring(y + 1, z));
050                                    String templateId = name.substring(z + 1);
051    
052                                    if (_log.isDebugEnabled()) {
053                                            _log.debug(
054                                                    "Loading {companyId=" + companyId + ",groupId=" +
055                                                            groupId + ",templateId=" + templateId + "}");
056                                    }
057    
058                                    JournalTemplate template =
059                                            JournalTemplateLocalServiceUtil.getTemplate(
060                                                    groupId, templateId);
061    
062                                    return template;
063                            }
064                    }
065                    catch (NoSuchTemplateException nste) {
066                            return null;
067                    }
068                    catch (PortalException pe) {
069                            throw new IOException("Template {" + name + "} not found");
070                    }
071                    catch (SystemException se) {
072                            throw new IOException("Template {" + name + "} not found");
073                    }
074    
075                    return null;
076            }
077    
078            public long getLastModified(Object templateSource) {
079                    if (templateSource instanceof JournalTemplate) {
080                            JournalTemplate template = (JournalTemplate)templateSource;
081    
082                            return template.getModifiedDate().getTime();
083                    }
084    
085                    return -1;
086            }
087    
088            public Reader getReader(Object templateSource, String encoding)
089                    throws IOException {
090    
091                    if (templateSource instanceof JournalTemplate) {
092                            JournalTemplate template = (JournalTemplate)templateSource;
093    
094                            String xsl = template.getXsl();
095    
096                            return new UnsyncBufferedReader(
097                                    new InputStreamReader(
098                                            new UnsyncByteArrayInputStream(xsl.getBytes()), encoding));
099                    }
100    
101                    return null;
102            }
103    
104            private static Log _log = LogFactoryUtil.getLog(
105                    JournalTemplateLoader.class);
106    
107    }