Getting To The Java Roots of XPages – Part 11

So far I’ve been dealing with a very simple class which just gets the current users information from the NAB, While this is useful wouldn’t it be better if I could get this info for any staff member?

To do this I have created a new class in my java package called StaffInfo. It is VERY similar to the original MyInfo class, in fact all I did was copy the code from that class and make a few minor changes to remove the references to ‘my’. Here is the entire class fro your review.

package scary.java.demo;

import java.io.Serializable;

import lotus.domino.Base;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.Name;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;

import com.ibm.xsp.extlib.util.ExtLibUtil;

public class StaffInfo implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String InternetAddress = null;
	private String CommonName = null;
	private String FirstName = null;
	private String LastName = null;
	private String Location = null;
	
	public StaffInfo() {
	    
	}

	public void load(String FQName) {
	    Session session = null;
	    Database thisDB = null;
	    Database nabDB = null;
	    View nabView = null;
	    Document nabDoc = null;
	    Name nabName = null;

	    try {
		session = ExtLibUtil.getCurrentSession();
		thisDB = session.getCurrentDatabase();
		nabDB = session.getDatabase(thisDB.getServer(), "names.nsf",false);
		nabView = nabDB.getView("($Users)");
		nabDoc = nabView.getDocumentByKey(FQName);
		nabName = session.createName(nabDoc.getItemValueString("FullName"));
		
		InternetAddress = nabDoc.getItemValueString("InternetAddress");
		CommonName = nabName.getCommon();
		FirstName = nabName.getGiven();
		LastName = nabName.getSurname();
		Location = nabDoc.getItemValueString("Location");
		
		} catch (NotesException e) {
			System.out.print(e);
		} finally {
			recycleDominoObjects(nabName,nabDoc, nabView, nabDB, thisDB, session);
		}
	}

	public String getInternetAddress() {
	    return InternetAddress;
	}

	public String getCommonName() {
	    return CommonName;
	}

	public String getFirstName() {
	    return FirstName;
	}

	public String getLastName() {
	    return LastName;
	}

	public String getLocation() {
	    return Location;
	}

	public static void recycleDominoObjects(Object... args) {
		for (Object o : args) {
			if (o != null) {
				if (o instanceof Base) {
					try {
						((Base) o).recycle();
					} catch (Throwable t) {
						// who cares?
					}
				}
			}
		}
	}
}

Everything in the above code is stuff that I have explained already but there is a minor change. The empty constructor has no code in it again because there is no way to know at construction time which staff member your looking to get and you can’t pass in any parameter to the constructor so a new public method has been added which contains the code to load in the users information into the variables.

However I’ve just introduced a maintainability issue. I have two classes that pretty much do the exact same thing so in the next part I’ll show how to change the MyInfo class to make use of this class.

Advertisements
Tagged with: ,
Posted in None
One comment on “Getting To The Java Roots of XPages – Part 11
  1. ericharleschmidt@me.com says:

    Declan, thanks for doing all of this!

    Like

Comments are closed.

Archives
%d bloggers like this: