001    /**
002     * Copyright (c) 2000-2011 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),
193                                            name, nameMap, descriptionMap, structureElementRootXML,
194                                            "xml", DDMStructureConstants.TYPE_DEFAULT, serviceContext);
195                            }
196                    }
197            }
198    
199            protected String buildDLRawMetadataElementXML(String name, Field field) {
200                    StringBundler sb = new StringBundler(12);
201    
202                    sb.append("<dynamic-element dataType=\"string\" name=\"");
203                    sb.append(field.getName());
204                    sb.append("\" type=\"text\">");
205                    sb.append("<meta-data locale=\"en_US\">");
206                    sb.append("<entry name=\"label\"><![CDATA[metadata.");
207                    sb.append(name);
208                    sb.append(StringPool.PERIOD);
209                    sb.append(field.getName());
210                    sb.append("]]></entry><entry name=\"predefinedValue\">");
211                    sb.append("<![CDATA[]]></entry><entry name=\"required\">");
212                    sb.append("<![CDATA[false]]></entry><entry name=\"showLabel\">");
213                    sb.append("<![CDATA[true]]></entry></meta-data></dynamic-element>");
214    
215                    return sb.toString();
216            }
217    
218            protected String buildDLRawMetadataStructureXML(
219                    String name, Field[] fields) {
220    
221                    StringBundler sb = new StringBundler(8 + fields.length);
222    
223                    sb.append("<structure><name><![CDATA[");
224                    sb.append(name);
225                    sb.append("]]></name>");
226                    sb.append("<description><![CDATA[");
227                    sb.append(name);
228                    sb.append("]]></description>");
229                    sb.append(
230                            "<root available-locales=\"en_US\" default-locale=\"en_US\">");
231    
232                    for (Field field : fields) {
233                            sb.append(buildDLRawMetadataElementXML(name, field));
234                    }
235    
236                    sb.append("</root></structure>");
237    
238                    return sb.toString();
239            }
240    
241            protected String buildDLRawMetadataXML(Map<String, Field[]> fields) {
242                    StringBundler sb = new StringBundler(2 + fields.size());
243    
244                    sb.append("<?xml version=\"1.0\"?><root>");
245    
246                    for (String key : fields.keySet()) {
247                            sb.append(buildDLRawMetadataStructureXML(key, fields.get(key)));
248                    }
249    
250                    sb.append("</root>");
251    
252                    return sb.toString();
253            }
254    
255            protected void doRun(long companyId) throws Exception {
256                    ServiceContext serviceContext = new ServiceContext();
257    
258                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
259    
260                    serviceContext.setScopeGroupId(group.getGroupId());
261    
262                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
263    
264                    serviceContext.setUserId(defaultUserId);
265    
266                    addDDMStructures(
267                            defaultUserId, group.getGroupId(),
268                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
269                            "document-library-structures.xml", serviceContext);
270                    addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);
271                    addDLRawMetadataStructures(
272                            defaultUserId, group.getGroupId(), serviceContext);
273            }
274    
275    }