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.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.Iterator;
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);
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                    Iterator<Element> itr = parentEl.elements(_DYNAMIC_ELEMENT).iterator();
073    
074                    while (itr.hasNext()) {
075                            Element dynamicEl = itr.next();
076    
077                            dynamicEl.addAttribute(_PARENT_STRUCTURE_ID, parentStructureId);
078    
079                            addParentStructureId(dynamicEl, parentStructureId);
080                    }
081            }
082    
083            private static final String _DYNAMIC_ELEMENT = "dynamic-element";
084    
085            private static final String _PARENT_STRUCTURE_ID = "parent-structure-id";
086    
087    }