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        // detach user workspace as we will use another session to update it
085        userWorkspace.detach(true);
086    }
087
088    protected UserWorkspaceTopLevelFolderItem() {
089        // Needed for JSON deserialization
090    }
091
092    /*--------------------- AbstractFileSystemItem ---------------------*/
093    @Override
094    public void rename(String name) {
095        throw new UnsupportedOperationException("Cannot rename the top level folder item.");
096    }
097
098    @Override
099    public void delete() {
100        throw new UnsupportedOperationException("Cannot delete the top level folder item.");
101    }
102
103    @Override
104    public FileSystemItem move(FolderItem dest) {
105        throw new UnsupportedOperationException("Cannot move the top level folder item.");
106    }
107
108    /*--------------------- FolderItem -----------------*/
109    @Override
110    public List<FileSystemItem> getChildren() {
111
112        // Register user workspace as a synchronization root if it is not
113        // already the case
114        if (!getNuxeoDriveManager().isSynchronizationRoot(principal, userWorkspace)) {
115            try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
116                getNuxeoDriveManager().registerSynchronizationRoot(principal, userWorkspace, session);
117            }
118        }
119
120        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
121
122        // Add user workspace children
123        children.addAll(super.getChildren());
124
125        // Add synchronization root parent folder
126        if (syncRootParentFactoryName == null) {
127            if (log.isDebugEnabled()) {
128                log.debug(String.format(
129                        "No synchronization root parent factory name parameter for factory %s, the synchronization roots won't be synchronized client side.",
130                        factoryName));
131            }
132
133        } else {
134            VirtualFolderItemFactory syncRootParentFactory = getFileSystemItemAdapterService().getVirtualFolderItemFactory(
135                    syncRootParentFactoryName);
136            FolderItem syncRootParent = syncRootParentFactory.getVirtualFolderItem(principal);
137            if (syncRootParent != null) {
138                children.add(syncRootParent);
139            }
140        }
141
142        return children;
143    }
144
145    @Override
146    public ScrollFileSystemItemList scrollDescendants(String scrollId, int batchSize, long keepAlive) {
147        throw new UnsupportedOperationException(
148                "Cannot scroll through the descendants of the user workspace top level folder item, please call getChildren() instead.");
149    }
150
151    protected NuxeoDriveManager getNuxeoDriveManager() {
152        return Framework.getService(NuxeoDriveManager.class);
153    }
154}