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