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.velocity;
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.UnsyncByteArrayInputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.CharPool;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portlet.journal.model.JournalTemplate;
026    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
027    
028    import java.io.InputStream;
029    
030    import org.apache.velocity.exception.ResourceNotFoundException;
031    
032    /**
033     * @author Alexander Chow
034     */
035    public class JournalTemplateVelocityResourceListener
036            extends VelocityResourceListener {
037    
038            public InputStream getResourceStream(String source)
039                    throws ResourceNotFoundException {
040    
041                    InputStream is = null;
042    
043                    try {
044                            int pos = source.indexOf(JOURNAL_SEPARATOR + StringPool.SLASH);
045    
046                            if (pos != -1) {
047                                    int x = source.indexOf(CharPool.SLASH, pos);
048                                    int y = source.indexOf(CharPool.SLASH, x + 1);
049                                    int z = source.indexOf(CharPool.SLASH, y + 1);
050    
051                                    long companyId = GetterUtil.getLong(source.substring(x + 1, y));
052                                    long groupId = GetterUtil.getLong(source.substring(y + 1, z));
053                                    String templateId = source.substring(z + 1);
054    
055                                    if (_log.isDebugEnabled()) {
056                                            _log.debug(
057                                                    "Loading {companyId=" + companyId + ",groupId=" +
058                                                            groupId + ",templateId=" + templateId + "}");
059                                    }
060    
061                                    JournalTemplate template =
062                                            JournalTemplateLocalServiceUtil.getTemplate(
063                                                    groupId, templateId);
064    
065                                    String buffer = template.getXsl();
066    
067                                    is = new UnsyncByteArrayInputStream(buffer.getBytes());
068                            }
069                    }
070                    catch (PortalException pe) {
071                            throw new ResourceNotFoundException(source);
072                    }
073                    catch (SystemException se) {
074                            throw new ResourceNotFoundException(source);
075                    }
076    
077                    return is;
078            }
079    
080            private static Log _log = LogFactoryUtil.getLog(
081                    JournalTemplateVelocityResourceListener.class);
082    
083    }