Pages

Monday, December 28, 2009

Get All Documents from a View

/**
* Method to obtain a collection of all documents from a specified view in a specified database
* @param targetDB - the database from which the document collection is to be obtained
* @param viewName - the name of the view from which the document collection is to be obtained
* @return String[]
* @author karthikeyan_a
* @since 30-April-2009
*/

public DocumentCollection getViewDocuments(Database targetDB, String viewName) {
//if any of the input parameters in undefined then return null
if ((targetDB==null)|| (viewName==null) || (viewName.trim().equals(""))) {
return null;
}
//mark the return value and initialize other variables
DocumentCollection viewDocs=null;
Document viewDoc=null;
ViewEntryCollection viewEntries=null;
ViewEntry viewEntry=null;
View chosenView=null;
try {
//set the handle for the view mentioned by the view name
chosenView=targetDB.getView(viewName);
//if the view handle is not set then return null
if (chosenView==null) {
return null;
}

//ensure that an empty document collection is created
Random generator = new Random();
viewDocs=chosenView.getAllDocumentsByKey(".~^$#$^&~."+generator.toString());

/**
* loop through all the entries in the view and push their associated
* documents into the document collection
*/
viewEntries= chosenView.getAllEntries();
//get the handle for the first entry in the collection
viewEntry=viewEntries.getFirstEntry();
while(viewEntry!=null) {
if (viewEntry.isDocument()) {
viewDoc=viewEntry.getDocument();
/*
* If the document concerned with the entry is already present in the
* collection then a duplicate exception is thrown. So catch the same and
* dont allow that to hurt the process
*/
try {
viewDocs.addDocument(viewDoc);
}
catch (NotesException duplicateException) {
//by pass exception
duplicateException=null;
}
}
//push the handle to the next entry in the collection
viewEntry=viewEntries.getNextEntry(viewEntry);
}

} catch (NotesException e) {
e.printStackTrace();
}
//return the collection of documents thus obtained
return viewDocs;
}

No comments:

Post a Comment