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