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.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
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.portlet.dynamicdatamapping.model.DDMStructure;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructureConstants;
029    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
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 DocumentException, PortalException, SystemException {
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                            DDMStructureLocalServiceUtil.addStructure(
085                                    userId, groupId, classNameId, ddmStructureKey, nameMap,
086                                    descriptionMap, xsd, "xml", DDMStructureConstants.TYPE_DEFAULT,
087                                    serviceContext);
088                    }
089            }
090    
091            protected List<Element> getDDMStructures(String fileName)
092                    throws DocumentException {
093    
094                    String xml = ContentUtil.get(
095                            "com/liferay/portal/events/dependencies/" + fileName);
096    
097                    Document document = SAXReaderUtil.read(xml);
098    
099                    Element rootElement = document.getRootElement();
100    
101                    return rootElement.elements("structure");
102            }
103    
104            protected String getDynamicDDMStructureXSD(
105                            String fileName, String dynamicDDMStructureName)
106                    throws DocumentException {
107    
108                    List<Element> structureElements = getDDMStructures(fileName);
109    
110                    for (Element structureElement : structureElements) {
111                            boolean dynamicStructure = GetterUtil.getBoolean(
112                                    structureElement.elementText("dynamic-structure"));
113    
114                            if (!dynamicStructure) {
115                                    continue;
116                            }
117    
118                            String name = structureElement.elementText("name");
119    
120                            if (!name.equals(dynamicDDMStructureName)) {
121                                    continue;
122                            }
123    
124                            Element structureElementRootElement = structureElement.element(
125                                    "root");
126    
127                            return structureElementRootElement.asXML();
128                    }
129    
130                    return null;
131            }
132    
133    }