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.SimpleAction;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Attribute;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.DocumentException;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.upgrade.UpgradeProcessUtil;
028    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
031    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
032    import com.liferay.portlet.dynamicdatamapping.util.DDMXMLUtil;
033    import com.liferay.util.ContentUtil;
034    
035    import java.util.HashMap;
036    import java.util.List;
037    import java.util.Locale;
038    import java.util.Map;
039    
040    /**
041     * @author Michael C. Han
042     */
043    public abstract class BaseDefaultDDMStructureAction extends SimpleAction {
044    
045            protected void addDDMStructures(
046                            long userId, long groupId, long classNameId, String fileName,
047                            ServiceContext serviceContext)
048                    throws Exception {
049    
050                    List<Element> structureElements = getDDMStructures(fileName);
051    
052                    for (Element structureElement : structureElements) {
053                            boolean dynamicStructure = GetterUtil.getBoolean(
054                                    structureElement.elementText("dynamic-structure"));
055    
056                            if (dynamicStructure) {
057                                    continue;
058                            }
059    
060                            String name = structureElement.elementText("name");
061    
062                            String description = structureElement.elementText("description");
063    
064                            String ddmStructureKey = name;
065    
066                            DDMStructure ddmStructure =
067                                    DDMStructureLocalServiceUtil.fetchStructure(
068                                            groupId, ddmStructureKey);
069    
070                            if (ddmStructure != null) {
071                                    continue;
072                            }
073    
074                            Element structureElementRootElement = structureElement.element(
075                                    "root");
076    
077                            String xsd = structureElementRootElement.asXML();
078    
079                            Map<Locale, String> nameMap = new HashMap<Locale, String>();
080    
081                            nameMap.put(LocaleUtil.getDefault(), name);
082    
083                            Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
084    
085                            descriptionMap.put(LocaleUtil.getDefault(), description);
086    
087                            Attribute defaultLocaleAttribute =
088                                    structureElementRootElement.attribute("default-locale");
089    
090                            Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(
091                                    defaultLocaleAttribute.getValue());
092    
093                            xsd = DDMXMLUtil.updateXMLDefaultLocale(
094                                    xsd, ddmStructureDefaultLocale, LocaleUtil.getDefault());
095    
096                            if (name.equals(DLFileEntryTypeConstants.NAME_IG_IMAGE) &&
097                                    !UpgradeProcessUtil.isCreateIGImageDocumentType()) {
098    
099                                    continue;
100                            }
101    
102                            DDMStructureLocalServiceUtil.addStructure(
103                                    userId, groupId,
104                                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, classNameId,
105                                    ddmStructureKey, nameMap, descriptionMap, xsd, "xml",
106                                    DDMStructureConstants.TYPE_DEFAULT, serviceContext);
107                    }
108            }
109    
110            protected List<Element> getDDMStructures(String fileName)
111                    throws DocumentException {
112    
113                    String xml = ContentUtil.get(
114                            "com/liferay/portal/events/dependencies/" + fileName);
115    
116                    Locale locale = LocaleUtil.getDefault();
117    
118                    xml = StringUtil.replace(xml, "[$LOCALE_DEFAULT$]", locale.toString());
119    
120                    Document document = SAXReaderUtil.read(xml);
121    
122                    Element rootElement = document.getRootElement();
123    
124                    return rootElement.elements("structure");
125            }
126    
127            protected String getDynamicDDMStructureXSD(
128                            String fileName, String dynamicDDMStructureName)
129                    throws DocumentException {
130    
131                    List<Element> structureElements = getDDMStructures(fileName);
132    
133                    for (Element structureElement : structureElements) {
134                            boolean dynamicStructure = GetterUtil.getBoolean(
135                                    structureElement.elementText("dynamic-structure"));
136    
137                            if (!dynamicStructure) {
138                                    continue;
139                            }
140    
141                            String name = structureElement.elementText("name");
142    
143                            if (!name.equals(dynamicDDMStructureName)) {
144                                    continue;
145                            }
146    
147                            Element structureElementRootElement = structureElement.element(
148                                    "root");
149    
150                            return structureElementRootElement.asXML();
151                    }
152    
153                    return null;
154            }
155    
156    }