001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.adapter.impl;
018
019import static org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.drive.adapter.FileItem;
028import org.nuxeo.drive.adapter.FileSystemItem;
029import org.nuxeo.drive.adapter.FolderItem;
030import org.nuxeo.ecm.collections.api.CollectionConstants;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.CoreInstance;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.platform.query.api.PageProvider;
036import org.nuxeo.ecm.platform.query.api.PageProviderService;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * Default implementation of a collection synchronization root {@link FolderItem}.
041 *
042 * @author Antoine Taillefer
043 * @since 6.0
044 */
045public class CollectionSyncRootFolderItem extends DefaultSyncRootFolderItem implements FolderItem {
046
047    private static final long serialVersionUID = 1L;
048
049    public CollectionSyncRootFolderItem(String factoryName, FolderItem parentItem, DocumentModel doc) {
050        this(factoryName, parentItem, doc, false);
051    }
052
053    public CollectionSyncRootFolderItem(String factoryName, FolderItem parentItem, DocumentModel doc,
054            boolean relaxSyncRootConstraint) {
055        super(factoryName, parentItem, doc, relaxSyncRootConstraint);
056    }
057
058    protected CollectionSyncRootFolderItem() {
059        // Needed for JSON deserialization
060    }
061
062    @Override
063    @SuppressWarnings("unchecked")
064    public List<FileSystemItem> getChildren() {
065        try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
066            PageProviderService pageProviderService = Framework.getLocalService(PageProviderService.class);
067            Map<String, Serializable> props = new HashMap<String, Serializable>();
068            props.put(CORE_SESSION_PROPERTY, (Serializable) session);
069            PageProvider<DocumentModel> childrenPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(
070                    CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER, null, null, 0L, props, docId);
071            List<DocumentModel> dmChildren = childrenPageProvider.getCurrentPage();
072
073            List<FileSystemItem> children = new ArrayList<FileSystemItem>(dmChildren.size());
074            for (DocumentModel dmChild : dmChildren) {
075                FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(dmChild, this);
076                if (child != null) {
077                    children.add(child);
078                }
079            }
080            return children;
081        }
082    }
083
084    @Override
085    public FolderItem createFolder(String name) {
086        throw new UnsupportedOperationException("Cannot create a folder in a collection synchronization root.");
087    }
088
089    @Override
090    public FileItem createFile(Blob blob) {
091        throw new UnsupportedOperationException("Cannot create a file in a collection synchronization root.");
092    }
093
094    @Override
095    protected final void initialize(DocumentModel doc) {
096        super.initialize(doc);
097        // Cannot create a document in a collection sync root (could be
098        // implemented as adding it to the collection if only we new the doc
099        // path).
100        this.canCreateChild = false;
101    }
102
103}