001    /**
002     * Copyright (c) 2000-present 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.SimpleAction;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.template.TemplateConstants;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeProcessUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.xml.Attribute;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.DocumentException;
028    import com.liferay.portal.kernel.xml.Element;
029    import com.liferay.portal.kernel.xml.SAXReaderUtil;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
035    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
036    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
037    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
038    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
039    import com.liferay.util.ContentUtil;
040    
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    /**
047     * @author Michael C. Han
048     */
049    public abstract class BaseDefaultDDMStructureAction extends SimpleAction {
050    
051            protected void addDDMStructures(
052                            long userId, long groupId, long classNameId, String fileName,
053                            ServiceContext serviceContext)
054                    throws Exception {
055    
056                    Locale locale = PortalUtil.getSiteDefaultLocale(groupId);
057    
058                    List<Element> structureElements = getDDMStructures(fileName, locale);
059    
060                    for (Element structureElement : structureElements) {
061                            boolean dynamicStructure = GetterUtil.getBoolean(
062                                    structureElement.elementText("dynamic-structure"));
063    
064                            if (dynamicStructure) {
065                                    continue;
066                            }
067    
068                            String name = structureElement.elementText("name");
069    
070                            String description = structureElement.elementText("description");
071    
072                            String ddmStructureKey = name;
073    
074                            DDMStructure ddmStructure =
075                                    DDMStructureLocalServiceUtil.fetchStructure(
076                                            groupId, classNameId, ddmStructureKey);
077    
078                            if (ddmStructure != null) {
079                                    continue;
080                            }
081    
082                            Element structureElementRootElement = structureElement.element(
083                                    "root");
084    
085                            String definition = structureElementRootElement.asXML();
086    
087                            Map<Locale, String> nameMap = new HashMap<Locale, String>();
088                            Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
089    
090                            Locale[] locales = LanguageUtil.getAvailableLocales(groupId);
091    
092                            for (Locale curLocale : locales) {
093                                    nameMap.put(curLocale, LanguageUtil.get(curLocale, name));
094                                    descriptionMap.put(
095                                            curLocale, LanguageUtil.get(curLocale, description));
096                            }
097    
098                            Attribute defaultLocaleAttribute =
099                                    structureElementRootElement.attribute("default-locale");
100    
101                            Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(
102                                    defaultLocaleAttribute.getValue());
103    
104                            definition = DDMXMLUtil.updateXMLDefaultLocale(
105                                    definition, ddmStructureDefaultLocale, locale);
106    
107                            if (name.equals(DLFileEntryTypeConstants.NAME_IG_IMAGE) &&
108                                    !UpgradeProcessUtil.isCreateIGImageDocumentType()) {
109    
110                                    continue;
111                            }
112    
113                            ddmStructure = DDMStructureLocalServiceUtil.addStructure(
114                                    userId, groupId,
115                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, classNameId,
116                                    ddmStructureKey, nameMap, descriptionMap, definition, "xml",
117                                    DDMStructureConstants.TYPE_DEFAULT, serviceContext);
118    
119                            Element templateElement = structureElement.element("template");
120    
121                            if (templateElement == null) {
122                                    continue;
123                            }
124    
125                            String templateFileName = templateElement.elementText("file-name");
126    
127                            boolean templateCacheable = GetterUtil.getBoolean(
128                                    templateElement.elementText("cacheable"));
129    
130                            DDMTemplateLocalServiceUtil.addTemplate(
131                                    userId, groupId, PortalUtil.getClassNameId(DDMStructure.class),
132                                    ddmStructure.getStructureId(), null, nameMap, null,
133                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY,
134                                    DDMTemplateConstants.TEMPLATE_MODE_CREATE,
135                                    TemplateConstants.LANG_TYPE_FTL, getContent(templateFileName),
136                                    templateCacheable, false, StringPool.BLANK, null,
137                                    serviceContext);
138                    }
139            }
140    
141            protected String getContent(String fileName) {
142                    return ContentUtil.get(
143                            "com/liferay/portal/events/dependencies/" + fileName);
144            }
145    
146            protected List<Element> getDDMStructures(String fileName, Locale locale)
147                    throws DocumentException {
148    
149                    String xml = getContent(fileName);
150    
151                    xml = StringUtil.replace(xml, "[$LOCALE_DEFAULT$]", locale.toString());
152    
153                    Document document = SAXReaderUtil.read(xml);
154    
155                    Element rootElement = document.getRootElement();
156    
157                    return rootElement.elements("structure");
158            }
159    
160            protected String getDynamicDDMStructureDefinition(
161                            String fileName, String dynamicDDMStructureName, Locale locale)
162                    throws DocumentException {
163    
164                    List<Element> structureElements = getDDMStructures(fileName, locale);
165    
166                    for (Element structureElement : structureElements) {
167                            boolean dynamicStructure = GetterUtil.getBoolean(
168                                    structureElement.elementText("dynamic-structure"));
169    
170                            if (!dynamicStructure) {
171                                    continue;
172                            }
173    
174                            String name = structureElement.elementText("name");
175    
176                            if (!name.equals(dynamicDDMStructureName)) {
177                                    continue;
178                            }
179    
180                            Element structureElementRootElement = structureElement.element(
181                                    "root");
182    
183                            return structureElementRootElement.asXML();
184                    }
185    
186                    return null;
187            }
188    
189    }