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