001    /**
002     * Copyright (c) 2000-2012 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.portal.model.Group;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
023    import com.liferay.portlet.journal.NoSuchStructureException;
024    import com.liferay.portlet.journal.model.JournalStructure;
025    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
026    
027    import java.util.Iterator;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class JournalStructureImpl extends JournalStructureBaseImpl {
033    
034            public JournalStructureImpl() {
035            }
036    
037            public String getMergedXsd() {
038                    String parentStructureId = getParentStructureId();
039    
040                    String xsd = getXsd();
041    
042                    if (Validator.isNull(parentStructureId)) {
043                            return xsd;
044                    }
045    
046                    try {
047                            JournalStructure parentStructure = null;
048    
049                            try {
050                                    parentStructure = JournalStructureLocalServiceUtil.getStructure(
051                                            getGroupId(), parentStructureId);
052                            }
053                            catch (NoSuchStructureException nsse) {
054                                    Group group = GroupLocalServiceUtil.getGroup(getGroupId());
055    
056                                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
057                                            group.getCompanyId());
058    
059                                    if (getGroupId() == companyGroup.getGroupId()) {
060                                            throw new NoSuchStructureException();
061                                    }
062    
063                                    parentStructure = JournalStructureLocalServiceUtil.getStructure(
064                                            companyGroup.getGroupId(), parentStructureId);
065                            }
066    
067                            Document doc = SAXReaderUtil.read(getXsd());
068    
069                            Element root = doc.getRootElement();
070    
071                            Document parentDoc = SAXReaderUtil.read(
072                                    parentStructure.getMergedXsd());
073    
074                            Element parentRoot = parentDoc.getRootElement();
075    
076                            addParentStructureId(parentRoot, parentStructureId);
077    
078                            root.content().addAll(0, parentRoot.content());
079    
080                            xsd = root.asXML();
081                    }
082                    catch (Exception e) {
083                    }
084    
085                    return xsd;
086            }
087    
088            protected void addParentStructureId(
089                    Element parentEl, String parentStructureId) {
090    
091                    Iterator<Element> itr = parentEl.elements(_DYNAMIC_ELEMENT).iterator();
092    
093                    while (itr.hasNext()) {
094                            Element dynamicEl = itr.next();
095    
096                            dynamicEl.addAttribute(_PARENT_STRUCTURE_ID, parentStructureId);
097    
098                            addParentStructureId(dynamicEl, parentStructureId);
099                    }
100            }
101    
102            private static final String _DYNAMIC_ELEMENT = "dynamic-element";
103    
104            private static final String _PARENT_STRUCTURE_ID = "parent-structure-id";
105    
106    }