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:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static List<PlacementModel> getUnassignedPlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(false)));
    crit.add(Restrictions.eq("deleted", new Boolean(false)));
    crit.add(Restrictions.isNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static List<PlacementModel> getActivePlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(true)));
    crit.add(Restrictions.eq("deleted", new Boolean(false)));
    crit.add(Restrictions.isNotNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static Object getInactivePlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(false)));
    crit.add(Restrictions.eq("deleted", new Boolean(false)));
    crit.add(Restrictions.isNotNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:ca.myewb.logic.PlacementLogic.java

License:Open Source License

public static Object getDeletedPlacements() {
    Criteria crit = HibernateUtil.currentSession().createCriteria(PlacementModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("active", new Boolean(false)));
    crit.add(Restrictions.eq("deleted", new Boolean(true)));
    crit.add(Restrictions.isNull("ov"));
    crit.addOrder(Order.asc("name"));
    return (new SafeHibList<PlacementModel>(crit)).list();
}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public PagingLoadResult<UserDetail> getAllUserDetail(PagingLoadConfig config) throws PigeException {

    PermissionHelper.checkUserManagementPermission(getThreadLocalRequest());

    logger.debug("Rcupration des dtails de tous les usagers " + "[Pagination: dpart=" + config.getOffset()
            + ", max=" + config.getLimit() + "] ...");

    BaseFilterPagingLoadConfig xConfig = null;
    if (config instanceof BaseFilterPagingLoadConfig) {
        xConfig = (BaseFilterPagingLoadConfig) config;
    }//from   ww  w.j a v  a2s  . c  om

    Session session = null;
    Transaction tx = null;
    List<User> users = null;
    List<UserDetail> userDetailList = null;
    Integer userCount = 0;
    Integer start;
    Integer limit;

    try {

        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(User.class);
        Criterion filterCriterion = PigeHibernateUtil.buildFilterCriterion(xConfig.getFilterConfigs());
        if (filterCriterion != null) {
            criteria.add(filterCriterion);
        }

        userCount = (Integer) session.createCriteria(User.class).setProjection(Projections.rowCount())
                .uniqueResult();

        start = config.getOffset();
        limit = userCount;
        if (limit > 0 && config.getLimit() > 0) {
            limit = Math.min(config.getLimit(), limit);
        }

        logger.debug("Paramtres d'extraction des donnes: dpart=" + start + ", max=" + limit + "] ...");

        criteria.setProjection(null);
        criteria.setResultTransformer(Criteria.ROOT_ENTITY);

        users = (List) criteria.setFirstResult(start).setMaxResults(limit).list();

        tx.commit();

        userDetailList = new ArrayList();
        for (User u : users) {
            userDetailList.add(u.getUserDetail());
        }

        logger.debug("Rcupration russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return new BasePagingLoadResult(userDetailList, config.getOffset(), userCount);
}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public PagingLoadResult<Loan> getLoans(PagingLoadConfig configs) throws PigeException {

    PermissionHelper.checkLoanManagementPermission(getThreadLocalRequest());

    logger.debug("Rcupration des emprunts " + "[Pagination: dpart=" + configs.getOffset() + ", max="
            + configs.getLimit() + "] ...");

    Transaction tx = null;/* w w w  . ja v  a  2  s.  com*/
    List<Loan> loans = null;
    Session session = null;
    Integer loanCount = 0;
    Integer offset = 0;
    Integer limit = 0;

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        Date startDate = null;
        Date endDate = null;

        Criteria loansCriteria = session.createCriteria(Loan.class);
        Criteria usersCriteria = null;

        List<FilterConfig> searchConfigs = configs.get(PIGE.SEARCH_CONFIGS);
        List<FilterConfig> filterConfigs = configs.get(PIGE.FILTER_CONFIGS);

        List<FilterConfig> userParam = null;
        List<FilterConfig> loanParam = null;

        if (searchConfigs != null) {
            for (FilterConfig fc : searchConfigs) {
                if (fc.getField().equals("params")) {
                    logger.debug("Extraction du FilterConfig 'params'...");
                    BaseListFilterConfig blfc = (BaseListFilterConfig) fc;
                    userParam = blfc.get(PIGE.USER_CONFIGS);
                    logger.debug("Extraction de la liste 'user-param'..." + (userParam == null ? "N/D" : "OK"));
                    loanParam = blfc.get(PIGE.LOAN_CONFIGS);
                    logger.debug("Extraction de la liste 'loan-param'..." + (loanParam == null ? "N/D" : "OK"));
                    break;
                }
            }
        }

        Criterion filterCriterion = null;
        Iterator<FilterConfig> itr = null;
        FilterConfig fc = null;

        if (loanParam != null) {
            itr = loanParam.iterator();
            while (itr.hasNext()) {
                fc = itr.next();
                if (fc instanceof BaseDateFilterConfig) {
                    BaseDateFilterConfig dateFC = (BaseDateFilterConfig) fc;
                    startDate = dateFC.get(PIGE.START_DATE, null);
                    endDate = dateFC.get(PIGE.END_DATE, null);
                    itr.remove();
                    break;
                }
            }

            FilterConfig matchModeConfig = new BaseBooleanFilterConfig();
            matchModeConfig.setField(PIGE.MATCH_MODE);
            matchModeConfig.setValue(Boolean.FALSE);
            loanParam.add(matchModeConfig);
            filterCriterion = PigeHibernateUtil.buildFilterCriterion(loanParam);
            if (filterCriterion != null) {
                loansCriteria.add(filterCriterion);
            }

            if (startDate != null) {
                logger.debug("Restrictions sur la date d'chance: entre " + startDate.toString() + " et "
                        + (endDate == null ? new Date() : endDate) + " inclusivement...");
                loansCriteria.add(Restrictions.between(Loan.START_DATE_REF, startDate,
                        (endDate == null ? new Date() : endDate)));
            } else if (endDate != null) {
                logger.debug("Restrictions sur la date d'chance: <= " + endDate.toString());
                loansCriteria.add(Restrictions.le(Loan.START_DATE_REF, endDate));
            }
        }

        if (filterConfigs != null && filterConfigs.size() > 0) {
            filterCriterion = PigeHibernateUtil.buildFilterCriterion(filterConfigs);
            if (filterCriterion != null) {
                loansCriteria.add(filterCriterion);
            }
        }

        usersCriteria = loansCriteria.createCriteria(Loan.USER_REF, "usr", Criteria.LEFT_JOIN);

        if (userParam != null) {
            String userScope = null;
            itr = userParam.iterator();
            while (itr.hasNext()) {
                fc = itr.next();
                if (fc.getField().equals("scope")) {
                    userScope = (String) fc.getValue();
                    itr.remove();
                    break;
                }
            }
            if (userScope != null && !userScope.isEmpty() && !userScope.equals("*")) {
                logger.debug(
                        "Restriction de la recherche sur un usager " + "spcifique: [" + userScope + "] ...");
                usersCriteria.add(Restrictions.like(User.IDENTIFIER_REF, userScope, MatchMode.EXACT));
            } else {
                logger.debug("Restriction de la recherche sur un ou des " + "usager spcifique...");
                filterCriterion = PigeHibernateUtil.buildFilterCriterion(userParam);
                if (filterCriterion != null) {
                    usersCriteria.add(filterCriterion);
                }
            }
        }

        loanCount = (Integer) loansCriteria.setProjection(Projections.rowCount()).uniqueResult();

        offset = configs.getOffset();
        limit = loanCount;
        if (limit > 0 && configs.getLimit() > 0) {
            limit = Math.min(configs.getLimit(), limit);
        }

        logger.debug("Paramtres d'extraction des donnes: dpart=" + offset + ", max=" + limit + "] ...");

        loansCriteria.setProjection(null);
        loansCriteria.setResultTransformer(Criteria.ROOT_ENTITY);

        loans = (List) loansCriteria.addOrder(Order.asc("usr." + User.LOAN_NO_REF)).setFirstResult(offset)
                .setMaxResults(limit).list();

        tx.commit();
        logger.debug("Rcupration russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

    if (loans == null) {
        loans = new ArrayList();
    }

    return new BasePagingLoadResult(loans, offset, loanCount);
}

From source file:ca.qc.cegepoutaouais.tge.pige.server.ManagementServiceImpl.java

License:Open Source License

@Override
public List<Category> getCategories(Category parent, Boolean includeUnclassified) throws PigeException {

    logger.debug("Rcupration des catgories...");

    Transaction tx = null;/* w w  w .j av  a2  s.  c o  m*/
    List<Category> categories = null;
    Session session = null;

    try {
        session = PigeHibernateUtil.openSession();
        tx = session.beginTransaction();

        Criteria criteria = session.createCriteria(Category.class);
        if (parent != null) {
            logger.debug("category != null: " + parent.getName());
            criteria.add(Restrictions.eq(Category.PARENT_REF, parent));
        } else {
            criteria.add(Restrictions.isNull(Category.PARENT_REF));
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

        categories = (List) criteria.addOrder(Order.asc(Category.NAME_REF)).list();

        if (categories != null) {
            for (Category c : categories) {
                Criteria itemCrit = session.createCriteria(Item.class);
                itemCrit.createCriteria(Item.CATEGORIES_REF)
                        .add(Restrictions.like(Category.PATH_REF, c.getPath(), MatchMode.START));
                itemCrit.setProjection(Projections.distinct(Projections.rowCount()));
                c.setItemCount((Integer) itemCrit.uniqueResult());
            }
        }

        //  la racine seulement.
        if (includeUnclassified && parent == null) {
            Category unclassified = new Category();
            unclassified.setId(Category.UNCLASSIFIED_CATEGORY_ID);
            Criteria itemCrit = session.createCriteria(Item.class);
            itemCrit.add(Restrictions.sizeEq(Item.CATEGORIES_REF, 0));
            itemCrit.setProjection(Projections.distinct(Projections.rowCount()));
            unclassified.setItemCount((Integer) itemCrit.uniqueResult());
            categories.add(unclassified);
        }

        tx.commit();
        logger.debug("Rcupration russie!");
    } catch (Exception hex) {
        logger.error(hex);
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }

    return categories;
}

From source file:cc.cnfc.core.orm.hibernate.SimpleHibernateDao.java

License:Open Source License

/**
 * Criteriadistinct transformer.//w w  w . j ava 2  s.  c om
 */
public Criteria distinct(Criteria criteria) {
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    return criteria;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DAOUtils.java

License:Apache License

/**
 * Returns the number of entities that the given <var>critera</var> will return.
 *//* w  w w  .j a v a 2 s . c  o  m*/
static int getCount(final Criteria criteria) {
    int count = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
    // Undo the rowCount projection
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
    return count;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DataStoreDAO.java

License:Apache License

public List<DataStorePE> listDataStores() {
    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    criteria.setFetchMode("servicesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    final List<DataStorePE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%d data stores have been found.", list.size()));
    }// w w  w  . ja v a 2  s.c o m
    return list;
}