javaalfrescoopencmis

OpenCMIS searching for all Documents


I'm trying to do findAllByName functionality through OpenCMIS over an Alfresco DMS. Basically it's supposed to find all Documents with a certain name.

Currently I've tried to do a recursive search, which is unfortunately very costly.

private List<Document> traverseAndFind(String name, Folder currentFolder, List<Document> foundDocuments) {

    List<Folder> subFolders = new ArrayList<>();
    for (CmisObject obj : currentFolder.getChildren()) {
        if (obj.getName().equals(name) && obj instanceof Document) {
            foundDocuments.add((Document) obj);
        } else if (obj instanceof Folder) {
            subFolders.add((Folder) obj);
        }
    }

    for (Folder subFolder : subFolders) {
        traverseAndFind(name, subFolder, foundDocuments);
    }

    return foundDocuments;
} 

From what I've seen in the implementation of Folder::getChildren() it keeps working with Session and makes separate requests to find the children of a certain folder, leading to very long overall time to do a fairly shallow search, which probably means that I am approaching the problem incorrectly.

Is there a better approach to this? How would you for example do a search resulting in a List of absolutely every filename that is stored?

Thanks for any tips!


Solution

  • The best way to search in Alfresco is by using queries

    Your query should be like this :

    SELECT * FROM cmis:document where cmis:name = 'DOCUMENT_NAME' AND  IN_TREE('ID_FOLDER')
    

    IN_TREE is used for recursive search from Specific Folder, if you want to search in all repository, don't add IN_TREE

    Here sample in JAVA:

    private List<Document> getDocumentsByName(String name, Folder folder) {
    
        List<Document> documents = new ArrayList<>();
    
        String query = "SELECT * FROM cmis:document where cmis:name = '"+name+"' AND IN_TREE('" + folder.getId() +"'";
        ItemIterable<QueryResult> results = session.query( query, false);
    
        //process result
        for (QueryResult qResult : results) {
            String objectId = qResult.getPropertyValueByQueryName(objectIdQueryName);
            Document doc = (Document) session.getObject(session.createObjectId(objectId));
            documents.add(doc);
        }
    
        return documents;
    }