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.CoreInstance;
028import org.nuxeo.ecm.core.api.CoreSession;
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.PublishedDocument;
034import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
035
036/**
037 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
038 */
039public class VirtualCoreFolderPublicationNode extends AbstractPublicationNode {
040
041    private static final long serialVersionUID = 1L;
042
043    protected static String ACCESSIBLE_CHILDREN_QUERY = "SELECT * FROM Document"
044            + " WHERE ecm:primaryType = 'Section' AND ecm:path STARTSWITH %s"
045            + " AND ecm:isCheckedInVersion = 0 AND ecm:isProxy = 0 " + " AND ecm:currentLifeCycleState != 'deleted' ";
046
047    protected String coreSessionId;
048
049    protected String path;
050
051    protected String treeConfigName;
052
053    protected PublishedDocumentFactory factory;
054
055    protected String sid;
056
057    public VirtualCoreFolderPublicationNode(String coreSessionId, String documentPath, String treeConfigName,
058            String sid, PublishedDocumentFactory factory) {
059        this.coreSessionId = coreSessionId;
060        this.path = documentPath;
061        this.treeConfigName = treeConfigName;
062        this.factory = factory;
063        this.sid = sid;
064    }
065
066    public String getTitle() {
067        return "Sections";
068    }
069
070    public String getName() {
071        return "sections";
072    }
073
074    public PublicationNode getParent() {
075        return null;
076    }
077
078    public List<PublicationNode> getChildrenNodes() {
079        List<PublicationNode> childrenNodes = new ArrayList<PublicationNode>();
080        CoreSession session = getCoreSession();
081        if (session != null) {
082            String query = String.format(ACCESSIBLE_CHILDREN_QUERY, NXQL.escapeString(path));
083            List<DocumentModel> docs = session.query(query);
084            for (DocumentModel doc : docs) {
085                Path path = doc.getPath().removeLastSegments(1);
086                boolean foundParent = false;
087                for (DocumentModel d : docs) {
088                    if (d.getPath().equals(path)) {
089                        foundParent = true;
090                    }
091                }
092                if (!foundParent) {
093                    childrenNodes.add(new CoreFolderPublicationNode(doc, treeConfigName, sid, this, factory));
094                }
095            }
096        }
097        return childrenNodes;
098    }
099
100    protected CoreSession getCoreSession() {
101        return CoreInstance.getInstance().getSession(coreSessionId);
102    }
103
104    public List<PublishedDocument> getChildrenDocuments() {
105        return Collections.emptyList();
106    }
107
108    public String getPath() {
109        return path;
110    }
111
112    public String getSessionId() {
113        return sid;
114    }
115
116}