• Post Reply Bookmark Topic Watch Topic
  • New Topic

Search best way

 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I have a search form in my spring application.
It searches from 4 fields.
Now i have to make a single search query for all these fields depend upon which of them have been filled by the user.
But before making the search query i have to check which fields are filled and which fields are left blank. So it would require a lot of else depends upon the number of fields and their permutation combinations.
I want to know if there is any other way of solving this problem without writing so many if-else.
I will appreciate your ideas.
Thanks in advance.
Regards,
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben
I am using command pattern for rest of the application like for insertion and updating i am sending a complete object rather than separate fields.
But in search module i need to deal with the different objects as my search criteria many belong to different objects.
Any comments...
Regards
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so you're using the command pattern to implement a front controller.
Do you think the same pattern, on a smaller scale would work for building up your searches?



Something like this would eliminate the large switch or if/else stack.
It would also allow you to add more search commands at a later date without
the need to alter the code that executes the search.

Without knowing more about what you're doing, it would be hard to make any further recommendations.
[ January 25, 2008: Message edited by: Ben Souther ]
 
sudha swami
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
I am not clear on this:
---------------------------------------------------------------------------
code:
For non-empty search field in form submission match field name up with search command execute command and add the results to the list.
----------------------------------------------------------------------
I tried to do but still i am ending up with if else statements or switch block..

eg: If my form has two fields say name and city. i want to search based on name/city, so in my action class i tried to do

if(name != null)
executeName();
else if (city != null)
executeCity();



regards
sudha
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said before, I really don't know enough about what you're doing to be able to tell you how to write it.

In general when someone asks how to eliminate a large switch statement or a large stack of if/else blocks, the command pattern comes to mind.
This might not be the best solution for you. Only you can make that decision.
From what I've heard so far, and if each text field requires a separate search anyway, it shouldn't be hard to loop through all fields in the form and execute a search on its contents if its not empty.

On the other hand, if some of the fields are subtractive (new WHERE clauses to a SQL query) then this approach might be very inefficient and the if/else blocks to build up a single query might actually be your best approach.

Only you can tell.
If you're familiar with the command pattern, then you should be able to make this determination.

-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lastly.
A JSP is really not the best place to implement this type of logic.

If it were my project, I would write all the search logic and database access in plain old java objects, and not even touch a servlet or JSP until I had the whole search engine working from the command line.
 
sudha swami
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for the info
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Sudha and Ben for your valuable informations.
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sudha Swami

I have found a way to implement this search without that big if-else kind of structures. I want to discuss it with you, if you have any interest, you can feedback and we can discuss it further.
Regards,
 
sudha swami
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Definetly, I would like to know.
sudha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!