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