Tuesday, January 26, 2010

XPages: Custom Control that can help prevent save conflicts

Today, I share with you a demoapp with a basic "has document been modified validation" (in lack of a better description) custom control, ccValidateModified.

Just pop it into an XPage with a document data source, and it should in theory work out of the box. Plug and play.

This is by no means an absolute protection against save conflicts. For applications that it's critical to avoid save conflicts, you need better protection.

Below is a movie of the control in action. A document is open in Opera, Firefox and Internet Explorer. When the document is saved in one of the browsers, the "other browsers" gets a dialog saying that the document has been modified.

(Click the play-icon to start. Broadband recommended.)

The way it works is that a scoped variable with the modified-time from the stored doc is set on page load (editmode).

When the window gets focus/every two seconds (setInterval), a partial refresh is initiated. If the doc has been modified since the document was opened for editing, a hidden image is loaded. The image has an onload event (easiest way to make it cross browser) -> show a modal dojo dialog.

>> Download demoapp with custom control

Share and enjoy!


Erik Brooks said...

Awesome! Thanks for sharing these little nuggets you're working on.

Tommy Valand said...

No problem! Hope you do the same.. ;)

Rakesh said...

Great tip.
Can you suggest the best way to prevent save conflicts for critical apps, especially if they are being access using the client and web at the same time.

Tommy Valand said...

I suppose this is my best tip so far. :)

You can experiment with document locking. I haven't tried it out as it seems overly complicated.

Anonymous said...

Thanks so much - this is exactly what I needed! However, I'm getting an error on the beforeRenderResponse code:

HTTP JVM: com.ibm.xsp.exception.EvaluationExceptionEx: Error while executing JavaScript action expression

Do I have to do something for the Calendar statements to work?

Anonymous said...

sorry - The error is actually

'currentdocument' not found

Tommy Valand said...

If currentDocument isn't found, that suggests you're using the control on on an XPage without a document data source..?

Tommy Valand said...

If there is a document data source/it is bound to a panel, you probably will have to put the control inside the panel.

Anonymous said...

My data source is bound to a custom control, which is then used on an xpage. I embedded your custom control at the bottom of mine. It's not in a panel, just the last thing on my custom control.

Tommy Valand said...

Hm.. Strange.. Do you get the same result if you put the conflict notifier inside the XPage instead of the custom control with the data source?

Anonymous said...

When I put your control on the xpage, the page won't even open. It gives the same error - but this time directly in the browser from the afterPageLoad event. When I had it in my control, the page would open fine, but the error would appear in the server log. It didn't come from the afterPageLoad, though, it was from the beforeRenderResponse.

When I put it on the xpage, I tried within the same panel as my control and also outside of it - same result.

Thank you for your time, Mr. Valand, this will be an important feature of my app when we get it working.

Tommy Valand said...

If you could create a simplified demo of your XPage/control, I could take a look at it.

It's hard to know whats wrong without having something to work with.

Anonymous said...

I created a test xpage with my document data source, one field and the validate subform - no error. The subform giving me trouble is a rather extensive Engineering Change input form with 8 tabs of input fields and views. I had the same trouble with a simple comments subform I created, and ended up just copying the elements off the comments subform and putting them directly into the EC form itself.

I don't think I can do that here, though. I'm stumped. If it helps, here are the last few lines of my EC subform:

Anonymous said...



Tommy Valand said...

It's probably easier if we do this by mail, as blogger is quite hungry when it comes to < and >

tommyv at hotmail dot com

Anonymous said...

Hi Tommy,
Thanks for the custom control. It works well on validation. But I found that it still saves a response document. How can I prevent this?
Thanks in advance.

Anonymous said...

Hi Tommy,
Regarding the above comment, it was solved when I changed the save button from a 'Submit' to 'Button' and added a save() in the script.
Another issue: This doesn't work well on a custom control, when there are 2 data sources. I tried to contain data to resp. panels. But still doesn't work. Thanks.. Thomas.

Vijay O said...

This is the great . this will save lot time to me. I am also getting the same error 'currentDocument' not found in before render response event. My data Source name is document. I defined my data source for entire page. I put your custom control in another custom control which already included in my xpage. is there any other thing I am missing? please help .

Vijay O said...

I changed currentDocument to document. now i am getting document not found. can't we get document handle in beforerenderresponse event?

Tommy Valand said...

Sounds strange..

When a document data source is bound to the XPage, it should be available in all events except maybe beforePageLoad.

I haven't had issues with getting reference to document data sources in custom controls, but it might be related to the level of nesting in the custom control.

Strange things can also happen when code crashes/does not trigger crashing of the page.