001    /**
002     * Copyright (c) 2000-2013 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.DDMTemplateResource;
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.portal.model.Group;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
032    
033    /**
034     * @author Tina Tian
035     * @author Juan Fern??ndez
036     */
037    public class DDMTemplateResourceParser implements TemplateResourceParser {
038    
039            @Override
040            @SuppressWarnings("deprecation")
041            public TemplateResource getTemplateResource(String templateId)
042                    throws TemplateException {
043    
044                    int pos = templateId.indexOf(
045                            TemplateConstants.TEMPLATE_SEPARATOR + StringPool.SLASH);
046    
047                    if (pos == -1) {
048    
049                            // Backwards compatibility
050    
051                            pos = templateId.indexOf(
052                                    TemplateConstants.JOURNAL_SEPARATOR + StringPool.SLASH);
053    
054                            if (pos == -1) {
055                                    return null;
056                            }
057                    }
058    
059                    try {
060                            int x = templateId.indexOf(CharPool.SLASH, pos);
061                            int y = templateId.indexOf(CharPool.SLASH, x + 1);
062                            int z = templateId.indexOf(CharPool.SLASH, y + 1);
063    
064                            long companyId = GetterUtil.getLong(templateId.substring(x + 1, y));
065                            long groupId = GetterUtil.getLong(templateId.substring(y + 1, z));
066                            String ddmTemplateKey = templateId.substring(z + 1);
067    
068                            if (_log.isDebugEnabled()) {
069                                    _log.debug(
070                                            "Loading {companyId=" + companyId + ", groupId=" +
071                                                    groupId + ", ddmTemplateKey=" + ddmTemplateKey + "}");
072                            }
073    
074                            long classNameId = 0;
075    
076                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
077                                    groupId, classNameId, ddmTemplateKey);
078    
079                            if (ddmTemplate == null) {
080                                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
081                                            companyId);
082    
083                                    ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
084                                            companyGroup.getGroupId(), classNameId, ddmTemplateKey);
085    
086                                    if (ddmTemplate == null) {
087                                            classNameId = PortalUtil.getClassNameId(DDMStructure.class);
088    
089                                            ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
090                                                    groupId, classNameId, ddmTemplateKey);
091                                    }
092    
093                                    if (ddmTemplate == null) {
094                                            ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
095                                                    companyGroup.getGroupId(), classNameId, ddmTemplateKey);
096                                    }
097                            }
098    
099                            if (ddmTemplate == null) {
100                                    return null;
101                            }
102                            else {
103                                    return new DDMTemplateResource(
104                                            ddmTemplate.getTemplateKey(), ddmTemplate);
105                            }
106                    }
107                    catch (Exception e) {
108                            throw new TemplateException(
109                                    "Unable to find template " + templateId, e);
110                    }
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(
114                    DDMTemplateResourceParser.class);
115    
116    }