Program: 59411
Project: 59411
Buggy source:
/** 
 * Execute a select query on entityClass using <a href="http://www.querydsl.com/">Querydsl</a> which enables the construction of type-safe SQL-like queries.
 * @param entityClass entity to use in search
 * @param filterByAssociations (optional) for each related entity to joincontain as key the name of the association and as value the List of related entity fields to filter by
 * @param orderByAssociations (optional) for each related entity to ordercontain as key the name of the association and as value the List of related entity fields to order by
 * @param entityManager {@code entityClass} {@link EntityManager}
 * @param datatablesCriterias datatables parameters for query
 * @param baseSearchValuesMap (optional) base filter values
 * @param distinct use distinct query
 * @return
 */
@SuppressWarnings("String_Node_Str") public static <T,E extends Comparable<?>>SearchResults<T> findByCriteria(Class<T> entityClass,Map<String,List<String>> filterByAssociations,Map<String,List<String>> orderByAssociations,EntityManager entityManager,DatatablesCriterias datatablesCriterias,Map<String,Object> baseSearchValuesMap,boolean distinct) throws IllegalArgumentException {
  Assert.notNull(entityClass);
  Assert.notNull(entityManager);
  Assert.notNull(datatablesCriterias);
  if (CollectionUtils.isEmpty(filterByAssociations)) {
    filterByAssociations=new HashMap<String,List<String>>();
  }
  if (CollectionUtils.isEmpty(orderByAssociations)) {
    orderByAssociations=new HashMap<String,List<String>>();
  }
  boolean isPaged=datatablesCriterias.getDisplaySize() != null && datatablesCriterias.getDisplaySize() > 0;
  boolean findInAllColumns=StringUtils.isNotEmpty(datatablesCriterias.getSearch()) && datatablesCriterias.hasOneFilterableColumn();
  PathBuilder<T> entity=new PathBuilder<T>(entityClass,"String_Node_Str");
  JPAQuery query=new JPAQuery(entityManager);
  query=query.from(entity);
  JPAQuery baseQuery=new JPAQuery(entityManager);
  baseQuery=baseQuery.from(entity);
  Map<String,PathBuilder<?>> associationMap=new HashMap<String,PathBuilder<?>>();
  for (  ColumnDef column : datatablesCriterias.getColumnDefs()) {
    boolean findInColumn=StringUtils.isNotEmpty(column.getSearch());
    String associationName=column.getName();
    if (!filterByAssociations.containsKey(associationName)) {
      continue;
    }
    if (!column.isSortable() && !column.isFilterable()) {
      continue;
    }
    if (!column.isSortable() && !findInColumn && !findInAllColumns) {
      continue;
    }
    PathBuilder<?> associationPath=entity.get(associationName);
    query=query.join(associationPath);
    associationMap.put(associationName,associationPath);
  }
  BooleanBuilder filtersByColumnPredicate=new BooleanBuilder();
  BooleanBuilder filtersByTablePredicate=new BooleanBuilder();
  try {
    if (datatablesCriterias.hasOneFilteredColumn()) {
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        String searchStr=column.getSearch();
        boolean findInColumn=column.isFilterable() && StringUtils.isNotEmpty(searchStr);
        if (findInColumn) {
          String fieldName=column.getName();
          Class<?> fieldType=BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
          filtersByColumnPredicate=filtersByColumnPredicate.and(QuerydslUtils.createExpression(entity,fieldName,fieldType,searchStr));
          List<String> attributes=filterByAssociations.get(fieldName);
          if (attributes != null && attributes.size() > 0) {
            BooleanBuilder filtersByAssociationPredicate=new BooleanBuilder();
            PathBuilder<?> associationPath=associationMap.get(fieldName);
            List<String> associationFields=filterByAssociations.get(fieldName);
            for (            String associationFieldName : associationFields) {
              Class<?> associationFieldType=BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
              filtersByAssociationPredicate=filtersByAssociationPredicate.or(QuerydslUtils.createExpression(associationPath,associationFieldName,associationFieldType,searchStr));
            }
            filtersByColumnPredicate=filtersByColumnPredicate.and(filtersByAssociationPredicate.getValue());
          }
        }
      }
    }
    String searchStr=datatablesCriterias.getSearch();
    if (findInAllColumns) {
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable()) {
          String fieldName=column.getName();
          Class<?> fieldType=BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
          filtersByTablePredicate=filtersByTablePredicate.or(QuerydslUtils.createExpression(entity,fieldName,fieldType,searchStr));
          List<String> attributes=filterByAssociations.get(fieldName);
          if (attributes != null && attributes.size() > 0) {
            PathBuilder<?> associationPath=associationMap.get(fieldName);
            List<String> associationFields=filterByAssociations.get(fieldName);
            for (            String associationFieldName : associationFields) {
              Class<?> associationFieldType=BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
              filtersByTablePredicate=filtersByTablePredicate.or(QuerydslUtils.createExpression(associationPath,associationFieldName,associationFieldType,searchStr));
            }
          }
        }
      }
    }
  }
 catch (  Exception e) {
    SearchResults<T> searchResults=new SearchResults<T>(new ArrayList<T>(0),0,isPaged,new Long(datatablesCriterias.getDisplayStart()),new Long(datatablesCriterias.getDisplaySize()),0);
    return searchResults;
  }
  List<OrderSpecifier<?>> orderSpecifiersList=new ArrayList<OrderSpecifier<?>>();
  if (datatablesCriterias.hasOneSortedColumn()) {
    for (    ColumnDef column : datatablesCriterias.getSortingColumnDefs()) {
      if (!column.isSortable()) {
        continue;
      }
      if (column.getSortDirection() == null) {
        continue;
      }
      Order order=Order.DESC;
      if (column.getSortDirection() == SortDirection.ASC) {
        order=Order.ASC;
      }
      String fieldName=column.getName();
      Class<E> fieldType=(Class<E>)BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
      List<String> attributes=orderByAssociations.get(fieldName);
      try {
        if (attributes != null && attributes.size() > 0) {
          PathBuilder<?> associationPath=associationMap.get(fieldName);
          List<String> associationFields=orderByAssociations.get(fieldName);
          for (          String associationFieldName : associationFields) {
            Class<E> associationFieldType=(Class<E>)BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
            orderSpecifiersList.add(QuerydslUtils.createOrderSpecifier(associationPath,associationFieldName,associationFieldType,order));
          }
        }
 else {
          orderSpecifiersList.add(QuerydslUtils.createOrderSpecifier(entity,fieldName,fieldType,order));
        }
      }
 catch (      Exception ex) {
      }
    }
  }
  Long offset=null;
  Long limit=null;
  if (isPaged) {
    limit=new Long(datatablesCriterias.getDisplaySize());
  }
  if (datatablesCriterias.getDisplayStart() >= 0) {
    offset=new Long(datatablesCriterias.getDisplayStart());
  }
  QueryModifiers queryModifiers=new QueryModifiers(limit,offset);
  List<T> elements=null;
  if (distinct) {
    query=query.distinct();
  }
  BooleanBuilder basePredicate=QuerydslUtils.createPredicateByAnd(entityClass,baseSearchValuesMap);
  baseQuery.where(basePredicate);
  query=query.where(basePredicate.and(filtersByColumnPredicate.getValue()).and(filtersByTablePredicate.getValue()));
  elements=query.orderBy(orderSpecifiersList.toArray(new OrderSpecifier[orderSpecifiersList.size()])).restrict(queryModifiers).list(entity);
  long totalResultCount;
  if (isPaged) {
    totalResultCount=query.count();
  }
 else {
    totalResultCount=elements.size();
  }
  long totalBaseCount=baseQuery.count();
  SearchResults<T> searchResults=new SearchResults<T>(elements,totalResultCount,isPaged,offset,limit,totalBaseCount);
  return searchResults;
}

Fixed version (ground truth):
/** 
 * Execute a select query on entityClass using <a href="http://www.querydsl.com/">Querydsl</a> which enables the construction of type-safe SQL-like queries.
 * @param entityClass entity to use in search
 * @param filterByAssociations (optional) for each related entity to joincontain as key the name of the association and as value the List of related entity fields to filter by
 * @param orderByAssociations (optional) for each related entity to ordercontain as key the name of the association and as value the List of related entity fields to order by
 * @param entityManager {@code entityClass} {@link EntityManager}
 * @param datatablesCriterias datatables parameters for query
 * @param baseSearchValuesMap (optional) base filter values
 * @param distinct use distinct query
 * @return
 */
@SuppressWarnings("String_Node_Str") public static <T,E extends Comparable<?>>SearchResults<T> findByCriteria(Class<T> entityClass,Map<String,List<String>> filterByAssociations,Map<String,List<String>> orderByAssociations,EntityManager entityManager,DatatablesCriterias datatablesCriterias,Map<String,Object> baseSearchValuesMap,boolean distinct) throws IllegalArgumentException {
  Assert.notNull(entityClass);
  Assert.notNull(entityManager);
  Assert.notNull(datatablesCriterias);
  if (CollectionUtils.isEmpty(filterByAssociations)) {
    filterByAssociations=new HashMap<String,List<String>>();
  }
  if (CollectionUtils.isEmpty(orderByAssociations)) {
    orderByAssociations=new HashMap<String,List<String>>();
  }
  boolean isPaged=datatablesCriterias.getDisplaySize() != null && datatablesCriterias.getDisplaySize() > 0;
  boolean findInAllColumns=StringUtils.isNotEmpty(datatablesCriterias.getSearch()) && datatablesCriterias.hasOneFilterableColumn();
  PathBuilder<T> entity=new PathBuilder<T>(entityClass,"String_Node_Str");
  JPAQuery query=new JPAQuery(entityManager);
  query=query.from(entity);
  JPAQuery baseQuery=new JPAQuery(entityManager);
  baseQuery=baseQuery.from(entity);
  Map<String,PathBuilder<?>> associationMap=new HashMap<String,PathBuilder<?>>();
  for (  ColumnDef column : datatablesCriterias.getColumnDefs()) {
    boolean findInColumn=StringUtils.isNotEmpty(column.getSearch());
    String associationName=column.getName();
    if (!filterByAssociations.containsKey(associationName)) {
      continue;
    }
    if (!column.isSortable() && !column.isFilterable()) {
      continue;
    }
    if (!column.isSortable() && !findInColumn && !findInAllColumns) {
      continue;
    }
    PathBuilder<?> associationPath=entity.get(associationName);
    query=query.join(associationPath);
    associationMap.put(associationName,associationPath);
  }
  BooleanBuilder filtersByColumnPredicate=new BooleanBuilder();
  BooleanBuilder filtersByTablePredicate=new BooleanBuilder();
  try {
    if (datatablesCriterias.hasOneFilteredColumn()) {
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        String searchStr=column.getSearch();
        boolean findInColumn=column.isFilterable() && StringUtils.isNotEmpty(searchStr);
        if (findInColumn) {
          String fieldName=column.getName();
          Class<?> fieldType=BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
          filtersByColumnPredicate=filtersByColumnPredicate.and(QuerydslUtils.createExpression(entity,fieldName,fieldType,searchStr));
          List<String> attributes=filterByAssociations.get(fieldName);
          if (attributes != null && attributes.size() > 0) {
            BooleanBuilder filtersByAssociationPredicate=new BooleanBuilder();
            PathBuilder<?> associationPath=associationMap.get(fieldName);
            List<String> associationFields=filterByAssociations.get(fieldName);
            for (            String associationFieldName : associationFields) {
              Class<?> associationFieldType=BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
              filtersByAssociationPredicate=filtersByAssociationPredicate.or(QuerydslUtils.createExpression(associationPath,associationFieldName,associationFieldType,searchStr));
            }
            filtersByColumnPredicate=filtersByColumnPredicate.and(filtersByAssociationPredicate.getValue());
          }
        }
      }
    }
    String searchStr=datatablesCriterias.getSearch();
    if (findInAllColumns) {
      for (      ColumnDef column : datatablesCriterias.getColumnDefs()) {
        if (column.isFilterable()) {
          String fieldName=column.getName();
          Class<?> fieldType=BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
          filtersByTablePredicate=filtersByTablePredicate.or(QuerydslUtils.createExpression(entity,fieldName,fieldType,searchStr));
          List<String> attributes=filterByAssociations.get(fieldName);
          if (attributes != null && attributes.size() > 0) {
            PathBuilder<?> associationPath=associationMap.get(fieldName);
            List<String> associationFields=filterByAssociations.get(fieldName);
            for (            String associationFieldName : associationFields) {
              Class<?> associationFieldType=BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
              filtersByTablePredicate=filtersByTablePredicate.or(QuerydslUtils.createExpression(associationPath,associationFieldName,associationFieldType,searchStr));
            }
          }
        }
      }
    }
  }
 catch (  Exception e) {
    SearchResults<T> searchResults=new SearchResults<T>(new ArrayList<T>(0),0,isPaged,new Long(datatablesCriterias.getDisplayStart()),new Long(datatablesCriterias.getDisplaySize()),0);
    return searchResults;
  }
  List<OrderSpecifier<?>> orderSpecifiersList=new ArrayList<OrderSpecifier<?>>();
  if (datatablesCriterias.hasOneSortedColumn()) {
    for (    ColumnDef column : datatablesCriterias.getSortingColumnDefs()) {
      if (!column.isSortable()) {
        continue;
      }
      if (column.getSortDirection() == null) {
        continue;
      }
      Order order=Order.DESC;
      if (column.getSortDirection() == SortDirection.ASC) {
        order=Order.ASC;
      }
      String fieldName=column.getName();
      Class<E> fieldType=(Class<E>)BeanUtils.findPropertyType(fieldName,ArrayUtils.<Class<?>>toArray(entityClass));
      List<String> attributes=orderByAssociations.get(fieldName);
      try {
        if (attributes != null && attributes.size() > 0) {
          PathBuilder<?> associationPath=associationMap.get(fieldName);
          List<String> associationFields=orderByAssociations.get(fieldName);
          for (          String associationFieldName : associationFields) {
            Class<E> associationFieldType=(Class<E>)BeanUtils.findPropertyType(associationFieldName,ArrayUtils.<Class<?>>toArray(fieldType));
            orderSpecifiersList.add(QuerydslUtils.createOrderSpecifier(associationPath,associationFieldName,associationFieldType,order));
          }
        }
 else {
          orderSpecifiersList.add(QuerydslUtils.createOrderSpecifier(entity,fieldName,fieldType,order));
        }
      }
 catch (      Exception ex) {
      }
    }
  }
  Long offset=null;
  Long limit=null;
  if (isPaged) {
    limit=new Long(datatablesCriterias.getDisplaySize());
  }
  if (datatablesCriterias.getDisplayStart() >= 0) {
    offset=new Long(datatablesCriterias.getDisplayStart());
  }
  QueryModifiers queryModifiers=new QueryModifiers(limit,offset);
  List<T> elements=null;
  if (distinct) {
    query=query.distinct();
  }
  BooleanBuilder basePredicate=QuerydslUtils.createPredicateByAnd(entity,baseSearchValuesMap);
  baseQuery.where(basePredicate);
  query=query.where(basePredicate.and(filtersByColumnPredicate.getValue()).and(filtersByTablePredicate.getValue()));
  elements=query.orderBy(orderSpecifiersList.toArray(new OrderSpecifier[orderSpecifiersList.size()])).restrict(queryModifiers).list(entity);
  long totalResultCount;
  if (isPaged) {
    totalResultCount=query.count();
  }
 else {
    totalResultCount=elements.size();
  }
  long totalBaseCount=baseQuery.count();
  SearchResults<T> searchResults=new SearchResults<T>(elements,totalResultCount,isPaged,offset,limit,totalBaseCount);
  return searchResults;
}