Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified
Accepted Solution

Can persistent data thats backed up be editing on PC

[ Edited ]

Working on same project and got it to backup the persistent data. This is fine for restoring the database but i was hope to be able to edit the data and possibly add more data to it then restore it onto the device.

 

Q1: Am i using the right method to accomplish this?

 

I followed the example in chapert 9 of BlackBerry Java Development Environment version 4.1.0.

 

Q2: I ran into a problem.backing up my persistent data is only setup for one of my stores. How can i setup my backup so that both stores are backed up?

 

Thanks.

Message Edited by ipcTony on 09-20-2008 10:04 PM
Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

I'm not entirely sure of your objecitves and I have no idea what tools are available for a connected phone as

I'm afraid to load the utilities on my win 2k install and my USB connector is on the back of my computer. LOL.

 

However, anything can be edited on a PC but presumably you would want an app to do it in a consistent way.

 

I'm importing/exporting persistent data with HTTP for OTA "backup" and presumably you could use the XML facilities if you

like being verbose. My data happens to be text so it doesn't need a lot of reformating for serialization but personally

I wouldn't see a lot of benefit to a teathered -nly approach unless you want to automate everything under

PC control for a comprehensive backup that may include many apps but it sounds like you have one database

that you backup/edit manually.

 

 

Please use plain text.
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

When you say backup via HTTP or OTA and using XML, it sounds like I would need access to some kind of web server. I don’t have a server or website that I can use to put up any type of data and since this is just a demo app and my first real Blackberry project I thought I use resources that were readily available to me. However I do want to learn how to use the OTA sync in the future just right now it’s a mute point.

 

But regarding the USB backup I have a question.

 

I was able to backup multiple tables to the PC but I’m having problem restoring them back on the device. Only the data from the first table is making its way back.

 

This is the code I’m using:

 

 

public SyncObject convert(DataBuffer data, int version, int UID) { try { Order or = new Order(UID); Items it = new Items(UID); while(data.available() > 0) { int length = data.readShort(); byte[] bytes = new byte[length]; switch (data.readByte()) { case FIELDTAG_ORDERNO: data.readFully(bytes); or.setElement(Order.ORDERNO, new String(bytes).trim()); break; case FIELDTAG_SERVER: data.readFully(bytes); or.setElement(Order.SERVER, new String(bytes).trim()); break; case FIELDTAG_TABLE: data.readFully(bytes); or.setElement(Order.TABLE, new String(bytes).trim()); break; case FIELDTAG_ITEM: data.readFully(bytes); it.setElement(Items.ITEM, new String(bytes).trim()); break; case FIELDTAG_PRICE: data.readFully(bytes); it.setElement(Items.PRICE, new String(bytes).trim()); break; default: data.readFully(bytes); //email not supported break; } } return or; } catch (EOFException e) { System.err.println(e.toString()); } return null;

 

I can't seem to figure out how to check if the UID is for the Order table or the Items table. and I'm not sure if i'm using the proper method for multiple tables. I tried various getUID() but failed to compile.

 

Please use plain text.
Administrator
MSohm
Posts: 14,277
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook
My Carrier: Bell

Re: Can persistent data thats backed up be editing on PC

The logic for the 2 convert methods should be similar.  One will convert the data and send it to Desktop Manager, the second will read it from Desktop Manager and write it back to the BlackBerry handheld.  Therefore how you read data in depends on how it was backed up in the first place.

 

When you say table, do you mean FIELDTAG_TABLE or a table that is shown under Desktop Manager's advanced Backup/Restore screen?

 

I recommend adding some System.out.println statements within your convert methods to verify that the data is arriving as you expect and that no exceptions are being thrown.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

[ Edited ]

Hi Mark,

 

Thanks for responding.

 

I spent the better part of the weekend working on this persistent data backup.
I’m able to backup two (tables) I call them tables from my DB days with Access but they are 2 separate persistent objects with a handful of fields in each.

I spent hours stepping through using debugger to see what was happening over the weekend so I have a better understanding of both Read and Write methods of the SyncConverter.
 
Data from both objects are being saved in the IPD file and using a hex editor I can see all the test data and fields.

The problem that remains is how to update device multiple persistent objects with the SyncConverter Write method. It seems that first object gets written perfectly but the second object if empty or corrupted.

When I step through the SyncConverter’s Write method the (UID) is always 0x000000
So I assume data written is all part of object 0x000000 but towards the end of the data stream I see data for my second persistent object.

I just need to know how to correctly extract data for both objects and write them in their correct vector before the endTransaction() method is called and updates both object. I've search both support sites and your developer site but can't find any examples or suggestions.

 

any help you can provide will be appriciated...

 

 

Order or = new Order(UID); //Object #1
Items it = new Items(UID); //Object #2

while(data.available() > 0)
{
int length = data.readShort();
byte[] bytes = new byte[length];
switch (data.readByte())
{
// Object #1
case FIELDTAG_ORDERNO:
data.readFully(bytes);
or.setElement(Order.ORDERNO, new String(bytes).trim());
break;
// Object #1
case FIELDTAG_SERVER:
data.readFully(bytes);
or.setElement(Order.SERVER, new String(bytes).trim());
break;
// Object #1
case FIELDTAG_TABLE:
data.readFully(bytes);
or.setElement(Order.TABLE, new String(bytes).trim());
break;

 

 

// Object #2
case FIELDTAG_ITEM:
data.readFully(bytes);
it.setElement(Items.ITEM, new String(bytes).trim());
break;
// Object #2
case FIELDTAG_PRICE:
data.readFully(bytes);
it.setElement(Items.PRICE, new String(bytes).trim());
break;
default:
data.readFully(bytes);
//email not supported
break;
}
}
return or; // I don't know how to return both (or) + (it) objects????

 

 Made some progress.

 

I found out in that I needed to hard code a UID for each of the persistent objects. Once is did this I can manage theSyncObject converter (Write) method.

 

   public SyncObject convert(DataBuffer data, int version, int UID)
    {
        if ( UID == 0x00000001 )
        {

              do write Object #1

              upDateObject#1 = true;

              return object #1;

        }

       else if  ( UID == 0x00000002 )

        {

             do write Object #2

             upDateObject#2 = true;

             return object #2;

         }

   }

 

 Then at the end of the write process the [endTransaction()] is called:

 

    public void endTransaction()
    {      
        if ( upDateObject#1 == true )   
        {               
        storeOrders.setContents(_orders);
        storeOrders.commit();
        }
       
        if ( upDateObject#2 == true )   
        {               
        storeOrdersItems.setContents(_items);
        storeOrdersItems.commit();
        }
    }
 

 

Both objects are udated but still data in the Object#2 is currupted.

 

Any ideas?

 

 

 

Message Edited by ipcTony on 09-22-2008 03:33 PM
Please use plain text.
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

I added some code to view what was written from the SyncObject Write method and i get

an [java.lang.ClassCastException] exception at the line [Items i = (Items)_OrderItemsVector.elementAt(e);]

 

        try
        {
        // Attempt to retrieve a record from persistent store
        synchronized(storeOrdersItems)
        {
            Vector _OrderItemsVector = (Vector)storeOrdersItems.getContents();                                     
            if (!_OrderItemsVector.isEmpty())
            {
                // For performance, it is not recommended to enumerate the records
                for (int e=0; e<_OrderItemsVector.size(); e++)
                {
                    Items i = (Items)_OrderItemsVector.elementAt(e);
                    if (i.getElement(Items.ORDERNO).equals(_OrderNo))
                    {
                    String temp1 = (i.getElement(Items.ORDERNO));
                    String temp2 = (i.getElement(Items.ITEM));
                    String temp3 = (i.getElement(Items.PRICE));
                    String temp4 = (i.getElement(Items.QUANTITY));
                    }
                 }
             }                                                                                                                                                             
        }
        }                   
        catch( Exception e)
            { System.out.println(e); }  

 

What does this java.lang.ClassCastException mean after trying to restore persistent data using SyncObject Write?

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

print out the class of the object you are trying to cast and it should be clear :smileyhappy:

 

Please use plain text.
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

Well i'm sure I don't understand what you mean by printing out the class. "you have to excuse me I'm not a seasoned java or BlackBerry programmer". I'm not even a software engineer. in any case not a good excuse since I made choice to work on this project.

 

I don't understand why the same piece of code that worked before i use the sync restore to display the data does not work after i restore the data. When i walk through with that last piece of code i see all the data using the debugger I just can't read it into my listfield like it did prior to the sync restore because of the ClassCastException.

 

if you could elaberate for me on printing the class i'd be most grateful. I tried println(Object x)  and getClass but that did not show me anything useful to figure out this problem.

 

 

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

[ can someone who understands why we need Html posts and not just text explain to me why "tab" and backspace navigates

you to another page and not just within this icongraphic world? ???? ]

 

At minimum you need to understand hierararchies and what a cast is- you can try to search on the classcastexception term

confined to sun.com as one good place to start ( they have great java tutorials and your question relates to java).

 

Anyway, after navigating back here and hoping the eidot r comes back after each keystroke, let me see what is on my clipboard.

 

If you take your unknown x and do something like this,


        String nm=x.getClass().getName(); 

the name should give you some idea what you have- in all liklihood it isn't what you expect but in any case you should

be able to determine if (x instanceof someclass) is true or false.  

 

With situations where you can't know apriori what you have ( not usually a good thing), you can invoke various trees using

castexceptions ( although it would probably be better to do instanceof tests or even string tests on the nm result above,

you almost never want to implement unexceptional logic in exception handlers but it can be easy sometimes),

 

try {  mi.add(new PhluBroMenuItem(app,(String)x,idx));}
    catch (ClassCastException e)
       { try { mi.add((MenuItem)x); } catch (ClassCastException f)
       { mi.add(new PhluBroMenuItem(app,x.toString(),idx));}
       catch SomeDumException f)
       {mi.add(new PhluBroMenuItem(app,"no string avail",idx)); }

 

 

Please use plain text.
Developer
ipcTony
Posts: 107
Registered: ‎08-08-2008
My Device: Not Specified

Re: Can persistent data thats backed up be editing on PC

thanks for the detailed explanation marchywka. I haven't had the time to research exactly what classcastexception was and try to understand the mean. however, I was able to resolve my backup and restore problem and understand the "c" part of the "classcast" the exception.

 

in my problem SyncCollection methods I had originally combined the sync objects like so which caused something to go wrong. Not sure what but it seems to have combined both ojects into one.


    public boolean addSyncObject(SyncObject object)
    {
            _orders.addElement(object);

            _items.addElement(object);
     
        return true;
    }
 

 

in my working SyncCollection methods I used your if (object instanceof Order) recommendations like so and it cleared up the problem.

thanks for the help on this.


    public boolean addSyncObject(SyncObject object)
    {
        if (object instanceof Order)
        {       
            _orders.addElement(object);
        }
        else if (object instanceof Items)
        {
            _items.addElement(object);
        }
        return true;
    }

Please use plain text.