Search in sources :

Example 1 with QueryBuilder

use of org.meveo.commons.utils.QueryBuilder in project meveo by meveo-org.

the class CustomEntityTemplateService method findByClassAndCode.

/**
 * A generic method that returns a filtered list of ICustomFieldEntity given an entity class and code.
 *
 * @param entityClass - class of an entity. eg. org.meveo.catalog.OfferTemplate
 * @param entityCode  - code of entity
 * @return customer field entity
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public ICustomFieldEntity findByClassAndCode(Class entityClass, String entityCode) {
    ICustomFieldEntity result = null;
    QueryBuilder queryBuilder = new QueryBuilder(entityClass, "a", null);
    queryBuilder.addCriterion("code", "=", entityCode, true);
    List<ICustomFieldEntity> entities = (List<ICustomFieldEntity>) queryBuilder.getQuery(getEntityManager()).getResultList();
    if (entities != null && !entities.isEmpty()) {
        result = entities.get(0);
    }
    return result;
}
Also used : ICustomFieldEntity(org.meveo.model.ICustomFieldEntity) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) QueryBuilder(org.meveo.commons.utils.QueryBuilder)

Example 2 with QueryBuilder

use of org.meveo.commons.utils.QueryBuilder in project meveo by meveo-org.

the class CustomEntityInstanceService method list.

public List<CustomEntityInstance> list(String cetCode, boolean isStoreAsTable, Map<String, Object> values, PaginationConfiguration paginationConfiguration) {
    QueryBuilder qb = new QueryBuilder(getEntityClass(), "cei", null);
    qb.addCriterion("cei.cetCode", "=", cetCode, true);
    qb.addPaginationConfiguration(paginationConfiguration);
    final List<CustomEntityInstance> resultList = qb.getTypedQuery(getEntityManager(), CustomEntityInstance.class).getResultList();
    if (values != null && !values.isEmpty()) {
        return resultList.stream().filter(customEntityInstance -> filterOnValues(values, customEntityInstance, isStoreAsTable)).collect(Collectors.toList());
    }
    return resultList;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) ELException(org.meveo.elresolver.ELException) NoResultException(javax.persistence.NoResultException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) Matcher(java.util.regex.Matcher) Map(java.util.Map) Observes(javax.enterprise.event.Observes) BigDecimal(com.ibm.icu.math.BigDecimal) Stateless(javax.ejb.Stateless) ModuleItem(org.meveo.model.ModuleItem) SqlConfiguration(org.meveo.model.sql.SqlConfiguration) WFTransitionService(org.meveo.service.wf.WFTransitionService) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) MeveoModule(org.meveo.model.module.MeveoModule) Collection(java.util.Collection) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Workflow(org.meveo.model.wf.Workflow) WFAction(org.meveo.model.wf.WFAction) MeveoModuleHelper(org.meveo.service.admin.impl.MeveoModuleHelper) List(java.util.List) ModuleUninstall(org.meveo.service.admin.impl.ModuleUninstall) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Pattern(java.util.regex.Pattern) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) HashMap(java.util.HashMap) ModulePostUninstall(org.meveo.model.ModulePostUninstall) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomFieldValues(org.meveo.model.crm.custom.CustomFieldValues) MeveoFileUtils(org.meveo.commons.utils.MeveoFileUtils) BusinessService(org.meveo.service.base.BusinessService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) CollectionUtils(org.apache.commons.collections.CollectionUtils) MeveoValueExpressionWrapper(org.meveo.service.base.MeveoValueExpressionWrapper) WFTransition(org.meveo.model.wf.WFTransition) CEIUtils(org.meveo.model.persistence.CEIUtils) WFActionService(org.meveo.service.wf.WFActionService) WorkflowService(org.meveo.service.wf.WorkflowService) QueryBuilder(org.meveo.commons.utils.QueryBuilder) Files(java.nio.file.Files) DBStorageType(org.meveo.model.persistence.DBStorageType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) GitHelper(org.meveo.service.git.GitHelper) File(java.io.File) Collections(java.util.Collections) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 3 with QueryBuilder

use of org.meveo.commons.utils.QueryBuilder in project meveo by meveo-org.

the class CustomEntityInstanceService method list.

public List<CustomEntityInstance> list(String cetCode, Map<String, Object> values) {
    QueryBuilder qb = new QueryBuilder(getEntityClass(), "cei", null);
    qb.addCriterion("cei.cetCode", "=", cetCode, true);
    final List<CustomEntityInstance> resultList = qb.getTypedQuery(getEntityManager(), CustomEntityInstance.class).getResultList();
    var ownValues = new HashMap<>(values);
    for (var entry : values.entrySet()) {
        if (entry.getValue() instanceof EntityReferenceWrapper) {
            ownValues.remove(entry.getKey());
        }
    }
    if (ownValues != null && !ownValues.isEmpty()) {
        return resultList.stream().filter(customEntityInstance -> filterOnValues(ownValues, customEntityInstance)).collect(Collectors.toList());
    }
    return resultList;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Date(java.util.Date) ELException(org.meveo.elresolver.ELException) NoResultException(javax.persistence.NoResultException) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) Matcher(java.util.regex.Matcher) Map(java.util.Map) Observes(javax.enterprise.event.Observes) BigDecimal(com.ibm.icu.math.BigDecimal) Stateless(javax.ejb.Stateless) ModuleItem(org.meveo.model.ModuleItem) SqlConfiguration(org.meveo.model.sql.SqlConfiguration) WFTransitionService(org.meveo.service.wf.WFTransitionService) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) MeveoModule(org.meveo.model.module.MeveoModule) Collection(java.util.Collection) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Workflow(org.meveo.model.wf.Workflow) WFAction(org.meveo.model.wf.WFAction) MeveoModuleHelper(org.meveo.service.admin.impl.MeveoModuleHelper) List(java.util.List) ModuleUninstall(org.meveo.service.admin.impl.ModuleUninstall) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Pattern(java.util.regex.Pattern) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) HashMap(java.util.HashMap) ModulePostUninstall(org.meveo.model.ModulePostUninstall) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) CustomFieldValues(org.meveo.model.crm.custom.CustomFieldValues) MeveoFileUtils(org.meveo.commons.utils.MeveoFileUtils) BusinessService(org.meveo.service.base.BusinessService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) CollectionUtils(org.apache.commons.collections.CollectionUtils) MeveoValueExpressionWrapper(org.meveo.service.base.MeveoValueExpressionWrapper) WFTransition(org.meveo.model.wf.WFTransition) CEIUtils(org.meveo.model.persistence.CEIUtils) WFActionService(org.meveo.service.wf.WFActionService) WorkflowService(org.meveo.service.wf.WorkflowService) QueryBuilder(org.meveo.commons.utils.QueryBuilder) Files(java.nio.file.Files) DBStorageType(org.meveo.model.persistence.DBStorageType) IOException(java.io.IOException) CustomFieldInstanceService(org.meveo.service.crm.impl.CustomFieldInstanceService) GitHelper(org.meveo.service.git.GitHelper) File(java.io.File) Collections(java.util.Collections) HashMap(java.util.HashMap) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance)

Example 4 with QueryBuilder

use of org.meveo.commons.utils.QueryBuilder in project meveo by meveo-org.

the class CustomEntityInstanceService method findByCode.

@SuppressWarnings("unchecked")
public List<CustomEntityInstance> findByCode(String cetCode, String code) {
    QueryBuilder qb = new QueryBuilder(getEntityClass(), "cei", null);
    qb.addCriterion("cei.cetCode", "=", cetCode, true);
    if (StringUtils.isNotEmpty(code)) {
        qb.like("cei.code", code, QueryBuilder.QueryLikeStyleEnum.MATCH_ANYWHERE, false);
    }
    return qb.getQuery(getEntityManager()).getResultList();
}
Also used : QueryBuilder(org.meveo.commons.utils.QueryBuilder)

Example 5 with QueryBuilder

use of org.meveo.commons.utils.QueryBuilder in project meveo by meveo-org.

the class NativePersistenceService method getQuery.

/**
 * Creates NATIVE query to filter entities according data provided in pagination
 * configuration.
 * <p>
 * Search filters (key = Filter key, value = search pattern or value).
 * <p>
 * Filter key can be:
 * <ul>
 * <li>SQL. Additional sql to apply. Value is either a sql query or an array
 * consisting of sql query and one or more parameters to apply</li>
 * <li>&lt;condition&gt; &lt;fieldname1&gt; &lt;fieldname2&gt; ...
 * &lt;fieldnameN&gt;. Value is a value to apply in condition</li>
 * </ul>
 * <p>
 * A union between different filter items is AND.
 * <p>
 * <p>
 * Condition is optional. Number of fieldnames depend on condition used. If no
 * condition is specified an "equals ignoring case" operation is considered.
 * <p>
 * <p>
 * Following conditions are supported:
 * <ul>
 * <li>fromRange. Ranged search - field value in between from - to values.
 * Specifies "from" part value: e.g value&lt;=fiel.value. Applies to date and
 * number type fields.</li>
 * <li>toRange. Ranged search - field value in between from - to values.
 * Specifies "to" part value: e.g field.value&lt;=value</li>
 * <li>list. Value is in field's list value. Applies to date and number type
 * fields.</li>
 * <li>inList/not-inList. Field value is [not] in value (list). A comma
 * separated string will be parsed into a list if values. A single value will be
 * considered as a list value of one item</li>
 * <li>minmaxRange. The value is in between two field values. TWO field names
 * must be provided. Applies to date and number type fields.</li>
 * <li>minmaxOptionalRange. Similar to minmaxRange. The value is in between two
 * field values with either them being optional. TWO fieldnames must be
 * specified.</li>
 * <li>overlapOptionalRange. The value range is overlapping two field values
 * with either them being optional. TWO fieldnames must be specified. Value must
 * be an array of two values.</li>
 * <li>likeCriterias. Multiple fieldnames can be specified. Any of the multiple
 * field values match the value (OR criteria). In case value contains *, a like
 * criteria match will be used. In either case case insensative matching is
 * used. Applies to String type fields.</li>
 * <li>wildcardOr. Similar to likeCriterias. A wildcard match will always used.
 * A * will be appended to start and end of the value automatically if not
 * present. Applies to
 * <li>wildcardOrIgnoreCase. Similar to wildcardOr but ignoring case String type
 * fields.</li>
 * <li>ne. Not equal.
 * </ul>
 * <p>
 * Following special meaning values are supported:
 * <ul>
 * <li>IS_NULL. Field value is null</li>
 * <li>IS_NOT_NULL. Field value is not null</li>
 * </ul>
 * <p>
 * <p>
 * <p>
 * To filter by a related entity's field you can either filter by related
 * entity's field or by related entity itself specifying code as value. These
 * two example will do the same in case when quering a customer account:
 * customer.code=aaa OR customer=aaa
 * <p>
 * To filter a list of related entities by a list of entity codes use "inList"
 * on related entity field. e.g. for quering offer template by sellers: inList
 * sellers=code1,code2
 *
 * <b>Note:</b> Quering by related entity field directly will result in
 * exception when entity with a specified code does not exists
 * <p>
 * <p>
 * Examples:
 * <ul>
 * <li>invoice number equals "1578AU": Filter key: invoiceNumber. Filter value:
 * 1578AU</li>
 * <li>invoice number is not "1578AU": Filter key: ne invoiceNumber. Filter
 * value: 1578AU</li>
 * <li>invoice number is null: Filter key: invoiceNumber. Filter value:
 * IS_NULL</li>
 * <li>invoice number is not empty: Filter key: invoiceNumber. Filter value:
 * IS_NOT_NULL</li>
 * <li>Invoice date is between 2017-05-01 and 2017-06-01: Filter key: fromRange
 * invoiceDate. Filter value: 2017-05-01 Filter key: toRange invoiceDate. Filter
 * value: 2017-06-01</li>
 * <li>Date is between creation and update dates: Filter key: minmaxRange
 * audit.created audit.updated. Filter value: 2017-05-25</li>
 * <li>invoice number is any of 158AU, 159KU or 189LL: Filter key: inList
 * invoiceNumber. Filter value: 158AU,159KU,189LL</li>
 * <li>any of param1, param2 or param3 fields contains "energy": Filter key:
 * wildcardOr param1 param2 param3. Filter value: energy</li>
 * <li>any of param1, param2 or param3 fields start with "energy": Filter key:
 * likeCriterias param1 param2 param3. Filter value: *energy</li>
 * <li>any of param1, param2 or param3 fields is "energy": Filter key:
 * likeCriterias param1 param2 param3. Filter value: energy</li>
 * </ul>
 *
 * @param tableName A name of a table to query
 * @param config    Data filtering, sorting and pagination criteria
 * @return Query builder to filter entities according to pagination
 *         configuration data.
 */
@SuppressWarnings({ "rawtypes" })
public QueryBuilder getQuery(String tableName, PaginationConfiguration config) {
    String startQuery;
    tableName = PostgresReserverdKeywords.escapeAndFormat(tableName);
    String superType = config != null ? config.getSuperType() : null;
    if (superType != null) {
        superType = PostgresReserverdKeywords.escapeAndFormat(superType);
    }
    // If no fetch fields are defined, return everything
    if (config == null || config.getFetchFields() == null) {
        if (superType != null) {
            startQuery = "select a.*";
            // Declaratively fetch super-type fields
            if (config.getSuperTypeFields() != null) {
                for (var superTypeField : config.getSuperTypeFields()) {
                    String fieldName = PostgresReserverdKeywords.escapeAndFormat(superTypeField);
                    startQuery += ", b." + fieldName + " ";
                }
            } else {
                startQuery += ", b.*";
            }
            startQuery += "from {h-schema}" + tableName + " a ";
            startQuery += " INNER JOIN " + superType + " b ON a.uuid = b.uuid";
        } else {
            startQuery = "select * from {h-schema}" + tableName + " a ";
        }
    } else if (config.getFetchFields().isEmpty()) {
        // If fetch fields are empty, only return UUID
        startQuery = "select a.uuid from {h-schema}" + tableName + " a ";
        if (superType != null) {
            startQuery += " INNER JOIN " + superType + " b ON a.uuid = b.uuid";
        }
    } else {
        // Always return UUID
        StringBuilder builder = new StringBuilder("select a.uuid, ");
        config.getFetchFields().forEach(s -> {
            String fieldName = PostgresReserverdKeywords.escapeAndFormat(s);
            builder.append(fieldName).append(", ");
        });
        builder.delete(builder.length() - 2, builder.length());
        startQuery = builder.append(" from {h-schema}").append(tableName).append(" a ").toString();
        if (superType != null) {
            startQuery += " INNER JOIN " + superType + " b ON a.uuid = b.uuid";
        }
    }
    QueryBuilder queryBuilder = new QueryBuilder(startQuery, null);
    if (config == null) {
        return queryBuilder;
    }
    Map<String, Object> filters = config.getFilters();
    if (filters != null && !filters.isEmpty()) {
        for (String key : filters.keySet()) {
            Object filterValue = filters.get(key);
            if (filterValue == null) {
                continue;
            }
            // Key format is: condition field1 field2 or condition-field1-field2-fieldN
            // example: "ne code", condition=code, fieldName=code, fieldName2=null
            String[] fieldInfo = key.split(" ");
            String condition = fieldInfo.length == 1 ? null : fieldInfo[0];
            String fieldName = fieldInfo.length == 1 ? fieldInfo[0] : fieldInfo[1];
            String fieldName2 = fieldInfo.length == 3 ? fieldInfo[2] : null;
            String[] fields = null;
            if (condition != null) {
                fields = Arrays.copyOfRange(fieldInfo, 1, fieldInfo.length);
            }
            if (config.getSuperTypeFields() != null && config.getSuperTypeFields().contains(fieldName)) {
                fieldName = "b." + fieldName;
            } else {
                fieldName = "a." + fieldName;
            }
            // value: e.g value<=field.value
            if ("fromRange".equals(condition)) {
                if (filterValue instanceof Double) {
                    BigDecimal rationalNumber = new BigDecimal((Double) filterValue);
                    queryBuilder.addCriterion(fieldName, " >= ", rationalNumber, false);
                } else if (filterValue instanceof Number) {
                    queryBuilder.addCriterion(fieldName, " >= ", filterValue, false);
                } else if (filterValue instanceof Date) {
                    queryBuilder.addCriterionDateRangeFromTruncatedToDay(fieldName, ((Date) filterValue).toInstant());
                } else if (filterValue instanceof Instant) {
                    queryBuilder.addCriterionDateRangeFromTruncatedToDay(fieldName, (Instant) filterValue);
                }
            // if ranged search - field value in between from - to values. Specifies "to"
            // value: e.g field.value<=value
            } else if ("toRange".equals(condition)) {
                if (filterValue instanceof Double) {
                    BigDecimal rationalNumber = new BigDecimal((Double) filterValue);
                    queryBuilder.addCriterion(fieldName, " <= ", rationalNumber, false);
                } else if (filterValue instanceof Number) {
                    queryBuilder.addCriterion(fieldName, " <= ", filterValue, false);
                } else if (filterValue instanceof Date) {
                    queryBuilder.addCriterionDateRangeToTruncatedToDay(fieldName, ((Date) filterValue).toInstant());
                } else if (filterValue instanceof Instant) {
                    queryBuilder.addCriterionDateRangeToTruncatedToDay(fieldName, (Instant) filterValue);
                }
            // Value is in field value (list)
            } else if ("list".equals(condition)) {
                String paramName = queryBuilder.convertFieldToParam(fieldName);
                queryBuilder.addSqlCriterion(":" + paramName + " in elements(" + fieldName + ")", paramName, filterValue);
            // Field value is in value (list)
            } else if ("inList".equals(condition) || "not-inList".equals(condition)) {
                boolean isNot = "not-inList".equals(condition);
                if (filterValue instanceof String) {
                    queryBuilder.addSql(fieldName + (isNot ? " NOT " : "") + " IN (" + filterValue + ")");
                } else if (filterValue instanceof Collection) {
                    String paramName = queryBuilder.convertFieldToParam(fieldName);
                    queryBuilder.addSqlCriterion(fieldName + (isNot ? " NOT " : "") + " IN (:" + paramName + ")", paramName, filterValue);
                }
            // The value is in between two field values
            } else if ("minmaxRange".equals(condition)) {
                if (filterValue instanceof Double) {
                    BigDecimal rationalNumber = new BigDecimal((Double) filterValue);
                    queryBuilder.addCriterion(fieldName, " <= ", rationalNumber, false);
                    queryBuilder.addCriterion(fieldName2, " >= ", rationalNumber, false);
                } else if (filterValue instanceof Number) {
                    queryBuilder.addCriterion(fieldName, " <= ", filterValue, false);
                    queryBuilder.addCriterion(fieldName2, " >= ", filterValue, false);
                }
                if ((filterValue instanceof Date) || (filterValue instanceof Instant)) {
                    Date value = (filterValue instanceof Date) ? (Date) filterValue : (Date.from((Instant) filterValue));
                    Calendar c = Calendar.getInstance();
                    c.setTime(value);
                    int year = c.get(Calendar.YEAR);
                    int month = c.get(Calendar.MONTH);
                    int date = c.get(Calendar.DATE);
                    c.set(year, month, date, 0, 0, 0);
                    c.set(Calendar.MILLISECOND, 0);
                    value = c.getTime();
                    queryBuilder.addCriterion(fieldName, "<=", value, false);
                    queryBuilder.addCriterion(fieldName2, ">=", value, false);
                }
            // The value is in between two field values with either them being optional
            } else if ("minmaxOptionalRange".equals(condition)) {
                String paramName = queryBuilder.convertFieldToParam(fieldName);
                String sql = "((" + fieldName + " IS NULL and a." + fieldName2 + " IS NULL) or (" + fieldName + "<=:" + paramName + " and :" + paramName + "<a." + fieldName2 + ") or (" + fieldName + "<=:" + paramName + " and a." + fieldName2 + " IS NULL) or (a." + fieldName + " IS NULL and :" + paramName + "<a." + fieldName2 + "))";
                queryBuilder.addSqlCriterionMultiple(sql, paramName, filterValue);
            // The value range is overlapping two field values with either them being
            // optional
            } else if ("overlapOptionalRange".equals(condition)) {
                String paramNameFrom = queryBuilder.convertFieldToParam(fieldName);
                String paramNameTo = queryBuilder.convertFieldToParam(fieldName2);
                String sql = "((" + fieldName + " IS NULL and a." + fieldName2 + " IS NULL) or  (" + fieldName + " IS NULL and a." + fieldName2 + ">:" + paramNameFrom + ") or (a." + fieldName2 + " IS NULL and " + fieldName + "<:" + paramNameTo + ") or (" + fieldName + " IS NOT NULL and a." + fieldName2 + " IS NOT NULL and ((" + fieldName + "<=:" + paramNameFrom + " and :" + paramNameFrom + "<a." + fieldName2 + ") or (:" + paramNameFrom + "<=a." + fieldName + " and " + fieldName + "<:" + paramNameTo + "))))";
                if (filterValue.getClass().isArray()) {
                    queryBuilder.addSqlCriterionMultiple(sql, paramNameFrom, ((Object[]) filterValue)[0], paramNameTo, ((Object[]) filterValue)[1]);
                } else if (filterValue instanceof List) {
                    queryBuilder.addSqlCriterionMultiple(sql, paramNameFrom, ((List) filterValue).get(0), paramNameTo, ((List) filterValue).get(1));
                }
            // Any of the multiple field values wildcard or not wildcard match the value (OR
            // criteria)
            } else if ("likeCriterias".equals(condition)) {
                queryBuilder.startOrClause();
                if (filterValue instanceof String) {
                    String filterString = (String) filterValue;
                    for (String field : fields) {
                        queryBuilder.addCriterionWildcard(field, filterString, false);
                    }
                }
                queryBuilder.endOrClause();
            // Any of the multiple field values wildcard match the value (OR criteria) - a
            // diference from "likeCriterias" is that wildcard will be appended to the value
            // automatically
            } else if (PersistenceService.SEARCH_WILDCARD_OR.equals(condition)) {
                queryBuilder.startOrClause();
                for (String field : fields) {
                    queryBuilder.addSql(field + " like '%" + filterValue + "%'");
                }
                queryBuilder.endOrClause();
            // Just like wildcardOr but ignoring case :
            } else if (PersistenceService.SEARCH_WILDCARD_OR_IGNORE_CAS.equals(condition)) {
                queryBuilder.startOrClause();
                for (String field : fields) {
                    // since SEARCH_WILDCARD_OR_IGNORE_CAS , then filterValue is necessary a String
                    // lowercase functions may give different results in postgres/java => to avoid mismatch, postrges's function is the only one used. Example of mismath : Danışmanlık_İth
                    queryBuilder.addSqlCriterion("lower(a." + field + ") like concat('%', lower(:" + field + "), '%')", field, filterValue);
                }
                queryBuilder.endOrClause();
            // Search by additional Sql clause with specified parameters
            } else if (PersistenceService.SEARCH_SQL.equals(condition)) {
                if (filterValue.getClass().isArray()) {
                    String additionalSql = (String) ((Object[]) filterValue)[0];
                    Object[] additionalParameters = Arrays.copyOfRange(((Object[]) filterValue), 1, ((Object[]) filterValue).length);
                    queryBuilder.addSqlCriterionMultiple(additionalSql, additionalParameters);
                } else {
                    queryBuilder.addSql((String) filterValue);
                }
            } else {
                if (filterValue instanceof String && PersistenceService.SEARCH_IS_NULL.equals(filterValue)) {
                    queryBuilder.addSql(fieldName + " is null ");
                } else if (filterValue instanceof String && PersistenceService.SEARCH_IS_NOT_NULL.equals(filterValue)) {
                    queryBuilder.addSql(fieldName + " is not null ");
                } else if (filterValue instanceof String) {
                    // if contains dot, that means join is needed
                    String filterString = (String) filterValue;
                    boolean wildcard = (filterString.indexOf("*") != -1);
                    if (wildcard) {
                        queryBuilder.addCriterionWildcard(fieldName, filterString, false, "ne".equals(condition));
                    } else {
                        queryBuilder.addCriterion(fieldName, "ne".equals(condition) ? " != " : " = ", filterString, false);
                    }
                } else if (filterValue instanceof Date) {
                    queryBuilder.addCriterionDateTruncatedToDay(fieldName, ((Date) filterValue).toInstant());
                } else if (filterValue instanceof Instant) {
                    queryBuilder.addCriterionDateTruncatedToDay(fieldName, (Instant) filterValue);
                } else if (filterValue instanceof Number || filterValue instanceof Boolean) {
                    queryBuilder.addCriterion(fieldName, "ne".equals(condition) ? " != " : " = ", filterValue, false);
                } else if (filterValue instanceof Enum) {
                    if (filterValue instanceof IdentifiableEnum) {
                        String enumIdKey = new StringBuilder(fieldName).append("Id").toString();
                        queryBuilder.addCriterion(enumIdKey, "ne".equals(condition) ? " != " : " = ", ((IdentifiableEnum) filterValue).getId(), false);
                    } else {
                        queryBuilder.addCriterionEnum(fieldName, (Enum) filterValue, "ne".equals(condition) ? " != " : " = ");
                    }
                } else if (filterValue instanceof List) {
                    queryBuilder.addSqlCriterion(fieldName + ("ne".equals(condition) ? " not in  " : " in ") + ":" + fieldName, fieldName, filterValue);
                }
            }
        }
    }
    queryBuilder.addPaginationConfiguration(config, "a");
    // FIXME: Will only works for Postgres and few others ...
    if (config.isRandomize()) {
        queryBuilder.getSqlStringBuffer().append("ORDER BY RANDOM() ");
    }
    return queryBuilder;
}
Also used : Arrays(java.util.Arrays) CrossStorageTransaction(org.meveo.persistence.CrossStorageTransaction) Connection(java.sql.Connection) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) SQLQuery(org.hibernate.SQLQuery) IdentifiableEnum(org.meveo.model.IdentifiableEnum) StringUtils(org.meveo.commons.utils.StringUtils) ReflectionUtils(org.meveo.commons.utils.ReflectionUtils) BigDecimal(java.math.BigDecimal) NonUniqueResultException(javax.persistence.NonUniqueResultException) TransactionAttributeType(javax.ejb.TransactionAttributeType) SQLStorageConfiguration(org.meveo.model.persistence.sql.SQLStorageConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MeveoJpa(org.meveo.jpa.MeveoJpa) Map(java.util.Map) NotImplementedException(org.apache.commons.lang.NotImplementedException) BigInteger(java.math.BigInteger) MeveoParamBean(org.meveo.util.MeveoParamBean) SqlConfiguration(org.meveo.model.sql.SqlConfiguration) TxType(javax.transaction.Transactional.TxType) ValidationException(org.meveo.admin.exception.ValidationException) Transactional(javax.transaction.Transactional) CustomEntityInstance(org.meveo.model.customEntities.CustomEntityInstance) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) Objects(java.util.Objects) List(java.util.List) Query(javax.persistence.Query) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) PersistenceException(javax.persistence.PersistenceException) LocalDate(java.time.LocalDate) Entry(java.util.Map.Entry) CustomFieldsCacheContainerProvider(org.meveo.cache.CustomFieldsCacheContainerProvider) Optional(java.util.Optional) Types(java.sql.Types) PaginationConfiguration(org.meveo.admin.util.pagination.PaginationConfiguration) CustomTableService(org.meveo.service.custom.CustomTableService) EntityReferenceWrapper(org.meveo.model.crm.EntityReferenceWrapper) HibernateUtils(org.hibernate.util.HibernateUtils) CustomEntityTemplateService(org.meveo.service.custom.CustomEntityTemplateService) LocalDateTime(java.time.LocalDateTime) Session(org.hibernate.Session) HashMap(java.util.HashMap) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) Updated(org.meveo.event.qualifier.Updated) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) JacksonUtil(org.meveo.model.persistence.JacksonUtil) SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Removed(org.meveo.event.qualifier.Removed) CustomFieldTemplateService(org.meveo.service.crm.impl.CustomFieldTemplateService) TransactionAttribute(javax.ejb.TransactionAttribute) NativeQuery(org.hibernate.query.NativeQuery) ParamBean(org.meveo.commons.utils.ParamBean) LinkedList(java.util.LinkedList) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) Event(javax.enterprise.event.Event) QueryBuilder(org.meveo.commons.utils.QueryBuilder) CustomTableRecord(org.meveo.model.customEntities.CustomTableRecord) DBStorageType(org.meveo.model.persistence.DBStorageType) EntityManager(javax.persistence.EntityManager) File(java.io.File) EntityManagerWrapper(org.meveo.jpa.EntityManagerWrapper) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) PostgresReserverdKeywords(org.meveo.service.custom.PostgresReserverdKeywords) Statement(java.sql.Statement) AliasToEntityOrderedMapResultTransformer(org.meveo.model.transformer.AliasToEntityOrderedMapResultTransformer) DateUtils(org.meveo.model.shared.DateUtils) SQLConnectionProvider(org.meveo.persistence.sql.SQLConnectionProvider) Collections(java.util.Collections) SqlConfigurationService(org.meveo.persistence.sql.SqlConfigurationService) IdentifiableEnum(org.meveo.model.IdentifiableEnum) CustomFieldStorageTypeEnum(org.meveo.model.crm.custom.CustomFieldStorageTypeEnum) CustomFieldTypeEnum(org.meveo.model.crm.custom.CustomFieldTypeEnum) Instant(java.time.Instant) Calendar(java.util.Calendar) QueryBuilder(org.meveo.commons.utils.QueryBuilder) BigDecimal(java.math.BigDecimal) Date(java.util.Date) LocalDate(java.time.LocalDate) Collection(java.util.Collection) CustomModelObject(org.meveo.model.customEntities.CustomModelObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IdentifiableEnum(org.meveo.model.IdentifiableEnum)

Aggregations

QueryBuilder (org.meveo.commons.utils.QueryBuilder)72 NoResultException (javax.persistence.NoResultException)31 ArrayList (java.util.ArrayList)12 List (java.util.List)12 Query (javax.persistence.Query)12 FilteredQueryBuilder (org.meveo.commons.utils.FilteredQueryBuilder)9 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Collection (java.util.Collection)6 NonUniqueResultException (javax.persistence.NonUniqueResultException)6 BusinessException (org.meveo.admin.exception.BusinessException)6 CustomEntityInstance (org.meveo.model.customEntities.CustomEntityInstance)6 File (java.io.File)5 Date (java.util.Date)5 SQLQuery (org.hibernate.SQLQuery)5 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)5 DBStorageType (org.meveo.model.persistence.DBStorageType)5 BigInteger (java.math.BigInteger)4 Instant (java.time.Instant)4 Collections (java.util.Collections)4