001    /**
002     * Copyright (c) 2000-2011 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.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    }