Java Code Examples for com.mysema.query.types.Predicate

The following examples show how to use com.mysema.query.types.Predicate. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring-boot-practice   Source File: PermissionService.java    License: Apache License 2.0 6 votes vote down vote up
public Page<Permission> findAllByRoleId(Integer id, Pageable pageable) {
    QRole role = QRole.role;
    Predicate predicate = role.id.eq(id);

    PathBuilder<Permission> builder = new PathBuilder<Permission>(Permission.class, QPermission.permission.getMetadata());
    Querydsl querydsl = new Querydsl(em, builder);

    JPQLQuery countQuery = createQuery(predicate);
    JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));

    Path<Permission> path = QPermission.permission;
    Long total = countQuery.count();
    List<Permission> content = total > pageable.getOffset() ? query.list(path) : Collections.<Permission>emptyList();

    return new PageImpl<Permission>(content, pageable, total);
}
 
Example 2
Source Project: spring-boot-practice   Source File: PermissionService.java    License: Apache License 2.0 6 votes vote down vote up
public Page<Permission> findAllByRoleId(Integer id, Pageable pageable) {
    QRole role = QRole.role;
    Predicate predicate = role.id.eq(id);

    PathBuilder<Permission> builder = new PathBuilder<Permission>(Permission.class, QPermission.permission.getMetadata());
    Querydsl querydsl = new Querydsl(em, builder);

    JPQLQuery countQuery = createQuery(predicate);
    JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));

    Path<Permission> path = QPermission.permission;
    Long total = countQuery.count();
    List<Permission> content = total > pageable.getOffset() ? query.list(path) : Collections.<Permission>emptyList();

    return new PageImpl<Permission>(content, pageable, total);
}
 
Example 3
Source Project: spring-boot-practice   Source File: DepartmentService.java    License: Apache License 2.0 6 votes vote down vote up
public Page<Department> findAll(Pageable pageable) {
    Predicate predicate = null; // TODO

    SDepartment department = SDepartment.department;
    SEmployee employee = SEmployee.employee;

    PathBuilder<Department> builder = new PathBuilder<>(Department.class, department.getMetadata());
    QuerydslSQL querydsl = new QuerydslSQL(builder);

    JPASQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));
    Long total = createBaseQuery(predicate).count();

    List<Department> content =
        query.orderBy(department.id.asc()).list(
            Projections.constructor(
                Department.class,
                department.id,
                department.name,
                // SQLExpressions.groupConcat() is not available in Querydsl 3.6.7
                sqlTemplatesService.groupConcat(employee.name, employee.id.asc())
            ));

    return new PageImpl<>(content, pageable, total);
}
 
Example 4
@Override
public boolean supportsParameter(MethodParameter parameter) {

	if (Predicate.class.equals(parameter.getParameterType())) {
		return true;
	}

	if (parameter.hasParameterAnnotation(QuerydslPredicate.class)) {
		throw new IllegalArgumentException(
				String.format("Parameter at position %s must be of type Predicate but was %s.",
						parameter.getParameterIndex(), parameter.getParameterType()));
	}

	return false;
}
 
Example 5
Source Project: spring-boot-practice   Source File: PermissionService.java    License: Apache License 2.0 5 votes vote down vote up
public JPAQuery createQuery(Predicate predicate) {
    QPermission permission = QPermission.permission;
    QRolePermission rolePermission = QRolePermission.rolePermission;
    QRole role = QRole.role;
    return new JPAQuery(em)
        .from(permission)
        .leftJoin(permission.rolePermission, rolePermission)
        .leftJoin(rolePermission.role, role)
        .where(predicate);
}
 
Example 6
Source Project: spring-boot-practice   Source File: AService.java    License: Apache License 2.0 5 votes vote down vote up
public Page<ExtendedA> findAll(Pageable pageable) {
    Predicate predicate = null; // TODO

    SA a = SA.a;
    SB b = SB.b;
    SC c = SC.c;
    SD d = SD.d;
    SE e1 = new SE("E1");
    SE e2 = new SE("E2");
    SE e3 = new SE("E3");

    PathBuilder<A> builder = new PathBuilder<>(A.class, a.getMetadata());
    QuerydslSQL querydsl = new QuerydslSQL(builder);

    JPASQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));
    Long total = createQuery(predicate).count();

    List<ExtendedA> content =
        query.orderBy(a.id.asc()).list(
            Projections.constructor(
                ExtendedA.class,
                a.id,
                a.name,
                b.name,
                c.name,
                d.name,
                e1.name,
                e2.name,
                e3.name
            ));

    return new PageImpl<>(content, pageable, total);
}
 
Example 7
Source Project: spring-boot-practice   Source File: AService.java    License: Apache License 2.0 5 votes vote down vote up
public Page<Tuple> findAllAsTuple(Pageable pageable) {
    Predicate predicate = null; // TODO

    SA a = SA.a;
    SB b = SB.b;
    SC c = SC.c;
    SD d = SD.d;
    SE e1 = new SE("E1");
    SE e2 = new SE("E2");
    SE e3 = new SE("E3");

    PathBuilder<A> builder = new PathBuilder<>(A.class, a.getMetadata());
    QuerydslSQL querydsl = new QuerydslSQL(builder);

    JPASQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));
    Long total = createQuery(predicate).count();

    List<Tuple> content =
        query.orderBy(a.id.asc()).list(
            a.id,
            a.name,
            b.name,
            c.name,
            d.name,
            e1.name,
            e2.name,
            e3.name
        );

    return new PageImpl<>(content, pageable, total);
}
 
Example 8
Source Project: spring-boot-practice   Source File: AService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * <pre>{@code
 * select *
 * from a
 * join b on b.a_id = a.id
 * join c on c.b_id = b.id
 * left join d on c.d_id = d.id
 * left join (select * from e where sub_id = 1) as e1 on e1.d_id = d.id
 * left join (select * from e where sub_id = 2) as e2 on e2.d_id = d.id
 * left join (select * from e where sub_id = 3) as e3 on e3.d_id = d.id
 * }</pre>
 */
private JPASQLQuery createQuery(Predicate predicate) {
    SA a = SA.a;
    SB b = SB.b;
    SC c = SC.c;
    SD d = SD.d;
    SE e = SE.e;

    // Alias
    SE e1 = new SE("E1");
    SE e2 = new SE("E2");
    SE e3 = new SE("E3");

    JPASQLQuery query = new JPASQLQuery(em, sqlTemplatesService.getTemplates())
        .from(a)
        .join(b).on(b.aId.eq(a.id))
        .join(c).on(c.bId.eq(b.id))
        .leftJoin(d).on(c.dId.eq(d.id))
        // Sub-queries can be used in left join
        .leftJoin(new SQLSubQuery().from(e).where(e.subId.eq(1)).list(e.all()), e1).on(e1.dId.eq(d.id))
        .leftJoin(new SQLSubQuery().from(e).where(e.subId.eq(2)).list(e.all()), e2).on(e2.dId.eq(d.id))
        .leftJoin(new SQLSubQuery().from(e).where(e.subId.eq(3)).list(e.all()), e3).on(e3.dId.eq(d.id));
    if (predicate != null) {
        query.where(predicate);
    }
    return query;
}
 
Example 9
Source Project: spring-boot-practice   Source File: PermissionService.java    License: Apache License 2.0 5 votes vote down vote up
public JPAQuery createQuery(Predicate predicate) {
    QPermission permission = QPermission.permission;
    QRolePermission rolePermission = QRolePermission.rolePermission;
    QRole role = QRole.role;
    BooleanBuilder builder = new BooleanBuilder();
    builder.and(predicate);
    builder.and(permission.deletedAt.isNull());
    builder.and(rolePermission.deletedAt.isNull());
    builder.and(role.deletedAt.isNull());
    return new JPAQuery(em)
        .from(permission)
        .leftJoin(permission.rolePermission, rolePermission)
        .leftJoin(rolePermission.role, role)
        .where(builder);
}
 
Example 10
Source Project: spring-boot-practice   Source File: DepartmentService.java    License: Apache License 2.0 5 votes vote down vote up
private JPASQLQuery createBaseQuery(Predicate predicate) {
    SDepartment department = SDepartment.department;
    JPASQLQuery query = new JPASQLQuery(entityManager, sqlTemplatesService.getTemplates()).from(department);
    if (predicate != null) {
        query.where(predicate);
    }
    return query;
}
 
Example 11
Source Project: spring-boot-practice   Source File: DepartmentService.java    License: Apache License 2.0 5 votes vote down vote up
private JPASQLQuery createQuery(Predicate predicate) {
    SDepartment department = SDepartment.department;
    SEmployee employee = SEmployee.employee;
    JPASQLQuery query = createBaseQuery(predicate)
        .leftJoin(employee).on(employee.departmentId.eq(department.id))
        .groupBy(department.id);
    if (predicate != null) {
        query.where(predicate);
    }
    return query;
}
 
Example 12
Source Project: gvnix   Source File: QuerydslUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Utility for constructing where clause expressions.
 * 
 * @param entityPath Full path to entity and associations. For example:
 *        {@code Pet} , {@code Pet.owner}
 * @param fieldName Property name in the given entity path. For example:
 *        {@code name} in {@code Pet} entity, {@code firstName} in
 *        {@code Pet.owner} entity.
 * @param fieldType Property value {@code Class}
 * @param searchStr the value to find, may be null
 * @return Predicate
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Predicate createExpression(PathBuilder<T> entityPath,
        String fieldName, String searchStr,
        ConversionService conversionService) {

    TypeDescriptor descriptor = getTypeDescriptor(fieldName, entityPath);
    if (descriptor == null) {
        throw new IllegalArgumentException(String.format(
                "Can't found field '%s' on entity '%s'", fieldName,
                entityPath.getType()));
    }
    Class<?> fieldType = descriptor.getType();

    // Check for field type in order to delegate in custom-by-type
    // create expression method
    if (String.class == fieldType) {
        return createStringLikeExpression(entityPath, fieldName, searchStr);
    }
    else if (Boolean.class == fieldType || boolean.class == fieldType) {
        return createBooleanExpression(entityPath, fieldName, searchStr);
    }
    else if (Number.class.isAssignableFrom(fieldType)
            || NUMBER_PRIMITIVES.contains(fieldType)) {
        return createNumberExpressionGenerics(entityPath, fieldName,
                fieldType, descriptor, searchStr, conversionService);
    }
    else if (Date.class.isAssignableFrom(fieldType)
            || Calendar.class.isAssignableFrom(fieldType)) {
        BooleanExpression expression = createDateExpression(entityPath,
                fieldName, (Class<Date>) fieldType, searchStr);
        return expression;
    }

    else if (fieldType.isEnum()) {
        return createEnumExpression(entityPath, fieldName, searchStr,
                (Class<? extends Enum>) fieldType);
    }
    return null;
}
 
Example 13
Source Project: gvnix   Source File: QuerydslUtilsBeanImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> Predicate createFilterExpression(PathBuilder<T> entityPath,
        String fieldName, Class<?> fieldType, String searchStr) {

    return createFilterExpression(entityPath, fieldName, searchStr);

}
 
Example 14
Source Project: gvnix   Source File: QuerydslUtilsBeanImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public <T> Predicate createSearchExpression(PathBuilder<T> entityPath,
        String fieldName, String searchStr) {

    TypeDescriptor descriptor = getTypeDescriptor(fieldName, entityPath);
    if (descriptor == null) {
        throw new IllegalArgumentException(String.format(
                "Can't found field '%s' on entity '%s'", fieldName,
                entityPath.getType()));
    }
    Class<?> fieldType = descriptor.getType();

    // Check for field type in order to delegate in custom-by-type
    // create expression method
    if (String.class == fieldType) {
        return createStringLikeExpression(entityPath, fieldName, searchStr);
    }
    else if (Boolean.class == fieldType || boolean.class == fieldType) {
        return createBooleanExpression(entityPath, fieldName, searchStr);
    }
    else if (Number.class.isAssignableFrom(fieldType)
            || NUMBER_PRIMITIVES.contains(fieldType)) {
        return createNumberExpressionGenerics(entityPath, fieldName,
                fieldType, descriptor, searchStr);
    }
    else if (Date.class.isAssignableFrom(fieldType)
            || Calendar.class.isAssignableFrom(fieldType)) {
        BooleanExpression expression = createDateExpression(entityPath,
                fieldName, (Class<Date>) fieldType, searchStr);
        return expression;
    }

    else if (fieldType.isEnum()) {
        return createEnumExpression(entityPath, fieldName, searchStr,
                (Class<? extends Enum>) fieldType);
    }
    return null;
}
 
Example 15
/**
 * Method to create Bounding box intersects expression
 * 
 * @param entityPath
 * @param boundingBox
 * @param fieldName
 * @return
 */
public static <T> Predicate createIntersectsExpression(
        PathBuilder<T> entityPath, String fieldName, Geometry boundingBox) {
    JTSPolygonPath<Polygon> polygonPath = new JTSPolygonPath<Polygon>(
            entityPath, fieldName);
    BooleanExpression intersectsExpression = polygonPath
            .intersects(boundingBox);
    return intersectsExpression;
}
 
Example 16
Source Project: java-platform   Source File: ProductFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<Product> findByProductCategory(ProductCategory productCategory, int size) {
	QProduct qProduct = QProduct.product;
	Predicate predicate = qProduct.productCategory.eq(productCategory);
	return findAll(predicate, new PageRequest(0, size, new Sort(Direction.DESC, "createdDate")));
}
 
Example 17
Source Project: java-platform   Source File: RoleController.java    License: Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/userSelect", method = RequestMethod.GET)
@ResponseBody
public Page<User> userSelect(@RequestParam(value = "id", required = false) Role role, Predicate predicate,
		Pageable pageable) {
	return userService.findAll(QUser.user.userRoles.any().role.eq(role).not().and(predicate), pageable);
}
 
Example 18
Source Project: java-platform   Source File: RoleController.java    License: Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/user", method = RequestMethod.GET)
@ResponseBody
public Page<User> user(@RequestParam(value = "id", required = false) Role role, Predicate predicate,
		Pageable pageable) {
	return userService.findAll(QUser.user.userRoles.any().role.eq(role).and(predicate), pageable);
}
 
Example 19
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public T findOne(Predicate predicate) {
	return baseService.findOne(predicate);
}
 
Example 20
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate) {
	return baseService.findAll(predicate);
}
 
Example 21
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate, Sort sort) {
	return baseService.findAll(predicate, sort);
}
 
Example 22
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
	return baseService.findAll(predicate, orders);
}
 
Example 23
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public Page<T> findAll(Predicate predicate, Pageable pageable) {
	return baseService.findAll(predicate, pageable);
}
 
Example 24
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public long count(Predicate predicate) {
	return baseService.count(predicate);
}
 
Example 25
Source Project: java-platform   Source File: EntityFunctionPackage.java    License: Apache License 2.0 4 votes vote down vote up
public boolean exists(Predicate predicate) {
	return baseService.exists(predicate);
}
 
Example 26
Source Project: java-platform   Source File: EntityController.java    License: Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public Page<T> list(Predicate predicate, Pageable pageable) {
	return baseService.findAll(predicate, pageable);
}
 
Example 27
Source Project: java-platform   Source File: BaseService.java    License: Apache License 2.0 4 votes vote down vote up
public T findOne(Predicate predicate) {
	return baseRepository.findOne(predicate);
}
 
Example 28
Source Project: java-platform   Source File: BaseService.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate) {
	return baseRepository.findAll(predicate);
}
 
Example 29
Source Project: java-platform   Source File: BaseService.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate, Sort sort) {
	return baseRepository.findAll(predicate, sort);
}
 
Example 30
Source Project: java-platform   Source File: BaseService.java    License: Apache License 2.0 4 votes vote down vote up
public Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
	return baseRepository.findAll(predicate, orders);
}