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