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;
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 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.StringFilter;
import org.genesys2.server.service.FilterConstants;
......@@ -168,56 +178,61 @@ public class AppliedFiltersConverter {
private static void apply(AccessionFilter f, String filterName, AppliedFilter af) throws Exception {
switch (filterName) {
case "crops": {
case FilterConstants.CROPS: {
f.crop = take1String(af);
break;
}
case "cropName": {
case FilterConstants.CROPNAME: {
f.cropName = take1String(af);
break;
}
case "sampStat": {
case FilterConstants.SAMPSTAT: {
f.sampStat = toIntegerSet(af);
break;
}
case "storage": {
case FilterConstants.STORAGE: {
f.storage = toIntegerSet(af);
break;
}
case "available": {
case FilterConstants.AVAILABLE: {
f.available = convertBoolean(af.getValues());
break;
}
case "mlsStatus": {
case FilterConstants.ART15: {
f.art15 = convertBoolean(af.getValues());
break;
}
case FilterConstants.MLSSTATUS: {
f.mlsStatus = convertBoolean(af.getValues());
break;
}
case "acceNumb": {
f.acceNumb = convertStringFilter(af);
case FilterConstants.IN_SGSV:
case FilterConstants.SGSV: {
f.sgsv = convertBoolean(af.getValues());
break;
}
case "art15": {
f.art15 = convertBoolean(af.getValues());
case FilterConstants.ACCENUMB: {
f.acceNumb = convertStringFilter(af);
break;
}
case "historic": {
case FilterConstants.HISTORIC: {
f.historic = convertBoolean(af.getValues());
break;
}
case ACCEFILT_SGSV: {
f.sgsv = convertBoolean(af.getValues());
case FilterConstants.SEQUENTIAL_NUMBER: {
f.seqNo = convertNumberFilter(af);
break;
}
case "pdci": {
f.pdci = convertNumberFilter(af);
case FilterConstants.LISTS: {
f.list = toUUIDSet(af);
break;
}
case "seqNo": {
f.seqNo = convertNumberFilter(af);
case FilterConstants.PDCI: {
f.pdci = convertNumberFilter(af);
break;
}
case "lists": {
f.list = toUUIDSet(af);
case FilterConstants.LAST_MODIFIED_DATE: {
f.lastModifiedDate = convertDateFilter(af);
break;
}
default:
......@@ -275,6 +290,49 @@ public class AppliedFiltersConverter {
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) {
final List<Boolean> picks = new ArrayList<Boolean>(3);
values.stream().forEach(fv -> {
......
......@@ -4,6 +4,10 @@ import static com.google.common.collect.Sets.*;
import static org.hamcrest.Matchers.*;
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.service.FilterConstants;
import org.genesys2.server.service.filter.AccessionFilter;
......@@ -15,7 +19,9 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
......@@ -281,7 +287,6 @@ public class AccessionFilterTest {
LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'");
}
@Test
public void testIsNullFilter() {
AccessionFilter af = new AccessionFilter();
......@@ -295,7 +300,6 @@ public class AccessionFilterTest {
LOG.debug("curl -XGET 'localhost:9200/accession_read/_count?pretty' -d '{ \"query\": " + esQb.getQuery() + " }'");
}
@Test
public void testNotIsNullFilter() {
AccessionFilter af = new AccessionFilter();
......@@ -308,4 +312,55 @@ public class AccessionFilterTest {
af.buildQuery().accept(esQb, null);
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