001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.LocaleUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.service.GroupLocalServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.upgrade.UpgradeProcessUtil;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
036    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
037    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
038    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
039    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
040    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
041    
042    import java.io.StringReader;
043    
044    import java.lang.reflect.Field;
045    
046    import java.util.ArrayList;
047    import java.util.HashMap;
048    import java.util.List;
049    import java.util.Locale;
050    import java.util.Map;
051    
052    /**
053     * @author Sergio Gonz??lez
054     * @author Miguel Pastor
055     */
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 dlFileEntryTypeName,
071                            String dlFileEntryTypeDescription, String dynamicDDMStructureName,
072                            List<String> ddmStructureNames, ServiceContext serviceContext)
073                    throws Exception {
074    
075                    List<Long> ddmStructureIds = new ArrayList<Long>();
076    
077                    for (String ddmStructureName : ddmStructureNames) {
078                            String ddmStructureKey = ddmStructureName;
079    
080                            DDMStructure ddmStructure =
081                                    DDMStructureLocalServiceUtil.fetchStructure(
082                                            groupId, ddmStructureKey);
083    
084                            if (ddmStructure == null) {
085                                    continue;
086                            }
087    
088                            ddmStructureIds.add(ddmStructure.getStructureId());
089                    }
090    
091                    String xsd = getDynamicDDMStructureXSD(
092                            "document-library-structures.xml", dynamicDDMStructureName);
093    
094                    serviceContext.setAttribute("xsd", xsd);
095    
096                    try {
097                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
098                                    groupId, dlFileEntryTypeName);
099                    }
100                    catch (NoSuchFileEntryTypeException nsfete) {
101                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
102                                    userId, groupId, dlFileEntryTypeName,
103                                    dlFileEntryTypeDescription,
104                                    ArrayUtil.toArray(
105                                            ddmStructureIds.toArray(new Long[ddmStructureIds.size()])),
106                                    serviceContext);
107                    }
108            }
109    
110            protected void addDLFileEntryTypes(
111                            long userId, long groupId, ServiceContext serviceContext)
112                    throws Exception {
113    
114                    List<String> ddmStructureNames = new ArrayList<String>();
115    
116                    addDLFileEntryType(
117                            userId, groupId, DLFileEntryTypeConstants.NAME_CONTRACT,
118                            DLFileEntryTypeConstants.NAME_CONTRACT,
119                            DLFileEntryTypeConstants.NAME_CONTRACT, ddmStructureNames,
120                            serviceContext);
121    
122                    ddmStructureNames.clear();
123    
124                    ddmStructureNames.add("Marketing Campaign Theme Metadata");
125    
126                    addDLFileEntryType(
127                            userId, groupId, DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
128                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
129                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER, ddmStructureNames,
130                            serviceContext);
131    
132                    ddmStructureNames.clear();
133    
134                    ddmStructureNames.add("Learning Module Metadata");
135    
136                    addDLFileEntryType(
137                            userId, groupId, DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
138                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
139                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING, ddmStructureNames,
140                            serviceContext);
141    
142                    ddmStructureNames.clear();
143    
144                    ddmStructureNames.add("Meeting Metadata");
145    
146                    addDLFileEntryType(
147                            userId, groupId, DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
148                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
149                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION, ddmStructureNames,
150                            serviceContext);
151    
152                    if (UpgradeProcessUtil.isCreateIGImageDocumentType()) {
153                            addDLFileEntryType(
154                                    userId, groupId, DLFileEntryTypeConstants.NAME_IG_IMAGE,
155                                    DLFileEntryTypeConstants.NAME_IG_IMAGE,
156                                    DLFileEntryTypeConstants.NAME_IG_IMAGE, ddmStructureNames,
157                                    serviceContext);
158                    }
159            }
160    
161            protected void addDLRawMetadataStructures(
162                            long userId, long groupId, ServiceContext serviceContext)
163                    throws Exception {
164    
165                    String xsd = buildDLRawMetadataXML(
166                            RawMetadataProcessorUtil.getFields());
167    
168                    Document document = SAXReaderUtil.read(new StringReader(xsd));
169    
170                    Element rootElement = document.getRootElement();
171    
172                    List<Element> structureElements = rootElement.elements("structure");
173    
174                    for (Element structureElement : structureElements) {
175                            String name = structureElement.elementText("name");
176                            String description = structureElement.elementText("description");
177    
178                            Element structureElementRootElement = structureElement.element(
179                                    "root");
180    
181                            String structureElementRootXML =
182                                    structureElementRootElement.asXML();
183    
184                            DDMStructure ddmStructure =
185                                    DDMStructureLocalServiceUtil.fetchStructure(groupId, name);
186    
187                            if (ddmStructure != null) {
188                                    ddmStructure.setXsd(structureElementRootXML);
189    
190                                    DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
191                            }
192                            else {
193                                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
194    
195                                    nameMap.put(LocaleUtil.getDefault(), name);
196    
197                                    Map<Locale, String> descriptionMap =
198                                            new HashMap<Locale, String>();
199    
200                                    descriptionMap.put(LocaleUtil.getDefault(), description);
201    
202                                    DDMStructureLocalServiceUtil.addStructure(
203                                            userId, groupId,
204                                            PortalUtil.getClassNameId(DLFileEntry.class), name, nameMap,
205                                            descriptionMap, structureElementRootXML, "xml",
206                                            DDMStructureConstants.TYPE_DEFAULT, serviceContext);
207                            }
208                    }
209            }
210    
211            protected String buildDLRawMetadataElementXML(String name, Field field) {
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(LocaleUtil.getDefault());
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) {
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(LocaleUtil.getDefault());
250                    sb.append("\" default-locale=\"");
251                    sb.append(LocaleUtil.getDefault());
252                    sb.append("\">");
253    
254                    for (Field field : fields) {
255                            sb.append(buildDLRawMetadataElementXML(name, field));
256                    }
257    
258                    sb.append("</root></structure>");
259    
260                    return sb.toString();
261            }
262    
263            protected String buildDLRawMetadataXML(Map<String, Field[]> fields) {
264                    StringBundler sb = new StringBundler(2 + fields.size());
265    
266                    sb.append("<?xml version=\"1.0\"?><root>");
267    
268                    for (String key : fields.keySet()) {
269                            sb.append(buildDLRawMetadataStructureXML(key, fields.get(key)));
270                    }
271    
272                    sb.append("</root>");
273    
274                    return sb.toString();
275            }
276    
277            protected void doRun(long companyId) throws Exception {
278                    ServiceContext serviceContext = new ServiceContext();
279    
280                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
281    
282                    serviceContext.setScopeGroupId(group.getGroupId());
283    
284                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
285    
286                    serviceContext.setUserId(defaultUserId);
287    
288                    addDDMStructures(
289                            defaultUserId, group.getGroupId(),
290                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
291                            "document-library-structures.xml", serviceContext);
292                    addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);
293                    addDLRawMetadataStructures(
294                            defaultUserId, group.getGroupId(), serviceContext);
295            }
296    
297    }