Forget about the time when comparing dates in SSJS

I needed to compare two dates in SSJS and determine if a date was on or before tomorrow. Sounds pretty easy but it took me a while to figure this out. The hard part was that I needed to remove the time part, I only wanted to compare the dates.

As always (or most of the times) the solution turned out to be pretty simple and involved using the @Date() function. So if you ever need to do the same, just use the following (note that all dates are java.util.Date instances, not NotesDateTime objects).

//set an input date
//for this example the date is set to June 1st, 2011
//remember: in Java(script), a month is represented by an integer from 0 to 11;
//0 is January, 1 is February, and so forth; thus 11 is December
var inputDate = new Date();
inputDate.setFullYear(2011, 5, 1);

//set target date
//you can also use @Date( @Adjust(@Now(), 0,0,1,0,0,0) )
var targetDate = @Date( @Tomorrow() );

//check if the specified date is before or on the target date
var result = (@Date(inputDate).before( targetDate ) || 
    @Date(inputDate).equals(targetDate));

Error while using sessionAsSigner calls

Yesterday, apparently for no reason, I started getting all errors in my XPage application. Investigating it more I found out that the root cause was a problem with the sessionAsSigner function. The error I was getting was:

‘sessionAsSigner’ not found

So I started thinking what changed in the last week and might have caused this error. I made no configuration changes and performed no updates. The only thing I changed was that I started using the server with another user ID. I looked into the security and authentication settings and found absolutely no differences between the user ID’s.

After some more investigating I stumbled across a tweet from a user called HinkyMinky:

I resigned the database with my new user ID and all problems were solved!

Putting your Xpage to sleep

I’m currently working on rewriting my multiple-file uploader using Xpages/ custom controls. I encountered a problem when embedding the control on an Xpage based form. When the “save” button on the form is called, the uploads are started but I needed the Xpage to wait until all files had been uploaded.

My first try was to implement a sleep function in Javascript, but the only way this seemed to work was using a while loop. The big problem with that is that it eats up all CPU cycles and “locks” your browser. Not a good solution.

One of the articles I read mentioned the Java sleep method. This gave me the idea of implementing the wait-until function on the Xpage itself.

In the querySaveDocument event of the datasource on the Xpage I make a (conditional) call to the Java sleep method:

while (!condition) {

 java.lang.Thread.currentThread().sleep(1000);
 //re-check condition here

}

This causes the Xpage to wait for (in this case) 1 second (1000 milliseconds) until it continues to save the document.

To make sure that the script doesn’t go in an infinite loop I have included a timeout period:

var startTime = new Date().getTime();
var elapsedTime = 0;
var timeOutSeconds = 60;

while (!condition && elapsedTime < timeOutSeconds) {

 java.lang.Thread.currentThread().sleep(1000);
 //re-check condition here

 elapsedTime = (new Date().getTime() - startTime) / 1024;

}