Archives for the ‘Java’ Category

10 Jul 2013
2

XPages Video Tutorial: Signing a Notes Database using Java

In this video tutorial I show you how to sign a Notes Database using the Server’s ID via Java Code.

In Lotus Notes version 6, IBM introduced a new method for the NotesDatabase Object called “sign()”. this allowed you to sign either the current database or another database using your current Notes ID. The only issue with this is that the Sign Request would only work if triggered manually inside the Notes Client.

For XPages development, this becomes an issue because you cannot perform a NotesDatabase.sign() on the front-end of your XPages Application.

So without further adieu, here’s the Video :)

Cheers
John

5 Jun 2013
4

XPages Java Tip: Be careful with static variables in your Classes

I learned a very valuable lesson the other day when I relied on the Java Editor to recommend the setting of certain members in my classes. This is a silly mistake that occurred and only cost me about 1.5 hours of troubleshooting, but hopefully others can learn from this and not make the same mistake.

WHO TO THANK

I need to thank the following 4 individuals for assisting me with this issue:

1. Serdar Başeğmez – (Who pretty much knew what the issue was but I didn’t understand him properly. Sorry man)

2. Solly Bronkhorst - (My senior Domino Developer at Ukuvuma, who is quite new to Java, but managed to point out the issue fairly quickly. That’s mad skills Solly)

3. Thimo Jansen - (For really trying hard to help me out. I really appreciate the effort Thimo)

4. Paul Withers – (For providing me an alternative to the below issue in Java. Thanks man)

5. John Dalsgaard and Nathan T Freeman – (For providing valuable feedback on my Blog Post and helping me correct some of my statements which were quite misleading)

LET ME ADD SOME CONTEXT

In my XPages Applications, my Application Logic is designed around the MVC Architecture. For every Application I usually create a “Global Controller” Java class that initializes certain parts of my application and sets a few members. I access the Global Controller via a managed Bean which I store as a Session Scope Object (Session Scope meaning the Object will remain active for the current Notes Database/Application for the current User only).

Inside this Global Controller, I have a few member properties that get populated on load of the application. (e.g. The current Username, e-mail address, roles, etc). In one of my cases these members needed to be used in my XPages as well as in some of my other Java Classes.

WHAT WAS THE PROBLEM?

While I understood how to get a handle on my Managed Bean via my XPages and Custom Controls, I made an assumption on how to get a handle on it in my other Java Objects. I would import the “Global Controller” into my Java Class and reference its members directly.

Everything worked fine for me when I was testing the application, but the moment there was more than 1 user working in the Application, my Global Controller’s members would be overwritten (i.e the username is not mine, but the 2nd user’s. The same for the roles, e-mail address, etc).

It was as if my Global Controller was being stored as an Application Scope Object and not a Session Scope Object.

WHY DID IT HAPPEN?

When I imported my Global Controller into my other Java Objects and started referencing its member properties, the Java editor started returning errors, telling me that my Global Controller’s members need to be Static. It was here that I allowed the Java Editor to make the necessary changes in my code.

So what does it mean to have a static member? From my understanding, Static members are initialized only once and at the start of the execution. When I launched my XPages Application, my static members would be initialized and would then be stored as values to be used by everyone until they were re-initialized. So if I launched the application first (me as John Jardin), the member properties would be populated with my details, which is correct. If a second user launched the application on her side (Susan Smith), the member properties would be changed and would contain her details. This directly affects me and any user who launched the Application prior to Susan.

RESOLUTION

You cannot import a Java Object into another Java Object and assume to get a handle on it the same way you would in XPages via a Managed Bean. At least this is how I understand it. What you need to do in your Java Object is get a direct handle to your Managed Bean, which contains an Instance of your Global Controller.

Let’s say that your facesconfig.xml has the following code that defines your Managed Bean:

<managed-bean>
    <managed-bean-name>GlobalObject</managed-bean-name>
    <managed-bean-class>com.ukuvuma.designmanager.globals.GlobalObject</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

To use this Managed Bean in your Java Object, you’ll need the following code:

FacesContext context = FacesContext.getCurrentInstance();
GlobalObject globals = (GlobalObject) context.getApplication().getVariableResolver().resolveVariable(context,"GlobalObject");

Here you created a variable called “globals” and connected it to your GlobalObject Managed Bean. Now you can continue working with your Global Object in your Java Class. Simple as that.

Till next time. Cheers
John
28 Aug 2012
3

Important Tip when comparing values in Java

Hi everyone. Here’s a quick post that might save you a lot of time. With some programming languages like JavaScript, @Formula or LotusScript, to compare a String value is pretty simple

@Formula: tmp1 := “Test”;

tmp2 := “Test”;

(tmp1 = tmp2) = True


JavaScript:var tmp1 = “Test”;

var tmp2 = “Test”;

(tmp1 == tmp2) = True

 

LotusScript:dim tmp1 as String

dim tmp2 as Stringtmp1 = “Test”

tmp2 = “Test”

if(tmp1 = tmp2) = True

 

But when it comes to Java, the rules are slightly different:

String tmp1 = “Test”;

String tmp2 = “Other Test”;

(tmp1 == tmp2) = True

 

In Java, the == operator compares Objects and not the physical values contained within. The line above this is checking if the object tmp1 is the same object as tmp2.

So, in Java, the proper way to compare values is by using the equals() method:

String tmp1 = “Test”;

String tmp2 = “Test”;

(tmp1.equals(tmp2) = True

 

If it’s anything that I can teach you today, it’s that with Java almost everything is an Object and not just a data type. If it’s an Object, it has Properties and Methods that should be considered at all times, especially when parsing or comparing values.

Cheers for now :)

John

25 Jul 2012
9

XPages Tip: Determine the Object Type of your Scoped Variable in Java

Hi All. I’m still relatively new to Java, so I landed up in a scenario where I thought my object was a Vector, but it was actually a String Object.

This cost me some time as I was trying to pass the Scoped Variable into one of my existing Vector Objects. I had checks to see if the Scoped Variable was null or blank, but I needed a check to see if it was in fact a Vector or just a String.

It’s the old problem of if you only have 1 value, then it’s a String, and when you start having more than 1, it’s an Array.

So in Java, when faced with this problem, you can use the .getClass() and .getName() method to check what you actually have.

The example below shows how I used this. I’m sure there’s a much better ways of doing this check, but for now, it works.

Cheers all.

 

EXAMPLE:

Vector myarray = new Vector();

if (viewScope.get(“ScopeName”).getClass().getName().equals(“java.lang.String”))

     //It’s a String value, not an Array

     myarray.add(viewScope.get(“ScopeName”).toString());

} else {

     //It’s an Array. Yippee!!

     myarray = (Vector) viewScope.get(“ScopeName”);

}

UPDATE NOTE:

Thanks to Tommy Valand, below is another example of how to achieve this (See Red Text below). It’s also noted that the below method is preferred to what I show above.

Thanks Tommy for your feedback.

 

Vector myarray = new Vector();

if (viewScope.get(“ScopeName”) instanceof String)

     //It’s a String value, not an Array

     myarray.add(viewScope.get(“ScopeName”).toString());

} else {

     //It’s an Array. Yippee!!

     myarray = (Vector) viewScope.get(“ScopeName”);

}

28 Oct 2011
0

Xpages Video Tutorial – What Domino Designer is not telling you

Hi everyone. I’ve been recently blessed with some awesome information that I have to re-distribute. Last week I worked on an Xpage that made use of the DynamicViewPanel Custom Control that ships with the 852 XPages Extension Library. I needed to expand on certain functionality that just didn’t seem available, until I had a small chat with Mr Paul Withers.

He showed me how to access Properties and Methods of Java Objects that make up what we know as Xpages and Custom Controls. This allowed me in my Xpages, to get a handle on whichever Custom Control, and execute methods I didn’t know were there. You can imagine the possibilities.

A Quick Example:

Let’s say I have an Expand/Collapse Pager on my Xpage, and it’s bound to a ViewPanel. What if I wanted to expand, collapse the View Data based on certain Xpage Events, instead of manually clicking on the Expand/Collapse Pager. Thanks to a certain Eclipse View, and a button called “F4“, I  can view all the Properties and Methods that make up the Expand/Collapse Pager, (two of them been “expandAll” and “collapseAll“), and manipulate the View Panel Data during an onPageLoad or onClick event, instead of clicking on the actual Expand/Collapse Pager.

Now that’s what I call flexibility. In the embedded video below, which is published through David Leedy’s NotesIn9 Video Series, Mr Paul Withers will show you how to access these Properties and Methods.

IMPORTANT NOTE: To access these Properties and Methods, please make sure your Application is built. You do this by clicking on the “ProjectBuild Project” Menu.

One Final Note: Paul Withers, Paul Hannan, Declan Sciolla-Lynch, Jeremy Hodge and Tim Tripcony, are busy working on a book called “XPages Extension Library: A step-by-step guide to the next generation of XPages components“. I’m positive that many out there are waiting very impatiently for this to be released.

I hope you all found this Post valuable. Be sure to check out NotesIn9.com for more great video tutorials. Also check out Paul’s Blog.