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.Iterator;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class JournalStructureImpl extends JournalStructureBaseImpl {
030    
031            public JournalStructureImpl() {
032            }
033    
034            @Override
035            public String getMergedXsd() {
036                    String parentStructureId = getParentStructureId();
037    
038                    String xsd = getXsd();
039    
040                    if (Validator.isNull(parentStructureId)) {
041                            return xsd;
042                    }
043    
044                    try {
045                            JournalStructure parentStructure =
046                                    JournalStructureLocalServiceUtil.getStructure(
047                                            getGroupId(), parentStructureId, true);
048    
049                            Document doc = SAXReaderUtil.read(getXsd());
050    
051                            Element root = doc.getRootElement();
052    
053                            Document parentDoc = SAXReaderUtil.read(
054                                    parentStructure.getMergedXsd());
055    
056                            Element parentRoot = parentDoc.getRootElement();
057    
058                            addParentStructureId(parentRoot, parentStructureId);
059    
060                            root.content().addAll(0, parentRoot.content());
061    
062                            xsd = root.asXML();
063                    }
064                    catch (Exception e) {
065                    }
066    
067                    return xsd;
068            }
069    
070            protected void addParentStructureId(
071                    Element parentEl, String parentStructureId) {
072    
073                    Iterator<Element> itr = parentEl.elements(_DYNAMIC_ELEMENT).iterator();
074    
075                    while (itr.hasNext()) {
076                            Element dynamicEl = itr.next();
077    
078                            dynamicEl.addAttribute(_PARENT_STRUCTURE_ID, parentStructureId);
079    
080                            addParentStructureId(dynamicEl, parentStructureId);
081                    }
082            }
083    
084            private static final String _DYNAMIC_ELEMENT = "dynamic-element";
085    
086            private static final String _PARENT_STRUCTURE_ID = "parent-structure-id";
087    
088    }