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);
}
}
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));
}
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));
}
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;
}
}
Aggregations