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 <[email protected]> 018 */ 019package org.nuxeo.drive.hierarchy.userworkspace.adapter; 020 021import java.util.ArrayList; 022import java.util.List; 023 024import org.apache.logging.log4j.LogManager; 025import org.apache.logging.log4j.Logger; 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.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 Logger log = LogManager.getLogger(UserWorkspaceTopLevelFolderItem.class); 058 059 protected DocumentModel userWorkspace; 060 061 protected String syncRootParentFactoryName; 062 063 public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName, 064 String syncRootParentFactoryName) { 065 this(factoryName, userWorkspace, folderName, syncRootParentFactoryName, false); 066 } 067 068 public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName, 069 String syncRootParentFactoryName, boolean relaxSyncRootConstraint) { 070 this(factoryName, userWorkspace, folderName, syncRootParentFactoryName, relaxSyncRootConstraint, true); 071 } 072 073 public UserWorkspaceTopLevelFolderItem(String factoryName, DocumentModel userWorkspace, String folderName, 074 String syncRootParentFactoryName, boolean relaxSyncRootConstraint, boolean getLockInfo) { 075 super(factoryName, null, userWorkspace, relaxSyncRootConstraint, getLockInfo); 076 name = folderName; 077 canRename = false; 078 canDelete = false; 079 canScrollDescendants = false; 080 this.userWorkspace = userWorkspace; 081 this.syncRootParentFactoryName = syncRootParentFactoryName; 082 // detach user workspace as we will use another session to update it 083 userWorkspace.detach(true); 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 (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) { 114 getNuxeoDriveManager().registerSynchronizationRoot(principal, userWorkspace, session); 115 } 116 } 117 118 List<FileSystemItem> children = new ArrayList<>(); 119 120 // Add user workspace children 121 children.addAll(super.getChildren()); 122 123 // Add synchronization root parent folder 124 if (syncRootParentFactoryName == null) { 125 log.debug( 126 "No synchronization root parent factory name parameter for factory {}, the synchronization roots won't be synchronized client side.", 127 factoryName); 128 } else { 129 VirtualFolderItemFactory syncRootParentFactory = getFileSystemItemAdapterService().getVirtualFolderItemFactory( 130 syncRootParentFactoryName); 131 FolderItem syncRootParent = syncRootParentFactory.getVirtualFolderItem(principal); 132 if (syncRootParent != null) { 133 children.add(syncRootParent); 134 } 135 } 136 137 return children; 138 } 139 140 @Override 141 public ScrollFileSystemItemList scrollDescendants(String scrollId, int batchSize, long keepAlive) { 142 throw new UnsupportedOperationException( 143 "Cannot scroll through the descendants of the user workspace top level folder item, please call getChildren() instead."); 144 } 145 146 // Override equals and hashCode to explicitly show that their implementation rely on the parent class and doesn't 147 // depend on the fields added to this class. 148 @Override 149 public boolean equals(Object obj) { 150 return super.equals(obj); 151 } 152 153 @Override 154 public int hashCode() { 155 return super.hashCode(); 156 } 157 158 protected NuxeoDriveManager getNuxeoDriveManager() { 159 return Framework.getService(NuxeoDriveManager.class); 160 } 161}