logo

Using XML To Prevent Conflicts

As a follow-up to Monday's post about preventing save conflicts, here's a solution I cooked up. All you need is an XML Data Island, some JavaScript and a view (sorted by Doc UNID, which contains a column that shows the last modified date). The basic JavaScript involved is as follows:

var sDBPath = '/apps/noflicts.nsf';
var sUNID = 'B8CB8EA9C2F0934480256F65003F136A';
var sLastModified = '12/09/2004 11:29:00 AM';

var xIsland = document.getElementById( "XMLIsland" );

xIsland.load( "/" + sDBPath + "/allbyunid?ReadViewEntries&StartKey=" + sUNID + "&count=1" );

var node = xIsland.documentElement.selectSingleNode( "//entrydata[@name = 'Modified']/text" );

if ( sLastModified != node.text ) {
 alert( "Since you opened this document it has been saved by somebody else." );
}

This code dynamically fetches the updated XML from the view, restricting it to the document you're editing. It then uses an XPath query to check the values of the last modified date from the right column. If they're different the user is alerted.

IE users: You can test it in the simple demo I put together. Create a new document. After saving it, press the Edit button and then press Ctrl+N. This gives you two "copies" of the document, both in edit mode. Save one of the documents before saving the one in the other window. In the second window you should get prompted. All the code you need is in the view-source for the page!

This is a little simplistic. What should we do in this situation? If the user has spent ten minutes making changes they are going to want to save them!

I'm lucky in that the problem I was solving didn't involve any document changes. It was simply a user pressing a button to assign a call to them. Now it alerts them that somebody else already has it assigned to them. In the real world I don't know what solution there is to conflicts, or if there is one at all. It all depends on the scenario.

If nothing else it's a good example of how you can do "DBLookups" using JavaScript and XML. Should feed your imagination a little.

Comments

  1. So does this work in firefox? I'm guessing not based on the security hole the dynamic src attribute on script tags poses in IE, I would expect other things like this to be 'closed' in Firefox.

    Here's a brief outline of the scheme I used: document locking.

    Using a hidden frame (yep, not a good thing) I used a javascript timer to set the src for that fram to an agent that would time stamp the document being edited every 30 seconds or so, begining when the document was put into edit mode. This displayed text on the screen, Document locked for editing by you, [name].

    Other people trying to edit the document could IF the time stamp was more than 45 seconds old, meaning a fresh stamp was not present. Obviously, this isn't goign to be suited for a high-activity environment, but, the person doing the work was assured their work was not in vain.

  2. Very impressive Jake!

    I've been doing something similar, where say you enter an account number and the Name is retrieved without having to submit to the server.

    However this has been done using a hidden iframe requiring a form be created to take a parameter (account number), do a dblookup then use JavaScript to write the value back to the parent form.

    This gets rid of having to do all that.

    Nice one!!!

    • avatar
    • Jake
    • Thu 9 Dec 2004 09:17

    Jerry. As it stands it won't work in Mozilla. That's not to say it can't though. It's just that it's easier to code it for IE and I'm feeling lazy ;-)

    Dorian. Glad you like it. It's a really versatile trick and has many, many uses. You'll probably find yourself tailoring it to many different problems!

  3. Yep... but, as Dorian says, Nice one. I hadn't even heard of an XML Data Island before today... or if I had, it's among the many things that fall out of my brain... like manners enough to be congratulatory before critical. :-) Cool technique.

    • avatar
    • Jas
    • Thu 9 Dec 2004 10:44

    I have used XML to dblookups in the past using xml and javascripts. My solution were more to avoid refreshing the page (flicker, hit the server and refresh the field values) to get/update values in a field/drop down box depending on values from a field/drop down.

    This is surely another way of looking to update users (with js alert) while the documents are being update by user. Nice one Jake.

  4. I did a little "proof of concept" a while back that I was intending to write up as an article but never did. I used the XMLHTTP object, which is cross-browser compatible - with some minor differences of course, to make a browser based Notes View. It could page around, search (equivalent of the functionality in Notes client when you just start typing to search) all without reloading the page. The XMLHTTP object was just doing fetches via ?ReadViewEntries as required.

    When I get back from the 6 week holiday I'm about to take I may get around to finishing my tech blog site and i'll write it up then.

    • avatar
    • Blop
    • Fri 10 Dec 2004 03:42

    U hirdd (oops, wrong keys!)

    I guess if the user is going to do a lot of work in order to make their changes, this warrrants some kind of check-in/out mechanism. Otherwise you get into checking for changes on a field-by-field basis to see what changes you can apply.

    There might also be semantics that apply to a group of fields, so you're going to have to apply some intelligence around that ... ugh!

    It's a complex problem.

    Has anyone else done this kind of thing before?

    • avatar
    • Dave
    • Wed 19 Jan 2005 15:17

    How do you set up the XML Data Island? Is there something I need to add to the Notes form for this to work?

    I reviewed your example and it works great! Is it possible to get a copy of the notes database to view the design code?

    Thanks!

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Thu 9 Dec 2004

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content