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 { ...@@ -91,14 +91,16 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
sb.append(" inner join all_environment env on env.ALIS_Id=a.id "); sb.append(" inner join all_environment env on env.ALIS_Id=a.id ");
} }
createQuery(sb, "a.Origin", jsonTree.get("origin"), params); StringBuffer sbf=new StringBuffer();
createQuery(sb, "a.Institute", jsonTree.get("institute"), params); createQuery(sbf, "a.Origin", jsonTree.get("origin"), params);
createQuery(sb, "t.Genus", jsonTree.get("genus"), params); createQuery(sbf, "a.Institute", jsonTree.get("institute"), params);
createQuery(sb, "t.Taxon_Name", jsonTree.get("taxon"), params); createQuery(sbf, "t.Genus", jsonTree.get("genus"), params);
createQuery(sb, "env.LongitudeD", jsonTree.get("lon"), params); createQuery(sbf, "t.Taxon_Name", jsonTree.get("taxon"), params);
createQuery(sb, "env.LatitudeD", jsonTree.get("lat"), params); createQuery(sbf, "env.LongitudeD", jsonTree.get("lon"), params);
createQuery(sb, "env.Altitude", jsonTree.get("alt"), params); createQuery(sbf, "env.LatitudeD", jsonTree.get("lat"), params);
createQuery(sb, "crop.shortName", jsonTree.get("crop"), 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("Parameter count: " + params.size());
LOG.info("Count query:\n" + sb.toString()); LOG.info("Count query:\n" + sb.toString());
...@@ -146,7 +148,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -146,7 +148,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
LOG.debug("Handling " + dbName); LOG.debug("Handling " + dbName);
if (fieldQuery != null) { if (fieldQuery != null) {
LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size() + " t=" + fieldQuery.getNodeType()); LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size() + " t=" + fieldQuery.getNodeType());
if (params.size() == 0) if (sb.length() == 0)
sb.append(" where "); sb.append(" where ");
else else
sb.append(" and "); sb.append(" and ");
...@@ -159,13 +161,17 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -159,13 +161,17 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
// A filter value can be (a) explicit value or (b) an operation // 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 (?,?,..) // (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? // do we have more?
LOG.info("FQS=" + fieldQuery.size() + " >? " + handledCount);
if (handledCount > 0 && fieldQuery.size() > handledCount) { if (handledCount > 0 && fieldQuery.size() > handledCount) {
sb.append(" OR "); sb.append(" OR ");
} }
...@@ -173,7 +179,6 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -173,7 +179,6 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
// (b) operations are expressed as {"min":12} or {"max":33} or // (b) operations are expressed as {"min":12} or {"max":33} or
// {"range":[3,10]} or {"like":"test"} // {"range":[3,10]} or {"like":"test"}
handleOperations(sb, dbName, fieldQuery.elements(), params); handleOperations(sb, dbName, fieldQuery.elements(), params);
} }
// closing // closing
...@@ -182,7 +187,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -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; int counter = 0;
while (elements.hasNext()) { while (elements.hasNext()) {
...@@ -246,9 +251,29 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -246,9 +251,29 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
} }
} }
} }
return counter;
}
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 void handleExplicitValues(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) { private int handleExplicitValues(StringBuffer sb, String dbName, Iterator<JsonNode> elements, List<Object> params) {
int counter = 0; int counter = 0;
while (elements.hasNext()) { while (elements.hasNext()) {
...@@ -273,6 +298,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -273,6 +298,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
} }
sb.append(" ) )"); sb.append(" ) )");
} }
return counter;
} }
private void addParam(List<Object> params, JsonNode value) { 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