• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need Design advice...

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm implementing a search module for my app. I was thinking if there is a good technique I could use for this.

if the user entered no search options( there are 3 options required like age preference, religion and citizenship). I was thinking of an implementation like this but it doesn't look elegant...


if (preferredAge.equals("") || religion.equals("") || citizenship.equals("") )
{
dao.search()
}
else
{
dao.search(preferredAge, religion, citizenship)
}


that's making an overloaded version of search method. Any better ideas? Thanks!
 
Orem Hun
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you encapsulate this into dao.search(age, religion, citizen).
If this is sql query:

String query = "select * from user_table where <default_conditions> " +
age.equals("")? "" : "age = ? " +
religion.equals("")? "" : "religion = ? " +
citizen.equals("")? "" : "citizen = ? " ;

By no means this is complete code, but should give you the idea.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Years ago I had a complex search screen in an environment that did not allow dynamic SQL. I made a "bitmap" of which fields were entered by the user:

if first-name is not null, set bit 1
if last-name is not null, set bit 2
if city is not null, set bit 4
if state is not null, set bit 8

and then made a mapping of bitmap values to prepared sql statements

I might resort to the same trick if the combinations of fields were really tricky.

But it's simpler to test for a few valid ones. Today we do dynamic SQL and test for minimum required inputs:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic