Search in sources :

Example 1 with QueryOrder

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

the class QueryStringBuilder method buildOrder.

private QueryOrder buildOrder(String key, String value) {
    log.finest("Building order string: " + value);
    if (value == null || value.isEmpty())
        return null;
    QueryOrder o = new QueryOrder();
    String[] pair = value.split("(\\s|\\+)");
    if (pair[0].isEmpty()) {
        String msg = "Value for '" + key + "' is malformed: '" + value + "'";
        log.finest(msg);
        throw new QueryFormatException(msg, key, QueryFormatError.MALFORMED);
    }
    o.setField(pair[0]);
    if (pair.length > 1) {
        try {
            o.setOrder(OrderDirection.valueOf(pair[1].toUpperCase()));
        } catch (IllegalArgumentException e) {
            String msg = "Constant in '" + key + "' does not exist: '" + value + "'";
            log.finest(msg);
            throw new QueryFormatException(msg, key, QueryFormatError.NO_SUCH_CONSTANT);
        }
    } else {
        o.setOrder(OrderDirection.ASC);
    }
    return o;
}
Also used : QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) QueryFormatException(com.github.tfaga.lynx.exceptions.QueryFormatException)

Example 2 with QueryOrder

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

the class JPAUtilsOrderTest method testMultipleOrders.

@Test
public void testMultipleOrders() {
    QueryParameters q = new QueryParameters();
    QueryOrder qo = new QueryOrder();
    qo.setField("role");
    qo.setOrder(OrderDirection.DESC);
    q.getOrder().add(qo);
    qo = new QueryOrder();
    qo.setField("lastname");
    qo.setOrder(OrderDirection.ASC);
    q.getOrder().add(qo);
    qo = new QueryOrder();
    qo.setField("firstname");
    qo.setOrder(OrderDirection.DESC);
    q.getOrder().add(qo);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(100, users.size());
    Assert.assertNotNull(users.get(0).getFirstname());
    Assert.assertNotNull(users.get(0).getLastname());
    Assert.assertEquals("Larry", users.get(0).getFirstname());
    Assert.assertEquals("Bailey", users.get(0).getLastname());
    Assert.assertNotNull(users.get(99).getFirstname());
    Assert.assertNotNull(users.get(99).getLastname());
    Assert.assertEquals("Bonnie", users.get(99).getFirstname());
    Assert.assertEquals("Willis", users.get(99).getLastname());
}
Also used : User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) Test(org.junit.Test)

Example 3 with QueryOrder

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

the class JPAUtilsOrderTest method testCaseSensitiveField.

@Test
public void testCaseSensitiveField() {
    QueryOrder qo = new QueryOrder();
    qo.setField("firsTNAmE");
    qo.setOrder(OrderDirection.ASC);
    QueryParameters q = new QueryParameters();
    q.getOrder().add(qo);
    try {
        JPAUtils.queryEntities(em, User.class, q);
        Assert.fail("No exception was thrown");
    } catch (NoSuchEntityFieldException e) {
        Assert.assertEquals("firsTNAmE", e.getField());
    }
}
Also used : QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) NoSuchEntityFieldException(com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException) Test(org.junit.Test)

Example 4 with QueryOrder

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

the class JPAUtilsOrderTest method testOneToMany.

@Test(expected = InvalidEntityFieldException.class)
public void testOneToMany() {
    QueryOrder qo = new QueryOrder();
    qo.setField("projects.name");
    QueryParameters q = new QueryParameters();
    q.getOrder().add(qo);
    JPAUtils.queryEntities(em, User.class, q);
    Assert.fail("No exception was thrown");
}
Also used : QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) Test(org.junit.Test)

Example 5 with QueryOrder

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

the class JPAUtilsOrderTest method testSingleOrder.

@Test
public void testSingleOrder() {
    QueryOrder qo = new QueryOrder();
    qo.setField("firstname");
    qo.setOrder(OrderDirection.ASC);
    QueryParameters q = new QueryParameters();
    q.getOrder().add(qo);
    List<User> users = JPAUtils.queryEntities(em, User.class, q);
    Assert.assertNotNull(users);
    Assert.assertEquals(100, users.size());
    Assert.assertNotNull(users.get(0).getFirstname());
    Assert.assertEquals("Amanda", users.get(0).getFirstname());
    Assert.assertNotNull(users.get(99).getFirstname());
    Assert.assertEquals("Victor", users.get(99).getFirstname());
}
Also used : User(com.github.tfaga.lynx.test.entities.User) QueryParameters(com.github.tfaga.lynx.beans.QueryParameters) QueryOrder(com.github.tfaga.lynx.beans.QueryOrder) Test(org.junit.Test)

Aggregations

QueryOrder (com.github.tfaga.lynx.beans.QueryOrder)14 QueryParameters (com.github.tfaga.lynx.beans.QueryParameters)13 Test (org.junit.Test)13 User (com.github.tfaga.lynx.test.entities.User)7 NoSuchEntityFieldException (com.github.tfaga.lynx.exceptions.NoSuchEntityFieldException)2 Project (com.github.tfaga.lynx.test.entities.Project)2 QueryFormatException (com.github.tfaga.lynx.exceptions.QueryFormatException)1