aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes criteriaFind 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 "criteriaFind " Watch "criteriaFind " New topic
Author

criteriaFind

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Hi,
Is my logic for criteriaFind okay ?

public DataInfo[] criteriaFind(String criteria) throws DatabaseException{
// Create array of DataInfo
// Using StringTokenizer split the string based on delimiter
// Store Field Name(s) and Value(s) in an array
// Call getRecord repeatedly ( no. of records times )
// Compare FieldNames & Values in array with FieldNames & Values in DataInfo
// If match found put the data in array of DataInfo

}
Thanks
Dilip
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
You probably need 2 StringTokenizers - one for "," and one for "=". Also, how big of a DataInfo array? I found it best to put results into a vector, them create the DataInfo array.
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Thanks Paul,
Initially I thought of using Hashtable to store data from Criteria string. But Hashtable requires "key" name in order to get values and I don't want to hardcode any names/keys.
Hashtable ht = new Hashtable();
StringTokenizer tokens1 = new StringTokenizer(criteria,",");
while(tokens1.hasMoreTokens()) {
StringTokenizer tokens2 = new StringTokenizer(tokens1.nextToken(),"=");
while(tokens2.hasMoreTokens()) {
ht.put(tokens2.nextToken(),tokens2.nextToken());
}
}
Any suggestions ?
Thanks
Dilip
Paul Smiley
Ranch Hand

Joined: Jun 02, 2000
Posts: 244
I didn't hard code anything in the project - I made one class with all constants defined (column headers etc.) This way it is very flexible for future programming considerations and modifications per instructions. I used to work for a group which detested 'magic numbers' - they much preferred
if(tempInt == MyClass.NO_ROWS_RETURNED)
to
if(tempInt == -1)
this way it's more flexible code and more easily documented (and actually self doccumenting).
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
I was wondering which collection class suits to store data from criteria string.
Paul,
I liked the idea of maintaining separate class for constants.
Thanks
Dilip
Rahul Rathore
Ranch Hand

Joined: Sep 30, 2000
Posts: 324
An interface (rather than a class) may be better suited for defining constants. All member variables of an interface are implicitly
1. public,
2. static, and
3. final (see JLS)
Thus we may define
interface Metadata {
...
String ORIGIN_FIELD_NAME="origin";
int ORIGIN_FIELD_NUMBER=1;
...
}
But there is NO need for constants in criteriaFind() nor is there any need for hardcoding. Both the 'keys' and 'values' are contained within the database. The field names (i.e. 'keys' for criteria) are contained in the FieldInfo[] in Data. Thus the logic of criteriaFind() may be generically implemented as follows:-
1. Match the 'keys' provided in the criteria with the FieldInfo[] in Data to ascertain corresponding field Numbers.
2. If any 'key' is not found to have a match in the FieldInfo[] return null.
3. Otherwise- Iterate through all records. Using field Numbers found in Step 1, index into the values[] of each DataInfo, and match with value provided in the criteria. If all values match, add the record to a results Vector.
So implemented the criteriaFind() remains generic. It need not be changed even if the name/order/number of the fields is changed. So there is no need for any constants or any hardcoding.
I did not understand your question about the Collection class you need. If you mean a structure to hold the parsed criteria key-value pairs, then your choice of a hashtable is fine. I have done it differently.
If you mean a collection to hold the results of the criteriaFind, then I agree with Paul that we may use a Vector to store the results, then convert it to array before returning eg. return (DataInfo[])results.toArray(new DataInfo[0])
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
I was talking about the Collection to hold the parsed criteria key-value pairs. I will use Hashtable. Initially I thought passing "key" is the only way to get value from Hashtable. But I got idea how to iterate thorugh Hashtable without hardcoding key names.
Thank you guys..
Dilip
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
I have all the records returned by search in vector called results.
results = new Vector(1,1);
for (iLoop = 1; iLoop <= recordCount; iLoop++) {
   datainfo = getRecord(iLoop);
   :
   :
   // If match found add this datainfo to the vector
   results.add(datainfo);
}
I am wondering how to convert results into DataInfo array.
Thanks
Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Found the solution...
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
I have seen people mention the use of more than 1 tokenizer for the criteriaFind method. THIS IS NOT NECESSARY! The following example works fine:
import java.util.*;
public class Test
{
private static final String DELIMITERS = "',=";
public static void main(String[] args)
{
criteriaFind(args[0]);
}
public static void criteriaFind(String criteria)
{
StringTokenizer tokens = new StringTokenizer(criteria, DELIMITERS);
int numberOfKeys = tokens.countTokens() / 2;
String[] fields = new String[numberOfKeys];
String[] values = new String[numberOfKeys];
for (int i = 0; i < numberOfKeys; i++)
{
fields[i] = tokens.nextToken();
values[i] = tokens.nextToken();
}
for (int i = 0; i < numberOfKeys; i++)
{
System.out.println("Field: " + fields[i] + " -- Search Value: " + values[i]);
}
}
}
The key to take away from this example is that MULTIPLE delimiters can be specified when constructing the StringTokenizer object.
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
One other suggestion - instead of a Vector I think that an ArrayList is better since we only want to add elements to it dynamically. Somewhat of a performance gain.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: criteriaFind