Commit b421a92e authored by Matija Obreza's avatar Matija Obreza

Merge branch '411-bug-startswithfilter-cannot-be-cast' into 'master'

Resolve "Bug: StartsWithFilter cannot be cast"

Closes #411

See merge request genesys-pgr/genesys-server!344
parents 227bf48b 167eccae
......@@ -58,7 +58,7 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
public Set<String> crop;
/** The crop name. */
public String cropName;
public StringFilter cropName;
/** The holder. */
public InstituteFilter holder;
......@@ -165,7 +165,7 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
and.and(accession.historic.eq(historic));
}
if (cropName != null) {
and.and(accession.cropName.eq(cropName));
and.and(cropName.buildQuery(accession.cropName));
}
if (CollectionUtil.isNotEmpty(doi)) {
and.and(accession.doi.in(doi));
......@@ -241,7 +241,7 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
and.and(accession.crop.shortName.in(crop));
}
if (cropName != null) {
and.and(accession.cropName.eq(cropName));
and.and(cropName.buildQuery(accession.cropName));
}
if (holder != null) {
and.and(holder.buildQuery(accession.institute));
......
......@@ -17,6 +17,7 @@ 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.exception.InvalidApiUsageException;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -25,6 +26,7 @@ import org.genesys2.server.service.impl.FilterHandler.LiteralValueFilter;
import org.genesys2.server.service.impl.FilterHandler.MaxValueFilter;
import org.genesys2.server.service.impl.FilterHandler.MinValueFilter;
import org.genesys2.server.service.impl.FilterHandler.ValueRangeFilter;
import org.genesys2.server.service.impl.FilterHandler.StartsWithFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -192,7 +194,11 @@ public class AppliedFiltersConverter {
break;
}
case FilterConstants.CROPNAME: {
f.cropName = take1String(af);
f.cropName = convertStringFilter(af);
break;
}
case FilterConstants.DOI: {
f.doi = toStringSet(af);
break;
}
case FilterConstants.SAMPSTAT: {
......@@ -249,7 +255,7 @@ public class AppliedFiltersConverter {
break;
}
default:
throw new RuntimeException("Unhandled Accession filter property=" + filterName);
throw new InvalidApiUsageException("Unhandled Accession filter property=" + filterName);
}
}
......@@ -288,10 +294,10 @@ public class AppliedFiltersConverter {
private static StringFilter convertStringFilter(AppliedFilter af) throws Exception {
StringFilter f = new StringFilter();
for (FilterValue fv : af.getValues()) {
if (fv instanceof StringFilter) {
f.contains = ((StringFilter) fv).contains;
f.eq = ((StringFilter) fv).eq;
f.sw = ((StringFilter) fv).sw;
if (fv instanceof LiteralValueFilter) {
f.eq = (String) ((LiteralValueFilter) fv).getValue();
} else if (fv instanceof StartsWithFilter) {
f.sw = ((StartsWithFilter) fv).getStartsWith();
}
}
return f;
......@@ -520,7 +526,7 @@ public class AppliedFiltersConverter {
break;
}
default:
throw new RuntimeException("Unhandled Institute filter property=" + filterName);
throw new InvalidApiUsageException("Unhandled Institute filter property=" + filterName);
}
}
......
......@@ -70,12 +70,12 @@ public class AccessionFilterTest {
AccessionFilter af = AppliedFilters.convert(filters);
LOG.debug("-> {}", pretty.writeValueAsString(af));
assertThat(af.cropName, notNullValue());
assertThat(af.cropName, is("banana"));
assertThat(af.cropName.eq, is("banana"));
// 2nd is ignored
filter.addFilterValue(new FilterHandler.LiteralValueFilter("cassava"));
af = AppliedFilters.convert(filters);
assertThat(af.cropName, is("banana"));
// filter.addFilterValue(new FilterHandler.LiteralValueFilter("cassava"));
// af = AppliedFilters.convert(filters);
// assertThat(af.cropName.eq, is("banana"));
ElasticQueryBuilder esQb = new ElasticQueryBuilder();
af.buildPredicate().accept(esQb, null);
......
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