Search in sources :

Example 21 with CloseableCoreSession

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

the class AbstractDocumentBackedFileSystemItem method move.

@Override
public FileSystemItem move(FolderItem dest) {
    DocumentRef sourceDocRef = new IdRef(docId);
    AbstractDocumentBackedFileSystemItem docBackedDest = (AbstractDocumentBackedFileSystemItem) dest;
    String destRepoName = docBackedDest.getRepositoryName();
    DocumentRef destDocRef = new IdRef(docBackedDest.getDocId());
    // create doc in destination
    if (repositoryName.equals(destRepoName)) {
        try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
            DocumentModel movedDoc = session.move(sourceDocRef, destDocRef, null);
            session.save();
            return getFileSystemItemAdapterService().getFileSystemItem(movedDoc, dest);
        }
    } else {
        // TODO: implement move to another repository
        throw new UnsupportedOperationException("Multi repository move is not supported yet.");
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 22 with CloseableCoreSession

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

the class DefaultSyncRootFolderItem method delete.

@Override
public void delete() {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        DocumentModel doc = getDocument(session);
        Framework.getService(NuxeoDriveManager.class).unregisterSynchronizationRoot(principal, doc, session);
    }
}
Also used : CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) NuxeoDriveManager(org.nuxeo.drive.service.NuxeoDriveManager)

Example 23 with CloseableCoreSession

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

the class DocumentBackedFileItem method rename.

/*--------------------- FileSystemItem ---------------------*/
@Override
public void rename(String name) {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        /* Update doc properties */
        DocumentModel doc = getDocument(session);
        BlobHolder bh = getBlobHolder(doc);
        Blob blob = getBlob(bh);
        blob.setFilename(name);
        bh.setBlob(blob);
        updateDocTitleIfNeeded(doc, name);
        doc.putContextData(CoreSession.SOURCE, "drive");
        doc = session.saveDocument(doc);
        session.save();
        /* Update FileSystemItem attributes */
        this.name = name;
        updateDownloadURL();
        updateLastModificationDate(doc);
    }
}
Also used : Blob(org.nuxeo.ecm.core.api.Blob) BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 24 with CloseableCoreSession

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

the class DocumentBackedFileItem method setBlob.

@Override
public void setBlob(Blob blob) {
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        /* Update doc properties */
        DocumentModel doc = getDocument(session);
        // If blob's filename is empty, set it to the current name
        String blobFileName = blob.getFilename();
        if (StringUtils.isEmpty(blobFileName)) {
            blob.setFilename(name);
        } else {
            updateDocTitleIfNeeded(doc, blobFileName);
            name = blobFileName;
            updateDownloadURL();
        }
        BlobHolder bh = getBlobHolder(doc);
        bh.setBlob(blob);
        doc.putContextData(CoreSession.SOURCE, "drive");
        doc = session.saveDocument(doc);
        session.save();
        /* Update FileSystemItem attributes */
        updateLastModificationDate(doc);
        updateDigest(getBlob(doc));
    }
}
Also used : BlobHolder(org.nuxeo.ecm.core.api.blobholder.BlobHolder) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 25 with CloseableCoreSession

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

the class AbstractFileSystemItemFactory method getFileSystemItemById.

@Override
public FileSystemItem getFileSystemItemById(String id, String parentId, Principal principal) {
    String[] idFragments = parseFileSystemId(id);
    String repositoryName = idFragments[1];
    String docId = idFragments[2];
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
        FileSystemItem parentItem = Framework.getService(FileSystemItemAdapterService.class).getFileSystemItemFactoryForId(parentId).getFileSystemItemById(parentId, principal);
        if (!(parentItem instanceof FolderItem)) {
            throw new NuxeoException(String.format("FileSystemItem with id %s should be a FolderItem", parentId));
        }
        DocumentModel doc = getDocumentById(docId, session);
        return getFileSystemItem(doc, (FolderItem) parentItem);
    } catch (DocumentNotFoundException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("No doc related to id %s, returning null.", docId));
        }
        return null;
    } catch (DocumentSecurityException e) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("User %s cannot access doc %s, returning null.", principal.getName(), docId));
        }
        return null;
    }
}
Also used : AbstractFileSystemItem(org.nuxeo.drive.adapter.impl.AbstractFileSystemItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

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