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

Added conversion for date filters

parent c0913274
package org.genesys2.server.service.filter; package org.genesys2.server.service.filter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.genesys.blocks.model.filters.DateFilter;
import org.genesys.blocks.model.filters.NumberFilter; import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter; import org.genesys.blocks.model.filters.StringFilter;
import org.genesys2.server.service.FilterConstants; import org.genesys2.server.service.FilterConstants;
...@@ -168,56 +178,61 @@ public class AppliedFiltersConverter { ...@@ -168,56 +178,61 @@ public class AppliedFiltersConverter {
private static void apply(AccessionFilter f, String filterName, AppliedFilter af) throws Exception { private static void apply(AccessionFilter f, String filterName, AppliedFilter af) throws Exception {
switch (filterName) { switch (filterName) {
case "crops": { case FilterConstants.CROPS: {
f.crop = take1String(af); f.crop = take1String(af);
break; break;
} }
case "cropName": { case FilterConstants.CROPNAME: {
f.cropName = take1String(af); f.cropName = take1String(af);
break; break;
} }
case "sampStat": { case FilterConstants.SAMPSTAT: {
f.sampStat = toIntegerSet(af); f.sampStat = toIntegerSet(af);
break; break;
} }
case "storage": { case FilterConstants.STORAGE: {
f.storage = toIntegerSet(af); f.storage = toIntegerSet(af);
break; break;
} }
case "available": { case FilterConstants.AVAILABLE: {
f.available = convertBoolean(af.getValues()); f.available = convertBoolean(af.getValues());
break; break;
} }
case "mlsStatus": { case FilterConstants.ART15: {
f.art15 = convertBoolean(af.getValues());
break;
}
case FilterConstants.MLSSTATUS: {
f.mlsStatus = convertBoolean(af.getValues()); f.mlsStatus = convertBoolean(af.getValues());
break; break;
} }
case "acceNumb": { case FilterConstants.IN_SGSV:
f.acceNumb = convertStringFilter(af); case FilterConstants.SGSV: {
f.sgsv = convertBoolean(af.getValues());
break; break;
} }
case "art15": { case FilterConstants.ACCENUMB: {
f.art15 = convertBoolean(af.getValues()); f.acceNumb = convertStringFilter(af);
break; break;
} }
case "historic": { case FilterConstants.HISTORIC: {
f.historic = convertBoolean(af.getValues()); f.historic = convertBoolean(af.getValues());
break; break;
} }
case ACCEFILT_SGSV: { case FilterConstants.SEQUENTIAL_NUMBER: {
f.sgsv = convertBoolean(af.getValues()); f.seqNo = convertNumberFilter(af);
break; break;
} }
case "pdci": { case FilterConstants.LISTS: {
f.pdci = convertNumberFilter(af); f.list = toUUIDSet(af);
break; break;
} }
case "seqNo": { case FilterConstants.PDCI: {
f.seqNo = convertNumberFilter(af); f.pdci = convertNumberFilter(af);
break; break;
} }
case "lists": { case FilterConstants.LAST_MODIFIED_DATE: {
f.list = toUUIDSet(af); f.lastModifiedDate = convertDateFilter(af);
break; break;
} }
default: default:
...@@ -275,6 +290,49 @@ public class AppliedFiltersConverter { ...@@ -275,6 +290,49 @@ public class AppliedFiltersConverter {
return f; return f;
} }
private static DateFilter convertDateFilter(AppliedFilter af) {
LOG.debug("Date filters {}", af.getValues());
if (CollectionUtils.isEmpty(af.getValues())) {
return null;
}
DateFilter df = new DateFilter();
af.getValues().forEach(fv -> {
if (fv instanceof LiteralValueFilter) {
// Explicit 1 date
LiteralValueFilter lvf = (LiteralValueFilter) fv;
Date date = parseDate((String) lvf.getValue());
Calendar date1 = Calendar.getInstance();
date1.setTime(date);
date1.add(Calendar.DAY_OF_MONTH, 1);
df.between = new Date[] { date, date1.getTime() };
} else if (fv instanceof MinValueFilter) {
df.ge = parseDate((String) ((MinValueFilter) fv).getFrom());
} else if (fv instanceof MaxValueFilter) {
df.le = parseDate((String) ((MaxValueFilter) fv).getTo());
} else if (fv instanceof ValueRangeFilter) {
df.between = new Date[] { parseDate((String) ((ValueRangeFilter) fv).getFrom()), parseDate((String) ((ValueRangeFilter) fv).getTo()) };
} else {
throw new RuntimeException("Unhandled date filter type=" + fv.getClass());
}
});
return df;
}
private static Date parseDate(String filtersDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
try {
Date date = dateFormat.parse(filtersDate);
LOG.trace("Parsed date {} to {}", filtersDate, date);
return date;
} catch (ParseException e) {
throw new RuntimeException("Could not parse date yyyy-M-d from " + filtersDate);
}
}
private static Boolean convertBoolean(Set<FilterValue> values) { private static Boolean convertBoolean(Set<FilterValue> values) {
final List<Boolean> picks = new ArrayList<Boolean>(3); final List<Boolean> picks = new ArrayList<Boolean>(3);
values.stream().forEach(fv -> { values.stream().forEach(fv -> {
......
...@@ -4,6 +4,10 @@ import static com.google.common.collect.Sets.*; ...@@ -4,6 +4,10 @@ import static com.google.common.collect.Sets.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.genesys2.server.component.elastic.ElasticQueryBuilder; import org.genesys2.server.component.elastic.ElasticQueryBuilder;
import org.genesys2.server.service.FilterConstants; import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.filter.AccessionFilter; import org.genesys2.server.service.filter.AccessionFilter;
...@@ -15,7 +19,9 @@ import org.junit.Test; ...@@ -15,7 +19,9 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
...@@ -280,7 +286,6 @@ public class AccessionFilterTest { ...@@ -280,7 +286,6 @@ public class AccessionFilterTest {
af.buildQuery().accept(esQb, null); af.buildQuery().accept(esQb, null);
LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'"); LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'");
} }
@Test @Test
public void testIsNullFilter() { public void testIsNullFilter() {
...@@ -294,7 +299,6 @@ public class AccessionFilterTest { ...@@ -294,7 +299,6 @@ public class AccessionFilterTest {
af.buildQuery().accept(esQb, null); af.buildQuery().accept(esQb, null);
LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'"); LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'");
} }
@Test @Test
public void testNotIsNullFilter() { public void testNotIsNullFilter() {
...@@ -308,4 +312,55 @@ public class AccessionFilterTest { ...@@ -308,4 +312,55 @@ public class AccessionFilterTest {
af.buildQuery().accept(esQb, null); af.buildQuery().accept(esQb, null);
LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'"); LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'");
} }
@Test
public void testLastModifiedDateRangeFilter() throws ParseException, JsonParseException, JsonMappingException, IOException {
AppliedFilters filters = new ObjectMapper().readValue("{\"lastModifiedDate\":[\"2018-8-30\"]}", AppliedFilters.class);
// filters.add(new AppliedFilter().setFilterName(FilterConstants.LAST_MODIFIED_DATE).addFilterValue(new FilterHandler.LiteralValueFilter("2018-8-30")));
LOG.debug("Date filter {}", filters);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
AccessionFilter af = AccessionFilter.convert(filters);
assertThat(af.lastModifiedDate, notNullValue());
assertThat(af.lastModifiedDate.between, notNullValue());
assertThat(af.lastModifiedDate.between.length, is(2));
assertThat(af.lastModifiedDate.between[0], is(dateFormat.parse("2018-8-30")));
assertThat(af.lastModifiedDate.between[1], is(dateFormat.parse("2018-8-31")));
filters = new ObjectMapper().readValue("{\"lastModifiedDate\":[{\"dateRange\":[\"2017-12-12\",\"2018-02-02\"]}]}", AppliedFilters.class);
LOG.debug("Date filter {}", filters);
af = AccessionFilter.convert(filters);
assertThat(af.lastModifiedDate, notNullValue());
assertThat(af.lastModifiedDate.between, notNullValue());
assertThat(af.lastModifiedDate.between.length, is(2));
assertThat(af.lastModifiedDate.between[0], is(dateFormat.parse("2017-12-12")));
assertThat(af.lastModifiedDate.between[1], is(dateFormat.parse("2018-2-2")));
}
@Test
public void testLastModifiedDateMinMaxFilter() throws ParseException, JsonParseException, JsonMappingException, IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
AppliedFilters filters = new ObjectMapper().readValue("{\"lastModifiedDate\":[{\"dateMin\":\"2018-01-01\"}]}}", AppliedFilters.class);
LOG.debug("Date filter {}", filters);
AccessionFilter af = AccessionFilter.convert(filters);
assertThat(af.lastModifiedDate, notNullValue());
assertThat(af.lastModifiedDate.le, nullValue());
assertThat(af.lastModifiedDate.ge, notNullValue());
assertThat(af.lastModifiedDate.ge, is(dateFormat.parse("2018-1-1")));
filters = new ObjectMapper().readValue("{\"lastModifiedDate\":[{\"dateMax\":\"2017-12-12\"}]}", AppliedFilters.class);
LOG.debug("Date filter {}", filters);
af = AccessionFilter.convert(filters);
assertThat(af.lastModifiedDate, notNullValue());
assertThat(af.lastModifiedDate.ge, nullValue());
assertThat(af.lastModifiedDate.le, notNullValue());
assertThat(af.lastModifiedDate.le, is(dateFormat.parse("2017-12-12")));
}
} }
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