// for CustomerController
public List<Request> findByStateAndCustomer(List<StateRequest> listStateRequest, UserHD createdBy) {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery();
Root<Request> z = cq.from(Request.class);
Predicate whereState = null;
if(listStateRequest != null && !listStateRequest.isEmpty()) {
for(StateRequest st: listStateRequest) {
if(whereState == null) {
whereState = cb.equal(z.get(Request_.stateRequest), st);
} else {
whereState = cb.or(whereState, cb.equal(z.get(Request_.stateRequest), st));
}
}
}
Predicate whereCreatedBy = null;
Predicate whereCreatedByOrg = null;
if(createdBy != null) {
whereCreatedBy = cb.equal(z.get(Request_.createdBy), createdBy);
if(createdBy.getOrganization() != null) {
List<UserHD> listUsers = userHDService.findUsersByOrganization(createdBy.getOrganization());
for(UserHD us: listUsers) {
if(whereCreatedByOrg == null) {
whereCreatedByOrg = cb.equal(z.get(Request_.createdBy), us);
} else {
whereCreatedByOrg = cb.or(whereCreatedByOrg, cb.equal(z.get(Request_.createdBy), us));
}
}
}
}
// cq.where(cb.and(whereState, cb.or(whereCreatedBy, whereCreatedByOrg))); // у заказчика не задана орг-ия - whereCreatedByOrg - NPE
if(whereCreatedByOrg == null) {
cq.where(cb.and(whereState, whereCreatedBy));
} else {
cq.where(cb.and(whereState, cb.or(whereCreatedBy, whereCreatedByOrg)));
}
cq.orderBy(cb.desc(z.get(Request_.id)));
cq.select(z);
return getEntityManager().createQuery(cq).getResultList();
}