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.template;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.template.JournalTemplateResource;
020    import com.liferay.portal.kernel.template.TemplateException;
021    import com.liferay.portal.kernel.template.TemplateResource;
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.dynamicdatamapping.NoSuchTemplateException;
026    import com.liferay.portlet.journal.model.JournalTemplate;
027    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
028    
029    /**
030     * @author Tina Tian
031     */
032    public class JournalTemplateResourceParser implements TemplateResourceParser {
033    
034            public TemplateResource getTemplateResource(String templateId)
035                    throws TemplateException {
036    
037                    int pos = templateId.indexOf(
038                            TemplateResource.JOURNAL_SEPARATOR + StringPool.SLASH);
039    
040                    if (pos == -1) {
041                            return null;
042                    }
043    
044                    try {
045                            int x = templateId.indexOf(CharPool.SLASH, pos);
046                            int y = templateId.indexOf(CharPool.SLASH, x + 1);
047                            int z = templateId.indexOf(CharPool.SLASH, y + 1);
048    
049                            long companyId = GetterUtil.getLong(templateId.substring(x + 1, y));
050                            long groupId = GetterUtil.getLong(templateId.substring(y + 1, z));
051                            String journalTemplateId = templateId.substring(z + 1);
052    
053                            if (_log.isDebugEnabled()) {
054                                    _log.debug(
055                                            "Loading {companyId=" + companyId + ", groupId=" +
056                                                    groupId + ", templateId=" + journalTemplateId + "}");
057                            }
058    
059                            JournalTemplate journalTemplate =
060                                    JournalTemplateLocalServiceUtil.getTemplate(
061                                            groupId, journalTemplateId);
062    
063                            return new JournalTemplateResource(
064                                    journalTemplateId, journalTemplate);
065                    }
066                    catch (NoSuchTemplateException nste) {
067                            return null;
068                    }
069                    catch (Exception e) {
070                            throw new TemplateException(
071                                    "Unable to find template " + templateId, e);
072                    }
073            }
074    
075            private static Log _log = LogFactoryUtil.getLog(
076                    JournalTemplateResourceParser.class);
077    
078    }