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 {
private void createQuery(StringBuffer sb, String dbName, JsonNode fieldQuery, List<Object> params) {
LOG.debug("Handling " + dbName);
if (fieldQuery != null) {
LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size());
LOG.debug("Adding " + fieldQuery + " sz=" + fieldQuery.size() + " t=" + fieldQuery.getNodeType());
if (params.size() == 0)
sb.append(" where ");
else
sb.append(" and ");
// if (fieldQuery.getNodeType() == JsonNodeType.ARRAY) {
sb.append("\n ").append(dbName);
if (fieldQuery.size() <= 1)
sb.append(" = ");
else
sb.append(" IN (");
// Opening
sb.append(" ( ");
if (fieldQuery.size() > 1) {
int i = 0;
for (JsonNode x : fieldQuery) {
if (i > 0) {
sb.append(",?");
} else {
sb.append("?");
// Always an array of filters
if (fieldQuery.getNodeType() == JsonNodeType.ARRAY) {
// 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 = params.size() - paramCount;
// do we have more?
if (fieldQuery.size() > handledCount) {
sb.append(" OR ");
}
LOG.debug(" + " + x + " " + x.getNodeType());
addParam(params, x);
i++;
// (b) operations are expressed as {"min":12} or {"max":33} or
// {"range":[3,10]} or {"like":"test"}
handleOperations(sb, dbName, fieldQuery.elements(), params);
}
} else {
sb.append("?");
addParam(params, fieldQuery);
// 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 ");
}
if (fieldQuery.size() > 1)
sb.append(") ");
// }
counter++;
// must be an array
// addParam(params, filterValue);
}
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);
}
}
}
}
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) {
......
......@@ -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' ###
log4j.rootLogger=info, stdout
log4j.category.org.crophub=debug
log4j.category.org.hibernate.search=debug
#log4j.category.org.crophub=debug
#log4j.category.org.hibernate.search=debug
log4j.category.org.apache.tomcat.jdbc.pool=debug
......@@ -30,6 +30,9 @@
<property name="driverClassName" value="${db.driverClassName}" />
<property name="username" value="${db.username}" />
<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="minEvictableIdleTimeMillis" value="5000" />
......
......@@ -19,8 +19,8 @@
<c:forEach items="${filters.keySet()}" var="by">
<c:set value="${filters[by].getClass().simpleName}" var="clazz" />
<div>
<spring:message code="${by}" />
: <b> <c:choose>
<spring:message code="${by}" />: <b>
<c:choose>
<c:when test="${clazz eq 'Taxonomy'}">
${filters[by].taxonName}
</c:when>
......@@ -48,7 +48,7 @@
<spring:message code="accessions.number" arguments="${pagedData.totalElements}" />
<br />
<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>
</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