use of org.nuxeo.ecm.core.api.DocumentRef 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.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class AbstractChangeFinderTestCase method toSimpleFileSystemItemChange.
protected SimpleFileSystemItemChange toSimpleFileSystemItemChange(FileSystemItemChange change) {
SimpleFileSystemItemChange simpleChange = new SimpleFileSystemItemChange(change.getDocUuid(), change.getEventId(), change.getRepositoryId(), change.getFileSystemItemId(), change.getFileSystemItemName());
DocumentRef changeDocRef = new IdRef(change.getDocUuid());
if (session.exists(changeDocRef)) {
simpleChange.setLifeCycleState(session.getDocument(changeDocRef).getCurrentLifeCycleState());
}
return simpleChange;
}
use of org.nuxeo.ecm.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class AuditChangeFinder method getFileSystemChanges.
protected List<FileSystemItemChange> getFileSystemChanges(CoreSession session, Set<IdRef> lastActiveRootRefs, SynchronizationRoots activeRoots, Set<String> collectionSyncRootMemberIds, long lowerBound, long upperBound, boolean integerBounds, int limit) throws TooManyChangesException {
String principalName = session.getPrincipal().getName();
List<FileSystemItemChange> changes = new ArrayList<FileSystemItemChange>();
// Note: lastActiveRootRefs is not used: we could remove it from the
// public API
// and from the client as well but it might be useful to optimize future
// alternative implementations FileSystemChangeFinder component so it
// might
// be better to leave it part of the public API as currently.
// Find changes from the log under active roots or events that are
// linked to the un-registration or deletion of formerly synchronized
// roots
List<LogEntry> entries = queryAuditEntries(session, activeRoots, collectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
// query with the actual active roots.
for (LogEntry entry : entries) {
if (NuxeoDriveEvents.EVENT_CATEGORY.equals(entry.getCategory())) {
if (log.isDebugEnabled()) {
log.debug(String.format("Detected sync root change for user '%s' in audit log:" + " invalidating the root cache and refetching the changes.", principalName));
}
NuxeoDriveManager driveManager = Framework.getService(NuxeoDriveManager.class);
driveManager.invalidateSynchronizationRootsCache(principalName);
driveManager.invalidateCollectionSyncRootMemberCache(principalName);
Map<String, SynchronizationRoots> synchronizationRoots = driveManager.getSynchronizationRoots(session.getPrincipal());
SynchronizationRoots updatedActiveRoots = synchronizationRoots.get(session.getRepositoryName());
Set<String> updatedCollectionSyncRootMemberIds = driveManager.getCollectionSyncRootMemberIds(session.getPrincipal()).get(session.getRepositoryName());
entries = queryAuditEntries(session, updatedActiveRoots, updatedCollectionSyncRootMemberIds, lowerBound, upperBound, integerBounds, limit);
break;
}
}
if (entries.size() >= limit) {
throw new TooManyChangesException("Too many changes found in the audit logs.");
}
for (LogEntry entry : entries) {
if (log.isDebugEnabled()) {
log.debug(String.format("Handling log entry %s", entry));
}
FileSystemItemChange change = null;
DocumentRef docRef = new IdRef(entry.getDocUUID());
ExtendedInfo fsIdInfo = entry.getExtendedInfos().get("fileSystemItemId");
if (fsIdInfo != null) {
// been updated, we just know the FileSystemItem id and name.
if (log.isDebugEnabled()) {
log.debug(String.format("Found extended info in audit log entry: document has been deleted, moved," + " is an unregistered synchronization root or its security has been updated," + " we just know the FileSystemItem id and name."));
}
boolean isChangeSet = false;
// current user still has access to the document.
if (!"deleted".equals(entry.getEventId()) && session.exists(docRef)) {
change = getFileSystemItemChange(session, docRef, entry, fsIdInfo.getValue(String.class));
if (change != null) {
if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
// virtual event.
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) has been moved to another synchronzation root, not adding entry to the change summary.", entry.getDocPath(), docRef));
}
continue;
}
isChangeSet = true;
}
}
if (!isChangeSet) {
// FileSystemItem id and name to the FileSystemItemChange entry.
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) doesn't exist or is not adaptable as a FileSystemItem, only providing the FileSystemItem id and name to the FileSystemItemChange entry.", entry.getDocPath(), docRef));
}
String fsId = fsIdInfo.getValue(String.class);
String eventId;
if (NuxeoDriveEvents.MOVED_EVENT.equals(entry.getEventId())) {
// Move to a non synchronization root
eventId = NuxeoDriveEvents.DELETED_EVENT;
} else {
// Deletion, unregistration or security update
eventId = entry.getEventId();
}
change = new FileSystemItemChangeImpl(eventId, entry.getEventDate().getTime(), entry.getRepositoryId(), entry.getDocUUID(), fsId, null);
}
if (log.isDebugEnabled()) {
log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
}
changes.add(change);
} else {
// unregistered synchronization root nor a security update denying access to the current user.
if (log.isDebugEnabled()) {
log.debug(String.format("No extended info found in audit log entry %s (%s): this is not a deleted document, a moved document," + " an unregistered synchronization root nor a security update denying access to the current user.", entry.getDocPath(), docRef));
}
if (!session.exists(docRef)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) doesn't exist, not adding entry to the change summary.", entry.getDocPath(), docRef));
}
// try to propagate this event.
continue;
}
// Let's try to adapt the document as a FileSystemItem to
// provide it to the FileSystemItemChange entry.
change = getFileSystemItemChange(session, docRef, entry, null);
if (change == null) {
// Non-adaptable documents are ignored
if (log.isDebugEnabled()) {
log.debug(String.format("Document %s (%s) is not adaptable as a FileSystemItem, not adding any entry to the change summary.", entry.getDocPath(), docRef));
}
} else {
if (log.isDebugEnabled()) {
log.debug(String.format("Adding FileSystemItemChange entry to the change summary: %s", change));
}
changes.add(change);
}
}
}
return changes;
}
use of org.nuxeo.ecm.core.api.DocumentRef 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.core.api.DocumentRef in project nuxeo-drive-server by nuxeo.
the class TestNuxeoDriveManager method testFilterVersionSyncRoots.
@Test
public void testFilterVersionSyncRoots() {
// Create a version document
DocumentRef versionRef = session.checkIn(workspace_1.getRef(), VersioningOption.MAJOR, null);
DocumentModel version = session.getDocument(versionRef);
// Force its registration as a synchronization root
version.addFacet(NuxeoDriveManagerImpl.NUXEO_DRIVE_FACET);
Map<String, Serializable> subscription = new HashMap<>();
subscription.put("username", session.getPrincipal().getName());
subscription.put("enabled", Boolean.TRUE);
List<Map<String, Serializable>> subscriptions = Collections.singletonList(subscription);
version.setPropertyValue("drv:subscriptions", (Serializable) subscriptions);
version.putContextData(CoreSession.ALLOW_VERSION_WRITE, Boolean.TRUE);
session.saveDocument(version);
txFeature.nextTransaction();
// Check the version is filtered among the synchronization roots
assertFalse(nuxeoDriveManager.isSynchronizationRoot(session.getPrincipal(), version));
}
Aggregations