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(String sid, CoreSession coreSession, Map<String, String> parameters, 047 PublishedDocumentFactory factory, String configName, String title) { 048 super.initTree(sid, coreSession, parameters, factory, configName, title); 049 rootFinder = Framework.getLocalService(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( 064 new CoreFolderPublicationNode(rootSection, getConfigName(), sid, rootNode, factory)); 065 } 066 } 067 return publicationNodes; 068 } 069 return super.getChildrenNodes(); 070 } 071 072 @Override 073 public void setCurrentDocument(DocumentModel currentDocument) { 074 this.currentDocument = currentDocument; 075 rootFinder.reset(); 076 useRootSections = true; 077 } 078 079 @Override 080 public PublicationNode getNodeByPath(String path) { 081 if (!useRootSections) { 082 return super.getNodeByPath(path); 083 } 084 // if we ask for the root path of this tree, returns this because 085 // of the custom implementations of some methods (getChildrenNodes) 086 if (path.equals(rootPath)) { 087 return this; 088 } else { 089 // if we ask for a section root, returns a correct PublicationNode 090 // (with parent set to this tree) 091 List<PublicationNode> children = getChildrenNodes(); 092 for (PublicationNode child : children) { 093 if (child.getPath().equals(path)) { 094 return child; 095 } 096 } 097 return super.getNodeByPath(path); 098 } 099 } 100 101}