This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Handling the criteria in a business layer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Handling the criteria in a business layer" Watch "Handling the criteria in a business layer" New topic
Author

Handling the criteria in a business layer

Marcelo Camargo
Ranch Hand

Joined: Jul 05, 2007
Posts: 43
I am working on the URLyBird assignment, and I make the criteria in a way I think it can be better, but I dont know how to do that improvment.
(Rinke makes me think about this after I read his topic about using string array or an VO...)

I have created a business layer, and an exclusive package for that. This layer is an adapter (adapter pattern) between the DBAccess interface to my new clean and easy to use business interface. The business interface is the one I am exposing via RMI to the clients.

And I have the following method in the business interface:

public ArrayList<Room> searchRoomsByCriteria(SearchCriteria criteria) throws IllegalArgumentException, BusinessFailureException;
Look at the parameter: I created an SearchCriteria (serializable class), just with the hotel name string and city location string (2 strings).
When the client wants to serach for the hotel name called "Palace", it need to create a SearchCriteria object, set the hotelName instance variable to "Palace", and call the method passing the object as the parameter.

Inside the method, I am creating an array of string with the fields of SearchCriteria (criteriaFields), and comparing with the just readed values from the database for a record (fields), as follow:



But my assignment talks about to create a flexible search mechanism. Is my implementation that flexible? I am not sure, I heard people talking about to use the strategy pattern in the criteria... I would appreciate some oppinions.

Thanks a lot,


SCJP5(93%), SCJD(in progress...), SCEA(not started yet...)
rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Originally posted by Marcelo Camargo:
But my assignment talks about to create a flexible search mechanism. Is my implementation that flexible? I am not sure, I heard people talking about to use the strategy pattern in the criteria... I would appreciate some oppinions.


Yes, I have been bothering about that too. The instructions seem to be a bit contradicting each other on that point.

First of all, the interface instructions tell you to create a flexible search mechanism, but the client needs only to search on two fields, and only wants exact matches, which is not at all my idea of a flexible search mechanism. So apparantly we are to create a flexible search mechanism in the DB package, but we don't need to use it ourselves in the client.

I choose the easy way out: my search mechanism for the db package is not so flexible because I explicitly only implemented that what was mentioned in the instructions, and nothing more. This means that it is not able to search on minimum or maximum values of numeric fields, but just interprets these fields as strings - which I think is highly inflexible, but alas.

It does return matches starting with the criteria String, and then the client filters these results to only return exact matches.


_ _ ________________________ _ _ <br /> <br />Just SCJP (but 93%)
Marcelo Camargo
Ranch Hand

Joined: Jul 05, 2007
Posts: 43
Hi Rinke,

Exactly ! That's my thoughts. I did the same way as you, in the db access layer, any string field can be filtered (but only in string mode), using startsWith(). And, in the business layer, I filtered by the exact matches.

Another point is that I chose to make all searches case insensitive, and I documented it on the choices.txt, of course. Is that ok, do you have another opinion?

Thanks,
Marcelo.
rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Originally posted by Marcelo Camargo:

Another point is that I chose to make all searches case insensitive, and I documented it on the choices.txt, of course. Is that ok, do you have another opinion?


I used case insensitivity, because I think case sensitivity makes no sense. But some people on this forum believe this is not according to the instructions, because instructions are talking about exact matches. So it depends on how exact you interpret the word exact
Chris Be
Ranch Hand

Joined: Sep 11, 2006
Posts: 36
Originally posted by rinke hoekstra:


I used case insensitivity, because I think case sensitivity makes no sense. But some people on this forum believe this is not according to the instructions, because instructions are talking about exact matches. So it depends on how exact you interpret the word exact


I think an interpretation of exact is not neccessary, the phrase contains the word must:

It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.


I suspect this may have something to do with automated testability of our programs. Hence, exact match cannot be implemented in the database, when the interface specifies otherwise (e.g. my URLyBird).


ChrisBe<br />-------<br />SCJP, SCJD, SCEA in the making
Xabier Martija
Ranch Hand

Joined: Jul 18, 2007
Posts: 40
hi!
I have done something different:
I was wondering about this problem for a long time and got this solution:
i implemented both "Exact Match Only" and "Starts whith Match" and left the deccision in the user's hands checking a checkbox.
-My implementation of "Exact Match Only" method uses a new method created in the data class. I didn't want to add methods not includen
in the DB interface but finally i did it without including it to the DB, of course. It performs the search better since it returns a list of contractors directly
whithout n-read calls.
-My implementation of "Starts whith Match" method uses the public int[] find(String[] criteria) of the Data class. Then i read the records returned.

I like your solution very much and think it adjust to the requirement, but finally i will decided to left my solution,
becouse i don't want the user to type an incomplete criteria and displaying no records.
You could also include the checkbox and implement the "Exact Match Only" method your way and include the "Starts whith Match" method.
Seems to be the best solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Handling the criteria in a business layer