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