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