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 <ogrisel@nuxeo.com>
045 */
046public class DefaultCreationContainerListProvider extends AbstractCreationContainerListProvider {
047
048    public static final String CONTAINER_LIST_PROVIDER_QM = "DEFAULT_CREATION_CONTAINER_LIST_PROVIDER";
049
050    protected PageProviderService ppService;
051
052    protected PageProviderService getPageProviderService() {
053        if (ppService == null) {
054            ppService = Framework.getLocalService(PageProviderService.class);
055        }
056        return ppService;
057    }
058
059    @SuppressWarnings("unchecked")
060    public DocumentModelList getCreationContainerList(CoreSession documentManager, String docType) {
061        Map<String, Serializable> props = new HashMap<String, Serializable>();
062        props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) documentManager);
063
064        PageProvider<DocumentModel> allContainers = (PageProvider<DocumentModel>) getPageProviderService().getPageProvider(
065                CONTAINER_LIST_PROVIDER_QM, null, null, null, props);
066        DocumentModelList filteredContainers = new DocumentModelListImpl();
067        for (DocumentModel container : allContainers.getCurrentPage()) {
068            if (documentManager.hasPermission(container.getRef(), SecurityConstants.ADD_CHILDREN)) {
069                filteredContainers.add(container);
070            }
071        }
072        return filteredContainers;
073    }
074
075}