Pages

Monday, December 28, 2009

List Servers Indicated in Connection documents if Names.nsf

/**
* Method to return a list of server names present in the connection documents in names.nsf
* @param session- The Notes Session belonging to a particular user
* @return String Array
* @author karthikeyan_a
* @created 11-May-2009
* @see ArrayList removeDuplicates(ArrayList arrayList) ----> http://ozinisle.blogspot.com/2009/12/method-to-remove-duplicate-values-in.html
*/

public String[] getServerNames(Session session){
//initializing return type
String[] serverNames=null;
try {
//declaring variables and objects necessary for further manipulations
ArrayList serverNameList=new ArrayList();

Database addBook=null;
View connectionView=null;
ViewEntryCollection connectionEntries=null;
ViewEntry connectionEntry=null;
Document connectionDocument=null;

String connectionServerName=null;

int entryCount=0;
int serverNameCount=0;

//getting the handle for the names and address book in the local server
addBook=session.getDatabase("", "names.nsf");

//if the handle is not set then return the same
if (addBook==null) {
System.out.println("Names.nsf is not found");
return null;
}

// if the address book is not open before then open it again
if (!addBook.isOpen()) {
addBook.open();
}

//get the handle of the view which has the list of various connection documents in names.nsf
connectionView=addBook.getView("Adva_nced\\Connections");

//if the handle for the same is not set then return null
if (connectionView==null) {
System.out.println("Connections view is not found in names.nsf");
return null;
}

//get the handle of the collection of all entries present in the view
connectionEntries=connectionView.getAllEntries();

//if there are no entries found then return null
if (connectionEntries.getCount()==0) {
System.out.println("There are no connection documents found in names.nsf");
return null;
}
System.out.println("There are "+connectionEntries.getCount()+" connection documents found in names.nsf");

//set the handle for the first entry in the collection
connectionEntry=connectionEntries.getFirstEntry();

//loop through the entries in the collection and get the destination server's name
for (entryCount=0;entryCount<connectionEntries.getCount();entryCount++) { //start of entry collection for loop

//if the concerned entry is a document then proceed else skip
if (connectionEntry.isDocument()) {

//get the handle of the document associated with the current entry
connectionDocument=connectionEntry.getDocument();

//get the name of the server the connection document is associated with
connectionServerName=connectionDocument.getItemValueString("Destination");

//put that in the list of server names found so far
serverNameList.add(connectionServerName);
}

//proceed to the next entry in the collection
connectionEntry=connectionEntries.getNextEntry(connectionEntry);
} //end of entry collection for loop

//remove any duplicate entries present in the server list
serverNameList=removeDuplicates(serverNameList);

//convert the array list into a string array
serverNames=new String[serverNameList.size()];
for (serverNameCount=0;serverNameCount<serverNameList.size();serverNameCount++) {
serverNames[serverNameCount]=serverNameList.get(serverNameCount).toString();
}

} catch (NotesException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//return the resultant string array
return serverNames;
}

No comments:

Post a Comment