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.CoreInstance;
036import org.nuxeo.ecm.core.api.CoreSession;
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    private static final long serialVersionUID = 1L;
051
052    public CollectionSyncRootFolderItem(String factoryName, FolderItem parentItem, DocumentModel doc) {
053        this(factoryName, parentItem, doc, false);
054    }
055
056    public CollectionSyncRootFolderItem(String factoryName, FolderItem parentItem, DocumentModel doc,
057            boolean relaxSyncRootConstraint) {
058        this(factoryName, parentItem, doc, relaxSyncRootConstraint, true);
059    }
060
061    public CollectionSyncRootFolderItem(String factoryName, FolderItem parentItem, DocumentModel doc,
062            boolean relaxSyncRootConstraint, boolean getLockInfo) {
063        super(factoryName, parentItem, doc, relaxSyncRootConstraint, getLockInfo);
064        canScrollDescendants = false;
065    }
066
067    protected CollectionSyncRootFolderItem() {
068        // Needed for JSON deserialization
069    }
070
071    @Override
072    @SuppressWarnings("unchecked")
073    public List<FileSystemItem> getChildren() {
074        try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
075            PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
076            Map<String, Serializable> props = new HashMap<String, Serializable>();
077            props.put(CORE_SESSION_PROPERTY, (Serializable) session);
078            PageProvider<DocumentModel> childrenPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(
079                    CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER, null, null, 0L, props, docId);
080            List<DocumentModel> dmChildren = childrenPageProvider.getCurrentPage();
081
082            List<FileSystemItem> children = new ArrayList<FileSystemItem>(dmChildren.size());
083            for (DocumentModel dmChild : dmChildren) {
084                // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
085                FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(dmChild, this, false, false,
086                        false);
087                if (child != null) {
088                    children.add(child);
089                }
090            }
091            return children;
092        }
093    }
094
095    @Override
096    public ScrollFileSystemItemList scrollDescendants(String scrollId, int batchSize, long keepAlive) {
097        throw new UnsupportedOperationException(
098                "Cannot scroll through the descendants of a collection sync root folder item, please call getChildren() instead.");
099    }
100
101    @Override
102    public FolderItem createFolder(String name) {
103        throw new UnsupportedOperationException("Cannot create a folder in a collection synchronization root.");
104    }
105
106    @Override
107    public FileItem createFile(Blob blob) {
108        throw new UnsupportedOperationException("Cannot create a file in a collection synchronization root.");
109    }
110
111    @Override
112    protected final void initialize(DocumentModel doc) {
113        super.initialize(doc);
114        // Cannot create a document in a collection sync root (could be
115        // implemented as adding it to the collection if only we new the doc
116        // path).
117        this.canCreateChild = false;
118    }
119
120}