Program: 35343
Project: 35343
Buggy source:
@Override public List<Contact> findContactsByNameOrEmail(Integer organizationId,String search,boolean withDeleted,boolean onlyWithoutUser,ContactModelType allowedType,Set<Integer> allowedModelIds,Set<Integer> excludedIds,Integer checkboxElementId){
  CriteriaBuilder criteriaBuilder=em().getCriteriaBuilder();
  CriteriaQuery<Contact> criteriaQuery=criteriaBuilder.createQuery(Contact.class);
  Root<Contact> contactRoot=criteriaQuery.from(Contact.class);
  Join<Object,Object> userJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> organizationJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> contactModelJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> contactModelOrganizationJoin=contactModelJoin.join("String_Node_Str",JoinType.LEFT);
  List<Predicate> andPredicates=new ArrayList<>();
  List<Predicate> orPredicates=new ArrayList<>();
  andPredicates.add(criteriaBuilder.equal(contactModelOrganizationJoin.get("String_Node_Str"),organizationId));
  if (search != null && !search.isEmpty()) {
    Predicate searchContactCriteria;
    Predicate searchUserCriteria;
    Predicate searchOrganizationCriteria;
    if (isEmailAddress(search)) {
      searchContactCriteria=criteriaBuilder.equal(criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),search.toUpperCase());
      searchUserCriteria=criteriaBuilder.equal(criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),search.toUpperCase());
    }
 else     if (isNumber(search)) {
      searchContactCriteria=criteriaBuilder.or(criteriaBuilder.equal(contactRoot.get("String_Node_Str").as(String.class),search),criteriaBuilder.like(criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      searchUserCriteria=criteriaBuilder.or(criteriaBuilder.equal(userJoin.get("String_Node_Str").as(String.class),search),criteriaBuilder.like(criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      searchOrganizationCriteria=criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(organizationJoin.get("String_Node_Str")),criteriaBuilder.equal(criteriaBuilder.upper(organizationJoin.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      orPredicates.add(searchOrganizationCriteria);
    }
 else {
      searchContactCriteria=criteriaBuilder.or(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      searchUserCriteria=criteriaBuilder.or(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      searchOrganizationCriteria=criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(organizationJoin.get("String_Node_Str")),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(organizationJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      orPredicates.add(searchOrganizationCriteria);
    }
    orPredicates.add(searchContactCriteria);
    if (!onlyWithoutUser) {
      orPredicates.add(criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(userJoin.get("String_Node_Str")),criteriaBuilder.or(searchUserCriteria)));
    }
  }
  if (!withDeleted) {
    andPredicates.add(criteriaBuilder.and(contactRoot.get("String_Node_Str").isNull()));
  }
  if (onlyWithoutUser) {
    andPredicates.add(userJoin.get("String_Node_Str").isNull());
  }
  if (allowedModelIds != null && !allowedModelIds.isEmpty()) {
    andPredicates.add(criteriaBuilder.in(contactModelJoin.get("String_Node_Str").in(allowedModelIds)));
  }
  if (allowedType != null) {
    andPredicates.add(criteriaBuilder.equal(contactModelJoin.get("String_Node_Str"),allowedType));
  }
  if (excludedIds != null && !excludedIds.isEmpty()) {
    andPredicates.add(criteriaBuilder.not(contactRoot.get("String_Node_Str").in(excludedIds)));
  }
  if (checkboxElementId != null) {
    List<Integer> contactIds=valueDAO.findContainerIdByElementAndValue(checkboxElementId,"String_Node_Str");
    if (!contactIds.isEmpty()) {
      andPredicates.add(contactRoot.get("String_Node_Str").in(contactIds));
    }
 else {
      return Collections.emptyList();
    }
  }
  andPredicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
  criteriaQuery.where(andPredicates.toArray(new Predicate[andPredicates.size()]));
  criteriaQuery.select(contactRoot);
  criteriaQuery.orderBy(criteriaBuilder.asc(contactRoot.get("String_Node_Str")));
  return em().createQuery(criteriaQuery).getResultList();
}

Fixed version (ground truth):
@Override public List<Contact> findContactsByNameOrEmail(Integer organizationId,String search,boolean withDeleted,boolean onlyWithoutUser,ContactModelType allowedType,Set<Integer> allowedModelIds,Set<Integer> excludedIds,Integer checkboxElementId){
  CriteriaBuilder criteriaBuilder=em().getCriteriaBuilder();
  CriteriaQuery<Contact> criteriaQuery=criteriaBuilder.createQuery(Contact.class);
  Root<Contact> contactRoot=criteriaQuery.from(Contact.class);
  Join<Object,Object> userJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> organizationJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> contactModelJoin=contactRoot.join("String_Node_Str",JoinType.LEFT);
  Join<Object,Object> contactModelOrganizationJoin=contactModelJoin.join("String_Node_Str",JoinType.LEFT);
  List<Predicate> andPredicates=new ArrayList<>();
  List<Predicate> orPredicates=new ArrayList<>();
  andPredicates.add(criteriaBuilder.equal(contactModelOrganizationJoin.get("String_Node_Str"),organizationId));
  if (search != null && !search.isEmpty()) {
    Predicate searchContactCriteria;
    Predicate searchUserCriteria;
    Predicate searchOrganizationCriteria;
    if (isEmailAddress(search)) {
      searchContactCriteria=criteriaBuilder.equal(criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),search.toUpperCase());
      searchUserCriteria=criteriaBuilder.equal(criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),search.toUpperCase());
    }
 else     if (isNumber(search)) {
      searchContactCriteria=criteriaBuilder.or(criteriaBuilder.equal(contactRoot.get("String_Node_Str").as(String.class),search),criteriaBuilder.like(criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      searchUserCriteria=criteriaBuilder.or(criteriaBuilder.equal(userJoin.get("String_Node_Str").as(String.class),search),criteriaBuilder.like(criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      searchOrganizationCriteria=criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(organizationJoin.get("String_Node_Str")),criteriaBuilder.equal(criteriaBuilder.upper(organizationJoin.get("String_Node_Str").as(String.class)),"String_Node_Str" + search + "String_Node_Str"));
      orPredicates.add(searchOrganizationCriteria);
    }
 else {
      searchContactCriteria=criteriaBuilder.or(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(contactRoot.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      searchUserCriteria=criteriaBuilder.or(criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(userJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      searchOrganizationCriteria=criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(organizationJoin.get("String_Node_Str")),criteriaBuilder.greaterThanOrEqualTo(criteriaBuilder.function("String_Node_Str",Float.class,criteriaBuilder.upper(organizationJoin.get("String_Node_Str").as(String.class)),criteriaBuilder.literal(search.toUpperCase())),MIN_SIMILARITY_SCORE));
      orPredicates.add(searchOrganizationCriteria);
    }
    orPredicates.add(searchContactCriteria);
    if (!onlyWithoutUser) {
      orPredicates.add(criteriaBuilder.and(criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNull(contactRoot.get("String_Node_Str")),criteriaBuilder.isNotNull(userJoin.get("String_Node_Str")),criteriaBuilder.or(searchUserCriteria)));
    }
  }
  if (!withDeleted) {
    andPredicates.add(criteriaBuilder.and(contactRoot.get("String_Node_Str").isNull()));
  }
  if (onlyWithoutUser) {
    andPredicates.add(userJoin.get("String_Node_Str").isNull());
  }
  if (allowedModelIds != null && !allowedModelIds.isEmpty()) {
    andPredicates.add(contactModelJoin.get("String_Node_Str").in(allowedModelIds));
  }
  if (allowedType != null) {
    andPredicates.add(criteriaBuilder.equal(contactModelJoin.get("String_Node_Str"),allowedType));
  }
  if (excludedIds != null && !excludedIds.isEmpty()) {
    andPredicates.add(criteriaBuilder.not(contactRoot.get("String_Node_Str").in(excludedIds)));
  }
  if (checkboxElementId != null) {
    List<Integer> contactIds=valueDAO.findContainerIdByElementAndValue(checkboxElementId,"String_Node_Str");
    if (!contactIds.isEmpty()) {
      andPredicates.add(contactRoot.get("String_Node_Str").in(contactIds));
    }
 else {
      return Collections.emptyList();
    }
  }
  andPredicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
  criteriaQuery.where(andPredicates.toArray(new Predicate[andPredicates.size()]));
  criteriaQuery.select(contactRoot);
  criteriaQuery.orderBy(criteriaBuilder.asc(contactRoot.get("String_Node_Str")));
  return em().createQuery(criteriaQuery).getResultList();
}