This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird: reading database-file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird: reading database-file" Watch "URLyBird: reading database-file" New topic
Author

URLyBird: reading database-file

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi all,

I've been thinking on how i have to access my database-file to make it possible to make changes in future (e.g. changing the length of a data-field) on that part with a minimum on effort and changes. and also try not to affect other parts of the program (gui etc.)

that's the purpose i set for myself, now i've got my thinking done and i want to share the result with you guys to see if i'm on the right track, or maybe i missed some (crucial) things.
I don't have any code yet, because it's just some thoughts about how to do it and i'd find it better to think a bit longer instead of start coding and after 2 weeks noticing all code is for the GC

So here we go. my data-handling (package suncertify.db) would consist of following classes:
- DBMain = the required interface (it's kept very general with almost each method having a recNo (int) and an array of String)
- Data = required implementor of DBMain (i'm tempted here to use facade-pattern redirecting crud-actions to a seperate class, implementation of DataAccess, and locking-methods to another one, BookingManager)
- DataAccess = interface containing crud-actions and other methods
- FileAccess = implementation of DataAccess and will read the database-file, storing magic cookie, header info in memory. also it retrieves all records and keeps track of the position of each record in file. for crud-actions it will also validate input parameters against the header info, is length value not too long for that field e.g.)
- BookingManager = take care of booking and un-booking hotel-rooms
- HotelRoomData = extends the Data-class, containing specific checks for this application: checks if magic-cookie is equal to some constant + do some business checks, like the "Y"/"N" value for the smoking-flag and after validation calls the appropriate parent-method from Data.

Benefits in my opinion:
a) if you want to use a real database instead of a file, you have to make class (e.g. DBAccess) that implements DataAccess, and change instantion of the DataAccess-member in Data to DBAccess instead of FileAccess
b) you can reuse Data-class to read a file with e.g. contractors with complete different scheme. all you need is making ContractorData extending Data-class (if you need specific business checks)
c) if length of a field changes (and magic cookie doesn't change) program will keep working without change (if header info is updated appropriately of course)
d) if field is added at back of record (and magic cookie doesn't change) program will keep working without change (if header info is updated appropriately of course) and extra field will not visible in GUI (because maybe it's field for another program using same db-file and of no use in GUI). adding an extra field in JTable is not that hard.
e) if field is added in front or middle of record (and magic cookie doesn't change), program will not work anymore without change
f) if field is deleted (and magic cookie doesn't change), program will not work anymore without change
g) c + d + e + f (with magic-cookie change): program will tell it's not a valid data-file. it's also very easy to change a constant value (or maybe a set of business rules are available to determine when magic cookie is valid instead of using a hard-coded value). of course, if magic cookie is changed you get back to cases c + d + e + f

That's it! what do you think about it? did i miss something? maybe some people did it the same way as me and ran into some problems, glad to hear about them. or maybe this is far beyond the scope of the application?

is this a approach or am i a complete idiot and do i have to do some to get my thoughts back in order?

I appreciate all comments, suggestions, warnings, hints, tips,... from anyone.
Kind regards,
Roel


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

Joined: Jul 29, 2007
Posts: 40
Hi Roel,

i think it's good enough...

BTW,does your DBMain interface include the method to get the all room records in the data file, mine doesn't include it, so i think we need another interface, right?

Thank you!
Ronggen


SCJP 1.4,<br />SCJD Java 2,<br />...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Liu,

Thanks for your reply. That's the theory and it sounds nice, hopefully the code will work as nice

I have version 1.3.1 of the URLyBird and i was a little bit wrong: my interface doesn't have a get all room records. i was a little bit too fixed on the interface of Denny's DVD (from the SCJD book of Andrew Monkhouse), that's why i mentioned this method but it's not in my interface.

So glad you mentioned it, because that's making implementation of reading the file a whole lot easier. i will make 1 private method that's processing complete file (reading magic cookie + validating it ; reading header ; reading data to build record numbers)
Do you follow same approach ?
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi Roel,

i prefer to create a new interface which extends the MianDB interface, and implements the interface.

Thank you!
Ronggen
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi Roel,

I added a public method getRoomRecords to Data.java like this:


and the getRoomRecords method in RoomFileAccess.java like this:


Thank you!
Ronggen
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4908
    
  10

Hi Ronggen,

Thanks for your reply.

First little remark: i think your code could be a little more tweaked, made better. in the for-loop i would make it like this

improvements (imho):
- no cast is necessary because your list is type-safe (so cast is added at compile-time)
- you just have to call 1 time the get-method on your list (instead of for each data member of RoomRecord)
- and code is more obvious and a lot easier to read

furthermore i have a question: i guess the method getRoomRecordList has a boolean-parameter indicating to build recordnumbers or not (as in denny's dvds).
so that method is seperated from other methods (like reading the header-information). so how do you know on which position you have to start the reading of room-records? or do you just read each time the header information and just skip it?
and how do you take care of locking? because if you read the header information (for first time), you have to lock data-member that's storing the header info. but if you have to build record numbers also, you have to lock another data-member that's storing the recordnumbers. so not quiet sure which way i have to go with that one...
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi Roel,

Thank you for your advice, you are right. the method getRoomRecordList has a boolean parameter.

for the position where start the reading of room-records, i hardcode in method getRoomRecordList; in my data file, it's 74; i added some comments there and wrote down it in the design choice doc.

Thank you!
Ronggen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird: reading database-file
 
Similar Threads
How to check a valid database file?
B&S, hardcoding file headers
URLyBird: The Data File
NX: (Contractors) File I/O class
Relationship among Data class, RAF instance(s), and clients