query.append("FROM individual AS a, entity AS e ");
query.append("WHERE e.entityid = a.entity");
fieldList = criteria.getIndividualFieldList();
break;
default : // we aren't 1 or 2 we cannot continue.
throw new EJBException("Merge Type must be 1 or 2");
}
// If the list is not zero then we need to reduce the selection further.
if (list != 0)
{
query.append(" AND a.list = ");
query.append(list);
}
// The order of the returned results probably doesn't matter.
// Now the query is ready, lets fire up the data access layer.
StringBuffer domainQueryIds = new StringBuffer("(");
CVDal cvdal = new CVDal(this.dataSource);
ArrayList domain = new ArrayList();
cvdal.setSqlQuery(query.toString());
ResultSet domainResultSet = cvdal.executeQueryNonParsed();
// Do this non-parsed so on one pass through the resultset
// we can build the hashmap for display, and get the ids
// gathered up for the query.
try
{
boolean firstPass = true;
while (domainResultSet.next())
{
HashMap record = new HashMap();
Object id = domainResultSet.getObject(1);
record.put("id",id); // The Object can be cast to a Number
record.put("title", domainResultSet.getString(2));
if(mergeType == 1){
record.put("owner", domainResultSet.getString(3));
}
if(mergeType == 2){
record.put("entityID", domainResultSet.getString(3));
record.put("entityName", domainResultSet.getString(4));
}
// This TreeMap will allow us to easily obtain the underlying
// HashMap when we find matches.
domain.add(record);
if (!firstPass)
{
domainQueryIds.append(", ");
} else {
firstPass = false;
}
domainQueryIds.append(id);
} // end while (domainResultSet.next())
domainQueryIds.append(")");
} catch (SQLException e) {
System.out.println("[Exception][MergeEJB.performSearch] SQLException Thrown: " + e);
cvdal.destroy();
throw new EJBException(e);
} finally {
if (domainResultSet != null)
{
try { domainResultSet.close(); domainResultSet = null; } catch (SQLException e) {}
}
cvdal.setSqlQueryToNull();
}
// domain is now an TreeMap of hashmaps. HashMaps are used to minimize changes
// with the rest of the code during a rewrite of this algorithm on May 21, 2004.
// Previously we just used CVDal.executeQuery(); which outputted collections of hashmaps.
// Not enough results from the query to actually do anything
// So return an empty ResultVO.
if (domain.size() < 2)
{
return searchResults; // is currently empty
}
int threshhold = criteria.getThreshhold();
// Could potentially save a lot of time here by checking if the sum of the
// criteria scores is < threshhold and just returrning no results
// as there can never be a valid set of matches.
// criteriaFields is an arraylist of HashMaps. The internal HashMap contains the
// actual fields pulled from the database. Keyed to the ID
// So comparison can be done fast and in memory.
ArrayList criteriaFields = new ArrayList();
int criteriaSize = matchCriteria.size();
for (int i = 0; i < criteriaSize; i++)
{
HashMap criterion = (HashMap)matchCriteria.get(i);
int fieldIndex = ((Integer)criterion.get(SearchCriteriaVO.FIELD_KEY)).intValue();
MergeField field = (MergeField)fieldList.get(fieldIndex);
StringBuffer fieldQuery = new StringBuffer(field.getQuery());
fieldQuery.append(domainQueryIds);
cvdal.setSqlQuery(fieldQuery.toString());
ResultSet fieldResultSet = cvdal.executeQueryNonParsed();
HashMap fieldMap = new HashMap();
try
{
while(fieldResultSet.next())
{
// Put the String in the
if(fieldResultSet.getObject(2) != null && fieldResultSet.getString(1) != null){
fieldMap.put(fieldResultSet.getObject(2), fieldResultSet.getString(1).trim());
}
}
criteriaFields.add(fieldMap);
} catch (SQLException se) {
System.out.println("[Exception][MergeEJB.performSearch] SQLException Thrown: " + se);
throw new EJBException(se);
} finally {
if (domainResultSet != null)
{
try { fieldResultSet.close(); domainResultSet = null; } catch (SQLException e) {}
}