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