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 w = templateId.indexOf(CharPool.SLASH, pos);
061                            int x = templateId.indexOf(CharPool.SLASH, w + 1);
062                            int y = templateId.indexOf(CharPool.SLASH, x + 1);
063                            int z = templateId.indexOf(CharPool.SLASH, y + 1);
064    
065                            long companyId = GetterUtil.getLong(templateId.substring(w + 1, x));
066                            long groupId = GetterUtil.getLong(templateId.substring(x + 1, y));
067                            long classNameId = GetterUtil.getLong(
068                                    templateId.substring(y + 1, z));
069                            String ddmTemplateKey = templateId.substring(z + 1);
070    
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug(
073                                            "Loading {companyId=" + companyId + ", groupId=" +
074                                                    groupId + ", classNameId=" + classNameId +
075                                                            ", ddmTemplateKey=" + ddmTemplateKey + "}");
076                            }
077    
078                            DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
079                                    groupId, classNameId, ddmTemplateKey);
080    
081                            if (ddmTemplate == null) {
082                                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
083                                            companyId);
084    
085                                    ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
086                                            companyGroup.getGroupId(), classNameId, ddmTemplateKey);
087    
088                                    if (ddmTemplate == null) {
089                                            classNameId = PortalUtil.getClassNameId(DDMStructure.class);
090    
091                                            ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
092                                                    groupId, classNameId, ddmTemplateKey);
093                                    }
094    
095                                    if (ddmTemplate == null) {
096                                            ddmTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
097                                                    companyGroup.getGroupId(), classNameId, ddmTemplateKey);
098                                    }
099                            }
100    
101                            if (ddmTemplate == null) {
102                                    return null;
103                            }
104                            else {
105                                    return new DDMTemplateResource(
106                                            ddmTemplate.getTemplateKey(), ddmTemplate);
107                            }
108                    }
109                    catch (Exception e) {
110                            throw new TemplateException(
111                                    "Unable to find template " + templateId, e);
112                    }
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(
116                    DDMTemplateResourceParser.class);
117    
118    }