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    @Override
047    protected void computeUserSectionRoots(DocumentModel currentDoc) {
048        this.currentDocument = currentDoc;
049        this.runUnrestricted();
050
051        if (currentDoc != null) {
052            if (!unrestrictedSectionRootFromWorkspaceConfig.isEmpty()) {
053                accessibleSectionRoots = getFiltredSectionRoots(unrestrictedSectionRootFromWorkspaceConfig, true);
054            } else {
055                accessibleSectionRoots = getFiltredSectionRoots(unrestrictedDefaultSectionRoot, true);
056            }
057        }
058    }
059
060    @Override
061    protected String buildQuery(String path) {
062        // SELECT * FROM Document WHERE ecm:path STARTSWITH '/default-domain'
063        // and (ecm:primaryType = 'Section' or ecm:primaryType = 'SectionRoot'
064        // )
065        String query = "SELECT * FROM Document WHERE ecm:path STARTSWITH " + NXQL.escapeString(path) + " and (";
066
067        int i = 0;
068        for (String type : getSectionTypes()) {
069            query = query + " ecm:primaryType = '" + type + "'";
070            i++;
071            if (i < getSectionTypes().size()) {
072                query = query + " or ";
073            } else {
074                query = query + " )";
075            }
076        }
077        query = query + " order by ecm:path ";
078        return query;
079    }
080
081    @Override
082    protected void computeUnrestrictedRoots(CoreSession session) {
083
084        if (currentDocument != null) {
085            /*
086             * Get the first parent having "publishing" schema. In order to void infinite loop, if the parent is 'Root'
087             * type just break (NXP-3359).
088             */
089            DocumentModel parentDocumentModel = currentDocument;
090            while (!parentDocumentModel.hasSchema(SCHEMA_PUBLISHING)) {
091                if ("Root".equals(parentDocumentModel.getType())) {
092                    break;
093                }
094                parentDocumentModel = session.getDocument(parentDocumentModel.getParentRef());
095            }
096
097            DocumentModelList sectionRootsFromWorkspaceConfig = getSectionRootsFromWorkspaceConfig(parentDocumentModel,
098                    session);
099            unrestrictedSectionRootFromWorkspaceConfig = new ArrayList<>();
100            for (DocumentModel root : sectionRootsFromWorkspaceConfig) {
101                unrestrictedSectionRootFromWorkspaceConfig.add(root.getPathAsString());
102            }
103        }
104
105        if (unrestrictedDefaultSectionRoot == null) {
106            unrestrictedDefaultSectionRoot = Collections.emptyList();
107        }
108    }
109
110}