Skip to main content link. Accesskey S
  • Help
  • HCL Logo
  • HCL Notes and Domino Application Development wiki
  • THIS WIKI IS READ-ONLY. Individual names altered for privacy purposes.
  • HCL Forums and Blogs
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • API Documentation
Search
Community Articles > Developing Applications > Developing XPage Applications > XPage Samples > Converting Java sample code to JavaScript code
  • Share Show Menu▼
  • Subscribe Show Menu▼

Recent articles by this author

NotesName sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesName. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesMIMEEntity sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesMIMEEntity. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntryCollection sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntryCollection. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewNavigator sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewNavigator. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntry sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntry. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
Community articleConverting Java sample code to JavaScript code
Added by ~Elizabeth Umkroskiettu | Edited by IBM contributor~Howard Nonrepulikle on January 20, 2010 | Version 2
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
No abstract provided.
Tags: XPages, sample code, JavaScript, Java
The help for the JavaScript Domino Objects library for 8.5.1 has sample code for a small number of topics but not most. The next major release of Domino Designer should see improvements.

Meanwhile, you can interpolate by copying and adjusting the corresponding Java sample code. The JavaScript code is simpler than the Java.

Let's say you are interested in the getBlue method (also known as the Blue property) of NotesColorObject. In the help, search for getBlue or drill down to Lotus Domino Designer Basic User Guide and Reference > Java/CORBA Classes > Java Classes A-Z > ColorObject class > Blue property and select the example. Notice that the Java class name is ColorObject while the JavaScript class name is NotesColorObject like LotusScript.

The sample Java code for getBlue looks like this:


import lotus.domino.*;

public class JavaAgent extends AgentBase {

  public void NotesMain() {

    try {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();

      // (Your code goes here) 
      Database db = agentContext.getCurrentDatabase();
      View view = db.getView("Main View");
      ColorObject color = session.createColorObject();
      color.setNotesColor(view.getBackgroundColor());
      System.out.println(
      "Blue value of Main View background = " + color.getBlue());

    } catch(Exception e) {

      e.printStackTrace();
    }
  }
}

First strip everything through the try clause. You need no import or class statements and the try clause is not required (more on the try clause below). The code now looks like this.


Database db = agentContext.getCurrentDatabase();
View view = db.getView("Main View");
ColorObject color = session.createColorObject();
color.setNotesColor(view.getBackgroundColor());
System.out.println(
"Blue value of Main View background = " + color.getBlue());

You do not need the first line of the revised code nor is it legal. XPages JavaScript does not run in the context of an agent. Delete this line. Likewise if the sample Java code is running in the context of an application, servlet, or applet, delete the code associated with the context.

How do you get the current database? XPages JavaScript provides a global variable named database that represents the current database. It’s as simple as that. Use this variable on the new first line. Also on this line, JavaScript typing is implicit so replace View with var (or omit it). The code now looks like this:


var view = database.getView("Main View");
ColorObject color = session.createColorObject();
color.setNotesColor(view.getBackgroundColor());
System.out.println(
"Blue value of Main View background = " + color.getBlue());

You can comment a variable by following it with a colon and a type name. The first line may be rewritten:


var view:NotesView = database.getView("Main View");

Likewise the next line uses var in place of ColorObject. The session syntax works as is although the semantics are quite different. In the Java code, session is the name of a variable returned by getSession. In the JavaScript code, session is a global variable that means the current session. Only because the Java variable has the name session does it not have to be changed. The code now looks like this:


var view = database.getView("Main View");
var color = session.createColorObject();
color.setNotesColor(view.getBackgroundColor());
System.out.println(
"Blue value of Main View background = " + color.getBlue());

Again you could comment the variable as color:NotesColorObject. Remember it is only a comment - you could just as well specify color:Foo.

The next line needs no changes since view and color are set, and the syntax is the same for JavaScript as for Java.

The final line needs to be adjusted. JavaScript does not recognize the System object. If you want to send the return value of getBlue() to the console, the following code works:


var view = database.getView("Main View");
var color = session.createColorObject();
color.setNotesColor(view.getBackgroundColor());
print ("Blue value of Main View background = " + color.getBlue());

However, if the JavaScript is for the value of a control, you would return it as follows (the return keyword can be omitted):


var view = database.getView("Main View");
var color = session.createColorObject();
color.setNotesColor(view.getBackgroundColor());
return "Blue value of Main View background = " + color.getBlue());

You might also assign the value to a scoped variable or otherwise apply the return value of getBlue.

If you want to catch any errors, you can put in a try clause as shown below. This example also omits the semi-colons which is legal in JavaScript when statements are on their own lines.


try {
	var view = database.getView("Main View")
	var color = session.createColorObject()
	color.setNotesColor(view.getBackgroundColor())
	return "Blue value of Main View background = " + color.getBlue())
} catch (e) {
	return e.message
}

Here is sample JavaScript for a method that returns a list. A list is of type java.util.Vector which can return an iterator object. The iterator object has two simple methods for processing the list as shown.


var agents = database.getAgents().iterator();
var list = "";
while (agents.hasNext()) {
	list = list + agents.next().getName() + "\\n";
}
return list

Finally here is how to iterate through a getFirst … getNext sequence that terminates with null. Also in this example, notice that (through release 8.5.1) you have to define constants such as DATABASE.


var DATABASE = 1247;
var out = "";
var dir = session.getDbDirectory("");
var db = dir.getFirstDatabase(DATABASE);
while (db != null) {
	out = out + db.getFileName() + "\\n";
	db = dir.getNextDatabase();
}
return out;

  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (2)
collapsed Versions (2)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (2)Jan 20, 2010, 6:49:20 PM~Howard Nonrepulikle  IBM contributor
1Jan 20, 2010, 3:56:35 PM~Elizabeth Umvelusteretsi  IBM contributor
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL
  • Privacy
  • Accessibility