001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Antoine Taillefer <ataillefer@nuxeo.com>
016 */
017package org.nuxeo.drive.hierarchy.userworkspace.adapter;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.drive.adapter.FileSystemItem;
025import org.nuxeo.drive.adapter.FolderItem;
026import org.nuxeo.drive.adapter.impl.DocumentBackedFolderItem;
027import org.nuxeo.drive.service.NuxeoDriveManager;
028import org.nuxeo.drive.service.VirtualFolderItemFactory;
029import org.nuxeo.ecm.core.api.CoreInstance;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * User workspace based implementation of the top level {@link FolderItem}.
036 * <p>
037 * Implements the following tree:
038 *
039 * <pre>
040 * Nuxeo Drive
041 *  |-- User workspace child 1
042 *  |-- User workspace child 2
043 *  |-- ...
044 *  |-- My synchronized folders
045 *         |-- Synchronized folder 1
046 *         |-- Synchronized folder 2
047 *         |-- ...
048 * </pre>
049 *
050 * @author Antoine Taillefer
051 */
052public class UserWorkspaceTopLevelFolderItem extends DocumentBackedFolderItem {
053
054    private static final long serialVersionUID = 1L;
055
056    private static final Log log = LogFactory.getLog(UserWorkspaceTopLevelFolderItem.class);
057
058    protected DocumentModel userWorkspace;
059
060    protected String syncRootParentFactoryName;
061
062    public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName,
063            String syncRootParentFactoryName) {
064        this(factoryName, userWorkspace, folderName, syncRootParentFactoryName, false);
065    }
066
067    public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName,
068            String syncRootParentFactoryName, boolean relaxSyncRootConstraint) {
069        super(factoryName, null, userWorkspace, relaxSyncRootConstraint);
070        name = folderName;
071        canRename = false;
072        canDelete = false;
073        this.userWorkspace = userWorkspace;
074        this.syncRootParentFactoryName = syncRootParentFactoryName;
075    }
076
077    protected UserWorkspaceTopLevelFolderItem() {
078        // Needed for JSON deserialization
079    }
080
081    /*--------------------- AbstractFileSystemItem ---------------------*/
082    @Override
083    public void rename(String name) {
084        throw new UnsupportedOperationException("Cannot rename the top level folder item.");
085    }
086
087    @Override
088    public void delete() {
089        throw new UnsupportedOperationException("Cannot delete the top level folder item.");
090    }
091
092    @Override
093    public FileSystemItem move(FolderItem dest) {
094        throw new UnsupportedOperationException("Cannot move the top level folder item.");
095    }
096
097    /*--------------------- FolderItem -----------------*/
098    @Override
099    public List<FileSystemItem> getChildren() {
100
101        // Register user workspace as a synchronization root if it is not
102        // already the case
103        if (!getNuxeoDriveManager().isSynchronizationRoot(principal, userWorkspace)) {
104            try (CoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
105                getNuxeoDriveManager().registerSynchronizationRoot(principal, userWorkspace, session);
106            }
107        }
108
109        List<FileSystemItem> children = new ArrayList<FileSystemItem>();
110
111        // Add user workspace children
112        children.addAll(super.getChildren());
113
114        // Add synchronization root parent folder
115        if (syncRootParentFactoryName == null) {
116            if (log.isDebugEnabled()) {
117                log.debug(String.format(
118                        "No synchronization root parent factory name parameter for factory %s, the synchronization roots won't be synchronized client side.",
119                        factoryName));
120            }
121
122        } else {
123            VirtualFolderItemFactory syncRootParentFactory = getFileSystemItemAdapterService().getVirtualFolderItemFactory(
124                    syncRootParentFactoryName);
125            FolderItem syncRootParent = syncRootParentFactory.getVirtualFolderItem(principal);
126            if (syncRootParent != null) {
127                children.add(syncRootParent);
128            }
129        }
130
131        return children;
132    }
133
134    protected NuxeoDriveManager getNuxeoDriveManager() {
135        return Framework.getLocalService(NuxeoDriveManager.class);
136    }
137}