Search in sources :

Example 56 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DocumentBackedFolderItem method getChildren.

/*--------------------- FolderItem -----------------*/
@Override
@SuppressWarnings("unchecked")
public List<FileSystemItem> getChildren() {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
        Map<String, Serializable> props = new HashMap<>();
        props.put(CORE_SESSION_PROPERTY, (Serializable) session);
        PageProvider<DocumentModel> childrenPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(FOLDER_ITEM_CHILDREN_PAGE_PROVIDER, null, null, 0L, props, docId);
        long pageSize = childrenPageProvider.getPageSize();
        List<FileSystemItem> children = new ArrayList<>();
        int nbChildren = 0;
        boolean reachedPageSize = false;
        boolean hasNextPage = true;
        // FileSystemItems
        while (nbChildren < pageSize && hasNextPage) {
            List<DocumentModel> dmChildren = childrenPageProvider.getCurrentPage();
            for (DocumentModel dmChild : dmChildren) {
                // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(dmChild, this, false, false, false);
                if (child != null) {
                    children.add(child);
                    nbChildren++;
                    if (nbChildren == pageSize) {
                        reachedPageSize = true;
                        break;
                    }
                }
            }
            if (!reachedPageSize) {
                hasNextPage = childrenPageProvider.isNextPageAvailable();
                if (hasNextPage) {
                    childrenPageProvider.nextPage();
                }
            }
        }
        return children;
    }
}
Also used : Serializable(java.io.Serializable) PageProviderService(org.nuxeo.ecm.platform.query.api.PageProviderService) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) PageProvider(org.nuxeo.ecm.platform.query.api.PageProvider)

Example 57 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DocumentBackedFolderItem method populateAncestorCache.

protected FolderItem populateAncestorCache(Map<DocumentRef, FolderItem> cache, DocumentModel doc, CoreSession session, boolean cacheItem) {
    DocumentRef parentDocRef = session.getParentDocumentRef(doc.getRef());
    if (parentDocRef == null) {
        throw new RootlessItemException("Reached repository root");
    }
    FolderItem parentItem = cache.get(parentDocRef);
    if (parentItem != null) {
        if (log.isTraceEnabled()) {
            log.trace(String.format("Found parent FolderItem in cache for doc %s: %s", doc.getPathAsString(), parentItem.getPath()));
        }
        return getFolderItem(cache, doc, parentItem, cacheItem);
    }
    if (log.isTraceEnabled()) {
        log.trace(String.format("No parent FolderItem found in cache for doc %s, computing ancestor cache", doc.getPathAsString()));
    }
    DocumentModel parentDoc;
    try {
        parentDoc = session.getDocument(parentDocRef);
    } catch (DocumentSecurityException e) {
        throw new RootlessItemException(String.format("User %s has no READ access on parent of document %s (%s).", principal.getName(), doc.getPathAsString(), doc.getId()), e);
    }
    parentItem = populateAncestorCache(cache, parentDoc, session, true);
    if (parentItem == null) {
        return null;
    }
    return getFolderItem(cache, doc, parentItem, cacheItem);
}
Also used : FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 58 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class DocumentBackedFolderItem method rename.

/*--------------------- FileSystemItem ---------------------*/
@Override
public void rename(String name) {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        // Update doc properties
        DocumentModel doc = getDocument(session);
        doc.setPropertyValue("dc:title", name);
        doc.putContextData(CoreSession.SOURCE, "drive");
        doc = session.saveDocument(doc);
        session.save();
        // Update FileSystemItem attributes
        this.docTitle = name;
        this.name = name;
        updateLastModificationDate(doc);
    }
}
Also used : CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 59 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class SharedSyncRootParentFolderItem method getChildren.

@Override
public List<FileSystemItem> getChildren() {
    List<FileSystemItem> children = new ArrayList<FileSystemItem>();
    Map<String, SynchronizationRoots> syncRootsByRepo = Framework.getService(NuxeoDriveManager.class).getSynchronizationRoots(principal);
    for (String repositoryName : syncRootsByRepo.keySet()) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            Set<IdRef> syncRootRefs = syncRootsByRepo.get(repositoryName).getRefs();
            Iterator<IdRef> syncRootRefsIt = syncRootRefs.iterator();
            while (syncRootRefsIt.hasNext()) {
                IdRef idRef = syncRootRefsIt.next();
                // See https://jira.nuxeo.com/browse/NXP-11146
                if (!session.hasPermission(idRef, SecurityConstants.READ)) {
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("User %s has no READ access on synchronization root %s, not including it in children.", session.getPrincipal().getName(), idRef));
                    }
                    continue;
                }
                DocumentModel doc = session.getDocument(idRef);
                // principal)
                if (!session.getPrincipal().getName().equals(doc.getPropertyValue("dc:creator"))) {
                    // NXP-19442: Avoid useless and costly call to DocumentModel#getLockInfo
                    FileSystemItem child = getFileSystemItemAdapterService().getFileSystemItem(doc, this, false, false, false);
                    if (child == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("Synchronization root %s cannot be adapted as a FileSystemItem, maybe because user %s doesn't have the required permission on it (default required permission is ReadWrite). Not including it in children.", idRef, session.getPrincipal().getName()));
                        }
                        continue;
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(String.format("Including synchronization root %s in children.", idRef));
                    }
                    children.add(child);
                }
            }
        }
    }
    Collections.sort(children);
    return children;
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) SynchronizationRoots(org.nuxeo.drive.service.SynchronizationRoots) IdRef(org.nuxeo.ecm.core.api.IdRef) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 60 with DocumentModel

use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.

the class AbstractDocumentBackedFileSystemItem method handleCollectionMember.

protected boolean handleCollectionMember(DocumentModel doc, CoreSession session, boolean relaxSyncRootConstraint, boolean getLockInfo) {
    if (!doc.hasSchema(CollectionConstants.COLLECTION_MEMBER_SCHEMA_NAME)) {
        return false;
    }
    CollectionManager cm = Framework.getService(CollectionManager.class);
    List<DocumentModel> docCollections = cm.getVisibleCollection(doc, session);
    if (docCollections.isEmpty()) {
        if (log.isTraceEnabled()) {
            log.trace(String.format("Doc %s (%s) is not member of any collection", doc.getPathAsString(), doc.getId()));
        }
        return false;
    } else {
        FileSystemItem parent = null;
        DocumentModel collection = null;
        Iterator<DocumentModel> it = docCollections.iterator();
        while (it.hasNext() && parent == null) {
            collection = it.next();
            // as a FileSystemItem and this collection is not a synchronization root for the current user
            if (collection.getPathAsString().startsWith(doc.getPathAsString() + "/") && !Framework.getService(NuxeoDriveManager.class).isSynchronizationRoot(session.getPrincipal(), collection)) {
                continue;
            }
            try {
                parent = getFileSystemItemAdapterService().getFileSystemItem(collection, true, relaxSyncRootConstraint, getLockInfo);
            } catch (RootlessItemException e) {
                if (log.isTraceEnabled()) {
                    log.trace(String.format("The collection %s (%s) of which doc %s (%s) is a member cannot be adapted as a FileSystemItem.", collection.getPathAsString(), collection.getId(), doc.getPathAsString(), doc.getId()));
                }
                continue;
            }
        }
        if (parent == null) {
            if (log.isTraceEnabled()) {
                log.trace(String.format("None of the collections of which doc %s (%s) is a member can be adapted as a FileSystemItem.", doc.getPathAsString(), doc.getId()));
            }
            return false;
        }
        if (log.isTraceEnabled()) {
            log.trace(String.format("Using first collection %s (%s) of which doc %s (%s) is a member and that is adaptable as a FileSystemItem as a parent FileSystemItem.", collection.getPathAsString(), collection.getId(), doc.getPathAsString(), doc.getId()));
        }
        parentId = parent.getId();
        path = parent.getPath() + '/' + id;
        return true;
    }
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) RootlessItemException(org.nuxeo.drive.adapter.RootlessItemException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager)

Aggregations

DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)128 Test (org.junit.Test)58 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)26 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)25 FileSystemItemChange (org.nuxeo.drive.service.FileSystemItemChange)24 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)22 PathRef (org.nuxeo.ecm.core.api.PathRef)21 IdRef (org.nuxeo.ecm.core.api.IdRef)18 HashSet (java.util.HashSet)17 Blob (org.nuxeo.ecm.core.api.Blob)17 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)17 ArrayList (java.util.ArrayList)16 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)11 Principal (java.security.Principal)10 ACE (org.nuxeo.ecm.core.api.security.ACE)10 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)9 FileItem (org.nuxeo.drive.adapter.FileItem)8 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)8 HashMap (java.util.HashMap)7