001/* 002 * (C) Copyright 2013-2018 Nuxeo (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.factory; 020 021import java.util.Map; 022 023import org.apache.commons.lang3.StringUtils; 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.hierarchy.userworkspace.adapter.UserWorkspaceHelper; 029import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceTopLevelFolderItem; 030import org.nuxeo.drive.service.TopLevelFolderItemFactory; 031import org.nuxeo.drive.service.impl.AbstractFileSystemItemFactory; 032import org.nuxeo.ecm.core.api.CoreInstance; 033import org.nuxeo.ecm.core.api.CoreSession; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.core.api.NuxeoException; 036import org.nuxeo.ecm.core.api.NuxeoPrincipal; 037import org.nuxeo.ecm.core.api.repository.RepositoryManager; 038import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * User workspace based implementation of the {@link TopLevelFolderItemFactory}. 043 * 044 * @author Antoine Taillefer 045 */ 046public class UserWorkspaceTopLevelFactory extends AbstractFileSystemItemFactory implements TopLevelFolderItemFactory { 047 048 private static final Logger log = LogManager.getLogger(UserWorkspaceTopLevelFactory.class); 049 050 protected static final String FOLDER_NAME_PARAM = "folderName"; 051 052 protected static final String SYNC_ROOT_PARENT_FACTORY_PARAM = "syncRootParentFactory"; 053 054 protected static final String DEFAULT_FOLDER_NAME = "Nuxeo Drive"; 055 056 protected String folderName = DEFAULT_FOLDER_NAME; 057 058 protected String syncRootParentFactoryName; 059 060 /*---------------------- AbstractFileSystemItemFactory ---------------*/ 061 @Override 062 public void handleParameters(Map<String, String> parameters) { 063 // Look for the "folderName" parameter 064 String folderNameParam = parameters.get(FOLDER_NAME_PARAM); 065 if (!StringUtils.isEmpty(folderNameParam)) { 066 folderName = folderNameParam; 067 } else { 068 log.info( 069 "Factory {} has no {} parameter, you can provide one in the factory contribution to avoid using the default value '{}'.", 070 this::getName, () -> FOLDER_NAME_PARAM, () -> DEFAULT_FOLDER_NAME); 071 } 072 // Look for the "syncRootParentFactory" parameter 073 String syncRootParentFactoryParam = parameters.get(SYNC_ROOT_PARENT_FACTORY_PARAM); 074 if (!StringUtils.isEmpty(syncRootParentFactoryParam)) { 075 syncRootParentFactoryName = syncRootParentFactoryParam; 076 } else { 077 log.warn( 078 "Factory {} has no {} parameter, please provide one in the factory contribution to set the name of the synchronization root parent factory.", 079 this::getName, () -> SYNC_ROOT_PARENT_FACTORY_PARAM); 080 } 081 } 082 083 @Override 084 public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) { 085 // Check user workspace 086 boolean isUserWorkspace = UserWorkspaceHelper.isUserWorkspace(doc); 087 if (!isUserWorkspace) { 088 log.trace("Document {} is not a user workspace, it cannot be adapted as a FileSystemItem.", doc::getId); 089 return false; 090 } 091 return true; 092 } 093 094 @Override 095 protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem, 096 boolean relaxSyncRootConstraint, boolean getLockInfo) { 097 return new UserWorkspaceTopLevelFolderItem(getName(), doc, folderName, syncRootParentFactoryName, 098 relaxSyncRootConstraint, getLockInfo); 099 } 100 101 /*---------------------- VirtualFolderItemFactory ---------------*/ 102 @Override 103 public FolderItem getVirtualFolderItem(NuxeoPrincipal principal) { 104 return getTopLevelFolderItem(principal); 105 } 106 107 @Override 108 public String getFolderName() { 109 return folderName; 110 } 111 112 @Override 113 public void setFolderName(String folderName) { 114 this.folderName = folderName; 115 } 116 117 /*----------------------- TopLevelFolderItemFactory ---------------------*/ 118 @Override 119 public FolderItem getTopLevelFolderItem(NuxeoPrincipal principal) { 120 RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class); 121 // TODO: handle multiple repositories 122 CoreSession session = CoreInstance.getCoreSession(repositoryManager.getDefaultRepositoryName(), principal); 123 UserWorkspaceService userWorkspaceService = Framework.getService(UserWorkspaceService.class); 124 DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session); 125 if (userWorkspace == null) { 126 throw new NuxeoException( 127 String.format("No personal workspace found for user %s.", principal.getName())); 128 } 129 return (FolderItem) getFileSystemItem(userWorkspace); 130 } 131 132}