Search in sources :

Example 1 with QueryParameters

use of com.github.tfaga.lynx.beans.QueryParameters in project lynx by TFaga.

the class QueryStringBuilder method build.

public QueryParameters build() {
    log.finest("Building query string: " + query);
    QueryParameters params = new QueryParameters();
    if (paginationEnabled && defaultLimit != null)
        params.setLimit(defaultLimit);
    if (paginationEnabled && defaultOffset != null)
        params.setOffset(defaultOffset);
    if (query == null || query.isEmpty())
        return params;
    for (String pair : query.split("&+(?=([^']*'[^']*')*[^']*$)")) {
        int idxOfPair = pair.indexOf("=");
        if (idxOfPair == -1) {
            buildPair(params, pair, "");
            continue;
        }
        String key, value;
        key = pair.substring(0, idxOfPair);
        value = pair.substring(idxOfPair + 1);
        buildPair(params, key, value);
    }
    return params;
}
Also used : QueryParameters(com.github.tfaga.lynx.beans.QueryParameters)

Example 2 with QueryParameters

use of com.github.tfaga.lynx.beans.QueryParameters in project lynx by TFaga.

the class JPAUtilsCriteriaFilterTest method testQueryWithCriteriaFilterAndWithFields.

@Test
public void testQueryWithCriteriaFilterAndWithFields() {
    QueryParameters q = new QueryParameters();
    q.getFields().add("firstname");
    List<User> users = JPAUtils.queryEntities(em, User.class, q, (p, cb, r) -> cb.and(p, cb.equal(r.get("lastname"), "Stewart")));
    Assert.assertNotNull(users);
    Assert.assertEquals(1, users.size());
    Assert.assertNotNull(users.get(0));
    Assert.assertNotNull(users.get(0).getFirstname());
    Assert.assertNull(users.get(0).getLastname());
    Assert.assertEquals("Donald", users.get(0).getFirstname());
}
Also used : User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 3 with QueryParameters

use of com.github.tfaga.lynx.beans.QueryParameters in project lynx by TFaga.

the class JPAUtilsCriteriaFilterTest method testQueryCriteriaFilterWithParamsAnd.

@Test
public void testQueryCriteriaFilterWithParamsAnd() {
    QueryFilter qf = new QueryFilter();
    qf.setField("lastname");
    qf.setOperation(FilterOperation.EQ);
    qf.setValue("Turner");
    QueryParameters q = new QueryParameters();
    q.getFilters().add(qf);
    List<User> users = JPAUtils.queryEntities(em, User.class, q, (p, cb, r) -> cb.and(p, cb.equal(r.get("firstname"), "Antonio")));
    Assert.assertNotNull(users);
    Assert.assertEquals(1, users.size());
    Assert.assertNotNull(users.get(0));
    Assert.assertEquals("Antonio", users.get(0).getFirstname());
    Assert.assertEquals("Turner", users.get(0).getLastname());
    Long usersCount = JPAUtils.queryEntitiesCount(em, User.class, q, (p, cb, r) -> cb.and(p, cb.equal(r.get("firstname"), "Antonio")));
    Assert.assertNotNull(usersCount);
    Assert.assertEquals(1, usersCount.longValue());
}
Also used : QueryFilter(com.github.tfaga.lynx.beans.QueryFilter) User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 4 with QueryParameters

use of com.github.tfaga.lynx.beans.QueryParameters in project lynx by TFaga.

the class JPAUtilsCriteriaFilterTest method testQueryWithoutCriteriaFilter.

@Test
public void testQueryWithoutCriteriaFilter() {
    List<User> users = JPAUtils.queryEntities(em, User.class, new QueryParameters());
    Assert.assertNotNull(users);
    Assert.assertEquals(100, users.size());
}
Also used : User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Example 5 with QueryParameters

use of com.github.tfaga.lynx.beans.QueryParameters in project lynx by TFaga.

the class JPAUtilsFieldsTest method testIdParameter.

@Test
public void testIdParameter() {
    QueryParameters q = new QueryParameters();
    q.getFields().add("id");
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(100, users.size());
    Assert.assertNotNull(users.get(0).getId());
    Assert.assertNull(users.get(0).getFirstname());
    Assert.assertNotNull(users.get(99).getId());
    Assert.assertNull(users.get(99).getFirstname());
}
Also used : User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) Test(org.junit.Test)

Aggregations

QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)158 Test (org.junit.Test)155 User (com.github.tfaga.lynx.test.entities.User)46 QueryFilter (com.github.tfaga.lynx.beans.QueryFilter)44 QueryStringDefaults (com.github.tfaga.lynx.utils.QueryStringDefaults)14 QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)13 Project (com.github.tfaga.lynx.test.entities.Project)12 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)6 Date (java.util.Date)6 URI (java.net.URI)4 CriteriaField (com.github.tfaga.lynx.beans.CriteriaField)2 CriteriaWhereQuery (com.github.tfaga.lynx.beans.CriteriaWhereQuery)2 OrderDirection (com.github.tfaga.lynx.enums.OrderDirection)2 InvalidEntityFieldException (com.github.tfaga.lynx.exceptions.InvalidEntityFieldException)2 InvalidFieldValueException (com.github.tfaga.lynx.exceptions.InvalidFieldValueException)2 CriteriaFilter (com.github.tfaga.lynx.interfaces.CriteriaFilter)2 Field (java.lang.reflect.Field)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2