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    private static final long serialVersionUID = 1L;
040
041    protected DocumentModel currentDocument;
042
043    protected RootSectionFinder rootFinder;
044
045    protected boolean useRootSections = true;
046
047    @Override
048    public void initTree(CoreSession coreSession, Map<String, String> parameters, PublishedDocumentFactory factory,
049            String configName, String title) {
050        super.initTree(coreSession, parameters, factory, configName, title);
051        rootFinder = Framework.getService(PublisherService.class).getRootSectionFinder(coreSession);
052    }
053
054    @Override
055    public List<PublicationNode> getChildrenNodes() {
056        if (currentDocument != null && useRootSections) {
057            DocumentModelList rootSections = rootFinder.getAccessibleSectionRoots(currentDocument);
058            if (rootSections.isEmpty()) {
059                useRootSections = false;
060                return super.getChildrenNodes();
061            }
062            List<PublicationNode> publicationNodes = new ArrayList<>();
063            for (DocumentModel rootSection : rootSections) {
064                if (isPublicationNode(rootSection)) {
065                    publicationNodes.add(new CoreFolderPublicationNode(rootSection, this, rootNode, factory));
066                }
067            }
068            return publicationNodes;
069        }
070        return super.getChildrenNodes();
071    }
072
073    @Override
074    public void setCurrentDocument(DocumentModel currentDocument) {
075        this.currentDocument = currentDocument;
076        rootFinder.reset();
077        useRootSections = true;
078    }
079
080    @Override
081    public PublicationNode getNodeByPath(String path) {
082        if (!useRootSections) {
083            return super.getNodeByPath(path);
084        }
085        // if we ask for the root path of this tree, returns this because
086        // of the custom implementations of some methods (getChildrenNodes)
087        if (path.equals(rootPath)) {
088            return this;
089        } else {
090            // if we ask for a section root, returns a correct PublicationNode
091            // (with parent set to this tree)
092            List<PublicationNode> children = getChildrenNodes();
093            for (PublicationNode child : children) {
094                if (child.getPath().equals(path)) {
095                    return child;
096                }
097            }
098            return super.getNodeByPath(path);
099        }
100    }
101
102}