Getting To The Java Roots of XPages – Part 3

So here is the SSJS used in the previous part that gets the current users internet address from the addressbook. This is not an example of best practice for writing ssjs, I have purposely made it long-form so I can explain each line

try {
var nabDB:NotesDatabase = session.getDatabase(database.getServer(),"names.nsf",false);
var nabView:NotesView = nabDB.getView("($Users)");
var nabDoc:NotesDocument = nabView.getDocumentByKey(session.getEffectiveUserName());
return nabDoc.getItemValueString("InternetAddress");
} catch(e) {
print(e);
return "No Nab Doc";
}

There is a try/catch designed to always return a default if there is problem,otherwise it just gets the current session, gets the address book,gets the $Users view, looks up the document in the $Users view and returns the value. Pretty simple stuff and possibly something you have done in lotusscript for client apps, the pattern is the same.

Here is the java in the same over-simplified, expanded format. It looks scary because it looks like it is much longer and has some bits you may not be familiar with yet, but don’t worry I’ll try and explain them.

package scary.java.demo;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;
import com.ibm.xsp.extlib.util.ExtLibUtil;

public class AboutMe {
public String getMyInternetAddress() {
String result = null;
try {
Session session = ExtLibUtil.getCurrentSession();
Database thisDB = session.getCurrentDatabase();
Database nabDB = session.getDatabase(thisDB.getServer(),"names.nsf", false);
View nabView = nabDB.getView("($Users)");
Document nabDoc = nabView.getDocumentByKey(session.getEffectiveUserName());
result = nabDoc.getItemValueString("InternetAddress");
} catch (NotesException e) {
System.out.print(e);
result = "No NAB Doc";
}
return result;
}
}

Really! It is very simple.

I promise!

Ok, lets take it bit by bit.

The first bit is this thing called a package. I’ve named this package as scary.java.demo but in reality you should give your package names good values. The normal way is to reverse your domain name and add some sort of overall grouping name, so this blog runs on qtzar.com so I would name my packages as com.qtzar.groupingname

The next bit is a bunch of import statements. These tell the java compiler what a session or database or view or document consists of. You don’t really have to worry about these. If you use a database in your code the editor will let you know that you need to add the import and will even help out and do it for you ( more on that in another part ). They directly reference things in other packages. So lotus.domino.Database is a reference to something called Document in a package called lotus.domino

The next part is how the thing in the package is given a name. Public means that other things can see it, class just tells it that this is the thing and AboutMe is the name of the thing. If you have access to the source code for the lotus.domino package you would find one of these in there that read ‘Public class Document’ which is what gives a Notes Document a name.

Inside this class is where we can create what are called methods, think of these as the functions in your ssjs library. This particular class only has one method called getMyInternetAddress and it doesn’t accept any parameters.

Inside here is where we have the code that opens the nab, looks for the document and returns the internet email address. it is very similar to the original ssjs. First it gets the current session, then it gets the database, gets the view and gets the document and returns a value. if there is a problem in any stage the try/catch block just returns a default value instead.

The biggest difference between the two is that the java version doesn’t have a ‘var’ statement before each variable and doesn’t use the : objectType notation. Instead the Java line starts with the object type and then the variable name. There are a few other differences but I won’t go into them just yet

You may notice that to get the current session I’m calling a method called getCurrentSession() in an object called ExtLibUtil and if you look at the import statements you can see that this comes from a package called com.ibm.xsp.extlib.util. There are other ways to get the current session but i wanted to give an example of code reuse, somebody else wrote this code so I don’t have to write my own version I can just use theirs.

So that explains how the java code works at a very basic level, in the next part I’ll show you how to actually use the java from your xpage.

Advertisements
Tagged with: ,
Posted in None
One comment on “Getting To The Java Roots of XPages – Part 3
  1. Brian Moore says:

    Little typo, which I only mention since your work is a guide to people getting started. You said:

    So lotus.domino.Database is a reference to something called Document in a package called lotus.domino

    for that import, I think it’s a reference to something called a Database in a package called lotus.domino.

    Document would be lotus.domino.Document.

    Cheers,
    Brian

    Like

Comments are closed.

Archives
%d bloggers like this: