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.factory; 020 021import java.security.Principal; 022import java.util.Map; 023 024import org.apache.commons.lang.StringUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.drive.adapter.FileSystemItem; 028import org.nuxeo.drive.adapter.FolderItem; 029import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceHelper; 030import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceTopLevelFolderItem; 031import org.nuxeo.drive.service.TopLevelFolderItemFactory; 032import org.nuxeo.drive.service.impl.AbstractFileSystemItemFactory; 033import org.nuxeo.ecm.core.api.CoreInstance; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.NuxeoException; 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 Log log = LogFactory.getLog(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(String.format( 069 "Factory %s has no %s parameter, you can provide one in the factory contribution to avoid using the default value '%s'.", 070 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(String.format( 078 "Factory %s has no %s parameter, please provide one in the factory contribution to set the name of the synchronization root parent factory.", 079 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 if (log.isTraceEnabled()) { 089 log.trace(String.format( 090 "Document %s is not a user workspace, it cannot be adapted as a FileSystemItem.", doc.getId())); 091 } 092 return false; 093 } 094 return true; 095 } 096 097 @Override 098 protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem, 099 boolean relaxSyncRootConstraint, boolean getLockInfo) { 100 return new UserWorkspaceTopLevelFolderItem(getName(), doc, folderName, syncRootParentFactoryName, 101 relaxSyncRootConstraint, getLockInfo); 102 } 103 104 /*---------------------- VirtualFolderItemFactory ---------------*/ 105 @Override 106 public FolderItem getVirtualFolderItem(Principal principal) { 107 return getTopLevelFolderItem(principal); 108 } 109 110 @Override 111 public String getFolderName() { 112 return folderName; 113 } 114 115 @Override 116 public void setFolderName(String folderName) { 117 this.folderName = folderName; 118 } 119 120 /*----------------------- TopLevelFolderItemFactory ---------------------*/ 121 @Override 122 public FolderItem getTopLevelFolderItem(Principal principal) { 123 RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class); 124 // TODO: handle multiple repositories 125 try (CoreSession session = CoreInstance.openCoreSession(repositoryManager.getDefaultRepositoryName(), principal)) { 126 UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class); 127 DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session, null); 128 if (userWorkspace == null) { 129 throw new NuxeoException(String.format("No personal workspace found for user %s.", principal.getName())); 130 } 131 return (FolderItem) getFileSystemItem(userWorkspace); 132 } 133 } 134 135}