Getting To The Java Roots of XPages – Part 12

So following on from the last part I now have two packages in the scary.java.demo package, StaffInfo and AboutMe. Both of these classes do pretty much the exact same thing, AboutMe goes to the nab and gets your information while StaffInfo goes to the nab and gets the info for the selected person. As mentioned this introduces a maintenance issue, if the place you lookup staff info changes you will need to change two classes, wouldn’t it be better if you just needed to change one.

You can do this in numerous ways and for this particular way I’m going to simply reference the StaffInfo class from within the AboutMe class. As a result the AboutMe class becomes a lot simpler. Here is the full AboutMe class now after the changes.

package scary.java.demo;

import java.io.Serializable;
import lotus.domino.NotesException;
import lotus.domino.Session;
import com.ibm.xsp.extlib.util.ExtLibUtil;

public class AboutMe implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private StaffInfo myInfo = new StaffInfo();

	public AboutMe() {
	    Session session = ExtLibUtil.getCurrentSession();
	    try {
		myInfo.load(session.getEffectiveUserName());
	    } catch (NotesException e) {
		e.printStackTrace();
	    }
	}

	public String getMyInternetAddress() {
	    return myInfo.getInternetAddress();
	}

	public String getMyCommonName() {
	    return myInfo.getCommonName();
	}

	public String getMyFirstName() {
	    return myInfo.getFirstName();
	}

	public String getMyLastName() {
	    return myInfo.getLastName();
	}

	public String getMyLocation() {
	    return myInfo.getLocation();
	}

}

You will notice that I don’t have individual variables to store the current users information anymore, they have all been replaced with a single variable called myInfo that has a type of ‘StaffInfo’. It was initialized by calling the empty constructor when I was declaring it.

So I have a nice new empty variable called myInfo, next I need to load it up with the current users information. Well my variable has all the public methods that StaffInfo provides so all I need to do it call it’s load() method and pass in the current users name. Like with the previous AboutMe class I’m going to do this directly in the constructor. I get the Domino session and then call myInfo.load() passing in the current user name.

Now the variable myInfo has been loaded with all the relevant user information so my methods in the AboutMe class can just reference them directly by calling the methods in the myInfo variable. So you get the current users commonName I can just call myInfo.getCommonName()

I still haven’t changed anything on the XPage, my bean is still being created the exact same way and the Expression Language is still the same but the business logic code in the backend has changed significantly to be more efficient and easier to maintain in the long run. This java stuff is not so scary.

Advertisements
Tagged with: ,
Posted in None
Archives
%d bloggers like this: