001    /**
002     * Copyright (c) 2000-present 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.dynamic.data.mapping.kernel.DDMStructureManagerUtil;
018    import com.liferay.dynamic.data.mapping.kernel.DDMTemplate;
019    import com.liferay.dynamic.data.mapping.kernel.DDMTemplateManagerUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.model.Group;
023    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
024    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
025    import com.liferay.portal.kernel.template.DDMTemplateResource;
026    import com.liferay.portal.kernel.template.TemplateConstants;
027    import com.liferay.portal.kernel.template.TemplateException;
028    import com.liferay.portal.kernel.template.TemplateResource;
029    import com.liferay.portal.kernel.util.CharPool;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.PortalUtil;
032    import com.liferay.portal.kernel.util.StringPool;
033    
034    /**
035     * @author Tina Tian
036     * @author Juan Fern??ndez
037     */
038    @OSGiBeanProperties(
039            property = {
040                    "lang.type=" + TemplateConstants.LANG_TYPE_FTL,
041                    "lang.type=" + TemplateConstants.LANG_TYPE_VM
042            },
043            service = TemplateResourceParser.class
044    )
045    public class DDMTemplateResourceParser implements TemplateResourceParser {
046    
047            @Override
048            @SuppressWarnings("deprecation")
049            public TemplateResource getTemplateResource(String templateId)
050                    throws TemplateException {
051    
052                    int pos = templateId.indexOf(
053                            TemplateConstants.TEMPLATE_SEPARATOR + StringPool.SLASH);
054    
055                    if (pos == -1) {
056    
057                            // Backwards compatibility
058    
059                            pos = templateId.indexOf(
060                                    TemplateConstants.JOURNAL_SEPARATOR + StringPool.SLASH);
061    
062                            if (pos == -1) {
063                                    return null;
064                            }
065                    }
066    
067                    try {
068                            int w = templateId.indexOf(CharPool.SLASH, pos);
069                            int x = templateId.indexOf(CharPool.SLASH, w + 1);
070                            int y = templateId.indexOf(CharPool.SLASH, x + 1);
071                            int z = templateId.indexOf(CharPool.SLASH, y + 1);
072    
073                            long companyId = GetterUtil.getLong(templateId.substring(w + 1, x));
074                            long groupId = GetterUtil.getLong(templateId.substring(x + 1, y));
075                            long classNameId = GetterUtil.getLong(
076                                    templateId.substring(y + 1, z));
077                            String ddmTemplateKey = templateId.substring(z + 1);
078    
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug(
081                                            "Loading {companyId=" + companyId + ", groupId=" + groupId +
082                                                    ", classNameId=" + classNameId + ", ddmTemplateKey=" +
083                                                            ddmTemplateKey + "}");
084                            }
085    
086                            DDMTemplate ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
087                                    groupId, classNameId, ddmTemplateKey);
088    
089                            if (ddmTemplate == null) {
090                                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
091                                            companyId);
092    
093                                    ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
094                                            companyGroup.getGroupId(), classNameId, ddmTemplateKey);
095    
096                                    if (ddmTemplate == null) {
097                                            classNameId = PortalUtil.getClassNameId(
098                                                    DDMStructureManagerUtil.getDDMStructureModelClass());
099    
100                                            ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
101                                                    groupId, classNameId, ddmTemplateKey);
102                                    }
103    
104                                    if (ddmTemplate == null) {
105                                            ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
106                                                    companyGroup.getGroupId(), classNameId, ddmTemplateKey);
107                                    }
108                            }
109    
110                            if (ddmTemplate == null) {
111                                    return null;
112                            }
113                            else {
114                                    return new DDMTemplateResource(
115                                            ddmTemplate.getTemplateKey(), ddmTemplate);
116                            }
117                    }
118                    catch (Exception e) {
119                            throw new TemplateException(
120                                    "Unable to find template " + templateId, e);
121                    }
122            }
123    
124            @Override
125            @SuppressWarnings("deprecation")
126            public boolean isTemplateResourceValid(String templateId, String langType) {
127                    if (templateId.contains(TemplateConstants.JOURNAL_SEPARATOR) ||
128                            templateId.contains(TemplateConstants.TEMPLATE_SEPARATOR)) {
129    
130                            return true;
131                    }
132    
133                    return false;
134            }
135    
136            private static final Log _log = LogFactoryUtil.getLog(
137                    DDMTemplateResourceParser.class);
138    
139    }