First Steps in the Java Learning Curve

I posted a while back (hrrmp, here lately any post I made is a while back) about boldly going where everyone else has already gone. This post was about my first steps into the Java programming world. Since it’s been a couple of months since then, I’m here to report that while some of this change is a little intimidating (have you looked at the core Java API tree?) there isn’t really that big of a difference in techniques you already know with LotusScript and more specifically Server Side Javascript. So, with this post I’m going to show you a comparison between SSJS and Java just to ease your mind a little bit and hopefully show you that this is something you really should start to learn. Especially if you’re serious about XPage Development.

Let’s start with a simple managed bean that will go get some data and populate a computed text area, much like what you would use a SSJS Script Library for. I’m picking this scenario because this bean will not really be structured like a “traditional class” per-se with getters and setters, but just contain helper methods for a specific task. Our example will contain 1 method just for simplicity and space sake.

So, here’s the SSJS function we’re going to convert to Java, it’s pretty simple and only runs through the items of the document, builds some html and places it in a viewScope variable for display in our computed text areas:

function getSSJSValues() {
	var db:NotesDatabase = session.getCurrentDatabase();
	var view:NotesView = db.getView("Default");
	var doc:NotesDocument = view.getDocumentByKey("SSJSForm");
	if (doc != null) {
		var html = "<div>"
		for (var i=0;i < doc.getItems().size();i++) {
			var ssjsItem:NotesItem = doc.getItems().get(i);
			if (ssjsItem.getName().indexOf("$") == -1) {
				html += ssjsItem.getName() + " = " + ssjsItem.getValueString() + "<br />"
			}
		}
	}else{
		var html = "No Doc Found";
	}
	html += "</div>"
	viewScope.put("SSJSValues",html);
}

Pretty simple right?

And here is the same logic as a Java Bean:

package com.keithstric.demos.firststeps;

import java.util.Map;
import java.util.Vector;
import java.io.Serializable;

import javax.faces.context.FacesContext;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.Item;
import lotus.domino.NotesException;
import lotus.domino.View;

import com.ibm.domino.xsp.module.nsf.NotesContext;

public class BeanValues implements Serializable {
	
	public BeanValues() {
		System.out.println("WOOT!! Got us a BeanValues bean");
	}
	
	public void getBeanValues() {
		try {
			Database db = NotesContext.getCurrent().getCurrentDatabase();
			View view = db.getView("Default");
			Document doc = view.getDocumentByKey("BeanForm");
			String html = "<div>";
			if (doc != null) {
				Vector docItems = doc.getItems();
				for (Object docItemObj : docItems) {
					Item docItem = (Item) docItemObj;
					if (docItem.getName().indexOf("$") == -1) {
						html = html + docItem.getName() + " = " + docItem.getValueString() + "<br />";
					}
				}
			}else{
				html = "No Doc found";
			}
			html = html + "</div>";
			Map viewScope = (Map) getXVariableValue("viewScope");
			viewScope.put("BeanValues", html);
			
		} catch (NotesException e) {
			e.printStackTrace();
		}
	}
	
	private Object getXVariableValue(String varName) {
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getApplication().getVariableResolver().resolveVariable(context, varName);
	}
}

Pretty simple huh? If you’ll notice the getBeanValues method there really isn’t that much of a difference between SSJS and Java. Sure there are a few differences but the base logic is virtually identical.

For the bean to be available for use you’ll also need to added it to the FacesConfig file for the application. In DDE navigate to Windows\Show Ecplise Views\Other, underneath the Java category choose “Package Explorer”. This is something you should keep in your Eclipse perspective and it’s quite useful. Once that opens you should see all the applications that are in your workspace listed there. Choose your application and open that up. Then navigate to the folder “WebContent\WEB-INF” and open the “faces-config.xml” file. There you’ll need to add this:
faces-config.png

The code in the onClick of the buttons to call these methods is:

SSJS – getSSJSValues();
Bean – beanValues.getBeanValues();
Reset – viewScope.remove(“BeanValues”);viewScope.remove(“SSJSValues”);

Not really much difference there.

Now I know you’re saying, but Keith, this is an awful lot of code and effort for something that we did in just a few lines of SSJS. Well, my answer is you’re right, it is. However, by putting it in a Java Class and taking the steps to start learning Java you are setting the stage to do much more with XPage applications. This opens the door for you to be able to manipulate pretty much everything in the XPage or more specifically the JSF runtime, create your own components, change the way things are rendered and have much more control over the way data is manipulated within the application. Learning Java is also an excellent tool to add to your toolbox and makes you much more flexible also. Not to mention, as you start to move more and more of your SSJS into java classes, your XSP markup gets cleaner and cleaner and much easier to read.

If you want to see this demo in action you can do so here. Just keep in mind I didn’t add any styling or try to pretty it up in any way. I hope this post shows you that Java is not something to be feared and avoided but something to start learning and even embrace.

Share This: