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