001/* 002 * (C) Copyright 2012 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.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.permission.factory; 018 019import java.security.Principal; 020import java.util.Map; 021 022import org.apache.commons.lang.StringUtils; 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.drive.adapter.FileSystemItem; 026import org.nuxeo.drive.adapter.FolderItem; 027import org.nuxeo.drive.hierarchy.permission.adapter.UserSyncRootParentFolderItem; 028import org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceHelper; 029import org.nuxeo.drive.service.FileSystemItemFactory; 030import org.nuxeo.drive.service.FileSystemItemManager; 031import org.nuxeo.drive.service.VirtualFolderItemFactory; 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.LifeCycleConstants; 037import org.nuxeo.ecm.core.api.NuxeoException; 038import org.nuxeo.ecm.core.api.repository.RepositoryManager; 039import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 040import org.nuxeo.runtime.api.Framework; 041 042/** 043 * User workspace based implementation of {@link FileSystemItemFactory} for the parent {@link FolderItem} of the user's 044 * synchronization roots. 045 * 046 * @author Antoine Taillefer 047 */ 048public class UserSyncRootParentFactory extends AbstractFileSystemItemFactory implements VirtualFolderItemFactory { 049 050 private static final Log log = LogFactory.getLog(UserSyncRootParentFactory.class); 051 052 protected static final String FOLDER_NAME_PARAM = "folderName"; 053 054 protected String folderName; 055 056 /*------------------- AbstractFileSystemItemFactory ------------------- */ 057 @Override 058 public void handleParameters(Map<String, String> parameters) { 059 // Look for the "folderName" parameter 060 String folderNameParam = parameters.get(FOLDER_NAME_PARAM); 061 if (StringUtils.isEmpty(folderNameParam)) { 062 throw new NuxeoException(String.format("Factory %s has no %s parameter, please provide one.", getName(), 063 FOLDER_NAME_PARAM)); 064 } 065 folderName = folderNameParam; 066 } 067 068 @Override 069 public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) { 070 // Check user workspace 071 boolean isUserWorkspace = UserWorkspaceHelper.isUserWorkspace(doc); 072 if (!isUserWorkspace) { 073 if (log.isTraceEnabled()) { 074 log.trace(String.format( 075 "Document %s is not a user workspace, it cannot be adapted as a FileSystemItem.", doc.getId())); 076 } 077 return false; 078 } 079 // Check "deleted" life cycle state 080 if (!includeDeleted && LifeCycleConstants.DELETED_STATE.equals(doc.getCurrentLifeCycleState())) { 081 if (log.isDebugEnabled()) { 082 log.debug(String.format( 083 "Document %s is in the '%s' life cycle state, it cannot be adapted as a FileSystemItem.", 084 doc.getId(), LifeCycleConstants.DELETED_STATE)); 085 } 086 return false; 087 } 088 return true; 089 } 090 091 @Override 092 protected FileSystemItem adaptDocument(DocumentModel doc, boolean forceParentItem, FolderItem parentItem, 093 boolean relaxSyncRootConstraint) { 094 return new UserSyncRootParentFolderItem(getName(), doc, parentItem, folderName, relaxSyncRootConstraint); 095 } 096 097 /*------------------- FileSystemItemFactory ------------------- */ 098 /** 099 * Force parent item using {@link #getTopLevelFolderItem(Principal)}. 100 */ 101 @Override 102 public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) { 103 Principal principal = doc.getCoreSession().getPrincipal(); 104 return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted); 105 } 106 107 @Override 108 public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) { 109 Principal principal = doc.getCoreSession().getPrincipal(); 110 return getFileSystemItem(doc, getTopLevelFolderItem(principal), includeDeleted, relaxSyncRootConstraint); 111 } 112 113 /*------------------- VirtualFolderItemFactory ------------------- */ 114 @Override 115 public FolderItem getVirtualFolderItem(Principal principal) { 116 RepositoryManager repositoryManager = Framework.getLocalService(RepositoryManager.class); 117 // TODO: handle multiple repositories 118 try (CoreSession session = CoreInstance.openCoreSession(repositoryManager.getDefaultRepositoryName(), principal)) { 119 UserWorkspaceService userWorkspaceService = Framework.getLocalService(UserWorkspaceService.class); 120 DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session, null); 121 if (userWorkspace == null) { 122 throw new NuxeoException(String.format("No personal workspace found for user %s.", principal.getName())); 123 } 124 return (FolderItem) getFileSystemItem(userWorkspace); 125 } 126 } 127 128 @Override 129 public String getFolderName() { 130 return folderName; 131 } 132 133 @Override 134 public void setFolderName(String folderName) { 135 this.folderName = folderName; 136 } 137 138 /*------------------- Protected ------------------- */ 139 protected FolderItem getTopLevelFolderItem(Principal principal) { 140 FolderItem topLevelFolder = Framework.getLocalService(FileSystemItemManager.class).getTopLevelFolder(principal); 141 if (topLevelFolder == null) { 142 throw new NuxeoException("Found no top level folder item. Please check your " 143 + "contribution to the following extension point:" 144 + " <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\"" 145 + " point=\"topLevelFolderItemFactory\">."); 146 } 147 return topLevelFolder; 148 } 149 150}