Commit 72fb1734 authored by Maxym Borodenko's avatar Maxym Borodenko

StringFilter with array of options

parent 4cc0a535
...@@ -15,7 +15,18 @@ ...@@ -15,7 +15,18 @@
*/ */
package org.genesys.blocks.model.filters; package org.genesys.blocks.model.filters;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.collect.Sets;
import com.querydsl.core.BooleanBuilder; import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.StringPath; import com.querydsl.core.types.dsl.StringPath;
/** /**
...@@ -24,13 +35,16 @@ import com.querydsl.core.types.dsl.StringPath; ...@@ -24,13 +35,16 @@ import com.querydsl.core.types.dsl.StringPath;
public class StringFilter { public class StringFilter {
/** Equal. */ /** Equal. */
public String eq; @JsonDeserialize(using = StringFilter.SetDeserializer.class)
public Set<String> eq;
/** Contains substring. */ /** Contains substring. */
public String contains; @JsonDeserialize(using = StringFilter.SetDeserializer.class)
public Set<String> contains;
/** Starts with. */ /** Starts with. */
public String sw; @JsonDeserialize(using = StringFilter.SetDeserializer.class)
public Set<String> sw;
/** /**
* Builds the query. * Builds the query.
...@@ -40,17 +54,14 @@ public class StringFilter { ...@@ -40,17 +54,14 @@ public class StringFilter {
*/ */
public BooleanBuilder buildQuery(final StringPath val) { public BooleanBuilder buildQuery(final StringPath val) {
final BooleanBuilder and = new BooleanBuilder(); final BooleanBuilder and = new BooleanBuilder();
if (eq != null) { if (eq != null && !eq.isEmpty()) {
and.and(val.isNotNull()); and.and(val.isNotNull()).and(val.in(eq));
and.and(val.eq(eq));
} }
if (sw != null) { if (sw != null && !sw.isEmpty()) {
and.and(val.isNotNull()); and.and(val.isNotNull()).andAnyOf(sw.stream().map(val::startsWith).toArray(Predicate[]::new));
and.and(val.startsWith(sw));
} }
if (contains != null) { if (contains != null && !contains.isEmpty()) {
and.and(val.isNotNull()); and.and(val.isNotNull()).andAnyOf(contains.stream().map(val::contains).toArray(Predicate[]::new));
and.and(val.contains(contains));
} }
return and; return and;
} }
...@@ -61,9 +72,26 @@ public class StringFilter { ...@@ -61,9 +72,26 @@ public class StringFilter {
* @param val the val * @param val the val
* @return the string filter * @return the string filter
*/ */
public static StringFilter eq(final String val) { public static StringFilter eq(final String... val) {
final StringFilter filter = new StringFilter(); final StringFilter filter = new StringFilter();
filter.eq = val; filter.eq = Sets.newHashSet(val);
return filter; return filter;
} }
static class SetDeserializer extends JsonDeserializer<Set<String>> {
@Override
public Set<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
final Set<String> values = new HashSet<>();
if (jsonParser.getCurrentToken() == JsonToken.START_ARRAY) {
while (jsonParser.getCurrentToken() != null && jsonParser.getCurrentToken() != JsonToken.END_ARRAY) {
if (jsonParser.nextToken() == JsonToken.VALUE_STRING) {
values.add(jsonParser.getText());
}
}
} else if (jsonParser.getCurrentToken() == JsonToken.VALUE_STRING) {
values.add(jsonParser.getText());
}
return values;
}
}
} }
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.tests.filter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.genesys.blocks.model.filters.StringFilter;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
/**
* The Class StringFilterTest.
*/
public class StringFilterTest {
private static ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testDeserializationOfEq() throws JsonProcessingException {
StringFilter filter1 = objectMapper.readValue("{ \"eq\": \"AAA\" }", StringFilter.class);
assertNotNull(filter1);
assertEquals(1, filter1.eq.size());
assertEquals("AAA", filter1.eq.toArray()[0]);
StringFilter filter2 = objectMapper.readValue("{ \"eq\": [ \"AAA\", \"BBB\" ] }", StringFilter.class);
assertNotNull(filter2);
assertEquals(2, filter2.eq.size());
assertThat(Arrays.asList("AAA", "BBB"), containsInAnyOrder(filter2.eq.toArray()));
}
@Test
public void testDeserializationOfContains() throws JsonProcessingException {
StringFilter filter1 = objectMapper.readValue("{ \"contains\": \"AAA\" }", StringFilter.class);
assertNotNull(filter1);
assertEquals(1, filter1.contains.size());
assertEquals("AAA", filter1.contains.toArray()[0]);
StringFilter filter2 = objectMapper.readValue("{ \"contains\": [ \"AAA\", \"BBB\" ] }", StringFilter.class);
assertNotNull(filter2);
assertEquals(2, filter2.contains.size());
assertThat(Arrays.asList("AAA", "BBB"), containsInAnyOrder(filter2.contains.toArray()));
}
@Test
public void testDeserializationOfStartsWith() throws JsonProcessingException {
StringFilter filter1 = objectMapper.readValue("{ \"sw\": \"AAA\" }", StringFilter.class);
assertNotNull(filter1);
assertEquals(1, filter1.sw.size());
assertEquals("AAA", filter1.sw.toArray()[0]);
StringFilter filter2 = objectMapper.readValue("{ \"sw\": [ \"AAA\", \"BBB\" ] }", StringFilter.class);
assertNotNull(filter2);
assertEquals(2, filter2.sw.size());
assertThat(Arrays.asList("AAA", "BBB"), containsInAnyOrder(filter2.sw.toArray()));
}
}
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