Search in sources :

Example 16 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-drive-server by nuxeo.

the class UserWorkspaceSyncRootParentFactory method getVirtualFolderItem.

@Override
public FolderItem getVirtualFolderItem(Principal principal) {
    FileSystemItemManager fileSystemItemManager = Framework.getService(FileSystemItemManager.class);
    FolderItem topLevelFolder = fileSystemItemManager.getTopLevelFolder(principal);
    if (topLevelFolder == null) {
        throw new NuxeoException("Found no top level folder item. Please check your contribution to the following extension point: <extension target=\"org.nuxeo.drive.service.FileSystemItemAdapterService\" point=\"topLevelFolderItemFactory\">.");
    }
    return new UserWorkspaceSyncRootParentFolderItem(getName(), principal, topLevelFolder.getId(), topLevelFolder.getPath(), folderName);
}
Also used : FileSystemItemManager(org.nuxeo.drive.service.FileSystemItemManager) UserWorkspaceSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceSyncRootParentFolderItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) UserWorkspaceSyncRootParentFolderItem(org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceSyncRootParentFolderItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException)

Example 17 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-drive-server by nuxeo.

the class UserWorkspaceTopLevelFactory method getTopLevelFolderItem.

/*----------------------- TopLevelFolderItemFactory ---------------------*/
@Override
public FolderItem getTopLevelFolderItem(Principal principal) {
    RepositoryManager repositoryManager = Framework.getService(RepositoryManager.class);
    // TODO: handle multiple repositories
    try (CloseableCoreSession session = CoreInstance.openCoreSession(repositoryManager.getDefaultRepositoryName(), principal)) {
        UserWorkspaceService userWorkspaceService = Framework.getService(UserWorkspaceService.class);
        DocumentModel userWorkspace = userWorkspaceService.getCurrentUserPersonalWorkspace(session);
        if (userWorkspace == null) {
            throw new NuxeoException(String.format("No personal workspace found for user %s.", principal.getName()));
        }
        return (FolderItem) getFileSystemItem(userWorkspace);
    }
}
Also used : FolderItem(org.nuxeo.drive.adapter.FolderItem) UserWorkspaceTopLevelFolderItem(org.nuxeo.drive.hierarchy.userworkspace.adapter.UserWorkspaceTopLevelFolderItem) UserWorkspaceService(org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) RepositoryManager(org.nuxeo.ecm.core.api.repository.RepositoryManager) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 18 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-drive-server by nuxeo.

the class DocumentBackedFolderItem method getScrollBatch.

@SuppressWarnings("unchecked")
protected ScrollDocumentModelList getScrollBatch(String scrollId, int batchSize, CoreSession session, long keepAlive) {
    Cache scrollingCache = Framework.getService(CacheService.class).getCache(DESCENDANTS_SCROLL_CACHE);
    if (scrollingCache == null) {
        throw new NuxeoException("Cache not found: " + DESCENDANTS_SCROLL_CACHE);
    }
    String newScrollId;
    List<String> descendantIds;
    if (StringUtils.isEmpty(scrollId)) {
        // Perform initial query to fetch ids of all the descendant documents and put the result list in a
        // cache, aka "search context"
        descendantIds = new ArrayList<>();
        StringBuilder sb = new StringBuilder(String.format("SELECT ecm:uuid FROM Document WHERE ecm:ancestorId = '%s'", docId));
        sb.append(" AND ecm:isTrashed = 0");
        sb.append(" AND ecm:mixinType != 'HiddenInNavigation'");
        // Don't need to add ecm:isVersion = 0 because versions are already excluded by the
        // ecm:ancestorId clause since they have no path
        String query = sb.toString();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Executing initial query to scroll through the descendants of %s: %s", docPath, query));
        }
        try (IterableQueryResult res = session.queryAndFetch(sb.toString(), NXQL.NXQL)) {
            Iterator<Map<String, Serializable>> it = res.iterator();
            while (it.hasNext()) {
                descendantIds.add((String) it.next().get(NXQL.ECM_UUID));
            }
        }
        // Generate a scroll id
        newScrollId = UUID.randomUUID().toString();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Put initial query result list (search context) in the %s cache at key (scrollId) %s", DESCENDANTS_SCROLL_CACHE, newScrollId));
        }
        scrollingCache.put(newScrollId, (Serializable) descendantIds);
    } else {
        // Get the descendant ids from the cache
        descendantIds = (List<String>) scrollingCache.get(scrollId);
        if (descendantIds == null) {
            throw new NuxeoException(String.format("No search context found in the %s cache for scrollId [%s]", DESCENDANTS_SCROLL_CACHE, scrollId));
        }
        newScrollId = scrollId;
    }
    if (descendantIds.isEmpty()) {
        return new ScrollDocumentModelList(newScrollId, 0);
    }
    // Extract a batch of descendant ids
    List<String> descendantIdsBatch = getBatch(descendantIds, batchSize);
    // Update descendant ids in the cache
    scrollingCache.put(newScrollId, (Serializable) descendantIds);
    // Fetch documents from VCS
    DocumentModelList descendantDocsBatch = fetchFromVCS(descendantIdsBatch, session);
    return new ScrollDocumentModelList(newScrollId, descendantDocsBatch);
}
Also used : DocumentModelList(org.nuxeo.ecm.core.api.DocumentModelList) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) IterableQueryResult(org.nuxeo.ecm.core.api.IterableQueryResult) HashMap(java.util.HashMap) Map(java.util.Map) Cache(org.nuxeo.ecm.core.cache.Cache) CacheService(org.nuxeo.ecm.core.cache.CacheService)

Example 19 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException 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;
    }
}
Also used : AbstractFileSystemItem(org.nuxeo.drive.adapter.impl.AbstractFileSystemItem) FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) DocumentNotFoundException(org.nuxeo.ecm.core.api.DocumentNotFoundException) CloseableCoreSession(org.nuxeo.ecm.core.api.CloseableCoreSession) DocumentSecurityException(org.nuxeo.ecm.core.api.DocumentSecurityException) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException) DocumentModel(org.nuxeo.ecm.core.api.DocumentModel)

Example 20 with NuxeoException

use of org.nuxeo.ecm.core.api.NuxeoException in project nuxeo-drive-server by nuxeo.

the class FileSystemItemManagerImpl method move.

@Override
public FileSystemItem move(String srcId, String destId, Principal principal) {
    FileSystemItem srcFsItem = getFileSystemItemById(srcId, principal);
    if (srcFsItem == null) {
        throw new NuxeoException(String.format("Cannot move file system item with id %s because it doesn't exist.", srcId));
    }
    FileSystemItem destFsItem = getFileSystemItemById(destId, principal);
    if (destFsItem == null) {
        throw new NuxeoException(String.format("Cannot move a file system item to file system item with id %s because it doesn't exist.", destId));
    }
    if (!(destFsItem instanceof FolderItem)) {
        throw new NuxeoException(String.format("Cannot move a file system item to file system item with id %s because it is not a folder.", destId));
    }
    return srcFsItem.move((FolderItem) destFsItem);
}
Also used : FileSystemItem(org.nuxeo.drive.adapter.FileSystemItem) FolderItem(org.nuxeo.drive.adapter.FolderItem) NuxeoException(org.nuxeo.ecm.core.api.NuxeoException)

Aggregations

NuxeoException (org.nuxeo.ecm.core.api.NuxeoException)31 FolderItem (org.nuxeo.drive.adapter.FolderItem)14 DocumentModel (org.nuxeo.ecm.core.api.DocumentModel)11 FileSystemItem (org.nuxeo.drive.adapter.FileSystemItem)9 IOException (java.io.IOException)5 Blob (org.nuxeo.ecm.core.api.Blob)5 CloseableCoreSession (org.nuxeo.ecm.core.api.CloseableCoreSession)5 DocumentModelList (org.nuxeo.ecm.core.api.DocumentModelList)4 Principal (java.security.Principal)3 DefaultSyncRootFolderItem (org.nuxeo.drive.adapter.impl.DefaultSyncRootFolderItem)3 BlobHolder (org.nuxeo.ecm.core.api.blobholder.BlobHolder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Path (org.nuxeo.common.utils.Path)2