Probably more than you want to know about Java follows, but here it is:
There are a couple of different ways the agent could be written, and I will try to cover those here. The main thing is, we need to get a handle to each document that we want to process, the "processContextDocument" method doesn't care how we get the document handle there are several types of loops that can do this:
The simplest is to loop through all documents in the view by using View.getNextDocument such as:
Document doc = view.getFirstDocument()
while(doc != null){
WorkflowDocument.processContextDoc(doc);<br/>
// update the history status.<br/>
doc.save();<br/>
doc = view.getNextDocument(doc);<br/>
}
The problem with doing the loop this way, is that saving a document in the view can cause the view index to update while you are in the loop, and documents can be skipped while using the "getNextDocument" method of View.
It is generally safer to put all the documents you want to work with first into a Vector and then loop through all elements in the Vector. The Java Vector class is the most common of the many collections that are available in Java.
The code below puts all the documents from the View into a Vector for later use:
Document doc = view.getFirstDocument();
while(doc != null){
docs.addElement(doc);<br/>
doc = view.getNextDocument(doc);<br/>
}
The code uses a 'for' loop to process each document that has been added to the Vector. You could also use a while loop as follows:
Enumeration e = docs.elements();
while(e.hasMoreElements()){
doc = (Document)e.nextElement();<br/>
WorkflowDocument.processContextDoc(doc);<br/>
/// update the history ...<br/>
doc.save();<br/>
}
It is a much more common practice to use a 'for' loop than a 'while' loop for processing all elements in a Vector. The reason is because with the 'for' loop, the scope of the Enumeration is inside the loop where it is declared whereas for the 'while' loop the scope of the Enumeration is outside of the loop where the Enumeration is declared, and a variable such as this should use the smallest scope possible.
The Enumeration class is an older feature in Java, and has been largely replaced by the Iterator class for newer Java code. The above code will work with the 1.1 version of Java and thus will run on an R5 server. For the R6 server, which uses JVM version 1.3, an Iterator can be used instead of an Enumeration and the preffered form of the loop would be:
for(Iterator i=docs.iterator(); i.hasNext(); ){
Document doc = (Document)i.next();<br/>
//// process the document and do all the other stuff here<br/>
}
Probably the most confusing and clunky part of the loop is the statement:
Document doc = (Document)i.next();
Elements in an Iterator or Enumeration are always of type Object and have to be cast to the object class that you are actually using, which means it's up to the programmer to remember what they put into the Vector. Typecasting for Vectors is a hole the strict typing that is normally a part of Java. This issue is resolved in the latest Java release (Tiger) which supports type safe Collections and Vectors. Unfortunately, it will probably be years before the newest version of Java is available in Domino.
Hope this helps.