简体   繁体   中英

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 ;   
}

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);
}

You should try it using lucene search.In case of APIs, you can use webscript instead of CMIS as I am not sure about how you can use lucene search in CMIS API.

Below link will help you in searching content of PDF type.In Lucene query you can specify mimetype.So it will return only those document which are of type PDF.

https://forums.alfresco.com/forum/developer-discussions/other-apis/lucene-query-mimetype-10022007-1608

For lucene search please see below code.

var parentFolder=search.luceneSearch("PATH:\"/app:company_home/st:sites\"");

This is how it will return the Site node.

For getting more knowledge regarding lucene search you can find on below link.

https://wiki.alfresco.com/wiki/Full_Text_Search_Query_Syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM