001/*
002 * (C) Copyright 2009-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 */
019package org.nuxeo.ecm.platform.publisher.impl.core;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentModelList;
028import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
029import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
030import org.nuxeo.ecm.platform.publisher.api.PublisherService;
031import org.nuxeo.ecm.platform.publisher.helper.RootSectionFinder;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 */
037public class RootSectionsPublicationTree extends SectionPublicationTree {
038
039    protected DocumentModel currentDocument;
040
041    protected RootSectionFinder rootFinder;
042
043    protected boolean useRootSections = true;
044
045    @Override
046    public void initTree(CoreSession coreSession, Map<String, String> parameters, PublishedDocumentFactory factory,
047            String configName, String title) {
048        super.initTree(coreSession, parameters, factory, configName, title);
049        rootFinder = Framework.getService(PublisherService.class).getRootSectionFinder(coreSession);
050    }
051
052    @Override
053    public List<PublicationNode> getChildrenNodes() {
054        if (currentDocument != null && useRootSections) {
055            DocumentModelList rootSections = rootFinder.getAccessibleSectionRoots(currentDocument);
056            if (rootSections.isEmpty()) {
057                useRootSections = false;
058                return super.getChildrenNodes();
059            }
060            List<PublicationNode> publicationNodes = new ArrayList<>();
061            for (DocumentModel rootSection : rootSections) {
062                if (isPublicationNode(rootSection)) {
063                    publicationNodes.add(new CoreFolderPublicationNode(rootSection, this, rootNode, factory));
064                }
065            }
066            return publicationNodes;
067        }
068        return super.getChildrenNodes();
069    }
070
071    @Override
072    public void setCurrentDocument(DocumentModel currentDocument) {
073        this.currentDocument = currentDocument;
074        rootFinder.reset();
075        useRootSections = true;
076    }
077
078    @Override
079    public PublicationNode getNodeByPath(String path) {
080        if (!useRootSections) {
081            return super.getNodeByPath(path);
082        }
083        // if we ask for the root path of this tree, returns this because
084        // of the custom implementations of some methods (getChildrenNodes)
085        if (path.equals(rootPath)) {
086            return this;
087        } else {
088            // if we ask for a section root, returns a correct PublicationNode
089            // (with parent set to this tree)
090            List<PublicationNode> children = getChildrenNodes();
091            for (PublicationNode child : children) {
092                if (child.getPath().equals(path)) {
093                    return child;
094                }
095            }
096            return super.getNodeByPath(path);
097        }
098    }
099
100}