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