001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
040            public Object findTemplateSource(String name) throws IOException {
041                    try {
042                            int pos = name.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
043    
044                            if (pos != -1) {
045                                    int x = name.indexOf(CharPool.SLASH, pos);
046                                    int y = name.indexOf(CharPool.SLASH, x + 1);
047                                    int z = name.indexOf(CharPool.SLASH, y + 1);
048    
049                                    long companyId = GetterUtil.getLong(name.substring(x + 1, y));
050                                    long groupId = GetterUtil.getLong(name.substring(y + 1, z));
051                                    String templateId = name.substring(z + 1);
052    
053                                    if (_log.isDebugEnabled()) {
054                                            _log.debug(
055                                                    "Loading {companyId=" + companyId + ", groupId=" +
056                                                            groupId + ", templateId=" + templateId + "}");
057                                    }
058    
059                                    JournalTemplate template =
060                                            JournalTemplateLocalServiceUtil.getTemplate(
061                                                    groupId, templateId);
062    
063                                    return template;
064                            }
065                    }
066                    catch (NoSuchTemplateException nste) {
067                            return null;
068                    }
069                    catch (PortalException pe) {
070                            throw new IOException("Template {" + name + "} not found");
071                    }
072                    catch (SystemException se) {
073                            throw new IOException("Template {" + name + "} not found");
074                    }
075    
076                    return null;
077            }
078    
079            @Override
080            public long getLastModified(Object templateSource) {
081                    if (templateSource instanceof JournalTemplate) {
082                            JournalTemplate template = (JournalTemplate)templateSource;
083    
084                            return template.getModifiedDate().getTime();
085                    }
086    
087                    return -1;
088            }
089    
090            @Override
091            public Reader getReader(Object templateSource, String encoding)
092                    throws IOException {
093    
094                    if (templateSource instanceof JournalTemplate) {
095                            JournalTemplate template = (JournalTemplate)templateSource;
096    
097                            String xsl = template.getXsl();
098    
099                            return new UnsyncBufferedReader(
100                                    new InputStreamReader(
101                                            new UnsyncByteArrayInputStream(xsl.getBytes()), encoding));
102                    }
103    
104                    return null;
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(
108                    JournalTemplateLoader.class);
109    
110    }