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