* @param fields The search fields
* @return A string
*/
private String buildQuery(ArrayList<SearchField> fields)
{
Perl5Util util = new Perl5Util();
String query = "";
// Loop through the fields building the search query as we go.
for (SearchField field : fields)
{
// if the field is empty, then skip it and try a later one.
if (field.getQuery() == null)
continue;
// Add the conjunction for everything but the first field.
if (fields.indexOf(field) > 0)
query += " " + field.getConjunction() + " ";
// Two cases, one if a specific search field is specified or if
// ANY is given then just a general search is performed.
if ("ANY".equals(field.getField()))
{
// No field specified,
query += "(" + field.getQuery() + ")";
}
else
{
// Specific search field specified, add the field specific field.
// Replace singe quote's with double quotes (only if they match)
String subquery = util.substitute("s/\'(.*)\'/\"$1\"/g", field.getQuery());
// If the field is not quoted ...
if (!util.match("/\".*\"/", subquery))
{
// ... then seperate each word and re-specify the search field.
subquery = util.substitute("s/ / " + field.getField() + ":/g", subquery);
}
// Put the subquery into the general query
query += "("+field.getField()+":"+subquery+")";
}