com.mysema.query.types.expr.BooleanExpression Java Examples

The following examples show how to use com.mysema.query.types.expr.BooleanExpression. 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 File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createStringExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator) {
    if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
        return entityPath.getString(fieldName).goe((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "gt")) {
        return entityPath.getString(fieldName).gt((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
        return entityPath.getString(fieldName).loe((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "lt")) {
        return entityPath.getString(fieldName).lt((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "like")) {
        return entityPath.getString(fieldName).like((String) searchObj);
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #2
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Return where clause expression for {@code Boolean} fields by transforming
 * the given {@code searchStr} to {@code Boolean} before check its value.
 * <p/>
 * Expr: {@code entityPath.fieldName eq (TRUE | FALSE)}
 * 
 * @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 weight} in {@code Pet} entity, {@code age} in
 *        {@code Pet.owner} entity.
 * @param searchStr the boolean value to find, may be null. Supported string
 *        are: si, yes, true, on, no, false, off
 * @return BooleanExpression
 */
public static <T> BooleanExpression createBooleanExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr) {
    if (StringUtils.isBlank(searchStr)) {
        return null;
    }

    Boolean value = null;

    // I18N: Spanish (normalize search value: trim start-end and lower case)
    if ("si".equals(StringUtils.trim(searchStr).toLowerCase())) {
        value = Boolean.TRUE;
    }
    else {
        value = BooleanUtils.toBooleanObject(searchStr);
    }

    // if cannot parse to boolean or null input
    if (value == null) {
        return null;
    }

    BooleanExpression expression = entityPath.getBoolean(fieldName).eq(
            value);
    return expression;
}
 
Example #3
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Return an expression for {@code entityPath.fieldName} (for Strings) with
 * the {@code operator} or "equal" by default.
 * <p/>
 * Expr: {@code entityPath.fieldName eq searchObj}
 * 
 * @param entityPath
 * @param fieldName
 * @param searchObj
 * @param operator
 * @return
 */
public static <T> BooleanExpression createStringExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator) {
    if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
        return entityPath.getString(fieldName).goe((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "gt")) {
        return entityPath.getString(fieldName).gt((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
        return entityPath.getString(fieldName).loe((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "lt")) {
        return entityPath.getString(fieldName).lt((String) searchObj);
    }
    else if (StringUtils.equalsIgnoreCase(operator, "like")) {
        return entityPath.getString(fieldName).like((String) searchObj);
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #4
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Return an expression for {@code entityPath.fieldName} (for Booleans) with
 * the {@code operator} or "equal" by default.
 * <p/>
 * Expr: {@code entityPath.fieldName eq searchObj}
 * 
 * @param entityPath
 * @param fieldName
 * @param searchObj
 * @param operator
 * @return
 */
public static <T> BooleanExpression createBooleanExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator) {
    Boolean value = BooleanUtils.toBooleanObject((String) searchObj);
    if (value != null) {
        if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
            return entityPath.getBoolean(fieldName).goe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "gt")) {
            return entityPath.getBoolean(fieldName).gt(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
            return entityPath.getBoolean(fieldName).loe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "lt")) {
            return entityPath.getBoolean(fieldName).lt(value);
        }
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #5
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createBooleanExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator) {
    Boolean value = BooleanUtils.toBooleanObject((String) searchObj);
    if (value != null) {
        if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
            return entityPath.getBoolean(fieldName).goe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "gt")) {
            return entityPath.getBoolean(fieldName).gt(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
            return entityPath.getBoolean(fieldName).loe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "lt")) {
            return entityPath.getBoolean(fieldName).lt(value);
        }
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #6
Source File: PhotoPredicateBuilder.java    From PhotoAlbum-api with MIT License 6 votes vote down vote up
public BooleanExpression build() {
  if (this.getCriteria().size() == 0) {
    return null;
  }

  List<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
  PhotoPredicate predicate;
  for (SearchCriteria c : this.getCriteria()) {
    predicate = new PhotoPredicate(c);
    BooleanExpression exp = predicate.getPredicate();
    if (exp != null) {
      predicates.add(exp);
    }
  }

  BooleanExpression result = predicates.get(0);
  for (int i = 1; i < predicates.size(); i++) {
    result = result.and(predicates.get(i));
  }

  return result;
}
 
Example #7
Source File: AlbumPredicateBuilder.java    From PhotoAlbum-api with MIT License 6 votes vote down vote up
public BooleanExpression build() {
  if (this.getCriteria().size() == 0) {
    return null;
  }

  List<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
  AlbumPredicate predicate;
  for (SearchCriteria c : this.getCriteria()) {
    predicate = new AlbumPredicate(c);
    BooleanExpression exp = predicate.getPredicate();
    if (exp != null) {
      predicates.add(exp);
    }
  }

  BooleanExpression result = predicates.get(0);
  for (int i = 1; i < predicates.size(); i++) {
    result = result.and(predicates.get(i));
  }

  return result;
}
 
Example #8
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createBooleanExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr) {
    if (StringUtils.isBlank(searchStr)) {
        return null;
    }

    Boolean value = null;

    // I18N: Spanish (normalize search value: trim start-end and lower case)
    if ("si".equals(StringUtils.trim(searchStr).toLowerCase())) {
        value = Boolean.TRUE;
    }
    else {
        value = BooleanUtils.toBooleanObject(searchStr);
    }

    // if cannot parse to boolean or null input
    if (value == null) {
        return null;
    }

    BooleanExpression expression = entityPath.getBoolean(fieldName).eq(
            value);
    return expression;
}
 
Example #9
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public <T, N extends Number & Comparable<?>> BooleanExpression createNumberExpressionEqual(
        PathBuilder<T> entityPath, String fieldName, Class<N> fieldType,
        TypeDescriptor descriptor, String searchStr) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    NumberPath<N> numberExpression = entityPath.getNumber(fieldName,
            fieldType);

    TypeDescriptor strDesc = STRING_TYPE_DESCRIPTOR;

    if (conversionService != null) {
        try {
            return numberExpression.eq((N) conversionService.convert(
                    searchStr, strDesc, descriptor));
        }
        catch (ConversionException ex) {
            return numberExpression.stringValue().like(
                    "%".concat(searchStr).concat("%"));
        }
    }
    else {
        return numberExpression.stringValue().like(
                "%".concat(searchStr).concat("%"));
    }
}
 
Example #10
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createStringLikeExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    String str = "%".concat(searchStr.toLowerCase()).concat("%");
    BooleanExpression expression = entityPath.getString(fieldName).lower()
            .like(str);
    return expression;

}
 
Example #11
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createStringExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    BooleanExpression expression = entityPath.getString(fieldName).lower()
            .eq(searchStr.toLowerCase());
    return expression;
}
 
Example #12
Source File: QuerydslUtilsBeanGeoImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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 #13
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T, E> BooleanExpression createCollectionExpression(
        PathBuilder<T> entityPath, String fieldName, Collection<E> values) {
    if (StringUtils.isEmpty(fieldName) || values.isEmpty()) {
        return null;
    }

    if (values.size() > 500) {
        BooleanExpression expression = null;
        Iterable<List<E>> collectionParts = Iterables
                .partition(values, 500);
        for (List<E> part : collectionParts) {
            if (expression == null) {
                expression = doCreateCollectionExpression(entityPath,
                        fieldName, part);
            }
            else {
                expression = expression.or(doCreateCollectionExpression(
                        entityPath, fieldName, part));
            }
        }
        return expression;
    }
    else {
        return doCreateCollectionExpression(entityPath, fieldName, values);
    }
}
 
Example #14
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public <T> BooleanExpression createDateExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator, Class<?> fieldType) {
    DatePath<Date> dateExpression = entityPath.getDate(fieldName,
            (Class<Date>) fieldType);
    try {
        Date value = DateUtils.parseDateStrictly((String) searchObj,
                FULL_DATE_PATTERNS);
        if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
            return dateExpression.goe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "gt")
                || StringUtils.equalsIgnoreCase(operator, "after")) {
            return dateExpression.gt(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
            return dateExpression.loe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "lt")
                || StringUtils.equalsIgnoreCase(operator, "before")) {
            return dateExpression.lt(value);
        }
    }
    catch (ParseException e) {
        return entityPath.get(fieldName).eq(searchObj);
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #15
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createObjectExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj) {
    return createObjectExpression(entityPath, fieldName, searchObj, null);

}
 
Example #16
Source File: QuerydslUtilsBeanImpl.java    From gvnix with 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 #17
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return IN expression for {@code entityPath.fieldName}.
 * <p/>
 * Expr: <br/>
 * entityPath.fieldName IN ( values ) <br/>
 * <br/>
 * If values.size() > 500 its generates: <br/>
 * Expr: <br/>
 * (entityPath.fieldName IN ( values[0-500] ) OR [entityPath.fieldName IN (
 * values[501-100]... ])) <br/>
 * <br/>
 * 
 * @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 values the Set of values to find the given field name, may be null
 * @return BooleanExpression
 */
public static <T, E> BooleanExpression createCollectionExpression(
        PathBuilder<T> entityPath, String fieldName, Collection<E> values) {
    if (StringUtils.isEmpty(fieldName) || values.isEmpty()) {
        return null;
    }

    if (values.size() > 500) {
        BooleanExpression expression = null;
        Iterable<List<E>> collectionParts = Iterables
                .partition(values, 500);
        for (List<E> part : collectionParts) {
            if (expression == null) {
                expression = doCreateCollectionExpression(entityPath,
                        fieldName, part);
            }
            else {
                expression = expression.or(doCreateCollectionExpression(
                        entityPath, fieldName, part));
            }
        }
        return expression;
    }
    else {
        return doCreateCollectionExpression(entityPath, fieldName, values);
    }
}
 
Example #18
Source File: PhotoServiceImpl.java    From PhotoAlbum-api with MIT License 5 votes vote down vote up
@Override
public Iterable<Photo> getPhotos(String search, Pageable pageable) {
PhotoPredicateBuilder builder = new PhotoPredicateBuilder();

if (search != null) {
	Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
	Matcher matcher = pattern.matcher(search + ",");
	while (matcher.find()) {
		builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
	}
}
BooleanExpression exp = builder.build();

return this.photoRepository.findAll(exp, pageable).getContent();
}
 
Example #19
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return where clause expression for number properties by casting it to
 * string before check its value.
 * <p/>
 * Querydsl Expr:
 * {@code entityPath.fieldName.stringValue() eq searchStr
 * Database operation:
 * {@code str(entity.fieldName) = searchStr
 * <p/>
 * Like operation is case sensitive.
 *
 * @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 weight} in {@code Pet} entity, {@code age} in
 *        {@code Pet.owner} entity.
 * @param searchStr the value to find, may be null
 * @return PredicateOperation
 */
@SuppressWarnings("unchecked")
public static <T, N extends java.lang.Number & java.lang.Comparable<?>> BooleanExpression createNumberExpressionEqual(
        PathBuilder<T> entityPath, String fieldName, Class<N> fieldType,
        TypeDescriptor descriptor, String searchStr,
        ConversionService conversionService) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    NumberPath<N> numberExpression = entityPath.getNumber(fieldName,
            fieldType);

    TypeDescriptor strDesc = STRING_TYPE_DESCRIPTOR;

    if (conversionService != null) {
        try {
            return numberExpression.eq((N) conversionService.convert(
                    searchStr, strDesc, descriptor));
        }
        catch (ConversionException ex) {
            return numberExpression.stringValue().like(
                    "%".concat(searchStr).concat("%"));
        }
    }
    else {
        return numberExpression.stringValue().like(
                "%".concat(searchStr).concat("%"));
    }
}
 
Example #20
Source File: AlbumServiceImpl.java    From PhotoAlbum-api with MIT License 5 votes vote down vote up
@Override
public Iterable<Album> getAlbums(String search, Pageable pageable) {
	AlbumPredicateBuilder builder = new AlbumPredicateBuilder();

	if (search != null) {
		Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
		Matcher matcher = pattern.matcher(search + ",");
		while (matcher.find()) {
			builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
		}
	}
	BooleanExpression exp = builder.build();

   return this.albumRepository.findAll(exp, pageable).getContent();
 }
 
Example #21
Source File: QuerydslUtils.java    From gvnix with 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 #22
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Return an expression for {@code entityPath.fieldName} (for Dates) with
 * the {@code operator} or "equal" by default.
 * <p/>
 * Expr: {@code entityPath.fieldName eq searchObj}
 * 
 * @param entityPath
 * @param fieldName
 * @param searchObj
 * @param operator
 * @param fieldType
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> BooleanExpression createDateExpression(
        PathBuilder<T> entityPath, String fieldName, Object searchObj,
        String operator, Class<?> fieldType) {
    DatePath<Date> dateExpression = entityPath.getDate(fieldName,
            (Class<Date>) fieldType);
    try {
        Date value = DateUtils.parseDateStrictly((String) searchObj,
                FULL_DATE_PATTERNS);
        if (StringUtils.equalsIgnoreCase(operator, OPERATOR_GOE)) {
            return dateExpression.goe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "gt")
                || StringUtils.equalsIgnoreCase(operator, "after")) {
            return dateExpression.gt(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, OPERATOR_LOE)) {
            return dateExpression.loe(value);
        }
        else if (StringUtils.equalsIgnoreCase(operator, "lt")
                || StringUtils.equalsIgnoreCase(operator, "before")) {
            return dateExpression.lt(value);
        }
    }
    catch (ParseException e) {
        return entityPath.get(fieldName).eq(searchObj);
    }
    return entityPath.get(fieldName).eq(searchObj);
}
 
Example #23
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Predicate createFilterExpression(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 createStringExpressionWithOperators(entityPath, fieldName,
                searchStr);
    }
    else if (Boolean.class == fieldType || boolean.class == fieldType) {
        return createBooleanExpressionWithOperators(entityPath, fieldName,
                searchStr);
    }
    else if (Number.class.isAssignableFrom(fieldType)
            || NUMBER_PRIMITIVES.contains(fieldType)) {
        return createNumberExpressionGenericsWithOperators(entityPath,
                fieldName, descriptor, searchStr);
    }
    else if (Date.class.isAssignableFrom(fieldType)
            || Calendar.class.isAssignableFrom(fieldType)) {
        String datePattern = "dd/MM/yyyy";
        if (messageSource != null) {
            datePattern = messageSource.getMessage(
                    "global.filters.operations.date.pattern", null,
                    LocaleContextHolder.getLocale());
        }
        BooleanExpression expression = createDateExpressionWithOperators(
                entityPath, fieldName, (Class<Date>) fieldType, searchStr,
                datePattern);
        return expression;
    }

    else if (fieldType.isEnum()) {
        return createEnumExpression(entityPath, fieldName, searchStr,
                (Class<? extends Enum>) fieldType);
    }
    return null;
}
 
Example #24
Source File: QuerydslUtils.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return where clause expression for non-String
 * {@code entityPath.fieldName} by transforming it to text before check its
 * value.
 * <p/>
 * Expr:
 * {@code entityPath.fieldName.as(String.class) like ('%' + searchStr + '%')}
 * <p/>
 * Like operation is case insensitive.
 * 
 * @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 weight} in {@code Pet} entity, {@code age} in
 *        {@code Pet.owner} entity.
 * @param searchStr the value to find, may be null
 * @param enumClass Enumeration type. Needed to enumeration values
 * @return BooleanExpression
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> BooleanExpression createEnumExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr,
        Class<? extends Enum> enumClass) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    // Filter string to search than cannot be a identifier
    if (!StringUtils.isAlphanumeric(StringUtils.lowerCase(searchStr))) {
        return null;
    }

    // TODO i18n of enum name

    // normalize search string
    searchStr = StringUtils.trim(searchStr).toLowerCase();

    // locate enums matching by name
    Set matching = new HashSet();

    Enum<?> enumValue;
    String enumStr;

    for (Field enumField : enumClass.getDeclaredFields()) {
        if (enumField.isEnumConstant()) {
            enumStr = enumField.getName();
            enumValue = Enum.valueOf(enumClass, enumStr);

            // Check enum name contains string to search
            if (enumStr.toLowerCase().contains(searchStr)) {
                // Add to matching enum
                matching.add(enumValue);
                continue;
            }

            // Check using toString
            enumStr = enumValue.toString();
            if (enumStr.toLowerCase().contains(searchStr)) {
                // Add to matching enum
                matching.add(enumValue);
            }
        }
    }
    if (matching.isEmpty()) {
        return null;
    }

    // create a enum in matching condition
    BooleanExpression expression = entityPath.get(fieldName).in(matching);
    return expression;
}
 
Example #25
Source File: QuerydslUtilsBean.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
public <T, E> BooleanExpression doCreateCollectionExpression(
PathBuilder<T> entityPath, String fieldName, Collection<E> values);
 
Example #26
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
@Override
public <T, E> BooleanExpression doCreateCollectionExpression(
        PathBuilder<T> entityPath, String fieldName, Collection<E> values) {
    BooleanExpression expression = entityPath.get(fieldName).in(values);
    return expression;
}
 
Example #27
Source File: People.java    From ddd-javaee7 with Apache License 2.0 4 votes vote down vote up
public static BooleanExpression withSsn(Ssn ssn) {
	return QPerson.person.ssn.eq(ssn);
}
 
Example #28
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> BooleanExpression createBooleanExpressionWithOperators(
        PathBuilder<T> entityPath, String fieldName, String searchStr) {
    if (StringUtils.isBlank(searchStr)) {
        return null;
    }

    // Getting all operations
    String trueOperation = "TRUE";
    String falseOperation = "FALSE";
    String isNullOperation = OPERATOR_ISNULL;
    String isNotNullOperation = OPERATOR_NOTNULL;

    if (messageSource != null) {
        trueOperation = messageSource.getMessage(
                "global.filters.operations.boolean.true", null,
                LocaleContextHolder.getLocale());
        falseOperation = messageSource.getMessage(
                "global.filters.operations.boolean.false", null,
                LocaleContextHolder.getLocale());
        isNullOperation = messageSource.getMessage(G_FIL_OPE_ISNULL, null,
                LocaleContextHolder.getLocale());
        isNotNullOperation = messageSource.getMessage(G_FIL_OPE_NOTNULL,
                null, LocaleContextHolder.getLocale());
    }

    // If written function is TRUE
    Pattern trueOperator = Pattern.compile(String.format("%s",
            trueOperation));
    Matcher trueMatcher = trueOperator.matcher(searchStr);

    if (trueMatcher.matches()) {
        return entityPath.getBoolean(fieldName).eq(Boolean.TRUE);
    }

    // If written function is FALSE
    Pattern falseOperator = Pattern.compile(String.format("%s",
            falseOperation));
    Matcher falseMatcher = falseOperator.matcher(searchStr);

    if (falseMatcher.matches()) {
        return entityPath.getBoolean(fieldName).eq(Boolean.FALSE);
    }

    // If written expression is ISNULL operation
    Pattern isNullOperator = Pattern.compile(String.format("%s",
            isNullOperation));
    Matcher isNullMatcher = isNullOperator.matcher(searchStr);
    if (isNullMatcher.matches()) {
        return entityPath.getBoolean(fieldName).isNull();

    }

    // If written expression is ISNOTNULL operation
    Pattern isNotNullOperator = Pattern.compile(String.format("%s",
            isNotNullOperation));
    Matcher isNotNullMatcher = isNotNullOperator.matcher(searchStr);
    if (isNotNullMatcher.matches()) {
        return entityPath.getBoolean(fieldName).isNotNull();

    }

    return null;
}
 
Example #29
Source File: People.java    From ddd-javaee7 with Apache License 2.0 4 votes vote down vote up
public static BooleanExpression withName(String name) {
	return QPerson.person.name.containsIgnoreCase(name);
}
 
Example #30
Source File: QuerydslUtilsBeanImpl.java    From gvnix with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public <T> BooleanExpression createEnumExpression(
        PathBuilder<T> entityPath, String fieldName, String searchStr,
        Class<? extends Enum> enumClass) {
    if (StringUtils.isEmpty(searchStr)) {
        return null;
    }
    // Filter string to search than cannot be a identifier
    if (!StringUtils.isAlphanumeric(StringUtils.lowerCase(searchStr))) {
        return null;
    }

    // TODO i18n of enum name

    // normalize search string
    searchStr = StringUtils.trim(searchStr).toLowerCase();

    // locate enums matching by name
    Set matching = new HashSet();

    Enum<?> enumValue;
    String enumStr;

    for (Field enumField : enumClass.getDeclaredFields()) {
        if (enumField.isEnumConstant()) {
            enumStr = enumField.getName();
            enumValue = Enum.valueOf(enumClass, enumStr);

            // Check enum name contains string to search
            if (enumStr.toLowerCase().contains(searchStr)) {
                // Add to matching enum
                matching.add(enumValue);
                continue;
            }

            // Check using toString
            enumStr = enumValue.toString();
            if (enumStr.toLowerCase().contains(searchStr)) {
                // Add to matching enum
                matching.add(enumValue);
            }
        }
    }
    if (matching.isEmpty()) {
        return null;
    }

    // create a enum in matching condition
    BooleanExpression expression = entityPath.get(fieldName).in(matching);
    return expression;
}