001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.platform.publisher.impl.core;
019
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.List;
023
024import org.nuxeo.common.utils.Path;
025import org.nuxeo.ecm.core.api.CoreInstance;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.query.sql.NXQL;
029import org.nuxeo.ecm.platform.publisher.api.AbstractPublicationNode;
030import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
031import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
032import org.nuxeo.ecm.platform.publisher.api.PublishedDocumentFactory;
033
034/**
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 */
037public class VirtualCoreFolderPublicationNode extends AbstractPublicationNode {
038
039    private static final long serialVersionUID = 1L;
040
041    protected static String ACCESSIBLE_CHILDREN_QUERY = "SELECT * FROM Document"
042            + " WHERE ecm:primaryType = 'Section' AND ecm:path STARTSWITH %s"
043            + " AND ecm:isCheckedInVersion = 0 AND ecm:isProxy = 0 " + " AND ecm:currentLifeCycleState != 'deleted' ";
044
045    protected String coreSessionId;
046
047    protected String path;
048
049    protected String treeConfigName;
050
051    protected PublishedDocumentFactory factory;
052
053    protected String sid;
054
055    public VirtualCoreFolderPublicationNode(String coreSessionId, String documentPath, String treeConfigName,
056            String sid, PublishedDocumentFactory factory) {
057        this.coreSessionId = coreSessionId;
058        this.path = documentPath;
059        this.treeConfigName = treeConfigName;
060        this.factory = factory;
061        this.sid = sid;
062    }
063
064    public String getTitle() {
065        return "Sections";
066    }
067
068    public String getName() {
069        return "sections";
070    }
071
072    public PublicationNode getParent() {
073        return null;
074    }
075
076    public List<PublicationNode> getChildrenNodes() {
077        List<PublicationNode> childrenNodes = new ArrayList<PublicationNode>();
078        CoreSession session = getCoreSession();
079        if (session != null) {
080            String query = String.format(ACCESSIBLE_CHILDREN_QUERY, NXQL.escapeString(path));
081            List<DocumentModel> docs = session.query(query);
082            for (DocumentModel doc : docs) {
083                Path path = doc.getPath().removeLastSegments(1);
084                boolean foundParent = false;
085                for (DocumentModel d : docs) {
086                    if (d.getPath().equals(path)) {
087                        foundParent = true;
088                    }
089                }
090                if (!foundParent) {
091                    childrenNodes.add(new CoreFolderPublicationNode(doc, treeConfigName, sid, this, factory));
092                }
093            }
094        }
095        return childrenNodes;
096    }
097
098    protected CoreSession getCoreSession() {
099        return CoreInstance.getInstance().getSession(coreSessionId);
100    }
101
102    public List<PublishedDocument> getChildrenDocuments() {
103        return Collections.emptyList();
104    }
105
106    public String getPath() {
107        return path;
108    }
109
110    public String getSessionId() {
111        return sid;
112    }
113
114}