Search in sources :

Example 11 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project Settler by EmhyrVarEmreis.

the class PermissionManagerImplementation method isAuthorized.

@Override
public boolean isAuthorized(PrivilegeObject source, PrivilegeObject target, OperationType operationType) {
    BooleanExpression expression = jTBObjectHierarchy.objectFrom.id.eq(jTBObjectPrivilege.objectTo.id).and(jTBObjectHierarchy.objectTo.id.eq(target.getId()));
    expression = expression.or(jTBObjectPrivilege.objectTo.id.eq(target.getId()));
    expression = expression.or(jTBObjectPrivilege.objectTo.isNull());
    return new JPAQuery<Void>(entityManager).select(Expressions.ONE).from(jTBObjectPrivilege, jTBObjectHierarchy).where(jTBObjectPrivilege.objectFrom.id.eq(source.getId()), (jTBObjectPrivilege.operationType.eq(operationType)), expression).fetchFirst() != null;
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression)

Example 12 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class ExpressionTest method test.

@Test
public void test() throws Throwable {
    List<Expression<?>> exprs = new ArrayList<Expression<?>>();
    exprs.add(QAnimalTest_Animal.animal);
    exprs.add(QAnimalTest_Cat.cat);
    exprs.add(QConstructorTest_Category.category);
    exprs.add(QConstructorTest_ClassWithConstructor.classWithConstructor);
    exprs.add(QEntityTest_Entity1.entity1);
    exprs.add(QEntityTest_Entity2.entity2);
    exprs.add(QEntityTest_Entity3.entity3);
    exprs.add(QEmbeddableTest_EntityWithEmbedded.entityWithEmbedded);
    exprs.add(QGenericTest_GenericType.genericType);
    exprs.add(QInterfaceTypeTest_InterfaceType.interfaceType);
    exprs.add(QInterfaceTypeTest_InterfaceType2.interfaceType2);
    exprs.add(QInterfaceTypeTest_InterfaceType3.interfaceType3);
    exprs.add(QInterfaceTypeTest_InterfaceType4.interfaceType4);
    exprs.add(QInterfaceTypeTest_InterfaceType5.interfaceType5);
    exprs.add(QGenericTest_ItemType.itemType);
    exprs.add(QJodaTimeSupportTest_JodaTimeSupport.jodaTimeSupport);
    exprs.add(QQueryInitTest_PEntity.pEntity);
    exprs.add(QQueryInitTest_PEntity2.pEntity2);
    exprs.add(QQueryInitTest_PEntity3.pEntity3);
    exprs.add(QQueryInitTest_PEntity4.pEntity4);
    exprs.add(QQueryTypeTest_QueryTypeEntity.queryTypeEntity);
    exprs.add(QRelationTest_Reference.reference);
    exprs.add(QRelationTest_RelationType.relationType);
    exprs.add(QReservedNamesTest_ReservedNames.reservedNames);
    exprs.add(QSimpleTypesTest_SimpleTypes.simpleTypes);
    exprs.add(ConstantImpl.create("Hello World!"));
    exprs.add(ConstantImpl.create(1000));
    exprs.add(ConstantImpl.create(10L));
    exprs.add(ConstantImpl.create(true));
    exprs.add(ConstantImpl.create(false));
    Set<Expression<?>> toVisit = new HashSet<Expression<?>>();
    // all entities
    toVisit.addAll(exprs);
    // and all their direct properties
    for (Expression<?> expr : exprs) {
        for (Field field : expr.getClass().getFields()) {
            Object rv = field.get(expr);
            if (rv instanceof Expression) {
                if (rv instanceof StringExpression) {
                    StringExpression str = (StringExpression) rv;
                    toVisit.add(str.toLowerCase());
                    toVisit.add(str.charAt(0));
                    toVisit.add(str.isEmpty());
                } else if (rv instanceof BooleanExpression) {
                    BooleanExpression b = (BooleanExpression) rv;
                    toVisit.add(b.not());
                }
                toVisit.add((Expression<?>) rv);
            }
        }
    }
    Set<String> failures = new TreeSet<String>();
    for (Expression<?> expr : toVisit) {
        for (Method method : expr.getClass().getMethods()) {
            if (method.getName().equals("getParameter")) {
                continue;
            }
            if (method.getName().equals("getArg")) {
                continue;
            }
            if (method.getReturnType() != void.class && !method.getReturnType().isPrimitive()) {
                Class<?>[] types = method.getParameterTypes();
                Object[] args;
                if (types.length == 0) {
                    args = new Object[0];
                } else if (types.length == 1) {
                    if (types[0] == int.class) {
                        args = new Object[] { 1 };
                    } else if (types[0] == boolean.class) {
                        args = new Object[] { Boolean.TRUE };
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                Object rv = method.invoke(expr, args);
                if (method.invoke(expr, args) != rv) {
                    failures.add(expr.getClass().getSimpleName() + "." + method.getName() + " is unstable");
                }
            }
        }
    }
    if (failures.size() > 0) {
        System.err.println("Got " + failures.size() + " failures\n");
    }
    for (String failure : failures) {
        System.err.println(failure);
    }
//        assertTrue("Got "+failures.size()+" failures",failures.isEmpty());
}
Also used : Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Expression(com.querydsl.core.types.Expression) StringExpression(com.querydsl.core.types.dsl.StringExpression) Test(org.junit.Test)

Example 13 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class SearchQueryTest method list.

@Test
public void list() {
    BooleanExpression filter = user.emailAddress.eq("bob@example.com");
    List<User> list = query().where(filter).fetch();
    assertEquals(1, list.size());
    User u = query().where(filter).fetchOne();
    assertEquals(u, list.get(0));
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Test(org.junit.Test)

Example 14 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class SearchQueryTest method uniqueResult.

@Test
public void uniqueResult() {
    BooleanExpression filter = user.emailAddress.eq("bob@example.com");
    User u = query().where(filter).fetchOne();
    assertNotNull(u);
    assertEquals("bob@example.com", u.getEmailAddress());
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Test(org.junit.Test)

Example 15 with BooleanExpression

use of com.querydsl.core.types.dsl.BooleanExpression in project querydsl by querydsl.

the class JDOSQLQueryTest method scalarQueries.

@Test
public void scalarQueries() {
    BooleanExpression filter = product.name.startsWith("A");
    // fetchCount
    assertEquals(10L, sql().from(product).where(filter).fetchCount());
    // countDistinct
    assertEquals(10L, sql().from(product).where(filter).distinct().fetchCount());
    // fetch
    assertEquals(10, sql().from(product).where(filter).select(product.name).fetch().size());
    // fetch with limit
    assertEquals(3, sql().from(product).limit(3).select(product.name).fetch().size());
    // fetch with offset
    //        assertEquals(7, sql().from(product).offset(3).fetch(product.name).size());
    // fetch with limit and offset
    assertEquals(3, sql().from(product).offset(3).limit(3).select(product.name).fetch().size());
    // fetch multiple
    for (Tuple row : sql().from(product).select(product.productId, product.name, product.amount).fetch()) {
        assertNotNull(row.get(0, Object.class));
        assertNotNull(row.get(1, Object.class));
        assertNotNull(row.get(2, Object.class));
    }
    // fetchResults
    QueryResults<String> results = sql().from(product).limit(3).select(product.name).fetchResults();
    assertEquals(3, results.getResults().size());
    assertEquals(30L, results.getTotal());
}
Also used : BooleanExpression(com.querydsl.core.types.dsl.BooleanExpression) Tuple(com.querydsl.core.Tuple) Test(org.junit.Test)

Aggregations

BooleanExpression (com.querydsl.core.types.dsl.BooleanExpression)16 Test (org.junit.Test)13 Field (java.lang.reflect.Field)2 LinkedList (java.util.LinkedList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tuple (com.querydsl.core.Tuple)1 Expression (com.querydsl.core.types.Expression)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 ComparableExpressionBase (com.querydsl.core.types.dsl.ComparableExpressionBase)1 EntityPathBase (com.querydsl.core.types.dsl.EntityPathBase)1 StringExpression (com.querydsl.core.types.dsl.StringExpression)1 StringPath (com.querydsl.core.types.dsl.StringPath)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Function (java.util.function.Function)1