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.DDMStructureManagerUtil;
030 import com.liferay.portlet.dynamicdatamapping.DDMTemplate;
031 import com.liferay.portlet.dynamicdatamapping.DDMTemplateManagerUtil;
032
033
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
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 = DDMTemplateManagerUtil.fetchTemplate(
079 groupId, classNameId, ddmTemplateKey);
080
081 if (ddmTemplate == null) {
082 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
083 companyId);
084
085 ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
086 companyGroup.getGroupId(), classNameId, ddmTemplateKey);
087
088 if (ddmTemplate == null) {
089 classNameId = PortalUtil.getClassNameId(
090 DDMStructureManagerUtil.getDDMStructureModelClass());
091
092 ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
093 groupId, classNameId, ddmTemplateKey);
094 }
095
096 if (ddmTemplate == null) {
097 ddmTemplate = DDMTemplateManagerUtil.fetchTemplate(
098 companyGroup.getGroupId(), classNameId, ddmTemplateKey);
099 }
100 }
101
102 if (ddmTemplate == null) {
103 return null;
104 }
105 else {
106 return new DDMTemplateResource(
107 ddmTemplate.getTemplateKey(), ddmTemplate);
108 }
109 }
110 catch (Exception e) {
111 throw new TemplateException(
112 "Unable to find template " + templateId, e);
113 }
114 }
115
116 private static final Log _log = LogFactoryUtil.getLog(
117 DDMTemplateResourceParser.class);
118
119 }