Skip to content

Instantly share code, notes, and snippets.

@carloseduardosx
Last active March 29, 2016 17:10
Show Gist options
  • Save carloseduardosx/db1f959ba5a7562b847c to your computer and use it in GitHub Desktop.
Save carloseduardosx/db1f959ba5a7562b847c to your computer and use it in GitHub Desktop.
Builder to hibernate criteria
package com.carlosedurdo.core.builder;
import com.carlosedurdo.core.model.Criteria;
import com.carlosedurdo.core.util.ArrayUtil;
import org.hibernate.criterion.MatchMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CriteriaBuilder {
private List<Criteria> criteria = new ArrayList<>();
private List<Criteria> criteriaChain = new ArrayList<>();
private List<Criteria> and = new ArrayList<>();
private List<Criteria> or = new ArrayList<>();
private List<Criteria> eq = new ArrayList<>();
private List<Criteria> neq = new ArrayList<>();
private List<Criteria> lte = new ArrayList<>();
private List<Criteria> lt = new ArrayList<>();
private List<Criteria> gte = new ArrayList<>();
private List<Criteria> gt = new ArrayList<>();
private List<Criteria> in = new ArrayList<>();
public CriteriaBuilder in(String attr, List<Object> in, MatchMode matchMode) {
Criteria criteriaIn = new Criteria();
criteriaIn.setAttr(attr);
criteriaIn.setIn(in);
criteriaIn.setMatchMode(matchMode);
this.in.add(criteriaIn);
return this;
}
public CriteriaBuilder eq(String attr, Object eq, MatchMode matchMode) {
Criteria criteriaEq = new Criteria();
criteriaEq.setAttr(attr);
criteriaEq.setEq(eq);
criteriaEq.setMatchMode(matchMode);
this.eq.add(criteriaEq);
return this;
}
public CriteriaBuilder eq(String attr[], Object eq[]) {
List<Criteria> criteriaEqs = new ArrayList<>();
validateArraySizes(attr, eq);
for (int i = 0; i < eq.length; i++) {
Criteria criteriaEq = new Criteria();
criteriaEq.setAttr(attr[i]);
criteriaEq.setEq(eq[i]);
criteriaEqs.add(criteriaEq);
}
this.eq.addAll(criteriaEqs);
return this;
}
public CriteriaBuilder neq(String attr, Object neq, MatchMode matchMode) {
Criteria criteriaEq = new Criteria();
criteriaEq.setAttr(attr);
criteriaEq.setNeq(neq);
criteriaEq.setMatchMode(matchMode);
this.neq.add(criteriaEq);
return this;
}
public CriteriaBuilder neq(String attr[], Object neq[]) {
List<Criteria> criteriaNeqs = new ArrayList<>();
validateArraySizes(attr, neq);
for (int i = 0; i < neq.length; i++) {
Criteria criteriaNeq = new Criteria();
criteriaNeq.setAttr(attr[i]);
criteriaNeq.setNeq(neq[i]);
criteriaNeqs.add(criteriaNeq);
}
this.neq.addAll(criteriaNeqs);
return this;
}
public CriteriaBuilder lte(String attr, Object lte, MatchMode matchMode) {
Criteria criteriaLte = new Criteria();
criteriaLte.setAttr(attr);
criteriaLte.setLte(lte);
criteriaLte.setMatchMode(matchMode);
this.lte.add(criteriaLte);
return this;
}
public CriteriaBuilder lte(String attr[], Object lte[]) {
List<Criteria> criteriaLtes = new ArrayList<>();
validateArraySizes(attr, lte);
for (int i = 0; i < lte.length; i++) {
Criteria criteriaLte = new Criteria();
criteriaLte.setAttr(attr[i]);
criteriaLte.setLte(lte[i]);
criteriaLtes.add(criteriaLte);
}
this.lte.addAll(criteriaLtes);
return this;
}
public CriteriaBuilder lt(String attr, Object lt, MatchMode matchMode) {
Criteria criteriaLt = new Criteria();
criteriaLt.setAttr(attr);
criteriaLt.setLt(lt);
criteriaLt.setMatchMode(matchMode);
this.lt.add(criteriaLt);
return this;
}
public CriteriaBuilder lt(String attr[], Object lt[]) {
List<Criteria> criteriaLts = new ArrayList<>();
validateArraySizes(attr, lt);
for (int i = 0; i < lt.length; i++) {
Criteria criteriaLt = new Criteria();
criteriaLt.setAttr(attr[i]);
criteriaLt.setLt(lt[i]);
criteriaLts.add(criteriaLt);
}
this.lt.addAll(criteriaLts);
return this;
}
public CriteriaBuilder gt(String attr, Object gt, MatchMode matchMode) {
Criteria criteriaGt = new Criteria();
criteriaGt.setAttr(attr);
criteriaGt.setGt(gt);
criteriaGt.setMatchMode(matchMode);
this.gt.add(criteriaGt);
return this;
}
public CriteriaBuilder gt(String attr[], Object gt[]) {
List<Criteria> criteriaGts = new ArrayList<>();
validateArraySizes(attr, gt);
for (int i = 0; i < gt.length; i++) {
Criteria criteriaGt = new Criteria();
criteriaGt.setAttr(attr[i]);
criteriaGt.setGt(gt[i]);
criteriaGts.add(criteriaGt);
}
this.gt.addAll(criteriaGts);
return this;
}
public CriteriaBuilder gte(String attr, Object gte, MatchMode matchMode) {
Criteria criteriaGte = new Criteria();
criteriaGte.setAttr(attr);
criteriaGte.setGte(gte);
criteriaGte.setMatchMode(matchMode);
this.gte.add(criteriaGte);
return this;
}
public CriteriaBuilder gte(String attr[], Object gte[]) {
List<Criteria> criteriaGtes = new ArrayList<>();
validateArraySizes(attr, gte);
for (int i = 0; i < gte.length; i++) {
Criteria criteriaGte = new Criteria();
criteriaGte.setAttr(attr[i]);
criteriaGte.setGte(gte[i]);
criteriaGtes.add(criteriaGte);
}
this.gte.addAll(criteriaGtes);
return this;
}
public MonetCriteria build() {
MonetCriteria monetCriteria = new MonetCriteria();
if (!CollectionUtil.isEmpty(criteriaChain)) {
monetCriteria.setCriteriaChain(criteriaChain);
criteriaChain = new ArrayList<>();
return monetCriteria;
} else if (!CollectionUtil.isEmpty(and)) {
monetCriteria.setAnd(and);
and = new ArrayList<>();
return monetCriteria;
} else if (!CollectionUtil.isEmpty(or)) {
monetCriteria.setOr(or);
or = new ArrayList<>();
return monetCriteria;
} else {
cleanCriteriaLists();
return emptyCriteria();
}
}
public CriteriaBuilder criteriaChain() {
Criteria criteriaChain = new Criteria();
if (!ArrayUtil.isEmpty(and)) {
criteriaChain.setAnd(and);
and = new ArrayList<>();
}
if (!ArrayUtil.isEmpty(or)) {
criteriaChain.setOr(or);
or = new ArrayList<>();
}
this.criteriaChain.add(criteriaChain);
return this;
}
public CriteriaBuilder and() {
addAllCriteriaToCriteriaList();
and.addAll(criteria);
criteria = new ArrayList<>();
return this;
}
public CriteriaBuilder or() {
addAllCriteriaToCriteriaList();
or.addAll(criteria);
criteria = new ArrayList<>();
return this;
}
private void addAllCriteriaToCriteriaList() {
criteria.addAll(in);
criteria.addAll(eq);
criteria.addAll(neq);
criteria.addAll(lt);
criteria.addAll(lte);
criteria.addAll(gt);
criteria.addAll(gte);
cleanCriteriaLists();
}
private void cleanCriteriaLists() {
in = new ArrayList<>();
eq = new ArrayList<>();
neq = new ArrayList<>();
lt = new ArrayList<>();
lte = new ArrayList<>();
gt = new ArrayList<>();
gte = new ArrayList<>();
}
private Criteria emptyCriteria() {
Criteria emptyCriteria = new Criteria();
Criteria emptyCriteriaChain = new Criteria();
Criteria emptyAnd = new Criteria();
emptyCriteriaChain.setAnd(Collections.singletonList(emptyAnd));
emptyCriteria.setCriteriaChain(Collections.singletonList((emptyCriteriaChain)));
return emptyCriteria;
}
private void validateArraySizes(Object[] object1, Object[] object2) {
if (object1 == null || object2 == null) {
throw new IllegalArgumentException("Invalid value \"null\" for array");
}
if (object1.length != object2.length) {
throw new IllegalArgumentException("Invalid length for array");
}
}
}
@carloseduardosx
Copy link
Author

Sample code:

private CriteriaBuilder buildCriteriaEntity(Class<? extends Entity> entity) {

        return new CriteriaBuilder().eq(ATTR_NAME, entity.getSimpleName(), MatchMode.EXACT)
                .eq(ATTR_VISIBLE_TO_CUSTOMER, true, null)
                .and()
                .criteriaChain()
                .build();
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment