All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gvnix.web.datatables.util.QuerydslUtilsBean Maven / Gradle / Ivy

There is a newer version: 2.0.0.M1
Show newest version
/*
 * Copyright 2015 DiSiD Technologies S.L.L. All rights reserved.
 * 
 * Project  : DiSiD org.gvnix.web.datatables 
 * SVN Id   : $Id$
 */
package org.gvnix.web.datatables.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.core.convert.TypeDescriptor;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.types.Order;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.expr.BooleanExpression;
import com.mysema.query.types.path.PathBuilder;

public interface QuerydslUtilsBean {

    public static final String OPERATOR_GOE = "goe";
    public static final String OPERATOR_LOE = "loe";
    public static final String OPERATOR_ISNULL = "isnull";
    public static final String OPERATOR_NOTNULL = "notnull";
    public static final String G_FIL_OPE_ISNULL = "global.filters.operations.all.isnull";
    public static final String G_FIL_OPE_NOTNULL = "global.filters.operations.all.notnull";
    public static final TypeDescriptor STRING_TYPE_DESCRIPTOR = TypeDescriptor
            .valueOf(String.class);
    public static final Set> NUMBER_PRIMITIVES = new HashSet>(
            Arrays.asList(new Class[] { int.class, long.class, double.class,
                    float.class, short.class }));
    public static final String OPERATOR_PREFIX = "_operator_";
    public static final String SEPARATOR_FIELDS = ".";
    public static final String[] FULL_DATE_PATTERNS = new String[] {
            "dd-MM-yyyy HH:mm:ss", "dd/MM/yyyy HH:mm:ss",
            "MM-dd-yyyy HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "dd-MM-yyyy HH:mm",
            "dd/MM/yyyy HH:mm", "MM-dd-yyyy HH:mm", "MM/dd/yyyy HH:mm",
            "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy",
            "dd-MMMM-yyyy HH:mm:ss", "dd/MMMM/yyyy HH:mm:ss",
            "MMMM-dd-yyyy HH:mm:ss", "MMMM/dd/yyyy HH:mm:ss",
            "dd-MMMM-yyyy HH:mm", "dd/MMMM/yyyy HH:mm", "MMMM-dd-yyyy HH:mm",
            "MMMM/dd/yyyy HH:mm", "dd-MMMM-yyyy", "dd/MMMM/yyyy",
            "MMMM-dd-yyyy", "MMMM/dd/yyyy" };
    public static final String[] FULL_DATE_PATTERNS_WITH_TIME = new String[] {
            "dd-MM-yyyy HH:mm:ss", "dd/MM/yyyy HH:mm:ss",
            "MM-dd-yyyy HH:mm:ss", "MM/dd/yyyy HH:mm:ss", "dd-MM-yyyy HH:mm",
            "dd/MM/yyyy HH:mm", "MM-dd-yyyy HH:mm", "MM/dd/yyyy HH:mm",
            "dd-MMMM-yyyy HH:mm:ss", "dd/MMMM/yyyy HH:mm:ss",
            "MMMM-dd-yyyy HH:mm:ss", "MMMM/dd/yyyy HH:mm:ss",
            "dd-MMMM-yyyy HH:mm", "dd/MMMM/yyyy HH:mm", "MMMM-dd-yyyy HH:mm",
            "MMMM/dd/yyyy HH:mm" };
    public static final String[] FULL_DATE_PAT_WO_TIME = new String[] {
            "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MMMM/dd/yyyy",
            "dd-MMMM-yyyy", "dd/MMMM/yyyy", "MMMM-dd-yyyy", "MMMM/dd/yyyy" };
    public static final String[] DAY_AND_MONTH_DATE_PATTERNS = new String[] {
            "dd-MM", "dd/MM", "MM-dd", "MM/dd", "dd-MMMM", "dd/MMMM",
            "MMMM-dd", "MMMM/dd" };
    public static final String[] MONTH_AND_YEAR_DATE_PATTERNS = new String[] {
            "MM-yyyy", "MM/yyyy", "MMMM-yyyy", "MMMM/yyyy" };

    /**
     * Creates a WHERE clause by the intersection of the given search-arguments
     * 
     * @param entity Entity {@link PathBuilder}. It represents the entity for
     *        class generation and alias-usage for path generation.
     *        

* Example: To retrieve a {@code Customer} with the first name 'Bob' * entity must be a {@link PathBuilder} created for {@code Customer} * class and searchArgs must contain the entry * {@code 'firstName':'Bob'} * @param searchArgs Search arguments to be used to create the WHERE clause. * It can contain {@code _operator_} entries for each field that want * to use its own operator. By default {@code EQUALS} operator is * used. *

* Operator entry example: {@code _operator_weight = LT} the * expression for {@code weight} field will do a less-than value * comparison * @return the WHERE clause */ public BooleanBuilder createPredicateByAnd(PathBuilder entity, Map searchArgs); /** * Creates a WHERE clause to specify given {@code fieldName} must be equal * to one element of the provided Collection. * * @param entity Entity {@link PathBuilder}. It represents the entity for * class generation and alias-usage for path generation. *

* Example: To retrieve a {@code Customer} with the first name 'Bob' * entity must be a {@link PathBuilder} created for {@code Customer} * class and searchArgs must contain the entry * {@code 'firstName':'Bob'} * @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 the WHERE clause */ public BooleanBuilder createPredicateByIn(PathBuilder entity, String fieldName, Set values); /** * Utility for constructing where clause expressions on column filters. * * @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 * */ public Predicate createFilterExpression(PathBuilder entityPath, String fieldName, Class fieldType, String filterStr); /** * Utility for constructing where clause expressions on column filters. * * @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 searchStr the value to find, may be null * @return predicate */ public Predicate createFilterExpression(PathBuilder entityPath, String fieldName, String filterStr); /** * Utility for constructing where clause expressions on generic search. * * @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 * */ public Predicate createSearchExpression(PathBuilder entityPath, String fieldName, Class fieldType, String searchStr); /** * Utility for constructing where clause expressions on generic search. * * @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 searchStr the value to find, may be null * @return predicate */ public Predicate createSearchExpression(PathBuilder entityPath, String fieldName, String searchStr); public Predicate createNumberExpressionGenerics( PathBuilder entityPath, String fieldName, Class fieldType, TypeDescriptor descriptor, String searchStr); public Predicate createNumberExpressionGenericsWithOperators( PathBuilder entityPath, String fieldName, TypeDescriptor descriptor, String searchStr); /** * Return equal expression for {@code entityPath.fieldName}. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @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 searchObj the value to find, may be null * @return BooleanExpression */ public BooleanExpression createObjectExpression( PathBuilder entityPath, String fieldName, Object searchObj); /** * Return an expression for {@code entityPath.fieldName} with the * {@code operator} or "equal" by default. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @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 searchObj the value to find, may be null * @param operator the operator to use into the expression. Supported * operators: *

    *
  • For all types: {@code eq}, {@code in}, {@code ne}, * {@code notIn}, {@code isNull} and {@code isNotNull}.
  • For * strings and numbers: {@code goe}, {@code gt}, {@code loe}, * {@code lt} and {@code like}.
  • For booleans: {@code goe}, * {@code gt}, {@code loe} and {@code lt}.
  • For dates: * {@code goe}, {@code gt}, {@code before}, {@code loe}, {@code lt} * and {@code after}.
  • *
* @return BooleanExpression */ public BooleanExpression createObjectExpression( PathBuilder entityPath, String fieldName, Object searchObj, String operator); /** * Return an expression for {@code entityPath.fieldName} (for Dates) with * the {@code operator} or "equal" by default. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @param entityPath * @param fieldName * @param searchObj * @param operator * @param fieldType * @return */ public BooleanExpression createDateExpression( PathBuilder entityPath, String fieldName, Object searchObj, String operator, Class fieldType); /** * Return an expression for {@code entityPath.fieldName} (for Numerics) with * the {@code operator} or "equal" by default. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @param entityPath * @param fieldName * @param searchObj * @param operator * @param fieldType * @return */ public BooleanExpression createNumericExpression( PathBuilder entityPath, String fieldName, Object searchObj, String operator, Class fieldType); /** * Return an expression for {@code entityPath.fieldName} (for Booleans) with * the {@code operator} or "equal" by default. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @param entityPath * @param fieldName * @param searchObj * @param operator * @return */ public BooleanExpression createBooleanExpression( PathBuilder entityPath, String fieldName, Object searchObj, String operator); /** * Return an expression for {@code entityPath.fieldName} (for Strings) with * the {@code operator} or "equal" by default. *

* Expr: {@code entityPath.fieldName eq searchObj} * * @param entityPath * @param fieldName * @param searchObj * @param operator * @return */ public BooleanExpression createStringExpression( PathBuilder entityPath, String fieldName, Object searchObj, String operator); /** * Return equal expression for {@code entityPath.fieldName}. *

* Expr: {@code entityPath.fieldName eq 'searchStr'} *

* Equal 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 name} in {@code Pet} entity, {@code firstName} in * {@code Pet.owner} entity. * @param searchStr the value to find, may be null * @return BooleanExpression */ public BooleanExpression createStringExpression( PathBuilder entityPath, String fieldName, String searchStr); /** * Return like expression for {@code entityPath.fieldName}. *

* Expr: {@code entityPath.fieldName like ('%' + searchStr + '%')} *

* 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 name} in {@code Pet} entity, {@code firstName} in * {@code Pet.owner} entity. * @param searchStr the value to find, may be null * @return BooleanExpression */ public BooleanExpression createStringLikeExpression( PathBuilder entityPath, String fieldName, String searchStr); /** * Return like expression for {@code entityPath.fieldName}. *

* Expr: {@code entityPath.fieldName like ('%' + searchStr + '%')} *

* 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 name} in {@code Pet} entity, {@code firstName} in * {@code Pet.owner} entity. * @param searchStr the value to find, may be null * @return BooleanExpression */ public BooleanExpression createStringExpressionWithOperators( PathBuilder entityPath, String fieldName, String searchStr); /** * Return where clause expression for number properties by casting it to * string before check its value. *

* Querydsl Expr: * {@code entityPath.fieldName.stringValue() like ('%' + searchStr + '%')} * Database operation: * {@code str(entity.fieldName) like ('%' + searchStr + '%')} *

* 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 fieldType Property value {@code Class} * @param descriptor * @param searchStr the value to find, may be null * @return PredicateOperation */ public > BooleanExpression createNumberExpression( PathBuilder entityPath, String fieldName, Class fieldType, TypeDescriptor descriptor, String searchStr); /** * Return where clause expression for number properties by casting it to * string before check its value. *

* Querydsl Expr: * {@code entityPath.fieldName.stringValue() eq searchStr * Database operation: * {@code str(entity.fieldName) = searchStr *

* 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 fieldType Property value {@code Class} * @ param descriptor * @param searchStr the value to find, may be null * @return PredicateOperation */ public > BooleanExpression createNumberExpressionEqual( PathBuilder entityPath, String fieldName, Class fieldType, TypeDescriptor descriptor, String searchStr); /** * Return where clause expression for date properties, trying to parse the * value to find to date and comparing it to the value of the date; if the * value to find cannot be parsed to date, then try to cast the value to * string before check it. *

*

    *
  • If value to find {@code searchStr} can be parsed using the patterns * dd-MM-yyyy HH:mm:ss or dd-MM-yyyy HH:mm or * dd-MM-yyyy to {@code searchDate}, then search by specific date: *

    * - Querydsl Expr: {@code entityPath.fieldName = searchDate} *

    * - Database operation: {@code entity.fieldName = searchDate}

  • *
  • If value to find {@code searchStr} can be parsed using the pattern * dd-MM to {@code searchDate}, then search by specific day and * month: *

    * - Querydsl Expr: * {@code entityPath.fieldName.dayOfMonth() = searchDate.day and entityPath.fieldName.month() = searchDate.month} *

    * - Database operation: * {@code dayofmonth(entity.fieldName) = searchDate.day && month(entity.fieldName) = searchDate.month} *

  • *
  • If value to find {@code searchStr} can be parsed using the pattern * MM-aaaa to {@code searchDate}, then obtain the first day of the * month for that year and the last day of the month for that year and check * that value is into between theses values: *

    * - Querydsl Expr: * {@code entityPath.fieldName.between(searchDate.firstDayOfMonth, searchDate.lastDayOfMonth)} *

    * - Database operation: * {@code entity.fieldName between searchDate.firstDayOfMonth and searchDate.lastDayOfMonth} *

  • *
  • If value to find cannot be parsed as date, then try to cast the value * to string before check it: *

    * - Querydsl Expr: * {@code entityPath.fieldName.stringValue() like ('%' + searchStr + '%')} *

    * - Database operation: * {@code str(entity.fieldName) like ('%' + searchStr + '%')} *

    * Note that 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 fieldType Property value {@code Class} * @param searchStr the value to find, may be null * @return PredicateOperation */ public > BooleanExpression createDateExpression( PathBuilder entityPath, String fieldName, Class fieldType, String searchStr); /** * Return where clause expression for date properties, trying to parse the * value to find to date and comparing it to the value of the date; if the * value to find cannot be parsed to date, then try to cast the value to * string before check it. *

*

    *
  • If value to find {@code searchStr} can be parsed using the patterns * dd-MM-yyyy HH:mm:ss or dd-MM-yyyy HH:mm or * dd-MM-yyyy to {@code searchDate}, then search by specific date: *

    * - Querydsl Expr: {@code entityPath.fieldName = searchDate} *

    * - Database operation: {@code entity.fieldName = searchDate}

  • *
  • If value to find {@code searchStr} can be parsed using the pattern * dd-MM to {@code searchDate}, then search by specific day and * month: *

    * - Querydsl Expr: * {@code entityPath.fieldName.dayOfMonth() = searchDate.day and entityPath.fieldName.month() = searchDate.month} *

    * - Database operation: * {@code dayofmonth(entity.fieldName) = searchDate.day && month(entity.fieldName) = searchDate.month} *

  • *
  • If value to find {@code searchStr} can be parsed using the pattern * MM-aaaa to {@code searchDate}, then obtain the first day of the * month for that year and the last day of the month for that year and check * that value is into between theses values: *

    * - Querydsl Expr: * {@code entityPath.fieldName.between(searchDate.firstDayOfMonth, searchDate.lastDayOfMonth)} *

    * - Database operation: * {@code entity.fieldName between searchDate.firstDayOfMonth and searchDate.lastDayOfMonth} *

  • *
  • If value to find cannot be parsed as date, then try to cast the value * to string before check it: *

    * - Querydsl Expr: * {@code entityPath.fieldName.stringValue() like ('%' + searchStr + '%')} *

    * - Database operation: * {@code str(entity.fieldName) like ('%' + searchStr + '%')} *

    * Note that 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 fieldType Property value {@code Class} * @param searchStr the value to find, may be null * @param datePattern * @return PredicateOperation */ public > BooleanExpression createDateExpressionWithOperators( PathBuilder entityPath, String fieldName, Class fieldType, String searchStr, String datePattern); /** * Return where clause expression for non-String * {@code entityPath.fieldName} by transforming it to text before check its * value. *

* Expr: * {@code entityPath.fieldName.as(String.class) like ('%' + searchStr + '%')} *

* 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" }) public BooleanExpression createEnumExpression( PathBuilder entityPath, String fieldName, String searchStr, Class enumClass); /** * Return where clause expression for {@code Boolean} fields by transforming * the given {@code searchStr} to {@code Boolean} before check its value. *

* 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 BooleanExpression createBooleanExpression( PathBuilder entityPath, String fieldName, String searchStr); /** * Return where clause expression for {@code Boolean} fields by transforming * the given {@code searchStr} to {@code Boolean} before check its value. *

* 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 BooleanExpression createBooleanExpressionWithOperators( PathBuilder entityPath, String fieldName, String searchStr); /** * Return IN expression for {@code entityPath.fieldName}. *

* Expr:
* entityPath.fieldName IN ( values )
*
* If values.size() > 500 its generates:
* Expr:
* (entityPath.fieldName IN ( values[0-500] ) OR [entityPath.fieldName IN ( * values[501-100]... ]))
*
* * @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 BooleanExpression createCollectionExpression( PathBuilder entityPath, String fieldName, Collection values); public BooleanExpression doCreateCollectionExpression( PathBuilder entityPath, String fieldName, Collection values); /** * Create an order-by-element in a Query instance * * @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 fieldType Property value {@code Class}. Must implements * {@link Comparable} * @param order ascending or descending order * @return */ public > OrderSpecifier createOrderSpecifier( PathBuilder entityPath, String fieldName, Class fieldType, Order order); /** * This method returns the query expression based on String expression * user-written. Expression can be "=", ">", "<", ">=", "<=", "<>", "!=", * "ENTRENUMERO(n1;n2)" * * @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 fieldType Property value {@code Class}. Must implements * {@link Comparable} * @param descriptor * @param searchStr * @return */ public > BooleanExpression getNumericFilterExpression( PathBuilder entityPath, String fieldName, Class fieldType, TypeDescriptor descriptor, String searchStr); /** * Obtains the class type of the property named as {@code fieldName} of the * entity. * * @param fieldName the field name. * @param entity the entity with a property named as {@code fieldName} * @return the class type */ public Class getFieldType(String fieldName, PathBuilder entity); /** * Obtains the class type of the property named as {@code fieldName} of the * entity. * * @param fieldName the field name. * @param entity the entity with a property named as {@code fieldName} * @return the class type */ public Class getFieldType1(String fieldName, PathBuilder entity); /** * Obtains the descriptor of the filtered field * * @param fieldName * @param entity * @return */ public TypeDescriptor getTypeDescriptor(String fieldName, PathBuilder entity); /** * Obtains the descriptor of the filtered field * * @param fieldName * @param entityType * @return */ public TypeDescriptor getTypeDescriptor(String fieldName, Class entityType); /** * This method checks if the search string can be converted to a number * using conversionService with locale. * * @param searchStr * @param conversionService * @param descriptor * @return */ public boolean isNumber(String searchStr, TypeDescriptor descriptor); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy