Search in sources :

Example 1 with CollectionManager

use of org.nuxeo.ecm.collections.api.CollectionManager in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveManagerImpl method addToLocallyEditedCollection.

@Override
public void addToLocallyEditedCollection(CoreSession session, DocumentModel doc) {
    // Add document to "Locally Edited" collection, creating if if not
    // exists
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel userCollections = cm.getUserDefaultCollections(doc, session);
    DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), LOCALLY_EDITED_COLLECTION_NAME);
    DocumentModel locallyEditedCollection = null;
    if (session.exists(locallyEditedCollectionRef)) {
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
        cm.addToCollection(locallyEditedCollection, doc, session);
    } else {
        cm.addToNewCollection(LOCALLY_EDITED_COLLECTION_NAME, "Documents locally edited with Nuxeo Drive", doc, session);
        locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
    }
    // Register "Locally Edited" collection as a synchronization root if not
    // already the case
    Set<IdRef> syncRootRefs = getSynchronizationRootReferences(session);
    if (!syncRootRefs.contains(new IdRef(locallyEditedCollection.getId()))) {
        registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    }
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) PathRef(org.nuxeo.ecm.core.api.PathRef) IdRef(org.nuxeo.ecm.core.api.IdRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 2 with CollectionManager

use of org.nuxeo.ecm.collections.api.CollectionManager in project nuxeo-drive-server by nuxeo.

the class TestNuxeoDriveManager method testAddToLocallyEditedCollection.

@Test
public void testAddToLocallyEditedCollection() {
    // Create a test document and add it to the "Locally Edited" collection
    DocumentModel doc1 = session.createDocument(session.createDocumentModel(workspace_1.getPathAsString(), "driveEditFile1", "File"));
    nuxeoDriveManager.addToLocallyEditedCollection(session, doc1);
    // Check that the "Locally Edited" collection has been created, the test
    // document is member of it and the collection is registered as a
    // synchronization root
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel userCollections = cm.getUserDefaultCollections(doc1, session);
    DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME);
    assertTrue(session.exists(locallyEditedCollectionRef));
    DocumentModel locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
    assertTrue(cm.isCollection(locallyEditedCollection));
    // Re-fetch document from session to refresh it
    doc1 = session.getDocument(doc1.getRef());
    assertTrue(cm.isInCollection(locallyEditedCollection, doc1, session));
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
    // Add another document to the "Locally Edited" collection, check
    // collection membership
    DocumentModel doc2 = session.createDocument(session.createDocumentModel(workspace_1.getPathAsString(), "driveEditFile2", "File"));
    nuxeoDriveManager.addToLocallyEditedCollection(session, doc2);
    doc2 = session.getDocument(doc2.getRef());
    assertTrue(cm.isInCollection(locallyEditedCollection, doc2, session));
    // Unregister the "Locally Edited" collection and add another document
    // to it, check collection membership and the collection is registered
    // as a synchronization root once again
    nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    DocumentModel doc3 = session.createDocument(session.createDocumentModel(workspace_1.getPathAsString(), "driveEditFile3", "File"));
    nuxeoDriveManager.addToLocallyEditedCollection(session, doc3);
    doc3 = session.getDocument(doc3.getRef());
    assertTrue(cm.isInCollection(locallyEditedCollection, doc3, session));
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
}
Also used : DocumentRef(org.nuxeo.ecm.core.api.DocumentRef) CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) PathRef(org.nuxeo.ecm.core.api.PathRef) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 3 with CollectionManager

use of org.nuxeo.ecm.collections.api.CollectionManager in project nuxeo-drive-server by nuxeo.

the class TestNuxeoDriveManager method testChildRootRegistration.

@Test
public void testChildRootRegistration() {
    log.trace("Register a folder as a sync root");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder_1_1, session);
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), folder_1_1));
    log.trace("Create 'Locally Edited' collection and register it as a sync root");
    CollectionManager cm = Framework.getService(CollectionManager.class);
    DocumentModel locallyEditedCollection = cm.createCollection(session, NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME, "Locally Edited collection", workspace_1.getPathAsString());
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
    log.trace("Register a parent as a sync root, should unregister children sync roots, except for 'Locally Edited'");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), workspace_1, session);
    assertFalse(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), folder_1_1));
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
    log.trace("Register child folder as a sync root, should have no effect");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), folder_1_1, session);
    assertFalse(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), folder_1_1));
    log.trace("Unregister 'Locally Edited' collection");
    nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    assertFalse(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
    log.trace("Register 'Locally Edited' collection as a sync root, should be registered");
    nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
    assertTrue(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), locallyEditedCollection));
}
Also used : CollectionManager(org.nuxeo.ecm.collections.api.CollectionManager) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel) Test(org.junit.Test)

Example 4 with CollectionManager

use of org.nuxeo.ecm.collections.api.CollectionManager 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

CollectionManager (org.nuxeo.ecm.collections.api.CollectionManager)4 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)4 Test (org.junit.Test)2 DocumentRef (org.nuxeo.ecm.core.api.DocumentRef)2 PathRef (org.nuxeo.ecm.core.api.PathRef)2 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)1 RootlessItemException (org.nuxeo.drive.adapter.RootlessItemException)1 NuxeoDriveManager (org.nuxeo.drive.service.NuxeoDriveManager)1 IdRef (org.nuxeo.ecm.core.api.IdRef)1