Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

In this page you can find the example usage for org.hibernate Criteria setResultTransformer.

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:br.ufg.calendario.dao.UsuarioDao.java

@Transactional(readOnly = true)
public List<Usuario> listar() {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Usuario.class);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:br.ufg.calendario.dao.UsuarioDao.java

@Transactional(readOnly = true)
public List<Usuario> listar(int first, int pageSize, String sortField, String sortOrder,
        Map<String, Object> filters) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Usuario.class);
    criteria.setFirstResult(first);/*from w w w. j a  va2  s  . c o m*/
    criteria.setMaxResults(pageSize);
    if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
        if (sortOrder.equals("ASCENDING")) {
            criteria.addOrder(Order.asc(sortField));
        }
        if (sortOrder.equals("DESCENDING")) {
            criteria.addOrder(Order.desc(sortField));
        }
    } else {
        criteria.addOrder(Order.asc("id"));
    }
    if (filters != null && !filters.isEmpty()) {
        for (String key : filters.keySet()) {
            if (key.equals("termo")) {
                criteria.add(Restrictions.or(
                        Restrictions.like("nome", filters.get(key).toString(), MatchMode.ANYWHERE).ignoreCase(),
                        Restrictions.like("login", filters.get(key).toString(), MatchMode.ANYWHERE)
                                .ignoreCase()));
            }

            if (key.equals("perfil")) {
                PerfilEnum p = (PerfilEnum) filters.get(key);
                criteria.add(Restrictions.eq("perfil", p));
            }

        }
    }
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List<Usuario> resultado = criteria.list();
    for (Usuario u : resultado) {
        Hibernate.initialize(u.getPerfil());
    }
    return resultado;
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 * find by discente nome//  w w w  . j a va2  s .  c  om
 *
 * @param termo
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(String termo, String sortField, String sortOrder) {
    try {
        List<Requerimento> requerimentos;
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .createAlias("discente", "d").add(Restrictions.or(Restrictions.eq("d.matricula", termo),
                        Restrictions.like("d.nome", termo, MatchMode.ANYWHERE).ignoreCase()));

        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

/**
 *
 * @param first// w w  w  . j a  v a 2s . c  o m
 * @param pageSize
 * @param sortField
 * @param sortOrder
 * @return
 */
@Transactional(readOnly = true)
public List<Requerimento> find(int first, int pageSize, String sortField, String sortOrder) {
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class)
                .setFirstResult(first).setMaxResults(pageSize);
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.RequerimentoDao.java

@Transactional(readOnly = true)
public List<Requerimento> find(String sortField, String sortOrder, Map<String, Object> filters) {
    if (filters == null) {
        filters = new HashMap();
    }//from www .j  a va2s.c o m
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Requerimento.class);
        for (String field : filters.keySet()) {
            /**
             * login eq / tipoRequerimento eq / termo like / dataCriacao
             * between
             */
            if (field.equals("login")) {
                criteria.createAlias("discente", "d");
                criteria.add(Restrictions.and(Restrictions.eq("d.login", filters.get(field))));
            }
            if (field.equals("turmas")) {
                criteria.createAlias("itemRequerimentoList", "i");
                List<Turma> turmas = (List<Turma>) filters.get(field);
                criteria.add(Restrictions.and(Restrictions.in("i.turma", turmas)));
            }
            if (field.equals("tipoRequerimento")) {
                criteria.add(Restrictions.and(Restrictions.eq(field, filters.get(field))));
            }
            if (field.equals("termo")) {
                criteria.createAlias("discente", "d");
                criteria.add(Restrictions.or(Restrictions.eq("d.matricula", filters.get("termo")), Restrictions
                        .like("d.nome", filters.get("termo").toString(), MatchMode.ANYWHERE).ignoreCase()));
            }
            if (field.equals("curso")) {
                criteria.createAlias("discente.perfilList", "p");
                criteria.add(Restrictions.and(Restrictions.eq("p.curso", filters.get(field))));
            }
            if (field.equals("dataCriacao")) {
                Date[] arrayDate = (Date[]) filters.get("dataCriacao");
                criteria.add(Restrictions.and(Restrictions.between("dataCriacao", arrayDate[0], arrayDate[1])));
            }
        }
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.TurmaDao.java

@Transactional(readOnly = true)
public List<Turma> find(String termo, Curso curso, boolean periodoAtivo) {
    try {//from  w  ww  . j a  v a2s .  c o  m
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Turma.class);
        if (periodoAtivo) {
            criteria.createAlias("periodo", "p").add(Restrictions.eq("p.ativo", periodoAtivo));
        }
        if (termo != null && !termo.isEmpty()) {
            criteria.createAlias("disciplina", "d")
                    .add(Restrictions.like("d.nome", termo.toUpperCase(), MatchMode.ANYWHERE));
        }
        if (curso != null) {
            criteria.add(Restrictions.and(Restrictions.eq("d.curso", curso)));
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException | NumberFormatException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.TurmaDao.java

@Transactional(readOnly = true)
public List<Turma> find(int firstResult, int maxResult, String sortField, String sortOrder,
        Map<String, Object> filters) {
    if (filters == null) {
        filters = new HashMap();
    }/*from w  ww .ja v a  2s  . c o m*/
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Turma.class);
        criteria.createAlias("disciplina", "d");
        for (String field : filters.keySet()) {
            if (field.equals("termo")) {
                criteria.add(Restrictions.and(Restrictions
                        .like("d.nome", filters.get(field).toString(), MatchMode.ANYWHERE).ignoreCase()));
            }
            if (field.equals("periodo")) {
                criteria.add(Restrictions.and(Restrictions.eq("periodo", filters.get(field))));
            }
            if (field.equals("curso")) {
                criteria.add(Restrictions.and(Restrictions.eq("d.curso", filters.get(field))));
            }
        }
        if ((sortField != null && !sortField.isEmpty()) && (sortOrder != null && !sortOrder.isEmpty())) {
            System.out.format("sorted by: %s, ordering %s\n", sortField, sortOrder);
            if (sortOrder.toLowerCase().equals("asc")) {
                criteria.addOrder(Property.forName(sortField).asc());
            }
            if (sortOrder.toLowerCase().equals("desc")) {
                criteria.addOrder(Property.forName(sortField).desc());
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return new ArrayList<>();
    }
}

From source file:br.ufg.reqweb.dao.TurmaDao.java

@Transactional(readOnly = true)
public int count(Map<String, Object> filters) {
    if (filters == null) {
        filters = new HashMap();
    }/*  ww  w  .j  a  v a 2  s .  co  m*/
    try {
        Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Turma.class);
        criteria.createAlias("disciplina", "d");
        for (String field : filters.keySet()) {
            if (field.equals("termo")) {
                criteria.add(Restrictions.and(Restrictions
                        .like("d.nome", filters.get(field).toString(), MatchMode.ANYWHERE).ignoreCase()));
            }
            if (field.equals("periodo")) {
                criteria.add(Restrictions.and(Restrictions.eq("periodo", filters.get(field))));
            }
            if (field.equals("curso")) {
                criteria.add(Restrictions.and(Restrictions.eq("d.curso", filters.get(field))));
            }
        }
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
    } catch (HibernateException e) {
        System.out.println("query error: " + e.getMessage());
        return 0;
    }

}

From source file:ca.myewb.controllers.common.EventList.java

License:Open Source License

public Collection<EventModel> listVisibleEventsBetweenDates(Date start, Date end, GroupChapterModel chapter)
        throws HibernateException {
    Criteria criteria = hibernateSession.createCriteria(EventModel.class);

    LogicalExpression singleDayEvents = Restrictions.and(Restrictions.ge("startDate", start),
            Restrictions.le("endDate", end));
    LogicalExpression endsToday = Restrictions.and(Restrictions.lt("startDate", start),
            Restrictions.and(Restrictions.ge("endDate", start), Restrictions.le("endDate", end)));
    LogicalExpression startsToday = Restrictions.and(
            Restrictions.and(Restrictions.ge("startDate", start), Restrictions.le("startDate", end)),
            Restrictions.gt("endDate", end));
    LogicalExpression ongoing = Restrictions.and(Restrictions.lt("startDate", start),
            Restrictions.gt("endDate", end));

    criteria.add(Restrictions.or(singleDayEvents,
            Restrictions.or(endsToday, Restrictions.or(startsToday, ongoing))));

    if (chapter == null) {
        if (!currentUser.isAdmin()) {
            criteria.add(Restrictions.in("group", Permissions.visibleGroups(currentUser, true)));
        } else {/*from  ww  w  . j  a  va 2s.c  o  m*/
            List<GroupModel> adminGroups = Helpers.getNationalRepLists(true, true);
            adminGroups.add(Helpers.getGroup("Exec"));
            adminGroups.add(Helpers.getGroup("ProChaptersExec"));

            criteria.add(Restrictions.in("group", adminGroups));
        }
    } else {
        criteria.add(Restrictions.in("group", Permissions.visibleGroupsInChapter(currentUser, chapter)));
    }

    criteria.addOrder(Order.asc("startDate"));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return new SafeHibList<EventModel>(criteria).list();
}

From source file:ca.myewb.controllers.common.EventList.java

License:Open Source License

public List<EventModel> listPaginatedVisibleEvents(String filter, int startPage, int eventsPerPage,
        Date endAfter) throws HibernateException {
    Criteria criteria = hibernateSession.createCriteria(EventModel.class);

    if (!currentUser.isAdmin()) {
        log.info("EventList visible groups added to criteria");
        criteria.add(Restrictions.in("group", Permissions.visibleGroups(currentUser, true)));
    }/*from  w ww.  ja v a  2s. co  m*/

    criteria.addOrder(Order.asc("startDate"));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    addBooleanFilters(endAfter, criteria);
    addFilter(filter, criteria);
    addPagination(startPage, eventsPerPage, criteria);

    return getUniqueEventList(criteria);
}