Search code examples
javaalfrescoalfresco-shareopencmis

how to use Alfresco OperationContext to filter out documents?


I have a lot of documents in one of folder in Alfresco. some of them are pdf. I want to filter all my document on pdf and retrieve only pdf document. now i am doing it by iterating over a very big list which i get from alfresco through opencmis and look at the document name and filter them if they are .pdf. I researched the api and see the possibility of using OperationContext for this purpose but i dont know how to do it. any example will be more then welcome.

Is there a better approach to do it before that i get all documents to filter them?

This is what i do now:

public List< Document > retrieveAllPdfInFolder( Folder target )
{
    List< Document > documentList = GenericsUtil.makeList();
    for (CmisObject cmisObject: target.getChildren())
    {
        if (BaseTypeId.CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId()))
        {
            Document doc =( Document ) cmisObject;
            if(doc.getName().endsWith( ".pdf" ))
                documentList.add( doc);
            System.out.println("[Docment] " + cmisObject.getName());
        }
    }

    return  documentList ;
}

This is what i want to reach:

public List< Document > retrieveAllPdfInFolder( Folder target, boolean all )
{
    OperationContext operationContext = OperationContextUtils.createOperationContext();
    Set<String> propertyFilter = new HashSet<String>();
    propertyFilter.add( PropertyIds.CONTENT_STREAM_MIME_TYPE);
    operationContext.setFilter( propertyFilter );
    operationContext.setFilterString(  ".pdf" );
    // i dont know how to set filter on operationContext

    List< Document > finalDocumentList = GenericsUtil.makeList();

    ItemIterable< CmisObject > documents = all ? target.getChildren() :  target.getChildren(operationContext);
    for (CmisObject cmisObject: documents)
    {
        if (BaseTypeId.CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId()))
            finalDocumentList.add( ( Document ) cmisObject);
    }

    return  finalDocumentList ;   
}

Solution

  • You can use a CMIS query. Something like this should work:

    QueryStatement stmt = session.createQueryStatement("IN_FOLDER(?) AND cmis:contentStreamMimeType=?");
    stmt.setString(1, target.getId());
    stmt.setString(2, "application/pdf");
    
    ItemIterable<CmisObject> documents = session.queryObjects("cmis:document", stmt.toString(), false, session.getDefaultContext());
    
    for (CmisObject cmisObject: documents)
    {
      finalDocumentList.add((Document)cmisObject);
    }