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;
}
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());
}
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));
}
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());
}
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());
}
Aggregations