Exemple #1
0
  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;
  }
  private <P> Criteria buildProjectionCriteria(
      Class<P> projectClass, Criteria criteria, Projection projections, boolean distinctResult) {
    if (distinctResult) {
      criteria.setProjection(Projections.distinct(projections));
    } else {
      criteria.setProjection(projections);
    }

    return criteria.setResultTransformer(Transformers.aliasToBean(projectClass));
  }
 @Override
 public long count(Criteria criteria) {
   return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
 }