001    /**
002     * Copyright (c) 2000-2011 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.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.LocaleUtil;
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.util.ContentUtil;
030    
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    /**
037     * @author Michael C. Han
038     */
039    public abstract class BaseDefaultDDMStructureAction extends SimpleAction {
040    
041            protected void addDDMStructures(
042                            long userId, long groupId, long classNameId,
043                            String fileName, ServiceContext serviceContext)
044                    throws DocumentException, PortalException, SystemException {
045    
046                    String xml = ContentUtil.get(
047                            "com/liferay/portal/events/dependencies/" + fileName);
048    
049                    Document document = SAXReaderUtil.read(xml);
050    
051                    Element rootElement = document.getRootElement();
052    
053                    List<Element> structureElements = rootElement.elements("structure");
054    
055                    for (Element structureElement : structureElements) {
056                            String name = structureElement.elementText("name");
057    
058                            String description = structureElement.elementText("description");
059    
060                            String ddmStructureKey = name;
061    
062                            DDMStructure ddmStructure =
063                                    DDMStructureLocalServiceUtil.fetchStructure(
064                                            groupId, ddmStructureKey);
065    
066                            if (ddmStructure != null) {
067                                    continue;
068                            }
069    
070                            Element structureElementRootElement = structureElement.element(
071                                    "root");
072    
073                            String xsd = structureElementRootElement.asXML();
074    
075                            Map<Locale, String> nameMap = new HashMap<Locale, String>();
076    
077                            nameMap.put(LocaleUtil.getDefault(), name);
078    
079                            Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
080    
081                            descriptionMap.put(LocaleUtil.getDefault(), description);
082    
083                            DDMStructureLocalServiceUtil.addStructure(
084                                    userId, groupId, classNameId, ddmStructureKey, nameMap,
085                                    descriptionMap, xsd, "xml", DDMStructureConstants.TYPE_DEFAULT,
086                                    serviceContext);
087                    }
088            }
089    
090    }