Skip to main content

Add/RemoveRecord

2 replies [Last post]
Anonymous

Is there a way to remove an entire record from the DataModel? Or how about Adding a Record(though it is a bit more tricky)? I realize that there is quite the argument going on about this whole issue, but as i see it, DataModels contain a list of rows, or rowsets or records more accuratly, and there fore need a way to add and remove these records. Though some argue that DataModels should contain only one record, i'd disagree because (once again from what I understnad) datamodels are given a datasource, which uses an sql statement to pull data from a database correct? If that be the case then DataModels should be able to contain multiple records, since sql statements are used to obtain one or many records.

Any comments that might help me understand better the whole issue are welcome :) Though whether I understand the basics of DataModels or not, it just feels right to have the add/removeRecord methods on the DataModel level.

Gianni

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
rbair
Offline
Joined: 2003-07-08

Hey Gianni,

I talked over the problem with Scott & Bino over lunch, and here's what we're thinking. First, some basic methods that should be supported are add, remove, undo, and redo. The add (or addRow, or addRecord, or whatever its called) method will add a new row to the DataModel and will use the MetaData to populate the fields with default values (if the metadata has any default values specified). When saved to the backing data store, the DataSource will manage inserting a new record into the database, or whatnot.

The remove method will likewise remove the current record, or the record at a specified index. Undo/Redo will be for undo'ing & redo'ing deletions. So maybe they could be called undoDelete & redoDelete. Then again, maybe the undo/redo methods shouldn't exist since you could just rollback a transaction if necessary. Hmmm...

Anyway, add/remove is pretty straightforward for the RowSetDataModel, but a little more interesting for the JavaBeanDataModel. A JavaBean based solution will need some special code to figure out how to create an object (by default it will use reflection to figure out what kind of objects are in the JavaBeanDataModel, and try the default constructor on that object type. This will of course have pluggable behavior so that if you know you have to do something complex to create your bean, you can).

The more interesting problem is related to "moving" records from one DataModel to another. This doesn't turn out to be so pretty, because one DataModel cannot arbitrarily recieve data from another DataModel. Indeed, you could only move data between two DataModels that share the same (or a subset of the same, perhaps) meta data.

For examle, moving a bean from one JavaBeanDataModel into another would be pretty easy, assuming both models wrapped the same types of beans. But you don't necessarily want to use special JavaBeanDataModel methods, so it would be preferable to have some kind of generic API for moving data between two compatible DataModels.

The simplist (and perhaps best?) way of doing this would be to use the MetaData from both DataModels, and the built in "setValue" and "getValue" methods. So, you would loop through all of the MetaData for originalDM, call getValue(fieldName) for each field in originalDM, and call setValue(fieldName, val) for each corrosponding field in the destinationDM.

This job could be recursive in the sense that it would iterate also over all detail DataModels; needless to say this can be a bit tricky. But this code should be in some utility class, or perhaps I could add a static method on AbstractDataModel that performs the generic copy operation.

Obviously, this isn't a perfect solution for deep object graphs. In that situation you are probably better off knowing and working with JavaBeanDataModels than you are with using the generic solution.

Thoughts?
Richard

Anonymous

Well here's another question about it. I have been trying to "pretend" that the add and remove methods existed to see how they should work, or at least what I would need them to be able to do. There really needs to be a deleteRecord type of method (though something of the sort may exsist in some way, shape or form). The reason is because sometimes I'd like to remove a row from the datamodel, not deleting it though, and other times I would like to delete the entire row from the database.

Implementation of all of this would be quite interesting and fun ;)

Gianni