001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.events.SimpleAction;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.LocaleUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.xml.Attribute;
022 import com.liferay.portal.kernel.xml.Document;
023 import com.liferay.portal.kernel.xml.DocumentException;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
026 import com.liferay.portal.service.ServiceContext;
027 import com.liferay.portal.upgrade.UpgradeProcessUtil;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
030 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
031 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
032 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
033 import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
034 import com.liferay.util.ContentUtil;
035
036 import java.util.HashMap;
037 import java.util.List;
038 import java.util.Locale;
039 import java.util.Map;
040
041
044 public abstract class BaseDefaultDDMStructureAction extends SimpleAction {
045
046 protected void addDDMStructures(
047 long userId, long groupId, long classNameId, String fileName,
048 ServiceContext serviceContext)
049 throws Exception {
050
051 Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
052
053 List<Element> structureElements = getDDMStructures(fileName, locale);
054
055 for (Element structureElement : structureElements) {
056 boolean dynamicStructure = GetterUtil.getBoolean(
057 structureElement.elementText("dynamic-structure"));
058
059 if (dynamicStructure) {
060 continue;
061 }
062
063 String name = structureElement.elementText("name");
064
065 String description = structureElement.elementText("description");
066
067 String ddmStructureKey = name;
068
069 DDMStructure ddmStructure =
070 DDMStructureLocalServiceUtil.fetchStructure(
071 groupId, classNameId, ddmStructureKey);
072
073 if (ddmStructure != null) {
074 continue;
075 }
076
077 Element structureElementRootElement = structureElement.element(
078 "root");
079
080 String xsd = structureElementRootElement.asXML();
081
082 Map<Locale, String> nameMap = new HashMap<Locale, String>();
083
084 nameMap.put(locale, name);
085
086 Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
087
088 descriptionMap.put(locale, description);
089
090 Attribute defaultLocaleAttribute =
091 structureElementRootElement.attribute("default-locale");
092
093 Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(
094 defaultLocaleAttribute.getValue());
095
096 xsd = DDMXMLUtil.updateXMLDefaultLocale(
097 xsd, ddmStructureDefaultLocale, locale);
098
099 if (name.equals(DLFileEntryTypeConstants.NAME_IG_IMAGE) &&
100 !UpgradeProcessUtil.isCreateIGImageDocumentType()) {
101
102 continue;
103 }
104
105 DDMStructureLocalServiceUtil.addStructure(
106 userId, groupId,
107 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, classNameId,
108 ddmStructureKey, nameMap, descriptionMap, xsd, "xml",
109 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
110 }
111 }
112
113 protected List<Element> getDDMStructures(String fileName, Locale locale)
114 throws DocumentException {
115
116 String xml = ContentUtil.get(
117 "com/liferay/portal/events/dependencies/" + fileName);
118
119 xml = StringUtil.replace(xml, "[$LOCALE_DEFAULT$]", locale.toString());
120
121 Document document = UnsecureSAXReaderUtil.read(xml);
122
123 Element rootElement = document.getRootElement();
124
125 return rootElement.elements("structure");
126 }
127
128 protected String getDynamicDDMStructureXSD(
129 String fileName, String dynamicDDMStructureName, Locale locale)
130 throws DocumentException {
131
132 List<Element> structureElements = getDDMStructures(fileName, locale);
133
134 for (Element structureElement : structureElements) {
135 boolean dynamicStructure = GetterUtil.getBoolean(
136 structureElement.elementText("dynamic-structure"));
137
138 if (!dynamicStructure) {
139 continue;
140 }
141
142 String name = structureElement.elementText("name");
143
144 if (!name.equals(dynamicDDMStructureName)) {
145 continue;
146 }
147
148 Element structureElementRootElement = structureElement.element(
149 "root");
150
151 return structureElementRootElement.asXML();
152 }
153
154 return null;
155 }
156
157 }