If I wanted to add some more methods to my class to get additional details it could be as easy as duplicating the code I already have and end up with something like this for each method.
public String getMyIFirstName() { if (myFirstName == null) { Session session = null; Database thisDB = null; Database nabDB = null; View nabView = null; Document nabDoc = null; Name tmpName = null; try { session = ExtLibUtil.getCurrentSession(); thisDB = session.getCurrentDatabase(); nabDB = session.getDatabase(thisDB.getServer(),"names.nsf", false); nabView = nabDB.getView("($Users)"); nabDoc = nabView.getDocumentByKey(session.getEffectiveUserName()); tmpName = session.createName(nabDoc.getItemValueString("FullName")); myFirstname = tmpName.getGivenName(); } catch (NotesException e) { System.out.print(e); } finally { recycleDominoObjects(tmpName,nabDoc,nabView,nabDB,thisDB,session); } } return myFirstName; }
After creating about 2 methods like this you will start to see how inefficient it is, first of all your duplicating code over and over for each method which will be a maintenance nightmare, secondly you are hitting the nab and doing a document lookup each time the method is being called for the first time.
By reexamining the concept behind what is trying to be achieved you may come to the conclusion that it would be better if you only hit the nab once, loaded all the details and then your individual methods would use the preloaded data.
Remember that Empty Constructor we had
public AboutMe() { // Empty Constructor }
Well that gets called the first time the bean is used so it would be the perfect place to load all the variables. So after moving some code around the constructor now looks like this
public AboutMe() { 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(session.getEffectiveUserName()); nabName = session.createName(nabDoc.getItemValueString("FullName")); myInternetAddress = nabDoc.getItemValueString("InternetAddress"); myCommonName = nabName.getCommon(); myFirstName = nabName.getGiven(); myLastName = nabName.getSurname(); myLocation = nabDoc.getItemValueString("Location"); } catch (NotesException e) { System.out.print(e); } finally { recycleDominoObjects(nabName,nabDoc, nabView, nabDB, thisDB, session); } }
and the methods have been simplified to look like this
public String getMyInternetAddress() { return myInternetAddress; } public String getMyCommonName() { return myCommonName; } public String getMyFirstName() { return myFirstName; } public String getMyLastName() { return myLastName; } public String getMyLocation() { return myLocation; }
So now when the bean is called the first time it will access the nab, find the person doc and load up all the variables and the methods will just return whatever is stored in the variable. This is a lot easier to maintain, if you need to change the lookup database location you just do it in one place and adding additional methods is easier also as there is less duplicated code.
You may also have noticed that I didn’t have to make any changes on the xpage side of things. It is still calling the same bean and method as before. This is a great example of how the UI is separate to the code behind it.
in the next part I’m going to change it all again and make this even more generic and show you a powerful java feature called extending.