001/*
002 * (C) Copyright 2008 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 - initial API and implementation
018 *
019 * $Id: DefaultCreationContainerListProvider.java 30594 2008-02-26 17:21:10Z ogrisel $
020 */
021
022package org.nuxeo.ecm.platform.filemanager.service.extension;
023
024import java.io.Serializable;
025import java.util.HashMap;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
032import org.nuxeo.ecm.core.api.security.SecurityConstants;
033import org.nuxeo.ecm.platform.query.api.PageProvider;
034import org.nuxeo.ecm.platform.query.api.PageProviderService;
035import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Default contribution to the CreationContainerListProvider extension point that find the list of Workspaces the user
040 * has the right to create new document into.
041 * <p>
042 * The filtered list is sorted
043 *
044 * @author Olivier Grisel
045 */
046public class DefaultCreationContainerListProvider extends AbstractCreationContainerListProvider {
047
048    public static final String CONTAINER_LIST_PROVIDER_QM = "DEFAULT_CREATION_CONTAINER_LIST_PROVIDER";
049
050    /**
051     * @deprecated since 11.1. Use {@link Framework#getService(Class)} with {@link PageProviderService} instead.
052     */
053    @Deprecated
054    protected PageProviderService ppService;
055
056    /**
057     * @deprecated since 11.1. Use {@link Framework#getService(Class)} with {@link PageProviderService} instead.
058     */
059    @Deprecated
060    protected PageProviderService getPageProviderService() {
061        if (ppService == null) {
062            ppService = Framework.getService(PageProviderService.class);
063        }
064        return ppService;
065    }
066
067    @Override
068    @SuppressWarnings("unchecked")
069    public DocumentModelList getCreationContainerList(CoreSession documentManager, String docType) {
070        Map<String, Serializable> props = new HashMap<>();
071        props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) documentManager);
072
073        PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
074
075        PageProvider<DocumentModel> allContainers = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(
076                CONTAINER_LIST_PROVIDER_QM, null, null, null, props);
077        DocumentModelList filteredContainers = new DocumentModelListImpl();
078        for (DocumentModel container : allContainers.getCurrentPage()) {
079            if (documentManager.hasPermission(container.getRef(), SecurityConstants.ADD_CHILDREN)) {
080                filteredContainers.add(container);
081            }
082        }
083        return filteredContainers;
084    }
085
086}