use of org.nuxeo.ecm.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveSyncRootVersioningListener method handleEvent.
@Override
public void handleEvent(Event event) {
EventContext context = event.getContext();
DocumentRef checkedInVersionRef = (DocumentRef) context.getProperty("checkedInVersionRef");
if (checkedInVersionRef == null) {
return;
}
CoreSession session = context.getCoreSession();
DocumentModel doc = session.getDocument(checkedInVersionRef);
if (!(doc.isVersion() && doc.hasFacet(NuxeoDriveManagerImpl.NUXEO_DRIVE_FACET))) {
return;
}
doc.setPropertyValue(NuxeoDriveManagerImpl.DRIVE_SUBSCRIPTIONS_PROPERTY, null);
doc.putContextData(CoreSession.ALLOW_VERSION_WRITE, Boolean.TRUE);
doc.putContextData("source", "drive");
doc.putContextData(CoreSession.SOURCE, "drive");
session.saveDocument(doc);
}
use of org.nuxeo.ecm.core.api.DocumentRef 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.");
}
}
use of org.nuxeo.ecm.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class AuditChangeFinderTestSuite method testCollectionEvents.
@Test
public void testCollectionEvents() throws Exception {
DocumentModel doc1;
DocumentModel doc2;
DocumentModel doc3;
List<FileSystemItemChange> changes;
DocumentModel locallyEditedCollection;
try {
log.trace("Create 2 test docs and them to the 'Locally Edited' collection");
doc1 = session.createDocumentModel(folder1.getPathAsString(), "doc1", "File");
doc1.setPropertyValue("file:content", new StringBlob("File content."));
doc1 = session.createDocument(doc1);
doc2 = session.createDocumentModel(folder1.getPathAsString(), "doc2", "File");
doc2.setPropertyValue("file:content", new StringBlob("File content."));
doc2 = session.createDocument(doc2);
nuxeoDriveManager.addToLocallyEditedCollection(session, doc1);
nuxeoDriveManager.addToLocallyEditedCollection(session, doc2);
DocumentModel userCollections = collectionManager.getUserDefaultCollections(folder1, session);
DocumentRef locallyEditedCollectionRef = new PathRef(userCollections.getPath().toString(), NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME);
locallyEditedCollection = session.getDocument(locallyEditedCollectionRef);
// Re-fetch documents to get rid of the disabled events in context
// data
doc1 = session.getDocument(doc1.getRef());
doc2 = session.getDocument(doc2.getRef());
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 8 (among which 7 distinct) changes:
// - addedToCollection for doc2
// - documentModified for 'Locally Edited' collection (2 occurrences)
// - rootRegistered for 'Locally Edited' collection
// - addedToCollection for doc1
// - documentCreated for 'Locally Edited' collection
// - documentCreated for doc2
// - documentCreated for doc1
changes = getChanges(session.getPrincipal());
assertEquals(8, changes.size());
Set<SimpleFileSystemItemChange> expectedChanges = new HashSet<>();
expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "addedToCollection"));
expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"));
expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "addedToCollection"));
expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentCreated"));
expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "documentCreated"));
expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "documentCreated"));
assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
log.trace("Update doc1 member of the 'Locally Edited' collection");
doc1.setPropertyValue("file:content", new StringBlob("Updated file content."));
session.saveDocument(doc1);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 1 change: documentModified for doc1
changes = getChanges(session.getPrincipal());
assertEquals(1, changes.size());
assertEquals(new SimpleFileSystemItemChange(doc1.getId(), "documentModified"), toSimpleFileSystemItemChange(changes.get(0)));
log.trace("Remove doc1 from the 'Locally Edited' collection, delete doc2 and add doc 3 to the collection");
collectionManager.removeFromCollection(locallyEditedCollection, doc1, session);
doc2.followTransition(LifeCycleConstants.DELETE_TRANSITION);
doc3 = session.createDocumentModel(folder1.getPathAsString(), "doc3", "File");
doc3.setPropertyValue("file:content", new StringBlob("File content."));
doc3 = session.createDocument(doc3);
collectionManager.addToCollection(locallyEditedCollection, doc3, session);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 6 (among which 5 distinct) changes:
// - addedToCollection for doc3
// - documentModified for 'Locally Edited' collection (2 occurrences)
// - documentCreated for doc3
// - deleted for doc2
// - deleted for doc1
changes = getChanges(session.getPrincipal());
assertEquals(6, changes.size());
List<SimpleFileSystemItemChange> expectedChanges = new ArrayList<>();
expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "addedToCollection"));
expectedChanges.add(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "documentModified"));
expectedChanges.add(new SimpleFileSystemItemChange(doc3.getId(), "documentCreated"));
expectedChanges.add(new SimpleFileSystemItemChange(doc2.getId(), "deleted"));
expectedChanges.add(new SimpleFileSystemItemChange(doc1.getId(), "deleted"));
assertTrue(CollectionUtils.isEqualCollection(expectedChanges, toSimpleFileSystemItemChanges(changes)));
log.trace("Unregister the 'Locally Edited' collection as a sync root");
nuxeoDriveManager.unregisterSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 1 change: deleted for 'Locally Edited' collection
changes = getChanges(session.getPrincipal());
assertEquals(1, changes.size());
assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
log.trace("Register the 'Locally Edited' collection back as a sync root");
nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), locallyEditedCollection, session);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 1 change: rootRegistered for 'Locally Edited'
// collection
changes = getChanges(session.getPrincipal());
assertEquals(1, changes.size());
assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "rootRegistered"), toSimpleFileSystemItemChange(changes.get(0)));
log.trace("Delete the 'Locally Edited' collection");
locallyEditedCollection.followTransition(LifeCycleConstants.DELETE_TRANSITION);
} finally {
commitAndWaitForAsyncCompletion();
}
try {
// Expecting 1 change: deleted for 'Locally Edited' collection
changes = getChanges(session.getPrincipal());
assertEquals(1, changes.size());
assertEquals(new SimpleFileSystemItemChange(locallyEditedCollection.getId(), "deleted"), toSimpleFileSystemItemChange(changes.get(0)));
} finally {
commitAndWaitForAsyncCompletion();
}
}
use of org.nuxeo.ecm.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class DefaultFileSystemItemFactoryFixture method testGetFileSystemItem.
@Test
public void testGetFileSystemItem() throws Exception {
// ------------------------------------------------------
// Check downloadable FileSystemItems
// ------------------------------------------------------
// File
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(file));
FileSystemItem fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
assertNotNull(fsItem);
assertTrue(fsItem instanceof FileItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + file.getId(), fsItem.getId());
assertEquals(syncRootItemId, fsItem.getParentId());
assertEquals("Joe.odt", fsItem.getName());
assertFalse(fsItem.isFolder());
assertEquals("Administrator", fsItem.getCreator());
assertEquals("Administrator", fsItem.getLastContributor());
Blob fileItemBlob = ((FileItem) fsItem).getBlob();
assertEquals("Joe.odt", fileItemBlob.getFilename());
assertEquals("Content of Joe's file.", fileItemBlob.getString());
// Note
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(note));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
assertNotNull(fsItem);
assertTrue(fsItem instanceof FileItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + note.getId(), fsItem.getId());
assertEquals(syncRootItemId, fsItem.getParentId());
assertEquals("aNote.txt", fsItem.getName());
assertFalse(fsItem.isFolder());
assertEquals("Administrator", fsItem.getCreator());
assertEquals("Administrator", fsItem.getLastContributor());
fileItemBlob = ((FileItem) fsItem).getBlob();
assertEquals("aNote.txt", fileItemBlob.getFilename());
assertEquals("Content of Bob's note.", fileItemBlob.getString());
// Custom doc type with the "file" schema
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom);
assertNotNull(fsItem);
assertTrue(fsItem instanceof FileItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
assertEquals(syncRootItemId, fsItem.getParentId());
assertEquals("Bonnie's file.odt", fsItem.getName());
assertFalse(fsItem.isFolder());
assertEquals("Administrator", fsItem.getCreator());
assertEquals("Administrator", fsItem.getLastContributor());
fileItemBlob = ((FileItem) fsItem).getBlob();
assertEquals("Bonnie's file.odt", fileItemBlob.getFilename());
assertEquals("Content of Bonnie's file.", fileItemBlob.getString());
// File without a blob => not adaptable as a FileSystemItem
file.setPropertyValue("file:content", null);
file = session.saveDocument(file);
assertFalse(defaultFileSystemItemFactory.isFileSystemItem(file));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(file);
assertNull(fsItem);
// Deleted file => not adaptable as a FileSystemItem
custom.followTransition("delete");
assertFalse(defaultFileSystemItemFactory.isFileSystemItem(custom));
assertNull(defaultFileSystemItemFactory.getFileSystemItem(custom));
// Deleted file with explicit "includeDeleted" => adaptable as a
// FileSystemItem
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(custom, true));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(custom, true);
assertNotNull(fsItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + custom.getId(), fsItem.getId());
assertEquals("Bonnie's file.odt", fsItem.getName());
// Version
// Note is now automatically versioned at each save
assertEquals("0.1", note.getVersionLabel());
note.checkOut();
DocumentRef versionRef = session.checkIn(note.getRef(), VersioningOption.MINOR, null);
DocumentModel version = session.getDocument(versionRef);
assertFalse(defaultFileSystemItemFactory.isFileSystemItem(version));
// Proxy
DocumentModel proxy = session.createProxy(note.getRef(), folder.getRef());
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(proxy));
// HiddenInNavigation
note.addFacet("HiddenInNavigation");
assertFalse(defaultFileSystemItemFactory.isFileSystemItem(note));
note.removeFacet("HiddenInNavigation");
// ------------------------------------------------------
// Check folderish FileSystemItems
// ------------------------------------------------------
// Folder
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folder));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(folder);
assertNotNull(fsItem);
assertTrue(fsItem instanceof FolderItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folder.getId(), fsItem.getId());
assertEquals(syncRootItemId, fsItem.getParentId());
assertEquals("Jack's folder", fsItem.getName());
assertTrue(fsItem.isFolder());
assertEquals("Administrator", fsItem.getCreator());
assertEquals("Administrator", fsItem.getLastContributor());
FolderItem folderItem = (FolderItem) fsItem;
List<FileSystemItem> children = folderItem.getChildren();
assertNotNull(children);
assertEquals(0, children.size());
assertTrue(folderItem.getCanScrollDescendants());
ScrollFileSystemItemList descendants = folderItem.scrollDescendants(null, 10, 1000);
assertNotNull(descendants);
assertNotNull(descendants.getScrollId());
assertEquals(0, descendants.size());
// FolderishFile => adaptable as a FolderItem since the default
// FileSystemItem factory gives precedence to the Folderish facet
assertTrue(defaultFileSystemItemFactory.isFileSystemItem(folderishFile));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(folderishFile);
assertNotNull(fsItem);
assertTrue(fsItem instanceof FolderItem);
assertEquals(DEFAULT_FILE_SYSTEM_ITEM_ID_PREFIX + folderishFile.getId(), fsItem.getId());
assertEquals(syncRootItemId, fsItem.getParentId());
assertEquals("Sarah's folderish file", fsItem.getName());
assertTrue(fsItem.isFolder());
assertEquals("Administrator", fsItem.getCreator());
assertEquals("Administrator", fsItem.getLastContributor());
// ------------------------------------------------------
// Check not downloadable nor folderish
// ------------------------------------------------------
assertFalse(defaultFileSystemItemFactory.isFileSystemItem(notAFileSystemItem));
fsItem = defaultFileSystemItemFactory.getFileSystemItem(notAFileSystemItem);
assertNull(fsItem);
// -------------------------------------------------------------
// Check #getFileSystemItem(DocumentModel doc, FolderItem parentItem)
// -------------------------------------------------------------
FolderItem syncRootSystemItem = (FolderItem) fileSystemItemAdapterService.getFileSystemItemFactoryForId(syncRootItemId).getFileSystemItemById(syncRootItemId, principal);
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, syncRootSystemItem);
assertEquals(syncRootItemId, fsItem.getParentId());
// Passing a null parent will force a null parentId
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note, null);
assertNull(fsItem.getParentId());
// ------------------------------------------------------------------
// Check FileSystemItem#getCanRename and FileSystemItem#getCanDelete
// ------------------------------------------------------------------
// As Administrator
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
assertTrue(fsItem.getCanRename());
assertTrue(fsItem.getCanDelete());
// As a user with READ permission
DocumentModel rootDoc = session.getRootDocument();
setPermission(rootDoc, "joe", SecurityConstants.READ, true);
// Under Oracle, the READ ACL optims are not visible from the joe
// session while the transaction has not been committed.
TransactionHelper.commitOrRollbackTransaction();
TransactionHelper.startTransaction();
try (CloseableCoreSession joeSession = coreFeature.openCoreSession("joe")) {
nuxeoDriveManager.registerSynchronizationRoot(joeSession.getPrincipal(), syncRootFolder, session);
note = joeSession.getDocument(note.getRef());
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
assertFalse(fsItem.getCanRename());
assertFalse(fsItem.getCanDelete());
// As a user with WRITE permission
setPermission(rootDoc, "joe", SecurityConstants.WRITE, true);
fsItem = defaultFileSystemItemFactory.getFileSystemItem(note);
assertTrue(fsItem.getCanRename());
assertTrue(fsItem.getCanDelete());
}
resetPermissions(rootDoc, "joe");
}
use of org.nuxeo.ecm.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class TestNuxeoDriveManager method testResetSyncRootsOnVersioning.
@Test
public void testResetSyncRootsOnVersioning() {
nuxeoDriveManager.registerSynchronizationRoot(session.getPrincipal(), workspace_1, session);
DocumentRef syncRootVersionRef = session.checkIn(workspace_1.getRef(), VersioningOption.MAJOR, null);
txFeature.nextTransaction();
DocumentModel syncRootVersion = session.getDocument(syncRootVersionRef);
assertFalse(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), syncRootVersion));
}
Aggregations