It's not a secret anymore!*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "findByCriteria" Watch "findByCriteria" New topic
Author

findByCriteria

Sophie Jane
Greenhorn

Joined: Jul 25, 2006
Posts: 7
Hi All,

Just a Quick Question on the findByCriteria method.

It is defined as below:

// 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);


if the criteria was [Smallville, 210]?

Should I return only row numbers that have columns with both *Smallville* and 210 in this?

I guess this means looping through each record and then on each record looping through each field?

Am I on the correct track with this please?

Many thanks in advance,

Sophie J
Jared Cope
Ranch Hand

Joined: Aug 18, 2004
Posts: 243
Hi,

Originally posted by Sophie Jane:

Should I return only row numbers that have columns with both *Smallville* and 210 in this?

I guess this means looping through each record and then on each record looping through each field?


Yes, I do this -- checking each record in the database (that does not have the deleted flag) and each field of that record against the criteria. I interpret the search to be an 'AND' search where each record must have all components of the criteria to be included in the results.

Cheers, Jared.


SCJP 1.4 91%, SCJP 1.5 88%, SCJD B&S
Sophie Jane
Greenhorn

Joined: Jul 25, 2006
Posts: 7
Many Thanks Jared..
Peter Jakobsen
Greenhorn

Joined: Jul 26, 2006
Posts: 10
// Field n in the database file is described by
// criteria[n].

The way I understood and implemented this:

The criteria array has to have the same length as the number of columns on each field, so that: index 0 of the criteria needs to match the first column, index 1 of the criteria needs to match the second column and so on.

This includes some iteration over records and on each record matching each column with the corresponding value from the criteria.


SCJP, SCJD
Sophie Jane
Greenhorn

Joined: Jul 25, 2006
Posts: 7
Right I understand...

I guess this has an impact on how we let the user specify the search criteria in the front-end. If the search criteria array map directly onto the database record, I would have to specify a value for each, or a null in the case of a "Don't Care".

In two minds about this, Did you allow users to specify all values?

Thanks in advance...
Sophie J
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Sophie

I am working on URLyBird, just like you. It is a couple weeks ago since I implemented findByCriteria(String[] criteria).

My approach was fairly simple.

1. I check that the provided criteria array has the exact number of fields as the database file, if not throw an IllegalArgumentException. This way I make sure the array contains a field for every database column.
2. Then I start a loop that visits every record in the file. For that purpose I have a method isValidRecord(long recNo) that validates if a record number is withing the database scope.
3. If the record is valid I check the record is not deleted by means of reading its delete flag. If it is deleted, I ignore it and continue with the next record.
4. Then I read the record using the readRecord(long recNo) method already implemented.
5. Then I compare every field in the retrieved record with the fields in the provided criteria array. In this comparison I ignore any field which is null, and non-null fields are compared using the String.startsWith(String) method.
5. If a record matches all provided criteria, I add it to a List<Long>.
6. At the end I conver the List<Long> contents into a long[] and return it to the user. If no record is found I return an empty array.

However my implementation of DBAccess which I named DBAccessImpl is not the class directly exposed to the client applications through RMI. My RMI Server class works as wrapper (adapter) over the DBAccessImpl.

In the Server class I use a Map<String,String> to specify the search criteria, where the key is the column name, and the value is the criterium to be searched. If a column is not specified I assume it is null when I create the criteria array to be passed to the underlaying DBAccessImpl.

Now, I am working on my UI now. It is my intention that user fill in a form with all possible values he wants to filter the data with. Then I create a Map with all this values and pass it to the RMI Server. It will then convert it into an array and pass it to the underlaying DBAccessImpl.

That's the way I am doing it.

I hope this helps!
[ July 28, 2006: Message edited by: Edwin Dalorzo ]
Sophie Jane
Greenhorn

Joined: Jul 25, 2006
Posts: 7
Hi Edwin,

Thanks (again) for the advice. My initial thoughts were wide of the mark on this one, but I have a clear idea of this now.

Many thanks for helpful responses,
Sophie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: findByCriteria
 
Similar Threads
URLBird find by Hotel or Name
Clarification on findByCriteria interface documentation (B&S )
NX: findByCriteria Method
Searching
[NX] Question about the findByCriteria.