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