Strange problem with cacheddataprovider.

One of the projects I’m working on is huge.

The java server faces I have are multiple tabs, tabs within tabs (oh it goes on) and I came accross a strange problem where although I had made sure the cacheddataprovider was pointing to the right place it seemed to just go to the first record.

        String sMyAccountNo=getSessionBean1().sAccountNo;
        RowKey ask=cachedRowSetDataProvider1.findFirst("Accountno",sMyAccountNo);
        cachedRowSetDataProvider1.setCursorRow(ask);

This code looks up the selected customer and is run in the init() phase of the faces class. However, click more than 3 tabs and the findFirst stops working.

The fix which I’m not too happy about because it creates more overhead is to add a:-

cachedRowSetDataProvider1.refresh();
       

Before we do the findFirst.

Thus the code is

        String sMyAccountNo=getSessionBean1().sAccountNo;
        cachedRowSetDataProvider1.refresh();
        RowKey ask=cachedRowSetDataProvider1.findFirst("Accountno",sMyAccountNo);
        cachedRowSetDataProvider1.setCursorRow(ask);

Ok just a note here before some clever person decides to have a go at me for using text as a primary key – I didn’t build the database and I’d *NEVER* do that. I’ve inherited what is in my opinion one of the worst examples of database structures I’ve ever seen, but the problem is that the organisation that uses it is running 24/7 so I can’t really make the massive changes I would if I had the chance.

Now that refresh thing REALLY bothered me so I changed the code like this:-

        RowKey ask=cachedRowSetDataProvider1.findFirst("Accountno",sMyAccountNo);
        cachedRowSetDataProvider1.setCursorRow(ask);
        if (!cachedRowSetDataProvider1.getValue("AccountNo").equals(sMyAccountNo)) {
            cachedRowSetDataProvider1.refresh();
            ask=cachedRowSetDataProvider1.findFirst("Accountno",sMyAccountNo);
            cachedRowSetDataProvider1.setCursorRow(ask);
            
        }
       

So here we see that I try the find first, if it works – cool – onwards we go, if not then we do the refresh to make sure I do have the right record. Slightly more elegant.

Advertisements

About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on July 10, 2008, in Netbeans and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: