001/*
002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.publisher.impl.core;
019
020import org.nuxeo.ecm.core.api.*;
021import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
022import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
023import org.nuxeo.ecm.platform.publisher.helper.RootSectionFinder;
024import org.nuxeo.ecm.platform.publisher.helper.RootSectionsFinderHelper;
025
026import java.util.ArrayList;
027import java.util.List;
028import java.util.Map;
029
030/**
031 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
032 */
033public class RootSectionsPublicationTree extends SectionPublicationTree {
034
035    protected DocumentModel currentDocument;
036
037    protected RootSectionFinder rootFinder;
038
039    protected boolean useRootSections = true;
040
041    @Override
042    public void initTree(String sid, CoreSession coreSession, Map<String, String> parameters,
043            PublishedDocumentFactory factory, String configName, String title) {
044        super.initTree(sid, coreSession, parameters, factory, configName, title);
045        rootFinder = RootSectionsFinderHelper.getRootSectionsFinder(coreSession);
046    }
047
048    @Override
049    public List<PublicationNode> getChildrenNodes() {
050        if (currentDocument != null && useRootSections) {
051            DocumentModelList rootSections = rootFinder.getAccessibleSectionRoots(currentDocument);
052            if (rootSections.isEmpty()) {
053                useRootSections = false;
054                return super.getChildrenNodes();
055            }
056            List<PublicationNode> publicationNodes = new ArrayList<PublicationNode>();
057            for (DocumentModel rootSection : rootSections) {
058                if (isPublicationNode(rootSection)) {
059                    publicationNodes.add(new CoreFolderPublicationNode(rootSection, getConfigName(), sid, rootNode,
060                            factory));
061                }
062            }
063            return publicationNodes;
064        }
065        return super.getChildrenNodes();
066    }
067
068    @Override
069    public void setCurrentDocument(DocumentModel currentDocument) {
070        this.currentDocument = currentDocument;
071        rootFinder.reset();
072        useRootSections = true;
073    }
074
075    @Override
076    public PublicationNode getNodeByPath(String path) {
077        if (!useRootSections) {
078            return super.getNodeByPath(path);
079        }
080        // if we ask for the root path of this tree, returns this because
081        // of the custom implementations of some methods (getChildrenNodes)
082        if (path.equals(rootPath)) {
083            return this;
084        } else {
085            // if we ask for a section root, returns a correct PublicationNode
086            // (with parent set to this tree)
087            List<PublicationNode> children = getChildrenNodes();
088            for (PublicationNode child : children) {
089                if (child.getPath().equals(path)) {
090                    return child;
091                }
092            }
093            return super.getNodeByPath(path);
094        }
095    }
096
097}