use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.
the class QueryFilterTest method testFilterConstructorList.
@Test
public void testFilterConstructorList() {
List<String> values = new ArrayList<>();
values.add("test");
QueryFilter filter = new QueryFilter("username", FilterOperation.EQ, values);
Assert.assertNotNull(filter);
Assert.assertEquals("username", filter.getField());
Assert.assertEquals(FilterOperation.EQ, filter.getOperation());
Assert.assertNull(filter.getValue());
Assert.assertNull(filter.getDateValue());
Assert.assertNotNull(filter.getValues());
Assert.assertEquals(1, filter.getValues().size());
Assert.assertTrue(values.equals(filter.getValues()));
}
use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.
the class QueryFilterTest method testFilterConstrutorNoValue.
@Test
public void testFilterConstrutorNoValue() {
QueryFilter filter = new QueryFilter("username", FilterOperation.EQ);
Assert.assertNotNull(filter);
Assert.assertEquals("username", filter.getField());
Assert.assertEquals(FilterOperation.EQ, filter.getOperation());
Assert.assertNull(filter.getValue());
Assert.assertNull(filter.getDateValue());
Assert.assertNotNull(filter.getValues());
Assert.assertEquals(0, filter.getValues().size());
}
use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.
the class QueryFilterTest method testFilterConstrutorValue.
@Test
public void testFilterConstrutorValue() {
QueryFilter filter = new QueryFilter("username", FilterOperation.EQ, "test");
Assert.assertNotNull(filter);
Assert.assertEquals("username", filter.getField());
Assert.assertEquals(FilterOperation.EQ, filter.getOperation());
Assert.assertEquals("test", filter.getValue());
Assert.assertNull(filter.getDateValue());
Assert.assertNotNull(filter.getValues());
Assert.assertEquals(0, filter.getValues().size());
}
use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.
the class QueryStringBuilder method buildFilter.
private List<QueryFilter> buildFilter(String key, String value) {
log.finest("Building filter string: " + value);
List<QueryFilter> filterList = new ArrayList<>();
if (value == null || value.isEmpty())
return filterList;
Pattern filterPattern = Pattern.compile("[:]+(?=([^']*'[^']*')*[^']*$)");
List<String[]> filters = Arrays.stream(value.split("[(\\s|+)]+(?=([^']*'[^']*')*[^']*$)")).map(filterPattern::split).collect(Collectors.toList());
filters.stream().filter(f -> f.length == 2).forEach(f -> {
QueryFilter qf = new QueryFilter();
qf.setField(f[0]);
qf.setOperation(parseFilterOperation(key, f[1].toUpperCase()));
if (qf.getOperation() == FilterOperation.ISNULL || qf.getOperation() == FilterOperation.ISNOTNULL) {
filterList.add(qf);
}
});
Pattern valueListPattern = Pattern.compile("^\\[(.*)]$");
Pattern valuesPattern = Pattern.compile("[,]+(?=([^']*'[^']*')*[^']*$)");
Pattern valueDateTimePattern = Pattern.compile("^dt'(.*)'$");
Pattern valuePattern = Pattern.compile("(^')|('$)");
filters.stream().filter(f -> f.length == 3).forEach(f -> {
QueryFilter qf = new QueryFilter();
qf.setField(f[0]);
qf.setOperation(parseFilterOperation(key, f[1].toUpperCase()));
Matcher matcher;
if ((matcher = valueListPattern.matcher(f[2])).find() && (qf.getOperation() == FilterOperation.IN || qf.getOperation() == FilterOperation.NIN || qf.getOperation() == FilterOperation.NINIC || qf.getOperation() == FilterOperation.INIC)) {
String values = matcher.group(1);
Arrays.stream(valuesPattern.split(values)).filter(e -> !e.isEmpty()).distinct().map(e -> valuePattern.matcher(e).replaceAll("")).forEach(e -> qf.getValues().add(e));
} else if ((matcher = valueDateTimePattern.matcher(f[2])).find()) {
Date d = parseDate(matcher.group(1));
if (d == null) {
String msg = "Value for '" + key + "' is malformed: '" + value + "'";
log.finest(msg);
throw new QueryFormatException(msg, key, QueryFormatError.MALFORMED);
}
qf.setDateValue(d);
} else {
qf.setValue(valuePattern.matcher(f[2]).replaceAll(""));
}
filterList.add(qf);
});
return filterList;
}
use of com.github.tfaga.lynx.beans.QueryFilter in project lynx by TFaga.
the class JPAUtilsCountTest method testQueryCount.
@Test
public void testQueryCount() {
QueryParameters q = new QueryParameters();
QueryFilter qf = new QueryFilter();
qf.setField("firstname");
qf.setOperation(FilterOperation.IN);
qf.getValues().add("Bruce");
qf.getValues().add("Karen");
qf.getValues().add("Sandra");
qf.getValues().add("Laura");
q.getFilters().add(qf);
qf = new QueryFilter();
qf.setField("email");
qf.setOperation(FilterOperation.LIKE);
qf.setValue("%@mozilla.org");
q.getFilters().add(qf);
Long count = JPAUtils.queryEntitiesCount(em, User.class, q);
Assert.assertNotNull(count);
Assert.assertEquals((long) 2, count.longValue());
}
Aggregations