001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.events.ActionException;
018 import com.liferay.portal.kernel.metadata.RawMetadataProcessorUtil;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.GetterUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.xml.Document;
024 import com.liferay.portal.kernel.xml.Element;
025 import com.liferay.portal.kernel.xml.SAXReaderUtil;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.service.GroupLocalServiceUtil;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portal.upgrade.UpgradeProcessUtil;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
033 import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
034 import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
035 import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
036 import com.liferay.portlet.documentlibrary.util.RawMetadataProcessor;
037 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
038 import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
039 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
040
041 import java.io.StringReader;
042
043 import java.lang.reflect.Field;
044
045 import java.util.ArrayList;
046 import java.util.HashMap;
047 import java.util.List;
048 import java.util.Locale;
049 import java.util.Map;
050
051
056 public class AddDefaultDocumentLibraryStructuresAction
057 extends BaseDefaultDDMStructureAction {
058
059 @Override
060 public void run(String[] ids) throws ActionException {
061 try {
062 doRun(GetterUtil.getLong(ids[0]));
063 }
064 catch (Exception e) {
065 throw new ActionException(e);
066 }
067 }
068
069 protected void addDLFileEntryType(
070 long userId, long groupId, String dlFileEntryTypeKey,
071 List<String> ddmStructureNames, ServiceContext serviceContext)
072 throws Exception {
073
074 List<Long> ddmStructureIds = new ArrayList<Long>();
075
076 for (String ddmStructureName : ddmStructureNames) {
077 String ddmStructureKey = ddmStructureName;
078
079 DDMStructure ddmStructure =
080 DDMStructureLocalServiceUtil.fetchStructure(
081 groupId,
082 PortalUtil.getClassNameId(DLFileEntryMetadata.class),
083 ddmStructureKey);
084
085 if (ddmStructure == null) {
086 continue;
087 }
088
089 ddmStructureIds.add(ddmStructure.getStructureId());
090 }
091
092 Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
093
094 String xsd = getDynamicDDMStructureXSD(
095 "document-library-structures.xml", dlFileEntryTypeKey, locale);
096
097 serviceContext.setAttribute("xsd", xsd);
098
099 try {
100 DLFileEntryTypeLocalServiceUtil.getFileEntryType(
101 groupId, dlFileEntryTypeKey);
102 }
103 catch (NoSuchFileEntryTypeException nsfete) {
104 DLFileEntryTypeLocalServiceUtil.addFileEntryType(
105 userId, groupId, dlFileEntryTypeKey,
106 getLocalizationMap(dlFileEntryTypeKey, locale),
107 getLocalizationMap(dlFileEntryTypeKey, locale),
108 ArrayUtil.toArray(
109 ddmStructureIds.toArray(new Long[ddmStructureIds.size()])),
110 serviceContext);
111 }
112 }
113
114 protected void addDLFileEntryTypes(
115 long userId, long groupId, ServiceContext serviceContext)
116 throws Exception {
117
118 List<String> ddmStructureNames = new ArrayList<String>();
119
120 addDLFileEntryType(
121 userId, groupId, DLFileEntryTypeConstants.NAME_CONTRACT,
122 ddmStructureNames, serviceContext);
123
124 ddmStructureNames.clear();
125
126 ddmStructureNames.add("Marketing Campaign Theme Metadata");
127
128 addDLFileEntryType(
129 userId, groupId, DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
130 ddmStructureNames, serviceContext);
131
132 ddmStructureNames.clear();
133
134 ddmStructureNames.add("Learning Module Metadata");
135
136 addDLFileEntryType(
137 userId, groupId, DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
138 ddmStructureNames, serviceContext);
139
140 ddmStructureNames.clear();
141
142 ddmStructureNames.add("Meeting Metadata");
143
144 addDLFileEntryType(
145 userId, groupId, DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
146 ddmStructureNames, serviceContext);
147
148 if (UpgradeProcessUtil.isCreateIGImageDocumentType()) {
149 addDLFileEntryType(
150 userId, groupId, DLFileEntryTypeConstants.NAME_IG_IMAGE,
151 ddmStructureNames, serviceContext);
152 }
153 }
154
155 protected void addDLRawMetadataStructures(
156 long userId, long groupId, ServiceContext serviceContext)
157 throws Exception {
158
159 Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
160
161 String xsd = buildDLRawMetadataXML(
162 RawMetadataProcessorUtil.getFields(), locale);
163
164 Document document = SAXReaderUtil.read(new StringReader(xsd));
165
166 Element rootElement = document.getRootElement();
167
168 List<Element> structureElements = rootElement.elements("structure");
169
170 for (Element structureElement : structureElements) {
171 String name = structureElement.elementText("name");
172 String description = structureElement.elementText("description");
173
174 Element structureElementRootElement = structureElement.element(
175 "root");
176
177 String structureElementRootXML =
178 structureElementRootElement.asXML();
179
180 DDMStructure ddmStructure =
181 DDMStructureLocalServiceUtil.fetchStructure(
182 groupId,
183 PortalUtil.getClassNameId(RawMetadataProcessor.class),
184 name);
185
186 if (ddmStructure != null) {
187 ddmStructure.setXsd(structureElementRootXML);
188
189 DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
190 }
191 else {
192 Map<Locale, String> nameMap = new HashMap<Locale, String>();
193
194 nameMap.put(locale, name);
195
196 Map<Locale, String> descriptionMap =
197 new HashMap<Locale, String>();
198
199 descriptionMap.put(locale, description);
200
201 DDMStructureLocalServiceUtil.addStructure(
202 userId, groupId,
203 DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
204 PortalUtil.getClassNameId(RawMetadataProcessor.class), name,
205 nameMap, descriptionMap, structureElementRootXML, "xml",
206 DDMStructureConstants.TYPE_DEFAULT, serviceContext);
207 }
208 }
209 }
210
211 protected String buildDLRawMetadataElementXML(Field field, Locale locale) {
212 StringBundler sb = new StringBundler(16);
213
214 sb.append("<dynamic-element dataType=\"string\" name=\"");
215
216 Class<?> fieldClass = field.getDeclaringClass();
217
218 sb.append(fieldClass.getSimpleName());
219 sb.append(StringPool.UNDERLINE);
220 sb.append(field.getName());
221 sb.append("\" type=\"text\">");
222 sb.append("<meta-data locale=\"");
223 sb.append(locale);
224 sb.append("\">");
225 sb.append("<entry name=\"label\"><![CDATA[metadata.");
226 sb.append(fieldClass.getSimpleName());
227 sb.append(StringPool.PERIOD);
228 sb.append(field.getName());
229 sb.append("]]></entry><entry name=\"predefinedValue\">");
230 sb.append("<![CDATA[]]></entry><entry name=\"required\">");
231 sb.append("<![CDATA[false]]></entry><entry name=\"showLabel\">");
232 sb.append("<![CDATA[true]]></entry></meta-data></dynamic-element>");
233
234 return sb.toString();
235 }
236
237 protected String buildDLRawMetadataStructureXML(
238 String name, Field[] fields, Locale locale) {
239
240 StringBundler sb = new StringBundler(12 + fields.length);
241
242 sb.append("<structure><name><![CDATA[");
243 sb.append(name);
244 sb.append("]]></name>");
245 sb.append("<description><![CDATA[");
246 sb.append(name);
247 sb.append("]]></description>");
248 sb.append("<root available-locales=\"");
249 sb.append(locale);
250 sb.append("\" default-locale=\"");
251 sb.append(locale);
252 sb.append("\">");
253
254 for (Field field : fields) {
255 sb.append(buildDLRawMetadataElementXML(field, locale));
256 }
257
258 sb.append("</root></structure>");
259
260 return sb.toString();
261 }
262
263 protected String buildDLRawMetadataXML(
264 Map<String, Field[]> fields, Locale locale) {
265
266 StringBundler sb = new StringBundler(2 + fields.size());
267
268 sb.append("<?xml version=\"1.0\"?><root>");
269
270 for (String key : fields.keySet()) {
271 sb.append(
272 buildDLRawMetadataStructureXML(key, fields.get(key), locale));
273 }
274
275 sb.append("</root>");
276
277 return sb.toString();
278 }
279
280 protected void doRun(long companyId) throws Exception {
281 ServiceContext serviceContext = new ServiceContext();
282
283 serviceContext.setAddGuestPermissions(true);
284 serviceContext.setAddGroupPermissions(true);
285
286 Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
287
288 serviceContext.setScopeGroupId(group.getGroupId());
289
290 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
291
292 serviceContext.setUserId(defaultUserId);
293
294 addDDMStructures(
295 defaultUserId, group.getGroupId(),
296 PortalUtil.getClassNameId(DLFileEntryMetadata.class),
297 "document-library-structures.xml", serviceContext);
298 addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);
299 addDLRawMetadataStructures(
300 defaultUserId, group.getGroupId(), serviceContext);
301 }
302
303 protected Map<Locale, String> getLocalizationMap(
304 String content, Locale locale) {
305
306 Map<Locale, String> localizationMap = new HashMap<Locale, String>();
307
308 localizationMap.put(locale, content);
309
310 return localizationMap;
311 }
312
313 }