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.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portal.upgrade.UpgradeProcessUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntryMetadata;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
035    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.util.RawMetadataProcessor;
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     * @author Roberto D??az
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 dlFileEntryTypeKey,
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,
082                                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
083                                            ddmStructureKey);
084    
085                            if (ddmStructure == null) {
086                                    continue;
087                            }
088    
089                            ddmStructureIds.add(ddmStructure.getStructureId());
090                    }
091    
092                    Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
093    
094                    String xsd = getDynamicDDMStructureXSD(
095                            "document-library-structures.xml", dlFileEntryTypeKey, locale);
096    
097                    serviceContext.setAttribute("xsd", xsd);
098    
099                    try {
100                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
101                                    groupId, dlFileEntryTypeKey);
102                    }
103                    catch (NoSuchFileEntryTypeException nsfete) {
104                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
105                                    userId, groupId, dlFileEntryTypeKey,
106                                    getLocalizationMap(dlFileEntryTypeKey, locale),
107                                    getLocalizationMap(dlFileEntryTypeKey, locale),
108                                    ArrayUtil.toArray(
109                                            ddmStructureIds.toArray(new Long[ddmStructureIds.size()])),
110                                    serviceContext);
111                    }
112            }
113    
114            protected void addDLFileEntryTypes(
115                            long userId, long groupId, ServiceContext serviceContext)
116                    throws Exception {
117    
118                    List<String> ddmStructureNames = new ArrayList<String>();
119    
120                    addDLFileEntryType(
121                            userId, groupId, DLFileEntryTypeConstants.NAME_CONTRACT,
122                            ddmStructureNames, serviceContext);
123    
124                    ddmStructureNames.clear();
125    
126                    ddmStructureNames.add("Marketing Campaign Theme Metadata");
127    
128                    addDLFileEntryType(
129                            userId, groupId, DLFileEntryTypeConstants.NAME_MARKETING_BANNER,
130                            ddmStructureNames, serviceContext);
131    
132                    ddmStructureNames.clear();
133    
134                    ddmStructureNames.add("Learning Module Metadata");
135    
136                    addDLFileEntryType(
137                            userId, groupId, DLFileEntryTypeConstants.NAME_ONLINE_TRAINING,
138                            ddmStructureNames, serviceContext);
139    
140                    ddmStructureNames.clear();
141    
142                    ddmStructureNames.add("Meeting Metadata");
143    
144                    addDLFileEntryType(
145                            userId, groupId, DLFileEntryTypeConstants.NAME_SALES_PRESENTATION,
146                            ddmStructureNames, serviceContext);
147    
148                    if (UpgradeProcessUtil.isCreateIGImageDocumentType()) {
149                            addDLFileEntryType(
150                                    userId, groupId, DLFileEntryTypeConstants.NAME_IG_IMAGE,
151                                    ddmStructureNames, serviceContext);
152                    }
153            }
154    
155            protected void addDLRawMetadataStructures(
156                            long userId, long groupId, ServiceContext serviceContext)
157                    throws Exception {
158    
159                    Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
160    
161                    String xsd = buildDLRawMetadataXML(
162                            RawMetadataProcessorUtil.getFields(), locale);
163    
164                    Document document = SAXReaderUtil.read(new StringReader(xsd));
165    
166                    Element rootElement = document.getRootElement();
167    
168                    List<Element> structureElements = rootElement.elements("structure");
169    
170                    for (Element structureElement : structureElements) {
171                            String name = structureElement.elementText("name");
172                            String description = structureElement.elementText("description");
173    
174                            Element structureElementRootElement = structureElement.element(
175                                    "root");
176    
177                            String structureElementRootXML =
178                                    structureElementRootElement.asXML();
179    
180                            DDMStructure ddmStructure =
181                                    DDMStructureLocalServiceUtil.fetchStructure(
182                                            groupId,
183                                            PortalUtil.getClassNameId(RawMetadataProcessor.class),
184                                            name);
185    
186                            if (ddmStructure != null) {
187                                    ddmStructure.setXsd(structureElementRootXML);
188    
189                                    DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
190                            }
191                            else {
192                                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
193    
194                                    nameMap.put(locale, name);
195    
196                                    Map<Locale, String> descriptionMap =
197                                            new HashMap<Locale, String>();
198    
199                                    descriptionMap.put(locale, description);
200    
201                                    DDMStructureLocalServiceUtil.addStructure(
202                                            userId, groupId,
203                                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
204                                            PortalUtil.getClassNameId(RawMetadataProcessor.class), name,
205                                            nameMap, descriptionMap, structureElementRootXML, "xml",
206                                            DDMStructureConstants.TYPE_DEFAULT, serviceContext);
207                            }
208                    }
209            }
210    
211            protected String buildDLRawMetadataElementXML(Field field, Locale locale) {
212                    StringBundler sb = new StringBundler(14);
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("\" required=\"false\" showLabel=\"true\" type=\"text\">");
222                    sb.append("<meta-data locale=\"");
223                    sb.append(locale);
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></meta-data></dynamic-element>");
231    
232                    return sb.toString();
233            }
234    
235            protected String buildDLRawMetadataStructureXML(
236                    String name, Field[] fields, Locale locale) {
237    
238                    StringBundler sb = new StringBundler(12 + fields.length);
239    
240                    sb.append("<structure><name><![CDATA[");
241                    sb.append(name);
242                    sb.append("]]></name>");
243                    sb.append("<description><![CDATA[");
244                    sb.append(name);
245                    sb.append("]]></description>");
246                    sb.append("<root available-locales=\"");
247                    sb.append(locale);
248                    sb.append("\" default-locale=\"");
249                    sb.append(locale);
250                    sb.append("\">");
251    
252                    for (Field field : fields) {
253                            sb.append(buildDLRawMetadataElementXML(field, locale));
254                    }
255    
256                    sb.append("</root></structure>");
257    
258                    return sb.toString();
259            }
260    
261            protected String buildDLRawMetadataXML(
262                    Map<String, Field[]> fields, Locale locale) {
263    
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(
270                                    buildDLRawMetadataStructureXML(key, fields.get(key), locale));
271                    }
272    
273                    sb.append("</root>");
274    
275                    return sb.toString();
276            }
277    
278            protected void doRun(long companyId) throws Exception {
279                    ServiceContext serviceContext = new ServiceContext();
280    
281                    serviceContext.setAddGuestPermissions(true);
282                    serviceContext.setAddGroupPermissions(true);
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            protected Map<Locale, String> getLocalizationMap(
302                    String content, Locale locale) {
303    
304                    Map<Locale, String> localizationMap = new HashMap<Locale, String>();
305    
306                    localizationMap.put(locale, content);
307    
308                    return localizationMap;
309            }
310    
311    }