aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird : Need Suggestions on create method and update method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird : Need Suggestions on create method and update method" Watch "URLyBird : Need Suggestions on create method and update method" New topic
Author

URLyBird : Need Suggestions on create method and update method

Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Dear friends, I have an issue involving updating and creating record.

The create method throws DuplicateKeyException
The update method throws only RecordNotFoundException

I assume that DuplicateKeyException occurs when the user create the hotel records which is exactly the same with the existing record.

How about when the user update the record which is exactly the same with the existing record?

Do I have to make assumption to solve this problem ?

Do I have to wrap The DuplicateKeyException message to RecordNotFoundException message?

all the exception must only have no arg constructor and a constructor that takes String as an argument, so I can't wrap the the DuplicateKeyException object into RecordNotFoundException object.

I solves this problem by wrapping DuplicateKeyException message to RecordNotFoundException message and document this solution.

Do you have another solution that better than mine? I know that the best solution maybe will never exist. I think we must find the best solution from all of the bad solutions

thanks

Jeffry Kristianto Yanuar
SCJP 5.0
SCJA
SCJD (Working on UrlyBird 1.3.2)
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Hey partner, how's it going?!

You brought up an excelent point. I worked on the URLyBird 1.3.1, there shouldn't be much difference. Here's the signature of my create method:



Theoretically, we would have to choose one of the fields to be the record's primary key. But none of the fields is unique for each record. For instance, if there was a field for the room number, then it would be ok to have it as primary key.

Here's the update method:



So, if there was a field that could be the primary key, it would throw a DuplicateKeyException as well, but it doesn't, which leads me to think that the record's primary key is the record number. I myself assumed that the record number is the position that the record appears in the database (no matter if it is valid or deleted). I also wrote in my choices.txt file that I chose never to throw DuplicateKeyExceptions from the create method because none of the fields can be faced as the record's primary key.

About the exceptions, they must have a zero argument constructor and a second one that takes a String. That's right. But you can also have other constructors, which take a Throwable and a String, for instance.

Have a good day, buddy!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
The instruction said :

Each must have a zero argument constructor and a second constructor that takes a String that serves as the exception's description.


I interpret this that the maximum number of constructor is 2. One that has no arg and one that has 1 String arg.

Do you interpret that as the number of constructors is at least 2?

there is word "must" so I'm afraid I will get automatic fail if I don't follow that
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Well, there is absolutely no problem if you only have these two constructors. I myself only created these constructors for my exceptions. But you can have other constructors, as long as you have the zero argument constructor and the one that take a String. These two constructors must exist.
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Ok, lets get back to the previous issue about DuplicateKeyException.

How do you make sure that there is no duplicate key when the method header is:



That method doesn't accept integer parameter as the record number

My solution is like this:
- Override Hotel's equals method
- Store all the hotel object in HashSet when initiating the server, update if necessary (if there are any deletion, updation, or creation).

when the create method is called, convert the array into hotel object, check the HashSet if there is already the same Hotel. If true that throw DuplicateKeyException. Yes, I use the hotel object itself as a primary key.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Yes, I use the hotel object itself as a primary key.


Are you sure this is the best approach? There could easily be 2 rooms in the same hotel with the same characteristics, and your approach would not allow this. My approach was, since there are no fields that could be the primary key of a record, then I never throw a DuplicateKeyException from the create method.
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 158
Originally posted by Jeffry Kristianto Yanuar:
[qb]
I solves this problem by wrapping DuplicateKeyException message to RecordNotFoundException message and document this solution.

Do you have another solution that better than mine? I know that the best solution maybe will never exist. I think we must find the best solution from all of the bad solutions



Hi,
For all the exceptions that are thrown at DB layer and that are not included in the DB interface i have created a not checked DBException (specific for the DB layer) where I encapsulate them. For example IOExceptions are encapsulated into this DBException. I choose to encapsulate the DuplicateKeyException from the update method into this DBException. I documented this in my choices.txt. I think that absence of DuplicateKeyException from the signature of update method is a kind of inexactity in interface that must be fixed somehow.
Encapsulating the DuplicateKeyException in a RecordNotFoundException smells bad to me.
Just my 2 cents...
Liviu
[ July 01, 2008: Message edited by: Liviu Carausu ]

Oracle Certified Master Java SE6 Developer(SCJD),
OCE JEE 6 JSP and Servlets Developer.
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
I never throw a DuplicateKeyException from the create method.


Are your sure that approach is allowed ? Then all your method will never throw the DuplicateKeyException. Isn't it?
Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
For all the exceptions that are thrown at DB layer and that are not included in the DB interface i have created a not checked DBException (specific for the DB layer) where I encapsulate them.


Good solution!!! and what's the condition that the method will throw that exception?
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 158
Originally posted by Jeffry Kristianto Yanuar:


Good solution!!! and what's the condition that the method will throw that exception?


For example, in read method , when I try to return a String from the bytes that I read from the file, a UnsupportedEncodingException can be thrown. Instead of misusing the RecordNotFoundException for wrapping the UnsupportedEncodingException, I choose to use this DbLayer specific DbException. The same I have used for wrapping the DuplicateKeyException. These were my decisions, I do not know if they are good or not...
So the condition is : "If the exception that you need to throw from your implementation is not included in the provided interface, that wrap it into a not checked exception"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird : Need Suggestions on create method and update method