Content Engine API ♥ Java Collections

Code on github

To continue the “Love” series started with streams and lambda’s, now some love for collections. When you are programming code using the IBM FileNet P8 Content Engine, most of the times you will be writing code which involves iterating over collections. The collections of the Content Engine API do not implement the Iterable interface. So processing the elements in the collection requires an iterator. When you are writing code to walk through the elements of the collection the code will typically look like this:

DocumentSet documents = folder.get_ContainedDocuments();
Iterator<Document> iterator = documents.iterator();
while ( iterator.hasNext() ) {
    Document document = iterator.next();
    System.out.println(document.get_Name());
}

The iterator construct adds extra clutter to the code. It also adds the opportunity for error when you are nesting several loops. Therefore the for-each loop construct in Java is more favorable.

To fix this you can use a wrapper class. This class is generated by inspecting the com.filenet.api.collection package of the Content Engine API. (The code to generate this class is also included).

You can download the wrapper class from here, and add it to your project. For convenience, first do a static import of the asCollections method from the CEAPICollectionsclass in your Java class:

import static com.ecmdeveloper.jace.collections.CEAPICollections.asCollection;

Next you can write code like this, replacing the code above:

DocumentSet documents = folder.get_ContainedDocuments();
for (Document document : asCollection(documents) ) {
  System.out.println(document.get_Name());
}

In this case you no longer need the iterator and the document variable is ready for use. The original code also made the Java IDE complain about an unchecked conversion. You still have the complaints, but the are all contained in the single wrapper class.

Of course this solution also adds some clutter in the form of the wrapper class. So it is up to you which kind of clutter you prefer.

Leave a Comment