Search in sources :

Example 11 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession 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 12 with CloseableCoreSession

use of org.nuxeo.ecm.core.api.CloseableCoreSession 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 13 with CloseableCoreSession

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

the class AbstractDocumentBackedFileSystemItem method canMove.

@Override
public boolean canMove(FolderItem dest) {
    // Check source doc deletion
    if (!canDelete) {
        return false;
    }
    // Check add children on destination doc
    AbstractDocumentBackedFileSystemItem docBackedDest = (AbstractDocumentBackedFileSystemItem) dest;
    String destRepoName = docBackedDest.getRepositoryName();
    DocumentRef destDocRef = new IdRef(docBackedDest.getDocId());
    String sessionRepo = repositoryName;
    // session bound to the destination repository
    if (!repositoryName.equals(destRepoName)) {
        sessionRepo = destRepoName;
    }
    try (CloseableCoreSession session = CoreInstance.openCoreSession(sessionRepo, principal)) {
        if (!session.hasPermission(destDocRef, SecurityConstants.ADD_CHILDREN)) {
            return false;
        }
        return true;
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) IdRef(org.nuxeo.ecm.core.api.IdRef)

Example 14 with CloseableCoreSession

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

the class AbstractDocumentBackedFileSystemItem method delete.

/*--------------------- FileSystemItem ---------------------*/
@Override
public void delete() {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        DocumentModel doc = getDocument(session);
        FileSystemItemFactory parentFactory = getFileSystemItemAdapterService().getFileSystemItemFactoryForId(parentId);
        // Handle removal from a collection sync root
        if (CollectionSyncRootFolderItemFactory.FACTORY_NAME.equals(parentFactory.getName())) {
            String[] idFragments = parseFileSystemId(parentId);
            String parentRepositoryName = idFragments[1];
            String parentDocId = idFragments[2];
            if (!parentRepositoryName.equals(repositoryName)) {
                throw new UnsupportedOperationException(String.format("Found collection member: %s [repo=%s] in a different repository from the collection one: %s [repo=%s].", doc, repositoryName, parentDocId, parentRepositoryName));
            }
            DocumentModel collection = getDocumentById(parentDocId, session);
            Framework.getService(CollectionManager.class).removeFromCollection(collection, doc, session);
        } else {
            List<DocumentModel> docs = new ArrayList<DocumentModel>();
            docs.add(doc);
            getTrashService().trashDocuments(docs);
        }
    }
}
Also used : FileSystemItemFactory(org.nuxeo.drive.service.FileSystemItemFactory) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) ArrayList(java.util.ArrayList) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 15 with CloseableCoreSession

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

the class CollectionSyncRootFolderItem method getChildren.

@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<String, Serializable>();
        props.put(CORE_SESSION_PROPERTY, (Serializable) session);
        PageProvider<DocumentModel> childrenPageProvider = (PageProvider<DocumentModel>) pageProviderService.getPageProvider(CollectionConstants.COLLECTION_CONTENT_PAGE_PROVIDER, null, null, 0L, props, docId);
        List<DocumentModel> dmChildren = childrenPageProvider.getCurrentPage();
        List<FileSystemItem> children = new ArrayList<FileSystemItem>(dmChildren.size());
        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);
            }
        }
        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)

Aggregations

CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)35 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)25 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)12 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)9 FolderItem (org.nuxeo.drive.adapter.FolderItem)9 Blob (org.nuxeo.ecm.core.api.Blob)7 IdRef (org.nuxeo.ecm.core.api.IdRef)7 HashMap (java.util.HashMap)6 SynchronizationRoots (org.nuxeo.drive.service.SynchronizationRoots)6 StringBlob (org.nuxeo.ecm.core.api.impl.blob.StringBlob)6 FileItem (org.nuxeo.drive.adapter.FileItem)5 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)5 NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)5 RepositoryManager (org.nuxeo.ecm.core.api.repository.RepositoryManager)5 Serializable (java.io.Serializable)4 Deploy (org.nuxeo.runtime.test.runner.Deploy)4 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 PageProvider (org.nuxeo.ecm.platform.query.api.PageProvider)3