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:isCheckedInVersion = 0 AND ecm:isProxy = 0 " + " AND ecm:currentLifeCycleState != 'deleted' ";
048
049    protected String coreSessionId;
050
051    protected String path;
052
053    protected PublishedDocumentFactory factory;
054
055    public VirtualCoreFolderPublicationNode(String coreSessionId, String documentPath, PublicationTree tree,
056            PublishedDocumentFactory factory) {
057        super(tree);
058        this.coreSessionId = coreSessionId;
059        this.path = documentPath;
060        this.factory = factory;
061    }
062
063    public String getTitle() {
064        return "Sections";
065    }
066
067    public String getName() {
068        return "sections";
069    }
070
071    public PublicationNode getParent() {
072        return null;
073    }
074
075    public List<PublicationNode> getChildrenNodes() {
076        List<PublicationNode> childrenNodes = new ArrayList<PublicationNode>();
077        CoreSession session = getCoreSession();
078        if (session != null) {
079            String query = String.format(ACCESSIBLE_CHILDREN_QUERY, NXQL.escapeString(path));
080            List<DocumentModel> docs = session.query(query);
081            for (DocumentModel doc : docs) {
082                Path path = doc.getPath().removeLastSegments(1);
083                boolean foundParent = false;
084                for (DocumentModel d : docs) {
085                    if (d.getPath().equals(path)) {
086                        foundParent = true;
087                    }
088                }
089                if (!foundParent) {
090                    childrenNodes.add(new CoreFolderPublicationNode(doc, tree, this, factory));
091                }
092            }
093        }
094        return childrenNodes;
095    }
096
097    protected CoreSession getCoreSession() {
098        return Framework.getService(CoreSessionService.class).getCoreSession(coreSessionId);
099    }
100
101    public List<PublishedDocument> getChildrenDocuments() {
102        return Collections.emptyList();
103    }
104
105    public String getPath() {
106        return path;
107    }
108
109}