Commit 472c4a7e authored by Matija Obreza's avatar Matija Obreza
Browse files

Handle operators in filtering

parent baa57ba4
...@@ -119,41 +119,124 @@ public class GenesysFilterServiceImpl implements GenesysFilterService { ...@@ -119,41 +119,124 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
private void createQuery(StringBuffer sb, String dbName, JsonNode fieldQuery, List<Object> params) { private void createQuery(StringBuffer sb, String dbName, JsonNode fieldQuery, List<Object> params) {
LOG.debug("Handling " + dbName); LOG.debug("Handling " + dbName);
if (fieldQuery != null) { if (fieldQuery != null) {
LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size()); LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size() + " t=" + fieldQuery.getNodeType());
if (params.size() == 0) if (params.size() == 0)
sb.append(" where "); sb.append(" where ");
else else
sb.append(" and "); sb.append(" and ");
// if (fieldQuery.getNodeType() == JsonNodeType.ARRAY) { // Opening
sb.append("\n ").append(dbName); sb.append(" ( ");
if (fieldQuery.size() <= 1)
sb.append(" = "); // Always an array of filters
else if (fieldQuery.getNodeType() == JsonNodeType.ARRAY) {
sb.append(" IN (");
// A filter value can be (a) explicit value or (b) an operation
if (fieldQuery.size() > 1) {
int i = 0; int paramCount = params.size();
for (JsonNode x : fieldQuery) { // (a) explicit values are handled by =? or by IN (?,?,..)
if (i > 0) { handleExplicitValues(sb, dbName, fieldQuery.elements(), params);
sb.append(",?");
} else { int handledCount = params.size() - paramCount;
sb.append("?"); // do we have more?
if (fieldQuery.size() > handledCount) {
sb.append(" OR ");
}
// (b) operations are expressed as {"min":12} or {"max":33} or
// {"range":[3,10]} or {"like":"test"}
handleOperations(sb, dbName, fieldQuery.elements(), params);
}
// closing
sb.append(" ) ");
}
}
private void handleOperations(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.isObject()) {
LOG.debug("Adding " + filterValue);
JsonNode range = filterValue.findValue("range");
if (range.isArray()) {
LOG.debug("Adding array: " + range);
if (counter>0) {
sb.append(" or ");
}
counter++;
// must be an array
sb.append("\n ( ").append(dbName);
sb.append(" between ? and ? ) ");
addParam(params, range.get(0));
addParam(params, range.get(1));
}
JsonNode min = filterValue.findValue("min");
if (min != null && min.isNumber()) {
LOG.debug("Adding min number: " + min);
if (counter>0) {
sb.append(" or ");
} }
LOG.debug(" + " + x + " " + x.getNodeType()); counter++;
addParam(params, x); // must be an array
// addParam(params, filterValue);
}
i++; JsonNode max = filterValue.findValue("max");
if (max != null && max.isNumber()) {
LOG.debug("Adding max number: " + max);
if (counter>0) {
sb.append(" or ");
}
counter++;
// must be an array
// addParam(params, filterValue);
}
JsonNode like = filterValue.findValue("like");
if (like != null && like.isTextual()) {
LOG.debug("Adding LIKE : " + like);
if (counter>0) {
sb.append(" or ");
}
counter++;
// must be an array
// addParam(params, filterValue);
} }
} else {
sb.append("?");
addParam(params, fieldQuery);
} }
if (fieldQuery.size() > 1)
sb.append(") ");
// }
} }
}
private void handleExplicitValues(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.isNumber() || filterValue.isTextual() || filterValue.isBoolean()) {
LOG.debug("Adding " + filterValue);
counter++;
addParam(params, filterValue);
}
}
if (counter == 1) {
sb.append("\n ( ").append(dbName);
sb.append(" = ? ) ");
} else {
sb.append("\n ( ").append(dbName);
sb.append(" IN ( ?");
for (int i = counter - 1; i > 0; i--) {
sb.append(",?");
}
sb.append(" ) )");
}
} }
private void addParam(List<Object> params, JsonNode value) { private void addParam(List<Object> params, JsonNode value) {
......
...@@ -28,5 +28,6 @@ log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ...@@ -28,5 +28,6 @@ log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ### ### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout log4j.rootLogger=info, stdout
log4j.category.org.crophub=debug #log4j.category.org.crophub=debug
log4j.category.org.hibernate.search=debug #log4j.category.org.hibernate.search=debug
log4j.category.org.apache.tomcat.jdbc.pool=debug
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
<property name="driverClassName" value="${db.driverClassName}" /> <property name="driverClassName" value="${db.driverClassName}" />
<property name="username" value="${db.username}" /> <property name="username" value="${db.username}" />
<property name="password" value="${db.password}" /> <property name="password" value="${db.password}" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="true" />
<!-- <!--
<property name="timeBetweenEvictionRunsMillis" value="5000" /> <property name="timeBetweenEvictionRunsMillis" value="5000" />
<property name="minEvictableIdleTimeMillis" value="5000" /> <property name="minEvictableIdleTimeMillis" value="5000" />
......
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
<c:forEach items="${filters.keySet()}" var="by"> <c:forEach items="${filters.keySet()}" var="by">
<c:set value="${filters[by].getClass().simpleName}" var="clazz" /> <c:set value="${filters[by].getClass().simpleName}" var="clazz" />
<div> <div>
<spring:message code="${by}" /> <spring:message code="${by}" />: <b>
: <b> <c:choose> <c:choose>
<c:when test="${clazz eq 'Taxonomy'}"> <c:when test="${clazz eq 'Taxonomy'}">
${filters[by].taxonName} ${filters[by].taxonName}
</c:when> </c:when>
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
<spring:message code="accessions.number" arguments="${pagedData.totalElements}" /> <spring:message code="accessions.number" arguments="${pagedData.totalElements}" />
<br /> <br />
<spring:message code="paged.pageOfPages" arguments="${pagedData.number+1},${pagedData.totalPages}" /> <spring:message code="paged.pageOfPages" arguments="${pagedData.number+1},${pagedData.totalPages}" />
<a class="${pagedData.number eq 0 ? 'disabled' :''}" href="?page=${pagedData.number eq 0 ? 1 : pagedData.number}">⇇ Previous</a> <a href="<spring:url value=""><spring:param name="page" value="${pagedData.number eq 0 ? 1 : pagedData.number}" /><spring:param name="filter" value="${filter}" /></spring:url>">⇇ Previous</a>
<a href="<spring:url value=""><spring:param name="page" value="${pagedData.number+2}" /><spring:param name="filter" value="${filter}" /></spring:url>">Next ⇉</a> <a href="<spring:url value=""><spring:param name="page" value="${pagedData.number+2}" /><spring:param name="filter" value="${filter}" /></spring:url>">Next ⇉</a>
</div> </div>
......
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