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