001/*
002 * (C) Copyright 2006-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 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.platform.publisher.impl.core;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.nuxeo.common.utils.Path;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.CoreSessionService;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.query.sql.NXQL;
031import org.nuxeo.ecm.platform.publisher.api.AbstractPublicationNode;
032import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
033import org.nuxeo.ecm.platform.publisher.api.PublicationTree;
034import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
035import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 */
041public class VirtualCoreFolderPublicationNode extends AbstractPublicationNode {
042
043    private static final long serialVersionUID = 1L;
044
045    protected static String ACCESSIBLE_CHILDREN_QUERY = "SELECT * FROM Document"
046            + " WHERE ecm:primaryType = 'Section' AND ecm:path STARTSWITH %s"
047            + " AND ecm:isVersion = 0 AND ecm:isProxy = 0 " + " AND ecm:isTrashed = 0 ";
048
049    protected CoreSession coreSession;
050
051    protected String path;
052
053    protected PublishedDocumentFactory factory;
054
055    public VirtualCoreFolderPublicationNode(CoreSession coreSession, String documentPath, PublicationTree tree,
056            PublishedDocumentFactory factory) {
057        super(tree);
058        this.coreSession = coreSession;
059        this.path = documentPath;
060        this.factory = factory;
061    }
062
063    @Override
064    public String getTitle() {
065        return "Sections";
066    }
067
068    @Override
069    public String getName() {
070        return "sections";
071    }
072
073    @Override
074    public PublicationNode getParent() {
075        return null;
076    }
077
078    @Override
079    public List<PublicationNode> getChildrenNodes() {
080        List<PublicationNode> childrenNodes = new ArrayList<>();
081        CoreSession session = coreSession;
082        if (session != null) {
083            String query = String.format(ACCESSIBLE_CHILDREN_QUERY, NXQL.escapeString(path));
084            List<DocumentModel> docs = session.query(query);
085            for (DocumentModel doc : docs) {
086                Path path = doc.getPath().removeLastSegments(1);
087                boolean foundParent = false;
088                for (DocumentModel d : docs) {
089                    if (d.getPath().equals(path)) {
090                        foundParent = true;
091                    }
092                }
093                if (!foundParent) {
094                    childrenNodes.add(new CoreFolderPublicationNode(doc, tree, this, factory));
095                }
096            }
097        }
098        return childrenNodes;
099    }
100
101    @Override
102    public List<PublishedDocument> getChildrenDocuments() {
103        return Collections.emptyList();
104    }
105
106    @Override
107    public String getPath() {
108        return path;
109    }
110
111}