File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes findByCriteria implementation approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "findByCriteria implementation approach" Watch "findByCriteria implementation approach" New topic
Author

findByCriteria implementation approach

Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 44
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

Joined: Feb 16, 2009
Posts: 59

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}


SCJP 1.5, SCJD 1.6
Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 44
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
Bartender

Joined: Jul 19, 2004
Posts: 4919
    
  10

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


    SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Roberto Perillo
    Bartender

    Joined: Dec 28, 2007
    Posts: 2247

    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.


    Cheers, Bob "John Lennon" Perillo
    SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
    Tahir Abbas
    Ranch Hand

    Joined: May 05, 2007
    Posts: 44
    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
    Bartender

    Joined: Jul 19, 2004
    Posts: 4919
        
      10

    Hi Tahir,

    I simply did


    Kind regards,
    Roel
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: findByCriteria implementation approach
     
    Similar Threads
    B&S What Are You Guys Passing Into Your findByCriteria For criteria[2] Thru [5]?
    UrlyBird 1.2.1 findByCriteria(String[] criteria) and searching algorithm doubts
    B&S: findByCriteria - pls review
    [NX] Question about the findByCriteria.
    a question about requirement, findByCriteria