Commit e6997366 authored by Matija Obreza's avatar Matija Obreza
Browse files

Allow for IS NULL filter

parent 18787ab7
......@@ -91,14 +91,16 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
sb.append(" inner join all_environment env on env.ALIS_Id=a.id ");
}
createQuery(sb, "a.Origin", jsonTree.get("origin"), params);
createQuery(sb, "a.Institute", jsonTree.get("institute"), params);
createQuery(sb, "t.Genus", jsonTree.get("genus"), params);
createQuery(sb, "t.Taxon_Name", jsonTree.get("taxon"), params);
createQuery(sb, "env.LongitudeD", jsonTree.get("lon"), params);
createQuery(sb, "env.LatitudeD", jsonTree.get("lat"), params);
createQuery(sb, "env.Altitude", jsonTree.get("alt"), params);
createQuery(sb, "crop.shortName", jsonTree.get("crop"), params);
StringBuffer sbf=new StringBuffer();
createQuery(sbf, "a.Origin", jsonTree.get("origin"), params);
createQuery(sbf, "a.Institute", jsonTree.get("institute"), params);
createQuery(sbf, "t.Genus", jsonTree.get("genus"), params);
createQuery(sbf, "t.Taxon_Name", jsonTree.get("taxon"), params);
createQuery(sbf, "env.LongitudeD", jsonTree.get("lon"), params);
createQuery(sbf, "env.LatitudeD", jsonTree.get("lat"), params);
createQuery(sbf, "env.Altitude", jsonTree.get("alt"), params);
createQuery(sbf, "crop.shortName", jsonTree.get("crop"), params);
sb.append(sbf.toString());
LOG.info("Parameter count: " + params.size());
LOG.info("Count query:\n" + sb.toString());
......@@ -146,7 +148,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
LOG.debug("Handling " + dbName);
if (fieldQuery != null) {
LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size() + " t=" + fieldQuery.getNodeType());
if (params.size() == 0)
if (sb.length() == 0)
sb.append(" where ");
else
sb.append(" and ");
......@@ -159,13 +161,17 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
// A filter value can be (a) explicit value or (b) an operation
int paramCount = params.size();
// (a) explicit values are handled by =? or by IN (?,?,..)
handleExplicitValues(sb, dbName, fieldQuery.elements(), params);
int handledCount = handleExplicitValues(sb, dbName, fieldQuery.elements(), params);
// do we have more?
if (handledCount > 0 && fieldQuery.size() > handledCount) {
sb.append(" OR ");
}
handledCount += handleNullValues(sb, dbName, fieldQuery.elements(), params);
int handledCount = params.size() - paramCount;
// do we have more?
LOG.info("FQS=" + fieldQuery.size() + " >? " + handledCount);
if (handledCount > 0 && fieldQuery.size() > handledCount) {
sb.append(" OR ");
}
......@@ -173,7 +179,6 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
// (b) operations are expressed as {"min":12} or {"max":33} or
// {"range":[3,10]} or {"like":"test"}
handleOperations(sb, dbName, fieldQuery.elements(), params);
}
// closing
......@@ -182,7 +187,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
private void handleOperations(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
private int handleOperations(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
int counter = 0;
while (elements.hasNext()) {
......@@ -246,9 +251,29 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
}
}
return counter;
}
private void handleExplicitValues(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
private int handleNullValues(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
int counter = 0;
while (elements.hasNext()) {
JsonNode filterValue = elements.next();
LOG.debug("Inspecting " + dbName + " ... " + filterValue);
if (filterValue.isNull()) {
LOG.debug("Adding " + filterValue);
counter++;
}
}
if (counter > 0) {
sb.append("\n ( ").append(dbName);
sb.append(" is NULL ) ");
}
return counter;
}
private int handleExplicitValues(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
int counter = 0;
while (elements.hasNext()) {
......@@ -273,6 +298,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
sb.append(" ) )");
}
return counter;
}
private void addParam(List<Object> params, JsonNode value) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment