001/* 002 * (C) Copyright 2012 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.service.impl; 020 021import java.security.Principal; 022import java.util.Map; 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.service.FileSystemItemFactory; 029import org.nuxeo.drive.service.NuxeoDriveManager; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * Base {@link FileSystemItemFactory} for a synchronization root {@link FolderItem}. 035 * 036 * @author Antoine Taillefer 037 */ 038public abstract class AbstractSyncRootFolderItemFactory extends AbstractFileSystemItemFactory { 039 040 private static final Log log = LogFactory.getLog(AbstractSyncRootFolderItemFactory.class); 041 042 /** 043 * Returns the parent {@link FileSystemItem}. 044 */ 045 protected abstract FolderItem getParentItem(DocumentModel doc); 046 047 /** 048 * No parameters by default. 049 */ 050 @Override 051 public void handleParameters(Map<String, String> parameters) { 052 // Nothing to do as no parameters are contributed to the factory 053 if (!parameters.isEmpty()) { 054 throw new IllegalArgumentException( 055 "Parameter map is not empty whereas no parameters are contributed to the factory."); 056 } 057 if (log.isDebugEnabled()) { 058 log.debug(String.format("Factory %s has no parameters to handle.", getName())); 059 } 060 } 061 062 /** 063 * The factory considers that a {@link DocumentModel} is adaptable as a {@link FileSystemItem} if: 064 * <ul> 065 * <li>It is Folderish</li> 066 * <li>AND it is not a version nor a proxy</li> 067 * <li>AND it is not HiddenInNavigation</li> 068 * <li>AND it is not in the trash, unless {@code includeDeleted} is true</li> 069 * <li>AND it is a synchronization root registered for the current user, unless {@code relaxSyncRootConstraint} is 070 * true</li> 071 * </ul> 072 */ 073 @Override 074 public boolean isFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) { 075 076 // Check Folderish 077 if (!doc.isFolder()) { 078 if (log.isDebugEnabled()) { 079 log.debug(String.format("Document %s is not Folderish, it cannot be adapted as a FileSystemItem.", 080 doc.getId())); 081 } 082 return false; 083 } 084 // Check version 085 if (doc.isVersion()) { 086 if (log.isDebugEnabled()) { 087 log.debug(String.format("Document %s is a version, it cannot be adapted as a FileSystemItem.", 088 doc.getId())); 089 } 090 return false; 091 } 092 // Check proxy 093 if (doc.isProxy()) { 094 if (log.isDebugEnabled()) { 095 log.debug(String.format("Document %s is a proxy, it cannot be adapted as a FileSystemItem.", 096 doc.getId())); 097 } 098 return false; 099 } 100 // Check HiddenInNavigation 101 if (doc.hasFacet("HiddenInNavigation")) { 102 if (log.isDebugEnabled()) { 103 log.debug(String.format("Document %s is HiddenInNavigation, it cannot be adapted as a FileSystemItem.", 104 doc.getId())); 105 } 106 return false; 107 } 108 // Check if document is in the trash 109 if (!includeDeleted && doc.isTrashed()) { 110 if (log.isDebugEnabled()) { 111 log.debug(String.format("Document %s is in the trash, it cannot be adapted as a FileSystemItem.", 112 doc.getId())); 113 } 114 return false; 115 } 116 if (!relaxSyncRootConstraint) { 117 // Check synchronization root registered for the current user 118 NuxeoDriveManager nuxeoDriveManager = Framework.getService(NuxeoDriveManager.class); 119 Principal principal = doc.getCoreSession().getPrincipal(); 120 boolean isSyncRoot = nuxeoDriveManager.isSynchronizationRoot(principal, doc); 121 if (!isSyncRoot) { 122 if (log.isDebugEnabled()) { 123 log.debug(String.format( 124 "Document %s is not a registered synchronization root for user %s, it cannot be adapted as a FileSystemItem.", 125 doc.getId(), principal.getName())); 126 } 127 return false; 128 } 129 } 130 return true; 131 } 132 133 /** 134 * Force parent using {@link #getParentItem(DocumentModel)}. 135 */ 136 @Override 137 public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted) { 138 return getFileSystemItem(doc, getParentItem(doc), includeDeleted); 139 } 140 141 /** 142 * Force parent using {@link #getParentItem(DocumentModel)}. 143 */ 144 @Override 145 public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint) { 146 return getFileSystemItem(doc, getParentItem(doc), includeDeleted, relaxSyncRootConstraint); 147 } 148 149 /** 150 * Force parent using {@link #getParentItem(DocumentModel)}. 151 */ 152 @Override 153 public FileSystemItem getFileSystemItem(DocumentModel doc, boolean includeDeleted, boolean relaxSyncRootConstraint, 154 boolean getLockInfo) { 155 return getFileSystemItem(doc, getParentItem(doc), includeDeleted, relaxSyncRootConstraint, getLockInfo); 156 } 157 158}