001    /**
002     * Copyright (c) 2000-2013 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.portlet.journal.model.impl;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    import com.liferay.portal.kernel.xml.Document;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.SAXReaderUtil;
021    import com.liferay.portlet.journal.model.JournalStructure;
022    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
023    
024    import java.util.List;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class JournalStructureImpl extends JournalStructureBaseImpl {
030    
031            public JournalStructureImpl() {
032            }
033    
034            public String getMergedXsd() {
035                    String parentStructureId = getParentStructureId();
036    
037                    String xsd = getXsd();
038    
039                    if (Validator.isNull(parentStructureId)) {
040                            return xsd;
041                    }
042    
043                    try {
044                            JournalStructure parentStructure =
045                                    JournalStructureLocalServiceUtil.getStructure(
046                                            getGroupId(), parentStructureId, true);
047    
048                            Document doc = SAXReaderUtil.read(getXsd());
049    
050                            Element root = doc.getRootElement();
051    
052                            Document parentDoc = SAXReaderUtil.read(
053                                    parentStructure.getMergedXsd());
054    
055                            Element parentRoot = parentDoc.getRootElement();
056    
057                            addParentStructureId(parentRoot, parentStructureId);
058    
059                            root.content().addAll(0, parentRoot.content());
060    
061                            xsd = root.asXML();
062                    }
063                    catch (Exception e) {
064                    }
065    
066                    return xsd;
067            }
068    
069            protected void addParentStructureId(
070                    Element parentEl, String parentStructureId) {
071    
072                    List<Element> dynamicElements = parentEl.elements(_DYNAMIC_ELEMENT);
073    
074                    for (Element dynamicElement : dynamicElements) {
075                            dynamicElement.addAttribute(
076                                    _PARENT_STRUCTURE_ID, parentStructureId);
077    
078                            addParentStructureId(dynamicElement, parentStructureId);
079                    }
080            }
081    
082            private static final String _DYNAMIC_ELEMENT = "dynamic-element";
083    
084            private static final String _PARENT_STRUCTURE_ID = "parent-structure-id";
085    
086    }