public Map<String, Object> lista(Map<String, Object> params) { log.debug("Buscando lista de facturas con params {}", params); if (params == null) { params = new HashMap<>(); } if (!params.containsKey("max")) { params.put("max", 10); } else { params.put("max", Math.min((Integer) params.get("max"), 100)); } if (params.containsKey("pagina")) { Long pagina = (Long) params.get("pagina"); Long offset = (pagina - 1) * (Integer) params.get("max"); params.put("offset", offset.intValue()); } if (!params.containsKey("offset")) { params.put("offset", 0); } Criteria criteria = currentSession().createCriteria(FacturaAlmacen.class); Criteria countCriteria = currentSession().createCriteria(FacturaAlmacen.class); if (params.containsKey("almacen")) { criteria.createCriteria("almacen").add(Restrictions.idEq(params.get("almacen"))); countCriteria.createCriteria("almacen").add(Restrictions.idEq(params.get("almacen"))); } if (params.containsKey("filtro")) { String filtro = (String) params.get("filtro"); Disjunction propiedades = Restrictions.disjunction(); propiedades.add(Restrictions.ilike("folio", filtro, MatchMode.ANYWHERE)); criteria.add(propiedades); countCriteria.add(propiedades); } if (params.containsKey("order")) { String campo = (String) params.get("order"); if (params.get("sort").equals("desc")) { criteria.addOrder(Order.desc(campo)); } else { criteria.addOrder(Order.asc(campo)); } } else { criteria.createCriteria("estatus").addOrder(Order.asc("prioridad")); } criteria.addOrder(Order.desc("fechaModificacion")); if (!params.containsKey("reporte")) { criteria.setFirstResult((Integer) params.get("offset")); criteria.setMaxResults((Integer) params.get("max")); } params.put("facturas", criteria.list()); countCriteria.setProjection(Projections.rowCount()); params.put("cantidad", (Long) countCriteria.list().get(0)); return params; }
@Override public <T extends GettableById> List<T> get( Class<T> bookEntityClass, int from, int to, Map<String, List<Criterion>> restrictions, OrderByFilter filter) { validateTransaction(); Criteria criteria = getSession().createCriteria(bookEntityClass); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.setFirstResult(from); if (to != 0) criteria.setMaxResults(to); for (String key : restrictions.keySet()) { if (key.equals(RestrictionsFields.MAIN_RESTRICTIONS)) { for (Criterion restriction : restrictions.get(RestrictionsFields.MAIN_RESTRICTIONS)) criteria.add(restriction); } else if (restrictions.get(key).size() > 0) { Criteria c = criteria.createCriteria(key); for (Criterion restriction : restrictions.get(key)) c.add(restriction); } } if (filter != null) { if (filter.getDirection().equals(OrderByFilter.ASC)) criteria.addOrder(Order.asc(filter.field)); else criteria.addOrder(Order.desc(filter.field)); } return criteria.list(); }
/** Add order directly to criteria. */ private static void addOrder(Criteria c, String sort, String order, boolean ignoreCase) { if (ORDER_DESC.equals(order)) { c.addOrder(ignoreCase ? Order.desc(sort).ignoreCase() : Order.desc(sort)); } else { c.addOrder(ignoreCase ? Order.asc(sort).ignoreCase() : Order.asc(sort)); } }
public List<T> findAll(Integer number, Integer offset, String orderByPropertyName, boolean asc) { Session session = openSession(true); List<T> results = Lists.newArrayList(); try { Criteria criteria = criteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); if (number != 1) { criteria.setFirstResult(number - 1); } if (offset != 0) { criteria.setMaxResults(offset); } if (asc) { criteria.addOrder(Order.asc(orderByPropertyName)); } else { criteria.addOrder(Order.desc(orderByPropertyName)); } results = list(criteria); for (T result : results) { result.init(); } commitTransaction(session, true); } catch (Exception e) { rollbackTransaction(session, true); } finally { closeSession(session); } return results; }
@Override public <T extends GettableById> List<T> get( Class<T> bookEntityClass, int from, int to, OrderByFilter filter) { validateTransaction(); Criteria criteria = getSession().createCriteria(bookEntityClass); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.setFirstResult(from); if (to != 0) criteria.setMaxResults(to); if (filter != null) { if (filter.getDirection().equals(OrderByFilter.ASC)) criteria.addOrder(Order.asc(filter.field)); else criteria.addOrder(Order.desc(filter.field)); } return criteria.list(); }
public final Map<String, Object> findByMap( final Map<String, Object> params, int page, int rows, String order, boolean asc) throws HibernateException { Criteria criteria = getSession().createCriteria(clazz); Long total; if (null != params) criteria.add(Restrictions.allEq(params)); total = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult(); criteria.setProjection(null).setResultTransformer(Criteria.ROOT_ENTITY); if (page > 0 && rows > 0) criteria.setFirstResult((page - 1) * rows).setMaxResults(rows); if (null != order) { if (asc) criteria.addOrder(Order.asc(order)); else criteria.addOrder(Order.desc(order)); } Map<String, Object> result = new HashMap<String, Object>(); result.put("total", total); result.put("rows", (List<T>) criteria.list()); return result; }
@SuppressWarnings("unchecked") @Override public List<Lancamento> listar(Conta conta, Date dataInicio, Date dataFim) { Criteria criteria = this.session.createCriteria(Lancamento.class); if (dataInicio != null && dataFim != null) { criteria.add(Restrictions.between("data", dataInicio, dataFim)); } else if (dataInicio != null) { criteria.add(Restrictions.ge("data", dataInicio)); } else if (dataFim != null) { criteria.add(Restrictions.le("data", dataFim)); } criteria.add(Restrictions.eq("conta", conta)); criteria.addOrder(Order.asc("data")); return criteria.list(); }