001/*
002 * (C) Copyright 2013 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.hierarchy.userworkspace.adapter;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.drive.adapter.FileSystemItem;
027import org.nuxeo.drive.adapter.FolderItem;
028import org.nuxeo.drive.adapter.ScrollFileSystemItemList;
029import org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem;
030import org.nuxeo.drive.service.NuxeoDriveManager;
031import org.nuxeo.drive.service.VirtualFolderItemFactory;
032import org.nuxeo.ecm.core.api.CoreInstance;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * User workspace based implementation of the top level {@link FolderItem}.
039 * <p>
040 * Implements the following tree:
041 *
042 * <pre>
043 * Nuxeo Drive
044 *  |-- User workspace child 1
045 *  |-- User workspace child 2
046 *  |-- ...
047 *  |-- My synchronized folders
048 *         |-- Synchronized folder 1
049 *         |-- Synchronized folder 2
050 *         |-- ...
051 * </pre>
052 *
053 * @author Antoine Taillefer
054 */
055public class UserWorkspaceTopLevelFolderItem extends DocumentBackedFolderItem {
056
057    private static final long serialVersionUID = 1L;
058
059    private static final Log log = LogFactory.getLog(UserWorkspaceTopLevelFolderItem.class);
060
061    protected DocumentModel userWorkspace;
062
063    protected String syncRootParentFactoryName;
064
065    public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName,
066            String syncRootParentFactoryName) {
067        this(factoryName, userWorkspace, folderName, syncRootParentFactoryName, false);
068    }
069
070    public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName,
071            String syncRootParentFactoryName, boolean relaxSyncRootConstraint) {
072        this(factoryName, userWorkspace, folderName, syncRootParentFactoryName, relaxSyncRootConstraint, true);
073    }
074
075    public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName,
076            String syncRootParentFactoryName, boolean relaxSyncRootConstraint, boolean getLockInfo) {
077        super(factoryName, null, userWorkspace, relaxSyncRootConstraint, getLockInfo);
078        name = folderName;
079        canRename = false;
080        canDelete = false;
081        canScrollDescendants = false;
082        this.userWorkspace = userWorkspace;
083        this.syncRootParentFactoryName = syncRootParentFactoryName;
084    }
085
086    protected UserWorkspaceTopLevelFolderItem() {
087        // Needed for JSON deserialization
088    }
089
090    /*--------------------- AbstractFileSystemItem ---------------------*/
091    @Override
092    public void rename(String name) {
093        throw new UnsupportedOperationException("Cannot rename the top level folder item.");
094    }
095
096    @Override
097    public void delete() {
098        throw new UnsupportedOperationException("Cannot delete the top level folder item.");
099    }
100
101    @Override
102    public FileSystemItem move(FolderItem dest) {
103        throw new UnsupportedOperationException("Cannot move the top level folder item.");
104    }
105
106    /*--------------------- FolderItem -----------------*/
107    @Override
108    public List<FileSystemItem> getChildren() {
109
110        // Register user workspace as a synchronization root if it is not
111        // already the case
112        if (!getNuxeoDriveManager().isSynchronizationRoot(principal, userWorkspace)) {
113            try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
114                getNuxeoDriveManager().registerSynchronizationRoot(principal, userWorkspace, session);
115            }
116        }
117
118        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
119
120        // Add user workspace children
121        children.addAll(super.getChildren());
122
123        // Add synchronization root parent folder
124        if (syncRootParentFactoryName == null) {
125            if (log.isDebugEnabled()) {
126                log.debug(String.format(
127                        "No synchronization root parent factory name parameter for factory %s, the synchronization roots won't be synchronized client side.",
128                        factoryName));
129            }
130
131        } else {
132            VirtualFolderItemFactory syncRootParentFactory = getFileSystemItemAdapterService().getVirtualFolderItemFactory(
133                    syncRootParentFactoryName);
134            FolderItem syncRootParent = syncRootParentFactory.getVirtualFolderItem(principal);
135            if (syncRootParent != null) {
136                children.add(syncRootParent);
137            }
138        }
139
140        return children;
141    }
142
143    @Override
144    public ScrollFileSystemItemList scrollDescendants(String scrollId, int batchSize, long keepAlive) {
145        throw new UnsupportedOperationException(
146                "Cannot scroll through the descendants of the user workspace top level folder item, please call getChildren() instead.");
147    }
148
149    protected NuxeoDriveManager getNuxeoDriveManager() {
150        return Framework.getLocalService(NuxeoDriveManager.class);
151    }
152}