wood burning stoves*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes findByCriteria() 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()" Watch "findByCriteria()" New topic
Author

findByCriteria()

Alain Huber
Greenhorn

Joined: May 09, 2007
Posts: 5
Hi

I'm having a problem understanding the findByCriteria(String [] criteria) function of the DBAccess interface provided by sun. It says:

// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public long[] findByCriteria(String[] criteria);

the way I understand this the findByCriteria() would return all records in every scenario, because in the name / location search the other fields would be null and the record would be selected anyway. I searched the forum thoroughly but I didnt find anything that made this issue clear to me.

Futhermore, I find the follwing isssue unclear: "It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user". Does the "and" mean a logical "and" that is, both fields must match the specified string?

Can anybody help me?
Thanks Alain

Thanks in advance.
Alain
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

Originally posted by Alain Huber:
the way I understand this the findByCriteria() would return all records in every scenario, because in the name / location search the other fields would be null and the record would be selected anyway. I searched the forum thoroughly but I didnt find anything that made this issue clear to me.

no, no..
The "null value in criteria[n] matches any field value" is on a field by field basis:

=> {null, "Whoville", null, null, null, null} matches all records with location Whoville.

=> {"Bono", "Whoville", null, null, null, null} matches all records with name "Bono" and location "Whoville".

=> {null, "Whoville", null, "blah", null, null} matches all records with location "Whoville" and whatever-field4 = "blah".


Futhermore, I find the follwing isssue unclear: "It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user". Does the "and" mean a logical "and" that is, both fields must match the specified string?

First, you must understand that this requirement is for your GUI and not your Data in particular. Your Data implementation of findCriteria must still provide full search capability (for all fields).
Secondly, to me (and strongly almost without doubt ) I believe this is mentioned to point out that your GUI must allow search by "Name only", "Location only", "Name and Location" => (Name or Location or "name and location" = "name and/or location")

Regards,
Alex
[ November 23, 2007: Message edited by: Alex Belisle Turcot ]
Oguz Ozun
Greenhorn

Joined: May 10, 2007
Posts: 19
Hi Alex,

I think you mean that

=> {null, "Whoville", null, null, null, null} matches all records with location Whoville.
location starting with Whoville (including itself also).

I mean this criteria should also return record like location equals to Whovillea.

Am I right?
[ November 23, 2007: Message edited by: Oguz Ozun ]
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
yes yes.. thanks for the precision

=> starting by.. at the database layer

=> and exact match at the GUI/business level (depending on the assignment maybe)

Regards,
Alex
Alain Huber
Greenhorn

Joined: May 09, 2007
Posts: 5
Alex

Thanks so much for clarifying

Alain
Tomas Klubal
Greenhorn

Joined: Aug 16, 2004
Posts: 20
Hi Ranchers,
does following according to you mean that if user enters "Fred" to the name search field in the UI, Data class should return all the records that start with "Fred" like "Fredie" and you have to filter returned records either in your business layer or in the UI?

It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.


Cheers,
Tomas
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
that means exactly that to me! right on target!
[ November 28, 2007: Message edited by: Alex Belisle Turcot ]
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
Hello all,

I use 2 drop down boxes, populated with names and locations from the data file and added "any" to each box. Others have used this approach and it is OK. It
saves you from having to do any validation of the Strings other than by what they select in the drop down. No misspelled words, checking for numeric input or other invalid input such as symbols. I put the selected values into an array String criteria[], which of course, will always have exactly 2 elements. Then I compare the selection with the data file. "Any" and "any" returns all records. A specific name and a specific location will return 1 record or 0 records, and "any" with a specific name or location returns all matching records, usually more than 1. Hope this helps.


SCJP, SCJD
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,


wooa, thank you Anne, I can't believe I was not using a combobox either! How could I not think of that!!! Thanks a lot!!

Alex
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
Your welcome, Alex I decided to use this approach after reading part of Alain Trottier's book, it's the Exam Cram one. He gives good reason's for using it, mainly as I mentioned, the user can't enter a typo. His GUI got a pretty high mark, so I am basing mine on his.
Oguz Ozun
Greenhorn

Joined: May 10, 2007
Posts: 19
Originally posted by Anne Crace:
Hello all,

I use 2 drop down boxes, populated with names and locations from the data file and added "any" to each box. Others have used this approach and it is OK. It
saves you from having to do any validation of the Strings other than by what they select in the drop down. No misspelled words, checking for numeric input or other invalid input such as symbols. I put the selected values into an array String criteria[], which of course, will always have exactly 2 elements. Then I compare the selection with the data file. "Any" and "any" returns all records. A specific name and a specific location will return 1 record or 0 records, and "any" with a specific name or location returns all matching records, usually more than 1. Hope this helps.


That is very practical and eliminates typo mistakes. However, what if there are so many records in the db. As far as I see, each time drop down box got focus, the elements in it should be recomputed. Possible updated values for name or location (depending on the selected control) should be listed each time and this can reduce the response time of the user interface. Am I right or am I missing something?
Anne Crace
Ranch Hand

Joined: Aug 29, 2005
Posts: 223
The dropdowns use a BoxModel. I wrote my own and extended DefaultComboBoxModel. It has a ListDataListener. That was a good question, though. I think I need to add more to this class, to make sure it takes care of that situation. Thanks!
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I implemented it and yes I do think its very nice usability, but it makes the code a lot more error prone.
- must repopulate the drop down..
- reselect previous selected value (saved in property)
- what if the selected value is not there anymore
- provide an empty value for blank search..
- ...

Before I only had to check.. nothing actually, everything was handled by normal processing searching with empty string...

Now, I have to make sure that the drop down is populated every time.
What if another client creates a new record.. then you cannot "search" for it, since it's not in the choices.

Just sharing..

=> I repopulate the dropdown everytime a "get all" is done.

Regard,
Alex
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Guys, great thread, it answered my questions about this exact issue, with the exception of one small question. My instructions also state the below for the GUI requirement:


...where the name and/or location fields exactly match values specified...


Does this mean the search results are case-sensitive? In other words I'd return searches for Fred, but not frEd?

Thanks.


SCJP 1.4 SCJD 1.5
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

I guess this could be a design choice, but for me "exact match" meaning it is case sensitive..

Regards,
Alex
Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Yes, I think I'll take this literally and go case sensitive as well. Thanks.
uzma ali
Ranch Hand

Joined: Jun 22, 2007
Posts: 56
I implemented in a very different way and wants your opinion.

I searched the data base with the first letter of the name or location.
For example in case of Fred or Fredie the database searchs all that starts with F and brings all records.

What you guys say about that?

Uzma
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

don't you think this goes against the description of your find method ?

A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".

"Fred" should retrieve words starting by Fred, not starting by "F".

Regards,
Alex
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
Originally posted by Alex Belisle Turcot:
Hi,


First, you must understand that this requirement is for your GUI and not your Data in particular. Your Data implementation of findCriteria must still provide full search capability (for all fields).


I'm doing the Bodgitt & Scarper assignment. When you say the Data implementation of findCriteria must still provide full search capabilitiy for all fields, are you saying we can search by criteria such as specialties and size?? It sounds like we can search on anything (not just name and location).
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Originally posted by Aaron John:


I'm doing the Bodgitt & Scarper assignment. When you say the Data implementation of findCriteria must still provide full search capabilitiy for all fields, are you saying we can search by criteria such as specialties and size?? It sounds like we can search on anything (not just name and location).


Hi,

yes, your Data class must allow searching with any field. So, when you write unit test on your Data class, make sure you are able to search with all the fields.
From your GUI, the requirement is only to provide search capabilities using name and/or location fields.
Please confirm all this with your own requirements(and the comments from SUN in the Interface they provided).

Regards,
Alex
[ February 04, 2008: Message edited by: Alex Belisle Turcot ]
rinke hoekstra
Ranch Hand

Joined: Apr 06, 2007
Posts: 152
Originally posted by Anne Crace:

I use 2 drop down boxes, populated with names and locations from the data file and added "any" to each box. Others have used this approach and it is OK. It
saves you from having to do any validation of the Strings other than by what they select in the drop down. No misspelled words, checking for numeric input or other invalid input such as symbols.


Hi Anne, others,

Sounds good, but...
I'm not using this, for a simple reason: I think that all this checking is the responsibility of the user, not of me. If the user types "Whaville" in stead of "Whoville", then the user just will find nothing, meaning that he/she should just retype his search and this time do it better. I don't see any reason to check for misspelled words, numeric input, or whatsoever, nor any reason for providing a pre-read list.

My approach is: keep it as simple as possible: just two simple text boxes, with no pre-filling, no checks, no additional models, etc. There is no demand for this search function which obliges you to do it more complicated. The more complicated you make it, the more time it takes you, and the more points it may cost you.


_ _ ________________________ _ _ <br /> <br />Just SCJP (but 93%)
Paul Balm
Ranch Hand

Joined: Dec 13, 2008
Posts: 63
I agree with the above statement: Don't go with the combo boxes.

It's obvious that it would make the application much easier to use, if properly implemented. However, the requirements to implements this properly are missing: How do you handle the case where a record is added to the database? Either you
  • implement a refresh button,
  • you refresh secretly like Alex (which I dislike because such behaviours are totally obscure to the user - sorry ;-) ), or
  • implement a notification mechanism such that clients are notified when they need to refresh.


  • The third one would be the most "classy", but IMHO requirements are missing to justify spending the time to implement this.

    So I don't, and go with Rinke's basic solution.


    SCJP 1.4 -- SCJD Java 2 -- OCM JEA 5
    Rajesh Moorthy
    Ranch Hand

    Joined: Sep 23, 2008
    Posts: 30
    Following is my algorithm for the findByCriteria method:

    1) Read all records until EOF using readRecord() method.
    2) Each iteration returns a String[] containing a record. Keep on adding these String[] in an ArrayList. The result is an ArrayList containing String[].
    3) These String[] arrays contained in the ArrayList are compared against the input criteria[].
    4) The ArrayList indices (record numbers) matching the input criteria are stored in a long[] and returned.
    5) The GUI, in turn, receives this long[] and searches the stored ArrayList for the records (String[]) available at the indices stored in the long[] and displays the records.

    I feel that the algorithm is much cumbersome. According to the provided interface, findByCriteria method has to return only the record numbers. Due to this, the records have to read once again against these record numbers.

    I have to perform the whole algorithm, each time a search is done. Else, if the ArrayList is populated only once, the records will not be refreshed during subsequent searches.

    Does any one have a better idea?

    Thanks in advance !!!

    Regards,
    Rajesh.
    Alecsandru Cocarla
    Ranch Hand

    Joined: Feb 29, 2008
    Posts: 158
    Where do you store these ArrayLists? How do you know which ArrayList corresponds to each search/client?


    SCJP 1.4 100%
    SCJD 99.5%
    Rajesh Moorthy
    Ranch Hand

    Joined: Sep 23, 2008
    Posts: 30
    There is only one ArrayList, which I will instantiate and store as a Class variable. Every search re-populates this ArrayList. The contents of the ArrayList are String[] arrays, which will typically contain the records. The index of the ArrayList for each String[] array will be the record number.

    I am rather sure that this algorithm is not the right approach.

    Any suggestions, please.

    Thanks,
    Rajesh.
    Alecsandru Cocarla
    Ranch Hand

    Joined: Feb 29, 2008
    Posts: 158
    Not right, indeed. Because when two clients do searches at the same time, one of them can overwrite the other client's array list and replace it with its own. What will the first client receive then, when it "searches the stored ArrayList"?

    What I did was return the true record numbers (the record's place in the database), not some index in a temporary ArrayList. This way, you'll know your client always receives the record which is at that position in the database. You can use a cache for records (but a cache containing all records from the database, not the results of an arbitrary search), or you can always perform your reads/writes directly on the database.

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: findByCriteria()