Ejemplo n.º 1
0
  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));
  }
Ejemplo n.º 2
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;
  }
 @Override
 public <T extends GettableById> Number count(Class<T> currentClass, Criterion... restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(currentClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setProjection(Projections.rowCount());
   for (Criterion restriction : restrictions) criteria.add(restriction);
   return ((Number) criteria.uniqueResult());
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 public boolean isChangePersisted(PersistentChange change) {
   Criteria criteria =
       getSession()
           .createCriteria(PersistentChange.class)
           .add(Restrictions.eq("hash", change.getHash()))
           .add(Restrictions.eq("repositoryId", change.getRepositoryId()));
   Integer count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
   return count > 0;
 }
 @Override
 public <T extends GettableById> Collection<Integer> getIds(
     Class<T> currentClass, Criterion... restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(currentClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setProjection(Projections.id());
   for (Criterion restriction : restrictions) criteria.add(restriction);
   return criteria.list();
 }
 @Override
 public <T extends GettableById> Number count(
     Class<T> currentClass, Map<String, List<Criterion>> restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(currentClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setProjection(Projections.rowCount());
   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);
     }
   }
   return ((Number) criteria.uniqueResult());
 }
 @Override
 public <T extends GettableById> Collection<Integer> getIds(
     Class<T> bookKeywordEntityClass,
     String bookKeywordEntity,
     int offset,
     int limit,
     HashMap<String, FetchMode> fetchMode,
     HashMap<String, String> alias,
     Criterion... criterions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(bookKeywordEntityClass, bookKeywordEntity);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setFirstResult(offset);
   if (limit != 0) criteria.setMaxResults(limit);
   for (Criterion restriction : criterions) criteria.add(restriction);
   for (Map.Entry<String, FetchMode> stringObjectEntry : fetchMode.entrySet()) {
     criteria.setFetchMode(stringObjectEntry.getKey(), stringObjectEntry.getValue());
   }
   for (Map.Entry<String, String> stringObjectEntry : alias.entrySet()) {
     criteria.createAlias(stringObjectEntry.getKey(), stringObjectEntry.getValue());
   }
   criteria.setProjection(Projections.id());
   return criteria.list();
 }
Ejemplo n.º 9
0
 @Override
 public long count(Criteria criteria) {
   return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
 }