001/*
002 * (C) Copyright 2009 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 */
019
020package org.nuxeo.ecm.platform.publisher.impl.core;
021
022import org.nuxeo.ecm.core.api.*;
023import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
024import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
025import org.nuxeo.ecm.platform.publisher.helper.RootSectionFinder;
026import org.nuxeo.ecm.platform.publisher.helper.RootSectionsFinderHelper;
027
028import java.util.ArrayList;
029import java.util.List;
030import java.util.Map;
031
032/**
033 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
034 */
035public class RootSectionsPublicationTree extends SectionPublicationTree {
036
037    protected DocumentModel currentDocument;
038
039    protected RootSectionFinder rootFinder;
040
041    protected boolean useRootSections = true;
042
043    @Override
044    public void initTree(String sid, CoreSession coreSession, Map<String, String> parameters,
045            PublishedDocumentFactory factory, String configName, String title) {
046        super.initTree(sid, coreSession, parameters, factory, configName, title);
047        rootFinder = RootSectionsFinderHelper.getRootSectionsFinder(coreSession);
048    }
049
050    @Override
051    public List<PublicationNode> getChildrenNodes() {
052        if (currentDocument != null && useRootSections) {
053            DocumentModelList rootSections = rootFinder.getAccessibleSectionRoots(currentDocument);
054            if (rootSections.isEmpty()) {
055                useRootSections = false;
056                return super.getChildrenNodes();
057            }
058            List<PublicationNode> publicationNodes = new ArrayList<PublicationNode>();
059            for (DocumentModel rootSection : rootSections) {
060                if (isPublicationNode(rootSection)) {
061                    publicationNodes.add(new CoreFolderPublicationNode(rootSection, getConfigName(), sid, rootNode,
062                            factory));
063                }
064            }
065            return publicationNodes;
066        }
067        return super.getChildrenNodes();
068    }
069
070    @Override
071    public void setCurrentDocument(DocumentModel currentDocument) {
072        this.currentDocument = currentDocument;
073        rootFinder.reset();
074        useRootSections = true;
075    }
076
077    @Override
078    public PublicationNode getNodeByPath(String path) {
079        if (!useRootSections) {
080            return super.getNodeByPath(path);
081        }
082        // if we ask for the root path of this tree, returns this because
083        // of the custom implementations of some methods (getChildrenNodes)
084        if (path.equals(rootPath)) {
085            return this;
086        } else {
087            // if we ask for a section root, returns a correct PublicationNode
088            // (with parent set to this tree)
089            List<PublicationNode> children = getChildrenNodes();
090            for (PublicationNode child : children) {
091                if (child.getPath().equals(path)) {
092                    return child;
093                }
094            }
095            return super.getNodeByPath(path);
096        }
097    }
098
099}