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.finder;
019
020import java.util.ArrayList;
021import java.util.Collections;
022
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.DocumentModelList;
026import org.nuxeo.ecm.core.query.sql.NXQL;
027import org.nuxeo.ecm.platform.publisher.helper.RootSectionFinder;
028
029/**
030 * Helper class to manage:
031 * <ul>
032 * <li>unrestricted fetch of Sections
033 * <li>filtering according to user rights
034 * </ul>
035 *
036 * @author tiry
037 */
038public class DefaultRootSectionsFinder extends AbstractRootSectionsFinder implements RootSectionFinder {
039
040    public DefaultRootSectionsFinder(CoreSession userSession) {
041        super(userSession);
042    }
043
044    protected void computeUserSectionRoots(DocumentModel currentDoc) {
045        this.currentDocument = currentDoc;
046        this.runUnrestricted();
047
048        if (currentDoc != null) {
049            if (!unrestrictedSectionRootFromWorkspaceConfig.isEmpty()) {
050                accessibleSectionRoots = getFiltredSectionRoots(unrestrictedSectionRootFromWorkspaceConfig, true);
051            } else {
052                accessibleSectionRoots = getFiltredSectionRoots(unrestrictedDefaultSectionRoot, true);
053            }
054        }
055    }
056
057    protected String buildQuery(String path) {
058        // SELECT * FROM Document WHERE ecm:path STARTSWITH '/default-domain'
059        // and (ecm:primaryType = 'Section' or ecm:primaryType = 'SectionRoot'
060        // )
061        String query = "SELECT * FROM Document WHERE ecm:path STARTSWITH " + NXQL.escapeString(path) + " and (";
062
063        int i = 0;
064        for (String type : getSectionTypes()) {
065            query = query + " ecm:primaryType = '" + type + "'";
066            i++;
067            if (i < getSectionTypes().size()) {
068                query = query + " or ";
069            } else {
070                query = query + " )";
071            }
072        }
073        query = query + " order by ecm:path ";
074        return query;
075    }
076
077    protected void computeUnrestrictedRoots(CoreSession session) {
078
079        if (currentDocument != null) {
080            /*
081             * Get the first parent having "publishing" schema. In order to void infinite loop, if the parent is 'Root'
082             * type just break (NXP-3359).
083             */
084            DocumentModel parentDocumentModel = currentDocument;
085            while (!parentDocumentModel.hasSchema(SCHEMA_PUBLISHING)) {
086                if ("Root".equals(parentDocumentModel.getType())) {
087                    break;
088                }
089                parentDocumentModel = session.getDocument(parentDocumentModel.getParentRef());
090            }
091
092            DocumentModelList sectionRootsFromWorkspaceConfig = getSectionRootsFromWorkspaceConfig(parentDocumentModel,
093                    session);
094            unrestrictedSectionRootFromWorkspaceConfig = new ArrayList<String>();
095            for (DocumentModel root : sectionRootsFromWorkspaceConfig) {
096                unrestrictedSectionRootFromWorkspaceConfig.add(root.getPathAsString());
097            }
098        }
099
100        if (unrestrictedDefaultSectionRoot == null) {
101            unrestrictedDefaultSectionRoot = Collections.emptyList();
102        }
103    }
104
105}