• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Search best way

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
Read up on the command pattern.
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for the info
 
Khushwinder Chahal
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Definetly, I would like to know.
sudha
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic