use of org.nuxeo.ecm.core.api.DocumentModel 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));
}
}
use of org.nuxeo.ecm.core.api.DocumentModel 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;
}
}
use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.
the class AbstractFileSystemItemFactory method exists.
/**
* The default factory considers that a {@link FileSystemItem} with the given id exists if the backing
* {@link DocumentModel} can be fetched and {@link #isFileSystemItem(DocumentModel)} returns true.
*
* @see #isFileSystemItem(DocumentModel)
*/
@Override
public boolean exists(String id, Principal principal) {
String[] idFragments = parseFileSystemId(id);
String repositoryName = idFragments[1];
String docId = idFragments[2];
try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryName, principal)) {
DocumentModel doc = getDocumentById(docId, session);
return isFileSystemItem(doc);
} catch (DocumentNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug(String.format("No doc related to id %s, returning false.", docId));
}
return false;
} catch (DocumentSecurityException e) {
if (log.isDebugEnabled()) {
log.debug(String.format("User %s cannot access doc %s, returning false.", principal.getName(), docId));
}
return false;
}
}
use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveManagerImpl method registerSynchronizationRoot.
@Override
public void registerSynchronizationRoot(Principal principal, final DocumentModel newRootContainer, CoreSession session) {
final String userName = principal.getName();
if (log.isDebugEnabled()) {
log.debug(String.format("Registering synchronization root %s for %s", newRootContainer, userName));
}
// If new root is child of a sync root, ignore registration, except for
// the 'Locally Edited' collection: it is under the personal workspace
// and we want to allow both the personal workspace and the 'Locally
// Edited' collection to be registered as sync roots
Map<String, SynchronizationRoots> syncRoots = getSynchronizationRoots(principal);
SynchronizationRoots synchronizationRoots = syncRoots.get(session.getRepositoryName());
if (!NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME.equals(newRootContainer.getName())) {
for (String syncRootPath : synchronizationRoots.getPaths()) {
String syncRootPrefixedPath = syncRootPath + "/";
if (newRootContainer.getPathAsString().startsWith(syncRootPrefixedPath)) {
// the only exception is when the right inheritance is
// blocked
// in the hierarchy
boolean rightInheritanceBlockedInTheHierarchy = false;
// should get only parents up to the sync root
Path parentPath = newRootContainer.getPath().removeLastSegments(1);
while (!"/".equals(parentPath.toString())) {
String parentPathAsString = parentPath.toString() + "/";
if (!parentPathAsString.startsWith(syncRootPrefixedPath)) {
break;
}
PathRef parentRef = new PathRef(parentPathAsString);
if (!session.hasPermission(principal, parentRef, SecurityConstants.READ)) {
rightInheritanceBlockedInTheHierarchy = true;
break;
}
parentPath = parentPath.removeLastSegments(1);
}
if (!rightInheritanceBlockedInTheHierarchy) {
return;
}
}
}
}
checkCanUpdateSynchronizationRoot(newRootContainer, session);
// Unregister any sub-folder of the new root, except for the 'Locally
// Edited' collection
String newRootPrefixedPath = newRootContainer.getPathAsString() + "/";
for (String existingRootPath : synchronizationRoots.getPaths()) {
if (!existingRootPath.endsWith(NuxeoDriveManager.LOCALLY_EDITED_COLLECTION_NAME)) {
if (existingRootPath.startsWith(newRootPrefixedPath)) {
// Unregister the nested root sub-folder first
PathRef ref = new PathRef(existingRootPath);
if (session.exists(ref)) {
DocumentModel subFolder = session.getDocument(ref);
unregisterSynchronizationRoot(principal, subFolder, session);
}
}
}
}
UnrestrictedSessionRunner runner = new UnrestrictedSessionRunner(session) {
@Override
public void run() {
if (!newRootContainer.hasFacet(NUXEO_DRIVE_FACET)) {
newRootContainer.addFacet(NUXEO_DRIVE_FACET);
}
fireEvent(newRootContainer, session, NuxeoDriveEvents.ABOUT_TO_REGISTER_ROOT, userName);
@SuppressWarnings("unchecked") List<Map<String, Object>> subscriptions = (List<Map<String, Object>>) newRootContainer.getPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY);
boolean updated = false;
for (Map<String, Object> subscription : subscriptions) {
if (userName.equals(subscription.get("username"))) {
subscription.put("enabled", Boolean.TRUE);
subscription.put("lastChangeDate", Calendar.getInstance(UTC));
updated = true;
break;
}
}
if (!updated) {
Map<String, Object> subscription = new HashMap<String, Object>();
subscription.put("username", userName);
subscription.put("enabled", Boolean.TRUE);
subscription.put("lastChangeDate", Calendar.getInstance(UTC));
subscriptions.add(subscription);
}
newRootContainer.setPropertyValue(DRIVE_SUBSCRIPTIONS_PROPERTY, (Serializable) subscriptions);
newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, true);
newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, true);
newRootContainer.putContextData(CoreSession.SOURCE, "drive");
DocumentModel savedNewRootContainer = session.saveDocument(newRootContainer);
newRootContainer.putContextData(NXAuditEventsService.DISABLE_AUDIT_LOGGER, false);
newRootContainer.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, false);
fireEvent(savedNewRootContainer, session, NuxeoDriveEvents.ROOT_REGISTERED, userName);
session.save();
}
};
runner.runUnrestricted();
invalidateSynchronizationRootsCache(userName);
invalidateCollectionSyncRootMemberCache(userName);
}
use of org.nuxeo.ecm.core.api.DocumentModel in project nuxeo-drive-server by nuxeo.
the class NuxeoDriveManagerImpl method queryAndFetchSynchronizationRoots.
protected Map<String, SynchronizationRoots> queryAndFetchSynchronizationRoots(CoreSession session, String query) {
Map<String, SynchronizationRoots> syncRoots = new HashMap<String, SynchronizationRoots>();
Set<IdRef> references = new LinkedHashSet<IdRef>();
Set<String> paths = new LinkedHashSet<String>();
IterableQueryResult results = session.queryAndFetch(query, NXQL.NXQL);
try {
for (Map<String, Serializable> result : results) {
IdRef docRef = new IdRef(result.get("ecm:uuid").toString());
try {
DocumentModel doc = session.getDocument(docRef);
references.add(docRef);
paths.add(doc.getPathAsString());
} catch (DocumentNotFoundException e) {
log.warn(String.format("Document %s not found, not adding it to the list of synchronization roots for user %s.", docRef, session.getPrincipal().getName()));
} catch (DocumentSecurityException e) {
log.warn(String.format("User %s cannot access document %s, not adding it to the list of synchronization roots.", session.getPrincipal().getName(), docRef));
}
}
} finally {
results.close();
}
SynchronizationRoots repoSyncRoots = new SynchronizationRoots(session.getRepositoryName(), paths, references);
syncRoots.put(session.getRepositoryName(), repoSyncRoots);
return syncRoots;
}
Aggregations