aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Search Criteria in the GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Search Criteria in the GUI" Watch "NX: Search Criteria in the GUI" New topic
Author

NX: Search Criteria in the GUI

Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I am now working with the GUI.
I have some doubts about it:
1. In my assignment sheet, it said:

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.

a. How can I perform the "OR" operation?
For search cases (1) name ONLY, (2) location ONLY, (3) name and location,
I just simply construct the corresponding arrays:
(1) String criteria = {"name", null, null, ..., null};
(2) String criteria = {null, "location", null, ..., null};
(3) String criteria = {"name", "location", null, ..., null};
to pass in to the find() function.
But how about OR?
If I first passing in (1), and then passing in (2), even I can get a OR "query", but seems I need to take extra effort to remove "duplicate" data from the 2 result sets, and combined them into 1 single result set.
b. Do I need an exact match?
since the find() function only matches for: data.startsWith(input);
If I need an exact match, I need to change it to data.equals(input)?

Please advice.
Thanks


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Nicholas,
a. How can I perform the "OR" operation?

The "and/or" in your instructions can be translated in : search on name, or search on location , or search on name and location.
b. Do I need an exact match?
since the find() function only matches for: data.startsWith(input);
If I need an exact match, I need to change it to data.equals(input)?

A wildcard match may be this : you search from "fred" + 0x0 up to the field length, to "fred" + 0xFF up to the field length. If you implement your wildcard match that way (through an overloaded findByCriteria() method), your exact match search is easy to perform : you just need to search from "fred" + 0x0 up to the field length to "fred" + 0x0 up to the field length. Does it make sense ?
Best,
Phil.
[ November 18, 2003: Message edited by: Philippe Maquet ]
Seid Myadiyev
Ranch Hand

Joined: Jul 02, 2002
Posts: 196
Hello Nicholas,
I have the same requirement for my project. In my design I created a Criteria class which defines special fields to distinguish between whether user selected AND or OR and performs comparison of fields accordingly. Logic for field comparison is encapsulated in the same Criteria class.
Developers, please share your ideas on this design.
Thank you!
+Seid
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Seid,
I have the same requirement for my project. In my design I created a Criteria class which defines special fields to distinguish between whether user selected AND or OR and performs comparison of fields accordingly.

Oops ! AFAIK from what I've read on this forum in the last few months, the "and/or" doesn't mean more than what I wrote above. You go a step further (and I thought the same when I started). The possible issue is this one : is it really further (OK) or is it simply different from what SUN expects ?
Best,
Phil.
Seid Myadiyev
Ranch Hand

Joined: Jul 02, 2002
Posts: 196
Hello Philippe,
I agree with you on AND/OR logic. But the question is how do you know if you need to continue comparing if AND was selected.
For example "Name AND Location" - how do you know if you need to match Location against the Criteria after comparison on Name returned "true"?
Thanks!
+Seid
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Thanks for replying
1. That is to say, I can simply forget about the implementation for "name or location"?
2. For the wild card search, in fact, I can use String.indexOf(String).
If the result > -1, it means "match", mismatch otherwise.
Can I do that?
Many thanks.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Seid,
I agree with you on AND/OR logic. But the question is how do you know if you need to continue comparing if AND was selected.
For example "Name AND Location" - how do you know if you need to match Location against the Criteria after comparison on Name returned "true"?

If a comparison on Name returns true, you must go on with a comparison on Location.
If a comparison on Name returns false, you are done.
Cheers,
Phil.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Nicholas,
2. For the wild card search, in fact, I can use String.indexOf(String).
If the result > -1, it means "match", mismatch otherwise.
Can I do that?

No, you cannot. With indexOf() you would find "Alfred" !
But startsWith() looks fine.
Best,
Phil.
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Philippe Hi!
I'm new in the sort of terminology like "wild card".
Can you please, explain me your search options in simplest words?
For example I have to search by name AND by location.
Does this mean I have to search by name, then provide another search by location and then compare both recNo arrays?
If I have to search by name OR by location,
does this mean I search by name and then (if not found by name)search by location?
How to perform exactly matching by NOT exactly find method I don't understand too
Please, help.
[ November 18, 2003: Message edited by: Peter Kovgan ]
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
[REPOSTED BELOW (FIRST HALF OF THE POST LOST AFTER EDITING)]
Hi Peter,

and the overloaded one :

Functionally, the whole job is performed by the second one (in practice by a private doFindByCriteria() with the same second signature).
I have a search() business method wich uses the second overloaded findByCriteria() method.
To find "Fred" records (exact match), it passes to findByCriteria() "Fred" right-padded with 0x00 chars as fromCriteria and toCriteria. Notice that if I wanted to support wild-card-search at the business search() method level in the future, I'd just need to pass "Fred" right-padded with 0x00 chars as fromCriteria and "Fred" right-padded with 0xFF chars as toCriteria.
The first thing findByCriteria() does is to right-pad all non-null fromCriteria with 0x00 chars and all non-null toCriteria with 0xFF chars. As my search() method did it already, criteria stay unchanged.
Now the find itself is simple : you just keep all records whose corresponding field values fall between the fromCriteria and toCriteria values, applying a logical AND if criteria are multiple (it means that once one comparison fails you may reject the record without any further comparison).
Now my own implementation is a little more complex than that, because fields may optionally be indexed, but the main idea is there.
Best,
Phil.
[ November 18, 2003: Message edited by: Philippe Maquet ]
[ November 20, 2003: Message edited by: Philippe Maquet ]
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Thank you very much, Filippe!
The clearest explanation I ever saw.
Many, many thanks!
Peter.
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Filippe, why not simple:
int records[] = find(String[] criteria, int chars_from_right)
?
Peter Kovgan
Ranch Hand

Joined: Sep 18, 2003
Posts: 84
Hi!
Philippe,
your method find(Sring[],String[])- " exact search " calls find(String[])- " not exact search ",
you need twice read data from data file?
first time to make "wild card" serach, and second - else one time check selected records?
What you think - is it the best solution we can propose?
May be it is better to make some filter on the client side and not perform 2 searches?
[ November 19, 2003: Message edited by: Peter Kovgan ]
Terry Martinson
Ranch Hand

Joined: Oct 18, 2003
Posts: 293
Hello. My interpretation of how the two find methods work is that the first one just calls the second one with appropriate arguments, and that the work is all done in the second one. So, not reading the data from the data file twice.
Based on other stuff I've read in this forum, I am leaning towards doing something similar and doing all the work in the search rather than filtering data client side.
TJ


SCJP, SCJD, SCWCD, SCBCD
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
Seid Myadiyev -

Developers, please share your ideas on this design.

I think giving them the option to do an AND searh or an OR search is
the way to go. Then you are not second guessing yourself and you know
you have all areas covered. Plus once you do one the other comes easy.
I didn't even bother using the "find" method sun wanted us to implement.
I implemented it and tested it thoroughly but to me its a different type
of search and trying to use it for your location/name search is
very awkward. Another person did it this way, documented it, and scored
very high.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi all,
It looks like my post above is half lost and doesn't mean anything anymore... It comes from a bug on this forum software who looses part of your posts when you edit them and there is a mix of quote bloks and code bloks. When will they correct that ?!
I have no copy of the original text, but I'll try to edit and complete it tomorrow morning just to make it understandable.
Sorry about that.
Best,
Phil.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Peter,
From your questions above, it looks like that despite you wrote that my post was "The clearest explanation I ever saw", you didn't get it. I know that you read my original post, but in the meantime, my post is not clear at all anymore ! (the bug I mentioned above appears when you edit your post, and I had the very bad idea to correct a typo ).
Anyway it's too late for me here tonight to rewrite that post, I'll do it tomorrow morning, and I'll try to take your new questions into account.
Best,
Phil.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Peter,
Peter:
Can you please, explain me your search options in simplest words?

Here is it.
First of all, I must tell you that I decided to implement a findByCriteria() method which supports criteria ranges (from/to), in order to optionally take into account the URLyBird 48 hours criteria.
So I added to this original method :

this overloaded one :

Now as a wildcard search is just a special case of a range wildcard search, the whole job is done by the overloaded method.
My search() business method uses the second overloaded findByCriteria() method as well.
To find "Fred" records (exact match), it passes to findByCriteria() "Fred" right-padded with 0x00 chars as fromCriteria and toCriteria. Notice that if I wanted to support wild-card-search at the business search() method level in the future, I'd just need to pass "Fred" right-padded with 0x00 chars as fromCriteria and "Fred" right-padded with 0xFF chars as toCriteria.
The first thing findByCriteria() does is to right-pad all non-null fromCriteria with 0x00 chars and all non-null toCriteria with 0xFF chars. As my search() method did it already, criteria stay unchanged.
Now the find itself is simple : you just keep all records whose corresponding field values fall between the fromCriteria and toCriteria values (I use String.compareTo()), applying a logical AND if criteria are multiple (it means that once one comparison fails you may reject the record without any further comparison).
Now my own implementation is a little more complex than that, because fields may optionally be indexed, but the main idea is there.
Peter:
Filippe, why not simple:
int records[] = find(String[] criteria, int chars_from_right) ?

I don't understand what you mean.
Philippe,
your method find(Sring[],String[])- " exact search " calls find(String[])- " not exact search ",
you need twice read data from data file?
first time to make "wild card" serach, and second - else one time check selected records?
What you think - is it the best solution we can propose?
May be it is better to make some filter on the client side and not perform 2 searches?

No, both perform a wild-card search, the second one just supports criteria ranges. I simplify a bit because of course criteria are arrays and not Strings, but if you call findByCriteria("Fred"), this method will internally call findByCriteria("Fred", "Fred"), which this one will translate in something like "I must find all records where the field value falls between "Fred+0x00+...0x00" and "Fred+0xFF+...0xFF"".
It means that records are read only once.
Terry:
Hello. My interpretation of how the two find methods work is that the first one just calls the second one with appropriate arguments, and that the work is all done in the second one. So, not reading the data from the data file twice.

Exactly that !
Terry:
Based on other stuff I've read in this forum, I am leaning towards doing something similar and doing all the work in the search rather than filtering data client side.

And I think you're right, because it's more efficient : why should we send in the wire records we don't need ?
Bill:
I think giving them the option to do an AND searh or an OR search is
the way to go. Then you are not second guessing yourself and you know
you have all areas covered.

It's defendable too. But be aware that if it's not what SUN expects, you'll uselessly have a GUI which is more complex to the user than expected. You could also ask yourself if it makes any sense for a CSR to perform some OR search on location and name. Put in other words, do you think a customer could ask for a room in New York OR in a Hilton hotel ?

Best,
Phil.
[ November 20, 2003: Message edited by: Philippe Maquet ]
joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
How 'bout the following solution:
Overload the find() with a boolean, which designates whether or not the search will be an exact match( use equals ) or not exact ( use startsWith() )
public int[] find(String[] criteria, boolean isExact);
Then in the code where you compare the fieldvalue with the searchvalue you could use the boolean to determine whether or not to use equals() or startsWith().
Then you could use this new method in the original find().
public int[] find(String[] criteria){
find(criteria, false); // not an exact match
}
And also in a business search method.
find(criteria,true); // to get an exact match
[ December 21, 2003: Message edited by: Joe Black ]
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Joe,
If you don't need criteria ranges, I think it's a valid solution, *and* much simpler than what I described above (simplicity is an advantage).
Best,
Phil.
joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
Phil,
For your business search method, did you return a Collection/2d array with the matching records, or just the matching record numbers?
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Joe,
For your business search method, did you return a Collection/2d array with the matching records, or just the matching record numbers?

Record numbers *and* records data in *one* trip. BTW it relates to the main argument in favor of having such a business method implemented server-side.
Regards,
Phil.
 
 
subject: NX: Search Criteria in the GUI