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