001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.events.SimpleAction;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.template.TemplateConstants;
020 import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.LocaleUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.xml.Attribute;
026 import com.liferay.portal.kernel.xml.Document;
027 import com.liferay.portal.kernel.xml.DocumentException;
028 import com.liferay.portal.kernel.xml.Element;
029 import com.liferay.portal.kernel.xml.SAXReaderUtil;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
033 import com.liferay.portlet.dynamicdatamapping.io.DDMFormXSDDeserializerUtil;
034 import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
035 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
038 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
040 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
041 import com.liferay.util.ContentUtil;
042
043 import java.util.HashMap;
044 import java.util.List;
045 import java.util.Locale;
046 import java.util.Map;
047
048
051 public abstract class BaseDefaultDDMStructureAction extends SimpleAction {
052
053 protected void addDDMStructures(
054 long userId, long groupId, long classNameId, String fileName,
055 ServiceContext serviceContext)
056 throws Exception {
057
058 Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
059
060 List<Element> structureElements = getDDMStructures(fileName, locale);
061
062 for (Element structureElement : structureElements) {
063 boolean dynamicStructure = GetterUtil.getBoolean(
064 structureElement.elementText("dynamic-structure"));
065
066 if (dynamicStructure) {
067 continue;
068 }
069
070 String name = structureElement.elementText("name");
071
072 String description = structureElement.elementText("description");
073
074 String ddmStructureKey = name;
075
076 DDMStructure ddmStructure =
077 DDMStructureLocalServiceUtil.fetchStructure(
078 groupId, classNameId, ddmStructureKey);
079
080 if (ddmStructure != null) {
081 continue;
082 }
083
084 Element structureElementRootElement = structureElement.element(
085 "root");
086
087 String definition = structureElementRootElement.asXML();
088
089 Map<Locale, String> nameMap = new HashMap<Locale, String>();
090 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
091
092 Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
093
094 for (Locale curLocale : locales) {
095 nameMap.put(curLocale, LanguageUtil.get(curLocale, name));
096 descriptionMap.put(
097 curLocale, LanguageUtil.get(curLocale, description));
098 }
099
100 Attribute defaultLocaleAttribute =
101 structureElementRootElement.attribute("default-locale");
102
103 Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(
104 defaultLocaleAttribute.getValue());
105
106 definition = DDMXMLUtil.updateXMLDefaultLocale(
107 definition, ddmStructureDefaultLocale, locale);
108
109 if (name.equals(DLFileEntryTypeConstants.NAME_IG_IMAGE) &&
110 !UpgradeProcessUtil.isCreateIGImageDocumentType()) {
111
112 continue;
113 }
114
115 DDMForm ddmForm = DDMFormXSDDeserializerUtil.deserialize(
116 definition);
117
118 ddmStructure = DDMStructureLocalServiceUtil.addStructure(
119 userId, groupId,
120 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, classNameId,
121 ddmStructureKey, nameMap, descriptionMap, ddmForm, "xml",
122 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
123
124 Element templateElement = structureElement.element("template");
125
126 if (templateElement == null) {
127 continue;
128 }
129
130 String templateFileName = templateElement.elementText("file-name");
131
132 boolean templateCacheable = GetterUtil.getBoolean(
133 templateElement.elementText("cacheable"));
134
135 DDMTemplateLocalServiceUtil.addTemplate(
136 userId, groupId, PortalUtil.getClassNameId(DDMStructure.class),
137 ddmStructure.getStructureId(), null, nameMap, null,
138 DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
139 DDMTemplateConstants.TEMPLATE_MODE_CREATE,
140 TemplateConstants.LANG_TYPE_FTL, getContent(templateFileName),
141 templateCacheable, false, StringPool.BLANK, null,
142 serviceContext);
143 }
144 }
145
146 protected String getContent(String fileName) {
147 return ContentUtil.get(
148 "com/liferay/portal/events/dependencies/" + fileName);
149 }
150
151 protected List<Element> getDDMStructures(String fileName, Locale locale)
152 throws DocumentException {
153
154 String xml = getContent(fileName);
155
156 xml = StringUtil.replace(xml, "[$LOCALE_DEFAULT$]", locale.toString());
157
158 Document document = SAXReaderUtil.read(xml);
159
160 Element rootElement = document.getRootElement();
161
162 return rootElement.elements("structure");
163 }
164
165 protected String getDynamicDDMStructureDefinition(
166 String fileName, String dynamicDDMStructureName, Locale locale)
167 throws DocumentException {
168
169 List<Element> structureElements = getDDMStructures(fileName, locale);
170
171 for (Element structureElement : structureElements) {
172 boolean dynamicStructure = GetterUtil.getBoolean(
173 structureElement.elementText("dynamic-structure"));
174
175 if (!dynamicStructure) {
176 continue;
177 }
178
179 String name = structureElement.elementText("name");
180
181 if (!name.equals(dynamicDDMStructureName)) {
182 continue;
183 }
184
185 Element structureElementRootElement = structureElement.element(
186 "root");
187
188 return structureElementRootElement.asXML();
189 }
190
191 return null;
192 }
193
194 }