javafilenet-p8filenetfilenet-content-engine

Acquiring retrieval name for latest version of a document in a DocumentSet using FileNet API


I have this piece of code:

Folder fold = Factory.Folder.fetchInstance(os, folderPath, null);
DocumentSet docs = fold.get_ContainedDocuments();
Iterator it = docs.iterator();

Document retr;
try {
    while (it.hasNext()) {
        retr = (Document)it.next();
        String name = retr.get_Name();
        System.out.println(name);

        if(name.equals(fileName)) {
            System.out.println("Match Found");
            System.out.println("Document Name : " + retr.get_Name());
            return retr;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

Currently, this code works fine for retrieving a document with a certain file name. I want to add to this so that it can also select the latest version of the file with that name. What should i do?

I've considered adding an extra if inside the existing one, but i'm afraid of impacting the performance.


Solution

  • this is what you can do to get the latest version of the document

        Folder fold = Factory.Folder.fetchInstance(os, folderPath, null);
            DocumentSet docs = fold.get_ContainedDocuments();
            Iterator it = docs.iterator();
    
            Document retr;
            try {
                while (it.hasNext()) {
                    retr = (Document)it.next();
                    String name = retr.get_Name();
                    System.out.println(name);
    
                    if(name.equals(fileName)) {
    
                        System.out.println("Match Found");
                        System.out.println("Document Name : " + retr.get_Name());
    //here you are checking the document for the name. It also can happen that, this document might have more than version but the title remained same for each version. You can get the latest version like
    
                        return retr.get_CurrentVersion();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }