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

findByCriteria()

 
Alain Huber
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex

Thanks so much for clarifying

Alain
 
Tomas Klubal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means exactly that to me! right on target!
[ November 28, 2007: Message edited by: Alex Belisle Turcot ]
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think I'll take this literally and go case sensitive as well. Thanks.
 
uzma ali
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Balm
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Rajesh Moorthy
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 158
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Where do you store these ArrayLists? How do you know which ArrayList corresponds to each search/client?
     
    Rajesh Moorthy
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 158
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic