Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

findByCriteria implementation approach

 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am posting how I am going to implement the public long[] findByCriteria(String[] criteria) to know Is my approach reasonable or do I need some changes.

For each search Name, Location, Name or Location, Name and Location client has separate form.
Clint will call findByCriteria(String[] criteria) if it was from the search Name form then {"ABC", null, null, null, null, null} if location then {null, "Can", null, null, null, null} if Name and/or location {"ABC", "Can", null, null, null, null} will be set in the array (to pass String[] criteria) now control will transfer to findByCriteria() method in data file. Here it will check does first element in array has (non null)value then need to call Search(name) or 2nd has value then call Search(location) or 1st and 2nd both have values then call Search(name, OrLocation) ) / Search(name, andLocation) (or any of array item is non null). then according to array elements it will call Search(name), Search(Location), , (Search(specialty), Search(size), ...). These all methods I'll keep in another file. so findByCriteria() actually only tranfers control to the appropriate method in another file, is it okay.

If 1st value in array will non null then Program'll get it know that it need to search by name. If 2nd value in array will non null then Program'll get it know that it need to search by location.

Now problem is when it is Name or Location / name and Location then how to differenciate or/and to call the exact method.

I am thinking to pass and extra value in array (String[] criteria) to tell "Or condition will apply/ and condition will apply" Is it right or how should I recognize the and/or condition.

PS: One function may also be enough that search according to any one or more non-null value(s) but still thinking how to identify AND/OR. may be instance variable or pass some extra value in array parameter
Thanks! in advance
Best Regards
 
Ehsan Rahman
Ranch Hand
Posts: 59
Chrome Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about perhaps calling public long[] findByCriteria(String[] criteria) twice and analyzing the results? So for {"ABC", "Can", null, null, null, null} two calls can be made: {"ABC", null, null, null, null, null} and {null, "Can", null, null, null, null}
 
Tahir Abbas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea now I am thinking only one method before this my implementation approach requires more lines of code so I was thinking to organise it in multiple methods, in a separate file. But still thinking how to recognise that which guest selected And and which selected OR (Name and/or Location). Because main method will be in Data file and I used singleton.
 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tahir,

My possibilities in the gui, search on:
  • only name,
  • only location,
  • name and location
  • nothing (and return all records).


  • All these possibilities return in a search array (name or null, location or null, null, null, ..., null) being passed to my find-method. This find-method returns the matching records (combining different search criteria with OR, so 1 matching criterium results in record being returned. null-values in search array are ignored).

    At my gui i filter the returned records for exact matches and to allow only records with same name and location (if user entered both text fields).

    Kind regards,
    Roel
     
    Roberto Perillo
    Bartender
    Posts: 2271
    3
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Howdy, Tahir!

    Champion, please take a look here. This thread also takes to other threads. I think it will be helpful!

    Here it will check does first element in array has (non null)value then need to call Search(name) or 2nd has value then call Search(location) or 1st and 2nd both have values then call Search(name, OrLocation) ) / Search(name, andLocation) (or any of array item is non null). then according to array elements it will call Search(name), Search(Location), , (Search(specialty), Search(size), ...). These all methods I'll keep in another file.


    So, for each type of search, you are providing a different method in another class, right? Hum... you could provide more abstraction so your searching mechanism can be kept simple and flexible. What if tomorrow there's a new searching mechanism? Maybe the Strategy pattern could be a good idea here.
     
    Tahir Abbas
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much my javaranch buddies,

    Now I decided one single method to search:-
    {null, null, null, null, null, null} All recorcds No. will return then some method to get Records. Am I right ?

    For any other non-null value I'll get the Record number, if value exist. Non-null value may be one or may be all. So OR will apply automatically for Name or Location / (it may be AnyField OR AnyField). It looks me very reasonable approach (Almost implemented).

    For AND, Ignore case, Exact Match I should apply some filteration in gui, but this may increase number of lines over there. but looking good solution.

    Another thing I used Patterns [(Pattern.compile(field[0], Pattern.CASE_INSENSITIVE, Pattern.compile(field[1])]. Is it right or is there any better possibility available.
    Best Regards!

     
    Roel De Nijs
    Sheriff
    Posts: 10594
    143
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Tahir,

    I simply did


    Kind regards,
    Roel
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic