Working with Preclipse - Part 3 (Creating transactions)

<< Back to part 2

Part 1 - Part 2 - Part 3 - Part 4 - Part 5

We want to add transactions for creating, changing, and removing DVDs from the DVDStorage. Transactions are needed whenever changed occur on business objects, so Prevayler can store those changes to disk and repeat them on recovering from a system crash and/or an application restart.

Note that you musn't change the data of business objects outside of a transaction!

We start by creating a "create" transaction for DVS. Right-click on the "DVD" class select "Create 'Create' transactions for BO...":

We get the following dialog:

This time, we choose a new package for our object: The "transaction" package will be just fine for our transaction object. As we will later use a GUI to enter the data of our DVDs, we create a one-4-all create transaction, that is, a create transaction with takes care of all of the BOs fields.

Note that you need to provide a field containing the ID attribute in this dialog. Preclipse will exclude the ID from the field list below, as it is created in the parent BO automatically.

Hit "Finish" and you will get the following code:

As you can see, all of the data needed will be provided using the constructor. The real work still needs to be done in the executeAndQuery method. However, using the methods already provided in the parent object DVDStorage, implementing this method is really simple:

That's it! We just completed our first transaction!

Note that when we later invoke the transaction, the parent BO of DVD (which is the prevalent system, DVDStorage) will be asked to create a new DVD. It takes care of assigning a unique ID to the DVD and returns the virgin DVD. We then add all of the data to the DVD and return it.

Lets go on to the "change" transaction. Changing BOs does not mean simply changing the values of their fields - we need to extract the BO from the parent BO using its ID before we can change any values.

Right-click "DVD" again and invoke the "create 'change' transactions..." menu entry:

The following dialog pops up:

Again, we will create the new transaction in the "transaction" package. We can choose between creating one transaction for all of the selected fields (which we will do here, as the GUI will always provide us with all the information we need) and creating one transaction for each selected field (which will result in a lot of transaction classes). The code looks like this (note that the ID of the object to be changed is provided, not the object itself!):

We will need to provide an ID here, which will allow Preclipse to place the ID field upfront the 'change' transaction constructor for quick reference. The ID will be used inside the 'change' transaction to get hold of the object which will be changed.

We add the following code to the executeAndQuery method of the newly created 'change' transaction class:

Last but not least, we will add a 'delete' transaction in pretty much the same way as the other two:

The dialog, however, is much simpler, as it only contains three fields:

We add the following code to the executeOn method:

That's it! We finished the model of our DVDStorage application. The package tree looks like this:

Now it's time to look at the GUI.

Part 4 >>