Search in sources :

Example 21 with QueryFilter

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()));
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with QueryFilter

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());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Test(org.junit.Test)

Example 23 with QueryFilter

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());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Test(org.junit.Test)

Example 24 with QueryFilter

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;
}
Also used : java.util(java.util) URLDecoder(java.net.URLDecoder) OrderDirection(com.github.tfaga.lynx.enums.OrderDirection) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException) ZonedDateTime(java.time.ZonedDateTime) QueryFormatError(com.github.tfaga.lynx.enums.QueryFormatError) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) URLEncoder(java.net.URLEncoder) DateTimeParseException(java.time.format.DateTimeParseException) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Matcher(java.util.regex.Matcher) FilterOperation(com.github.tfaga.lynx.enums.FilterOperation) URI(java.net.URI) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) Pattern(java.util.regex.Pattern) QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) Matcher(java.util.regex.Matcher) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException)

Example 25 with QueryFilter

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());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Aggregations

QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)49 Test (org.junit.Test)47 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)44 User (com.github.tfaga.lynx.test.entities.User)28 Project (com.github.tfaga.lynx.test.entities.Project)10 Date (java.util.Date)6 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)3 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 java.util (java.util)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)1 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)1 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)1 FilterOperation (com.github.tfaga.lynx.enums.FilterOperation)1 QueryFormatError (com.github.tfaga.lynx.enums.QueryFormatError)1 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)1 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)1