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.DLFileEntryMetadata;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
036    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
037    import com.liferay.portlet.documentlibrary.util.RawMetadataProcessor;
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,
083                                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
084                                            ddmStructureKey);
085    
086                            if (ddmStructure == null) {
087                                    continue;
088                            }
089    
090                            ddmStructureIds.add(ddmStructure.getStructureId());
091                    }
092    
093                    String xsd = getDynamicDDMStructureXSD(
094                            "document-library-structures.xml", dynamicDDMStructureName);
095    
096                    serviceContext.setAttribute("xsd", xsd);
097    
098                    try {
099                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
100                                    groupId, dlFileEntryTypeName);
101                    }
102                    catch (NoSuchFileEntryTypeException nsfete) {
103                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
104                                    userId, groupId, dlFileEntryTypeName,
105                                    dlFileEntryTypeDescription,
106                                    ArrayUtil.toArray(
107                                            ddmStructureIds.toArray(new Long[ddmStructureIds.size()])),
108                                    serviceContext);
109                    }
110            }
111    
112            protected void addDLFileEntryTypes(
113                            long userId, long groupId, ServiceContext serviceContext)
114                    throws Exception {
115    
116                    List<String> ddmStructureNames = new ArrayList<String>();
117    
118                    addDLFileEntryType(
119                            userId, groupId, DLFileEntryTypeConstants.NAME_CONTRACT,
120                            DLFileEntryTypeConstants.NAME_CONTRACT,
121                            DLFileEntryTypeConstants.NAME_CONTRACT, ddmStructureNames,
122                            serviceContext);
123    
124                    ddmStructureNames.clear();
125    
126                    ddmStructureNames.add("Marketing Campaign Theme Metadata");
127    
128                    addDLFileEntryType(
129                            userId, groupId, DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
130                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
131                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER, ddmStructureNames,
132                            serviceContext);
133    
134                    ddmStructureNames.clear();
135    
136                    ddmStructureNames.add("Learning Module Metadata");
137    
138                    addDLFileEntryType(
139                            userId, groupId, DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
140                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
141                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING, ddmStructureNames,
142                            serviceContext);
143    
144                    ddmStructureNames.clear();
145    
146                    ddmStructureNames.add("Meeting Metadata");
147    
148                    addDLFileEntryType(
149                            userId, groupId, DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
150                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
151                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION, ddmStructureNames,
152                            serviceContext);
153    
154                    if (UpgradeProcessUtil.isCreateIGImageDocumentType()) {
155                            addDLFileEntryType(
156                                    userId, groupId, DLFileEntryTypeConstants.NAME_IG_IMAGE,
157                                    DLFileEntryTypeConstants.NAME_IG_IMAGE,
158                                    DLFileEntryTypeConstants.NAME_IG_IMAGE, ddmStructureNames,
159                                    serviceContext);
160                    }
161            }
162    
163            protected void addDLRawMetadataStructures(
164                            long userId, long groupId, ServiceContext serviceContext)
165                    throws Exception {
166    
167                    String xsd = buildDLRawMetadataXML(
168                            RawMetadataProcessorUtil.getFields());
169    
170                    Document document = SAXReaderUtil.read(new StringReader(xsd));
171    
172                    Element rootElement = document.getRootElement();
173    
174                    List<Element> structureElements = rootElement.elements("structure");
175    
176                    for (Element structureElement : structureElements) {
177                            String name = structureElement.elementText("name");
178                            String description = structureElement.elementText("description");
179    
180                            Element structureElementRootElement = structureElement.element(
181                                    "root");
182    
183                            String structureElementRootXML =
184                                    structureElementRootElement.asXML();
185    
186                            DDMStructure ddmStructure =
187                                    DDMStructureLocalServiceUtil.fetchStructure(
188                                            groupId,
189                                            PortalUtil.getClassNameId(RawMetadataProcessor.class),
190                                            name);
191    
192                            if (ddmStructure != null) {
193                                    ddmStructure.setXsd(structureElementRootXML);
194    
195                                    DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
196                            }
197                            else {
198                                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
199    
200                                    nameMap.put(LocaleUtil.getDefault(), name);
201    
202                                    Map<Locale, String> descriptionMap =
203                                            new HashMap<Locale, String>();
204    
205                                    descriptionMap.put(LocaleUtil.getDefault(), description);
206    
207                                    DDMStructureLocalServiceUtil.addStructure(
208                                            userId, groupId,
209                                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
210                                            PortalUtil.getClassNameId(RawMetadataProcessor.class), name,
211                                            nameMap, descriptionMap, structureElementRootXML, "xml",
212                                            DDMStructureConstants.TYPE_DEFAULT, serviceContext);
213                            }
214                    }
215            }
216    
217            protected String buildDLRawMetadataElementXML(Field field) {
218                    StringBundler sb = new StringBundler(16);
219    
220                    sb.append("<dynamic-element dataType=\"string\" name=\"");
221    
222                    Class<?> fieldClass = field.getDeclaringClass();
223    
224                    sb.append(fieldClass.getSimpleName());
225                    sb.append(StringPool.UNDERLINE);
226                    sb.append(field.getName());
227                    sb.append("\" type=\"text\">");
228                    sb.append("<meta-data locale=\"");
229                    sb.append(LocaleUtil.getDefault());
230                    sb.append("\">");
231                    sb.append("<entry name=\"label\"><![CDATA[metadata.");
232                    sb.append(fieldClass.getSimpleName());
233                    sb.append(StringPool.PERIOD);
234                    sb.append(field.getName());
235                    sb.append("]]></entry><entry name=\"predefinedValue\">");
236                    sb.append("<![CDATA[]]></entry><entry name=\"required\">");
237                    sb.append("<![CDATA[false]]></entry><entry name=\"showLabel\">");
238                    sb.append("<![CDATA[true]]></entry></meta-data></dynamic-element>");
239    
240                    return sb.toString();
241            }
242    
243            protected String buildDLRawMetadataStructureXML(
244                    String name, Field[] fields) {
245    
246                    StringBundler sb = new StringBundler(12 + fields.length);
247    
248                    sb.append("<structure><name><![CDATA[");
249                    sb.append(name);
250                    sb.append("]]></name>");
251                    sb.append("<description><![CDATA[");
252                    sb.append(name);
253                    sb.append("]]></description>");
254                    sb.append("<root available-locales=\"");
255                    sb.append(LocaleUtil.getDefault());
256                    sb.append("\" default-locale=\"");
257                    sb.append(LocaleUtil.getDefault());
258                    sb.append("\">");
259    
260                    for (Field field : fields) {
261                            sb.append(buildDLRawMetadataElementXML(field));
262                    }
263    
264                    sb.append("</root></structure>");
265    
266                    return sb.toString();
267            }
268    
269            protected String buildDLRawMetadataXML(Map<String, Field[]> fields) {
270                    StringBundler sb = new StringBundler(2 + fields.size());
271    
272                    sb.append("<?xml version=\"1.0\"?><root>");
273    
274                    for (String key : fields.keySet()) {
275                            sb.append(buildDLRawMetadataStructureXML(key, fields.get(key)));
276                    }
277    
278                    sb.append("</root>");
279    
280                    return sb.toString();
281            }
282    
283            protected void doRun(long companyId) throws Exception {
284                    ServiceContext serviceContext = new ServiceContext();
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    }