001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.ActionException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.metadata.RawMetadataProcessorUtil;
020    import com.liferay.portal.kernel.util.ArrayUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.UserLocalServiceUtil;
032    import com.liferay.portal.upgrade.UpgradeProcessUtil;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.documentlibrary.NoSuchFileEntryTypeException;
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.documentlibrary.util.RawMetadataProcessor;
039    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
040    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
041    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
042    
043    import java.io.StringReader;
044    
045    import java.lang.reflect.Field;
046    
047    import java.util.ArrayList;
048    import java.util.HashMap;
049    import java.util.List;
050    import java.util.Locale;
051    import java.util.Map;
052    
053    /**
054     * @author Sergio Gonz??lez
055     * @author Miguel Pastor
056     * @author Roberto D??az
057     */
058    public class AddDefaultDocumentLibraryStructuresAction
059            extends BaseDefaultDDMStructureAction {
060    
061            @Override
062            public void run(String[] ids) throws ActionException {
063                    try {
064                            doRun(GetterUtil.getLong(ids[0]));
065                    }
066                    catch (Exception e) {
067                            throw new ActionException(e);
068                    }
069            }
070    
071            /**
072             * @deprecated As of 7.0.0, replaced by {@link #addDLFileEntryType(long,
073             *             long, String, String, List, ServiceContext)}
074             */
075            protected void addDLFileEntryType(
076                            long userId, long groupId, String dlFileEntryTypeKey,
077                            List<String> ddmStructureNames, ServiceContext serviceContext)
078                    throws Exception {
079    
080                    addDLFileEntryType(
081                            userId, groupId, dlFileEntryTypeKey, dlFileEntryTypeKey,
082                            ddmStructureNames, serviceContext);
083            }
084    
085            protected void addDLFileEntryType(
086                            long userId, long groupId, String languageKey,
087                            String dlFileEntryTypeKey, List<String> ddmStructureNames,
088                            ServiceContext serviceContext)
089                    throws Exception {
090    
091                    List<Long> ddmStructureIds = new ArrayList<Long>();
092    
093                    for (String ddmStructureName : ddmStructureNames) {
094                            String ddmStructureKey = ddmStructureName;
095    
096                            DDMStructure ddmStructure =
097                                    DDMStructureLocalServiceUtil.fetchStructure(
098                                            groupId,
099                                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
100                                            ddmStructureKey);
101    
102                            if (ddmStructure == null) {
103                                    continue;
104                            }
105    
106                            ddmStructureIds.add(ddmStructure.getStructureId());
107                    }
108    
109                    Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
110    
111                    String xsd = getDynamicDDMStructureXSD(
112                            "document-library-structures.xml", languageKey, locale);
113    
114                    serviceContext.setAttribute("xsd", xsd);
115    
116                    try {
117                            DLFileEntryTypeLocalServiceUtil.getFileEntryType(
118                                    groupId, dlFileEntryTypeKey);
119                    }
120                    catch (NoSuchFileEntryTypeException nsfete) {
121                            Map<Locale, String> localizationMap = getLocalizationMap(
122                                    languageKey);
123    
124                            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
125                                    userId, groupId, dlFileEntryTypeKey, localizationMap,
126                                    localizationMap,
127                                    ArrayUtil.toArray(
128                                            ddmStructureIds.toArray(new Long[ddmStructureIds.size()])),
129                                    serviceContext);
130                    }
131            }
132    
133            protected void addDLFileEntryTypes(
134                            long userId, long groupId, ServiceContext serviceContext)
135                    throws Exception {
136    
137                    List<String> ddmStructureNames = new ArrayList<String>();
138    
139                    addDLFileEntryType(
140                            userId, groupId, DLFileEntryTypeConstants.NAME_LOCALIZED_CONTRACT,
141                            DLFileEntryTypeConstants.NAME_CONTRACT, ddmStructureNames,
142                            serviceContext);
143    
144                    ddmStructureNames.clear();
145    
146                    ddmStructureNames.add("Marketing Campaign Theme Metadata");
147    
148                    addDLFileEntryType(
149                            userId, groupId,
150                            DLFileEntryTypeConstants.NAME_LOCALIZED_MARKETING_BANNER,
151                            DLFileEntryTypeConstants.NAME_MARKETING_BANNER, ddmStructureNames,
152                            serviceContext);
153    
154                    ddmStructureNames.clear();
155    
156                    ddmStructureNames.add("Learning Module Metadata");
157    
158                    addDLFileEntryType(
159                            userId, groupId,
160                            DLFileEntryTypeConstants.NAME_LOCALIZED_ONLINE_TRAINING,
161                            DLFileEntryTypeConstants.NAME_ONLINE_TRAINING, ddmStructureNames,
162                            serviceContext);
163    
164                    ddmStructureNames.clear();
165    
166                    ddmStructureNames.add("Meeting Metadata");
167    
168                    addDLFileEntryType(
169                            userId, groupId,
170                            DLFileEntryTypeConstants.NAME_LOCALIZED_SALES_PRESENTATION,
171                            DLFileEntryTypeConstants.NAME_SALES_PRESENTATION, ddmStructureNames,
172                            serviceContext);
173    
174                    if (UpgradeProcessUtil.isCreateIGImageDocumentType()) {
175                            addDLFileEntryType(
176                                    userId, groupId,
177                                    DLFileEntryTypeConstants.NAME_LOCALIZED_IG_IMAGE,
178                                    DLFileEntryTypeConstants.NAME_IG_IMAGE, ddmStructureNames,
179                                    serviceContext);
180                    }
181            }
182    
183            protected void addDLRawMetadataStructures(
184                            long userId, long groupId, ServiceContext serviceContext)
185                    throws Exception {
186    
187                    Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
188    
189                    String xsd = buildDLRawMetadataXML(
190                            RawMetadataProcessorUtil.getFields(), locale);
191    
192                    Document document = UnsecureSAXReaderUtil.read(new StringReader(xsd));
193    
194                    Element rootElement = document.getRootElement();
195    
196                    List<Element> structureElements = rootElement.elements("structure");
197    
198                    for (Element structureElement : structureElements) {
199                            String name = structureElement.elementText("name");
200                            String description = structureElement.elementText("description");
201    
202                            Element structureElementRootElement = structureElement.element(
203                                    "root");
204    
205                            String structureElementRootXML =
206                                    structureElementRootElement.asXML();
207    
208                            DDMStructure ddmStructure =
209                                    DDMStructureLocalServiceUtil.fetchStructure(
210                                            groupId,
211                                            PortalUtil.getClassNameId(RawMetadataProcessor.class),
212                                            name);
213    
214                            if (ddmStructure != null) {
215                                    ddmStructure.setXsd(structureElementRootXML);
216    
217                                    DDMStructureLocalServiceUtil.updateDDMStructure(ddmStructure);
218                            }
219                            else {
220                                    Map<Locale, String> nameMap = new HashMap<Locale, String>();
221    
222                                    nameMap.put(locale, name);
223    
224                                    Map<Locale, String> descriptionMap =
225                                            new HashMap<Locale, String>();
226    
227                                    descriptionMap.put(locale, description);
228    
229                                    DDMStructureLocalServiceUtil.addStructure(
230                                            userId, groupId,
231                                            DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
232                                            PortalUtil.getClassNameId(RawMetadataProcessor.class), name,
233                                            nameMap, descriptionMap, structureElementRootXML, "xml",
234                                            DDMStructureConstants.TYPE_DEFAULT, serviceContext);
235                            }
236                    }
237            }
238    
239            protected String buildDLRawMetadataElementXML(Field field, Locale locale) {
240                    StringBundler sb = new StringBundler(14);
241    
242                    sb.append("<dynamic-element dataType=\"string\" name=\"");
243    
244                    Class<?> fieldClass = field.getDeclaringClass();
245    
246                    sb.append(fieldClass.getSimpleName());
247                    sb.append(StringPool.UNDERLINE);
248                    sb.append(field.getName());
249                    sb.append("\" required=\"false\" showLabel=\"true\" type=\"text\">");
250                    sb.append("<meta-data locale=\"");
251                    sb.append(locale);
252                    sb.append("\">");
253                    sb.append("<entry name=\"label\"><![CDATA[metadata.");
254                    sb.append(fieldClass.getSimpleName());
255                    sb.append(StringPool.PERIOD);
256                    sb.append(field.getName());
257                    sb.append("]]></entry><entry name=\"predefinedValue\">");
258                    sb.append("<![CDATA[]]></entry></meta-data></dynamic-element>");
259    
260                    return sb.toString();
261            }
262    
263            protected String buildDLRawMetadataStructureXML(
264                    String name, Field[] fields, Locale locale) {
265    
266                    StringBundler sb = new StringBundler(12 + fields.length);
267    
268                    sb.append("<structure><name><![CDATA[");
269                    sb.append(name);
270                    sb.append("]]></name>");
271                    sb.append("<description><![CDATA[");
272                    sb.append(name);
273                    sb.append("]]></description>");
274                    sb.append("<root available-locales=\"");
275                    sb.append(locale);
276                    sb.append("\" default-locale=\"");
277                    sb.append(locale);
278                    sb.append("\">");
279    
280                    for (Field field : fields) {
281                            sb.append(buildDLRawMetadataElementXML(field, locale));
282                    }
283    
284                    sb.append("</root></structure>");
285    
286                    return sb.toString();
287            }
288    
289            protected String buildDLRawMetadataXML(
290                    Map<String, Field[]> fields, Locale locale) {
291    
292                    StringBundler sb = new StringBundler(2 + fields.size());
293    
294                    sb.append("<?xml version=\"1.0\"?><root>");
295    
296                    for (String key : fields.keySet()) {
297                            sb.append(
298                                    buildDLRawMetadataStructureXML(key, fields.get(key), locale));
299                    }
300    
301                    sb.append("</root>");
302    
303                    return sb.toString();
304            }
305    
306            protected void doRun(long companyId) throws Exception {
307                    ServiceContext serviceContext = new ServiceContext();
308    
309                    serviceContext.setAddGuestPermissions(true);
310                    serviceContext.setAddGroupPermissions(true);
311    
312                    Group group = GroupLocalServiceUtil.getCompanyGroup(companyId);
313    
314                    serviceContext.setScopeGroupId(group.getGroupId());
315    
316                    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
317    
318                    serviceContext.setUserId(defaultUserId);
319    
320                    addDDMStructures(
321                            defaultUserId, group.getGroupId(),
322                            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
323                            "document-library-structures.xml", serviceContext);
324                    addDLFileEntryTypes(defaultUserId, group.getGroupId(), serviceContext);
325                    addDLRawMetadataStructures(
326                            defaultUserId, group.getGroupId(), serviceContext);
327            }
328    
329            /**
330             * @deprecated As of 7.0.0, replaced by {@link #getLocalizationMap(String)}
331             */
332            protected Map<Locale, String> getLocalizationMap(
333                    String content, Locale locale) {
334    
335                    Map<Locale, String> localizationMap = new HashMap<Locale, String>();
336    
337                    localizationMap.put(locale, content);
338    
339                    return localizationMap;
340            }
341    
342            protected Map<Locale, String> getLocalizationMap(String content) {
343                    Map<Locale, String> localizationMap = new HashMap<Locale, String>();
344    
345                    Locale defaultLocale = LocaleUtil.getDefault();
346    
347                    String defaultValue = LanguageUtil.get(defaultLocale, content);
348    
349                    for (Locale locale : LanguageUtil.getSupportedLocales()) {
350                            String value = LanguageUtil.get(locale, content);
351    
352                            if (!locale.equals(defaultLocale) && value.equals(defaultValue)) {
353                                    continue;
354                            }
355    
356                            localizationMap.put(locale, value);
357                    }
358    
359                    return localizationMap;
360            }
361    
362    }