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