@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();
 }
Esempio 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;
  }
  private void initializeMustMatchNotMatchCriteria() {

    Criterion criterionA = new Criterion(new Answer(questionA, Bool.TRUE), Weight.Important);
    Criterion criterionB = new Criterion(new Answer(questionB, Bool.TRUE), Weight.MustMatch);
    Criterion criterionC = new Criterion(new Answer(questionC, Bool.TRUE), Weight.Important);

    criteria.add(criterionA);
    criteria.add(criterionB);
    criteria.add(criterionC);
  }
Esempio n. 4
0
 /**
  * 根据Criterion条件创建Criteria.
  *
  * <p>本类封装的find()函数全部默认返回对象类型为T,当不为T时使用本函数.
  *
  * @param criterions 数量可变的Criterion.
  */
 protected Criteria createCriteria(final Criterion... criterions) {
   Criteria criteria = getSession().createCriteria(entityClass);
   for (Criterion c : criterions) {
     criteria.add(c);
   }
   return criteria;
 }
  @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();
  }
 /** Use this inside subclasses as a convenience method. */
 @SuppressWarnings("unchecked")
 protected List<T> findByCriteria(Criterion... criterion) {
   Criteria crit = getSession().createCriteria(getEntityClass());
   for (Criterion c : criterion) {
     crit.add(c);
   }
   return crit.list();
 }
Esempio n. 7
0
 public List<T> findByMap(String hql, final Map<String, Object> params) {
   //        Query query = getSession().createQuery(hql);
   //        for(String field:params.keySet())
   //            query.setParameter(field,params.get(field));
   Criteria criteria = getSession().createCriteria(clazz);
   criteria.add(Restrictions.allEq(params));
   return criteria.list();
 }
Esempio n. 8
0
 @SuppressWarnings("unchecked")
 protected T findByCriteriaSingle(Criterion... criterion) {
   Criteria crit = getSession().createCriteria(getPersistentClass());
   for (Criterion c : criterion) {
     crit.add(c);
   }
   crit.setCacheable(queriesCached);
   return (T) crit.uniqueResult();
 }
Esempio n. 9
0
 /** Use this inside subclasses as a convenience method. */
 @SuppressWarnings("unchecked")
 protected List<T> findByCriteria(Criterion... criterion) {
   Criteria crit = getSession().createCriteria(getPersistentClass());
   for (Criterion c : criterion) {
     crit.add(c);
   }
   crit.setCacheable(queriesCached);
   return crit.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> 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());
 }
 @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();
 }
Esempio n. 13
0
 @SuppressWarnings("unchecked")
 public T findByExampleSingle(T exampleInstance, String... excludeProperty) {
   Criteria crit = getSession().createCriteria(getPersistentClass());
   Example example = Example.create(exampleInstance);
   for (String exclude : excludeProperty) {
     example.excludeProperty(exclude);
   }
   crit.add(example);
   crit.setCacheable(queriesCached);
   return (T) crit.uniqueResult();
 }
Esempio n. 14
0
  public Collection findByExample(User exampleUser) throws InfrastructureException {

    Collection users;
    try {
      Criteria crit = HibernateUtil.getSession().createCriteria(User.class);
      users = crit.add(Example.create(exampleUser)).list();
    } catch (HibernateException ex) {
      throw new InfrastructureException(ex);
    }
    return users;
  }
 @Override
 public <T extends GettableById> List<T> get(
     Class<T> bookEntityClass, int from, int to, Criterion... restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(bookEntityClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setFirstResult(from);
   if (to != 0) criteria.setMaxResults(to);
   for (Criterion restriction : restrictions) criteria.add(restriction);
   return criteria.list();
 }
Esempio n. 16
0
 private List<ControlHoras> getListWithRestriction(Criterion res[], FetchMode mode) {
   List<ControlHoras> lista = null;
   Session session = HibernateUtil.getSessionFactory().openSession();
   // Obtener el control de Horas
   Criteria criteria =
       session
           .createCriteria(ControlHoras.class)
           .setFetchMode("horas", mode)
           .setFetchMode("estado", FetchMode.JOIN);
   for (Criterion crit : res) {
     criteria.add(crit);
   }
   lista = Collections.checkedList(criteria.list(), ControlHoras.class);
   session.close();
   return lista;
 }
Esempio n. 17
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;
 }
 @Override
 public <T extends GettableById> List<T> get(
     Class<T> bookEntityClass,
     int from,
     int to,
     OrderByFilter filter,
     Collection<Criterion> restrictions) {
   validateTransaction();
   Criteria criteria = getSession().createCriteria(bookEntityClass);
   criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
   criteria.setFirstResult(from);
   if (to != 0) criteria.setMaxResults(to);
   for (Criterion restriction : restrictions) criteria.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();
 }
 @Override
 public <T extends GettableById> List<T> get(
     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());
   }
   return criteria.list();
 }
Esempio n. 20
0
 public List<T> find(T example) throws HibernateException {
   Criteria criteria = getSession().createCriteria(clazz);
   return criteria.add(Example.create(example)).list();
 }