Java tutorial
package com.carser.viamais.vo; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Expression; import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import org.springframework.util.StringUtils; import com.carser.viamais.entity.Car_; import com.carser.viamais.entity.Customer_; import com.carser.viamais.entity.Model_; import com.carser.viamais.entity.Purchase; import com.carser.viamais.entity.Sale; import com.carser.viamais.entity.Sale_; import com.carser.viamais.entity.Seller_; import com.carser.viamais.entity.Transaction; import com.carser.viamais.entity.Transaction_; import com.carser.viamais.exception.BusinessException; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class TransactionFilter implements Serializable { enum OrderBy { TYPE, CUSTOMER_NAME, CAR_NAME, PLATE, YEAR, DATE; } enum Order { ASC, DESC; } private static final long serialVersionUID = -1356833058915399814L; private Order order = Order.ASC; private int limit = 10; private int offset; private OrderBy orderBy = OrderBy.CUSTOMER_NAME; private String pattern; private String rg; private String cpf; private String type; private Long seller; private String advertiser; private String financial; private Date startDate; private Date finalDate; private String renavam; private String plate; private String chassi; public Long count(final EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<Transaction> transaction = cq.from(Transaction.class); cq.select(cb.countDistinct((transaction))); cq.where(getPredicates(cb, transaction)); return em.createQuery(cq).getSingleResult(); } private Predicate[] getPredicates(CriteriaBuilder cb, Root<Transaction> transaction) { List<Predicate> predicates = new ArrayList<>(); if (StringUtils.hasLength(pattern)) { String[] words = pattern.split(" "); for (String word : words) { Predicate model = cb.like(transaction.get(Transaction_.car).get(Car_.model).get(Model_.name), "%" + word + "%"); Predicate customer = cb.like(transaction.get(Transaction_.customer).get(Customer_.name), "%" + word + "%"); predicates.add((cb.or(model, customer))); } } if (StringUtils.hasLength(rg)) { predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.rg), "%" + rg + "%")); } if (StringUtils.hasLength(cpf)) { predicates.add(cb.like(transaction.get(Transaction_.customer).get(Customer_.cpf).as(String.class), "%" + cpf + "%")); } if (StringUtils.hasLength(renavam)) { predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.renavam), "%" + renavam + "%")); } if (StringUtils.hasLength(plate)) { predicates.add(cb.equal(transaction.get(Transaction_.car).get(Car_.licensePlate), plate)); } if (StringUtils.hasLength(chassi)) { predicates.add(cb.like(transaction.get(Transaction_.car).get(Car_.chassi), "%" + chassi + "%")); } Expression<Date> date = cb.function("date", Date.class, transaction.get(Transaction_.transactionDate)); if (startDate != null) { predicates.add(cb.greaterThanOrEqualTo(date, startDate)); } if (finalDate != null) { predicates.add(cb.lessThanOrEqualTo(date, finalDate)); } if (StringUtils.hasLength(type)) { switch (type) { case "sale": predicates.add(cb.equal(transaction.get(Transaction_.type), Sale.class.getSimpleName())); break; case "purchase": predicates.add(cb.equal(transaction.get(Transaction_.type), Purchase.class.getSimpleName())); break; default: throw new BusinessException("transaction type not supported"); } } if (StringUtils.hasLength(advertiser)) { predicates.add(cb.equal(transaction.get(Transaction_.advertiser), advertiser)); } if (StringUtils.hasLength(financial)) { predicates.add(cb.like(cb.treat(transaction, Sale.class).get(Sale_.financial), financial)); } if (seller != null) { predicates.add(cb.equal(transaction.get(Transaction_.seller).get(Seller_.id), seller)); } return (predicates.toArray(new Predicate[predicates.size()])); } public String getChassi() { return chassi; } public String getCpf() { return cpf; } public Date getFinalDate() { return finalDate; } public int getLimit() { return limit; } public Long getSeller() { return seller; } public void setSeller(Long seller) { this.seller = seller; } public String getAdvertiser() { return advertiser; } public void setAdvertiser(String advertiser) { this.advertiser = advertiser; } public String getFinancial() { return financial; } public void setFinancial(String financial) { this.financial = financial; } public int getOffset() { return offset; } public String getPattern() { return pattern; } public String getPlate() { return plate; } public String getRenavam() { return renavam; } public String getRg() { return rg; } public Date getStartDate() { return startDate; } public String getType() { return type; } public List<Transaction> resultList(final EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class); Root<Transaction> transaction = cq.from(Transaction.class); cq.where(getPredicates(cb, transaction)); @SuppressWarnings("rawtypes") Path path; switch (orderBy) { case CUSTOMER_NAME: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; case DATE: path = transaction.get(Transaction_.transactionDate); break; case PLATE: path = transaction.get(Transaction_.car).get(Car_.licensePlate); break; case TYPE: path = transaction.get(Transaction_.type); break; case YEAR: path = transaction.get(Transaction_.car).get(Car_.yearOfModel); break; default: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; } switch (order) { case DESC: cq.orderBy(cb.desc(path)); break; default: cq.orderBy(cb.asc(path)); break; } TypedQuery<Transaction> query = em.createQuery(cq); query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); } public void setChassi(final String chassi) { this.chassi = chassi; } public void setCpf(final String cpf) { this.cpf = cpf; } public void setFinalDate(final Date finalDate) { this.finalDate = finalDate; } public void setLimit(final int limit) { this.limit = limit; } public void setOffset(final int offset) { this.offset = offset; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } public OrderBy getOrderBy() { return orderBy; } public void setOrderBy(OrderBy orderBy) { this.orderBy = orderBy; } public void setPattern(final String pattern) { this.pattern = pattern; } public void setPlate(final String plate) { this.plate = plate; } public void setRenavam(final String renavam) { this.renavam = renavam; } public void setRg(final String rg) { this.rg = rg; } public void setStartDate(final Date startDate) { this.startDate = startDate; } public void setType(final String type) { this.type = type; } }