File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Dynamic data class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Dynamic data class" Watch "Dynamic data class" New topic
Author

Dynamic data class

Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

I'm thinking about making my data class dynamic by not hardcoding
any of the field but rather make a kind of map <field,value>.
the server will populate the map anytime needed and the getter
and setter will work on the map (i.e. room.get("price") instead of
room.getPrice())

this way any future change to the database format won't affect
my data class at all. besides, all updates and seraches are working
with arrays so no hardcoding there either. i'm just worried that the
assesor would judge this as a "complicated solution" and the instructions
made it clear that the simpler the solution the better...
(but they also said to make the it easier for future changes)

what do you think?

P.S.
one drawback of this solution is if you have a typo you wont get any
compile time warning..


SCJP 5, SCWCD 5, SCBCD 5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

That's a possible approach, but what's the benefit? Making your class dynamic is nice of course. My data class is also dynamic and can handle minor changes to the structure of the database file. But I didn't use field/value pairs, but just a dynamically read database schema and simple String[]


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

even a string array have to assume a certain order..

now that i read the instruction again, it says:
"Your user interface should be designed with the expectation of future functionality enhancements"
apparently it's only for the UI.

well, it's always a possibility...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Jonathan Elkharrat wrote:even a string array have to assume a certain order..

That's true, but the order is defined by the database schema. And you have to assume a certain order too, because all interface methods use String[], so when I create a String[] and pass it to the create-method you assume that name will be on index 0, location on index 1,...

Like I said my Data class can cope with small changes in database structure, adding (and/or removing) fields can be handled under certain conditions. My Data class can be easily reused to handle a database file with hotels, customers,... and I managed to do all of that with a clear and simple design.
And of course you always will have some static part in your application, because you some mapping between the field positions in your database and the fields in your JTable and/or the input fields. So you just have to wonder if the extra complexity in your Data class has a true benefit (which you would then of course add in your choices.txt)
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

from sun assignment:
"Field n in the database file is described by criteria[n]"
the search function will works like a varargs function, matching
first criteria in the String array to the first field and so on.
the server can read the file at startup an keep an array of string with the
fields name if i'll really need them. (to send them to the client columnModel
for example)

i don't really have to use static part in the application, remember that JTable
is inherently MVC based so i can dictate my own column model.
furthermoreת since everything is based on strings i just pass parameters and
have less functions. i guess you had functions like:
room->getPrice(){return fields[3]}
room->getSmoking(){return fields[2]}
room->getCity(){return fields[1]}
or something like that....
while i can do
room->get("criteria"){return map.get("criteria")!=null?map.get("criteria"):null;}

the data and the ui doesn't need to change even if the whole structure of the
database is changed (given the rules stays the same)

do you think it's a good way to go or the assessor would judge this as a "too complicated solution"?
citation:
"For any design choice concerning topics not specifically described in the requirements, marks are awarded for a clear and consistent approach, rather than for any particular solution."
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Jonathan Elkharrat wrote:i don't really have to use static part in the application

Maybe I'm missing something, but when you have the input field to enter the name of a hotel (or contractor) you will have code like map.put("name", userValue);, so that's static to me (I used something similar: room.setName(userValue);).
I know a JTable uses MVC, but you still would have a static part there too: the order of columns can be different from the order in your database file, so in your custom table model you would have something like: if columnIndex is 0, return map.get("name");.

The properties in my value object are of a specific type (e.g. smoking is of type Boolean), so my JTable is able to have specific rendering based on the type (a bit of eye candy). Also using a value object is far more (compile-time) type safe than using a map with some strings as keys.

Jonathan Elkharrat wrote:do you think it's a good way to go or the assessor would judge this as a "too complicated solution"?

I will repeat the last sentence from my previous post: you just have to wonder if the extra complexity in your Data class (even your whole application) has a true benefit (which you would then of course add in your choices.txt). In my opinion it has not, just adds complexity for no reason (because I'm not convinced it's more dynamic than my solution). Is it a good way to go? Again no in my opinion, for the reasons I have mentioned in this and previous posts.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

i thought about it on my way to work and here are my conclusions:
- the database values must be formatted in a special manner so pre-knowledge of
the database fields and type is needed. (to parse a date, for example)
- this lead to the conclusion that there's no need for the overkill of abstracting the
databases fields (and it's maybe even impossible with such requirements) and my
solution is obsolete.



now just for the challenge:

Roel De Nijs wrote:
Maybe I'm missing something, but when you have the input field to enter the name of a hotel (or contractor) you will have code like map.put("name", userValue);, so that's static to me (I used something similar: room.setName(userValue);).

maybe the map wasn't the best idea. a string array would work better since it's indexed.
then you just map the array you get to the data array.
or you can keep a string array with the fields name. (the server would initialize such an array
every startup. by reading again the database header it also provide validation)


Roel De Nijs wrote:
I know a JTable uses MVC, but you still would have a static part there too: the order of columns can be different from the order in your database file, so in your custom table model you would have something like: if columnIndex is 0, return map.get("name");.

again, using a string array solve this problem since it's indexed..

Roel De Nijs wrote:
The properties in my value object are of a specific type (e.g. smoking is of type Boolean), so my JTable is able to have specific rendering based on the type (a bit of eye candy). Also using a value object is far more (compile-time) type safe than using a map with some strings as keys.

that's a strong argument. a user friendly UI is a worth a lot (of points ) so indeed, in this area
your approach is better than mine..



i have lots of ideas running in my head so it wil take some time to put them in order...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

I try it one more time. And to make it not that abstract, I'll provide a small example.

The database file has 3 fields: name, location and size. The Data class can be made 100% dynamic, that's no problem (I did that myself), whether you use maps or arrays make no difference (based on the interface methods I would opt for arrays).
But in your GUI you'll need always some kind of static (fixed) information. When you want to add an input field to search on location, you just have to hard-code that the value from this field is a location (2nd field in your array).
When the user wants to see [location, name, size] in the JTable you have to hard-code that again. In the 1st column of your JTable (index = 0) you have to show the 2nd element (index = 1) of your array from the Data class. You simply can't do that dynamically.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

as i said, i abandonned this way.
since the UI and the code clarity are much more important for the assigment
i'll go for the classical way of getter/setter.
(maybe i'll add a function to convert from/parse to an array)
but this way require extensive use of If's and Switch's......



as for your questions, here's how i would have dealt with them:

Roel De Nijs wrote:
The database file has 3 fields: name, location and size. The Data class can be made 100% dynamic, that's no problem (I did that myself), whether you use maps or arrays make no difference (based on the interface methods I would opt for arrays).
But in your GUI you'll need always some kind of static (fixed) information. When you want to add an input field to search on location, you just have to hard-code that the value from this field is a location (2nd field in your array).

as i said before, there could be an array with the fields name (either by setting iy every time the server
build a record or by map.keySet())
i could have done a form of this way
{fieldsName[1]}: [ ]
{fieldsName[2]}: [ ]
{fieldsName[3]}: [ ]
and also the JTable would place in column {i} the value map.get(fieldsName[i]) and so on..
(but then again, no type-safety and no formmating . i mean somewhere between the
user entering the price and the server persisting it, it must be checked that it's an Integer)

Roel De Nijs wrote:
When the user wants to see [location, name, size] in the JTable you have to hard-code that again. In the 1st column of your JTable (index = 0) you have to show the 2nd element (index = 1) of your array from the Data class. You simply can't do that dynamically.

i didn't quite understand that.
the JTable is taking care of displaying the columns and even when the user move them the JTable display the original index..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Jonathan Elkharrat wrote:as i said, i abandonned this way.

I know, but you took the challenge, that's why I still commented

Jonathan Elkharrat wrote:(maybe i'll add a function to convert from/parse to an array)
but this way require extensive use of If's and Switch's

I used also methods to convert a String[] to a value object and vice versa, but no ifs nor switches at all.

The solution you provided works well as long as the records in your database have to be presented as-is in your JTable. The part you don't understand is the part where that approach won't work If your records in your database are stored as [name, location, size] and you have a functional requirement to present the records in the JTable as [location, name, size] (so when you start the application, records must be shown in that order, without user moving the columns) you need hard-coding.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

Roel De Nijs wrote:
Jonathan Elkharrat wrote:(maybe i'll add a function to convert from/parse to an array)
but this way require extensive use of If's and Switch's

I used also methods to convert a String[] to a value object and vice versa, but no ifs nor switches at all.

that comment was about the getter/setter way. if i use getter and setter i think i'll have to make some use
of if's and switch's here and there. maybe you're right, i'll have to start and see (i'm must be quick if i want to
finish before august 1st)

Roel De Nijs wrote:
The solution you provided works well as long as the records in your database have to be presented as-is in your JTable. The part you don't understand is the part where that approach won't work If your records in your database are stored as [name, location, size] and you have a functional requirement to present the records in the JTable as [location, name, size] (so when you start the application, records must be shown in that order, without user moving the columns) you need hard-coding.

i still dont understand why can't you do:
new Jtable(rooms[][],fieldsName[])
this way the column will have the right names and the getValueAt(i,j) will return rooms[i][j]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5126
    
  12

Jonathan Elkharrat wrote:that comment was about the getter/setter way. if i use getter and setter i think i'll have to make some use
of if's and switch's here and there. maybe you're right, i'll have to start and see (i'm must be quick if i want to finish before august 1st)

My value object has getters/setters (otherwise my comment would be useless) and still no ifs/switches.

Jonathan Elkharrat wrote:i still dont understand why can't you do:new Jtable(rooms[][],fieldsName[])
this way the column will have the right names and the getValueAt(i,j) will return rooms[i][j]

My database file contains:
location;name;size
Paris;Ritz;2
Antwerp;Hilton;3

My JTable must present (by default) the records in the database file as follows:
name;location;size
Ritz;Paris;2
Hilton;Antwerp;3

So you can't follow your approach because the column indexes does NOT match. Your approach will only work if column indexes do match.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

oh i see what you mean...
that's yet another problem indeed, if you don't know the nature of the
objects you can't order them.
and if the database isn't ordered then it can be ugly...

you got me here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic data class
 
Similar Threads
String[] vs. value object
Question regarding Field Lengths
Reading XML
traversing map unsing taglib
static final variables