Example usage for org.hibernate Criteria addOrder

List of usage examples for org.hibernate Criteria addOrder

Introduction

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

Prototype

public Criteria addOrder(Order order);

Source Link

Document

Add an Order ordering to the result set.

Usage

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

License:Open Source License

private void addBooleanFilters(boolean onlyNew, boolean findReplies, boolean onlyFlagged, boolean onlyFeatured,
        boolean findEmails, boolean sortByLastReply, Criteria criteria) {

    if (!findReplies) {
        criteria.add(Restrictions.isNull("parent"));
    }/*  www  .j av  a  2 s. c o m*/

    if (!findEmails) {
        criteria.add(Restrictions.eq("emailed", false));
    }

    if (sortByLastReply) {
        criteria.addOrder(Order.desc("lastReply"));
    } else if (onlyNew) {
        SimpleExpression mainDate = Restrictions.gt("date", currentUser.getLastLogin());
        criteria.add(mainDate);
        criteria.addOrder(Order.asc("date"));
    } else {
        criteria.addOrder(Order.desc("date"));
    }

    if (onlyFlagged) {
        Set<PostModel> flaggedPosts2 = currentUser.getFlaggedPosts();
        if (flaggedPosts2.isEmpty()) {
            criteria.add(Restrictions.eq("id", 0));
        } else {
            Vector<Integer> flaggedIDs = new Vector<Integer>();
            for (PostModel p : flaggedPosts2) {
                flaggedIDs.add(p.getId());
            }

            Criterion flaggedSelf = Restrictions.in("id", flaggedIDs);
            Criterion flaggedParent = Restrictions.in("parent", flaggedPosts2);
            criteria.add(Restrictions.or(flaggedSelf, flaggedParent));
        }
    }

    if (onlyFeatured) {
        criteria.add(Restrictions.eq("featured", true));
    }

}

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

License:Open Source License

private Criteria getListPostsCriteria(GroupLogic theGroup) {
    Criteria criteria = hibernateSession.createCriteria(PostModel.class);

    criteria.add(Restrictions.eq("group", theGroup)); // just posts in this
    // group please
    criteria.add(Restrictions.isNull("parent")); // no replies
    criteria.addOrder(Order.desc("date"));
    return criteria;
}

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

License:Open Source License

private Criteria getUserPostCriteria(UserLogic targetUser) {
    // Find matching posts
    Criteria criteria = hibernateSession.createCriteria(PostModel.class);
    criteria.addOrder(Order.desc("date"));

    criteria.add(Restrictions.eq("poster", targetUser));

    if (!currentUser.isAdmin()) {
        criteria.add(Restrictions.in("group", Permissions.visibleGroups(currentUser, false)));
    }//from ww  w  .  j a v  a  2s  . c o  m
    return criteria;
}

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

License:Open Source License

public List<WhiteboardModel> listPaginatedVisibleWhiteboards(int startPage, int eventsPerPage)
        throws HibernateException {
    Criteria criteria = hibernateSession.createCriteria(WhiteboardModel.class);

    if (!currentUser.isAdmin() || !currentUser.getAdminToggle()) {
        criteria.add(Restrictions.in("group", Permissions.visibleGroups(currentUser, true)));
    }//from w  w  w . ja v  a  2s  . c  om
    criteria.add(Restrictions.ne("numEdits", 0));
    criteria.add(Restrictions.gt("lastEditDate", currentUser.getLastLogin()));
    criteria.addOrder(Order.desc("lastEditDate"));
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    addPagination(startPage, eventsPerPage, criteria);

    return getUniqueWhiteboardList(criteria);
}

From source file:ca.myewb.controllers.mailing.AvailableLists.java

License:Open Source License

public void handle(Context ctx) throws Exception {
    List<GroupModel> currentLists = currentUser.getGroups();
    GroupChapterModel chapter = currentUser.getChapter();
    Criteria crit = null;
    Hashtable<String, List<GroupModel>> hash = new Hashtable<String, List<GroupModel>>();
    List<String> names = new Vector<String>();

    //chapter lists
    crit = hibernateSession.createCriteria(GroupChapterModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("visible", new Boolean(true)));
    crit.addOrder(Order.asc("name"));

    List<GroupModel> chapterLists = (new SafeHibList<GroupModel>(crit)).list();
    chapterLists.removeAll(currentLists);

    //general public lists
    crit = hibernateSession.createCriteria(GroupModel.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.add(Restrictions.eq("admin", new Boolean(false)));
    crit.add(Restrictions.eq("visible", new Boolean(true)));

    crit.add(Restrictions.isNull("parent"));
    crit.add(Restrictions.eq("public", new Boolean(true)));

    List<GroupModel> generalPublicLists = (new SafeHibList<GroupModel>(crit)).list();
    generalPublicLists.removeAll(currentLists);
    generalPublicLists.removeAll(chapterLists);

    log.debug("Populating available lists:");

    if (currentUser.getUsername().equals("guest")) {
        generalPublicLists.add(0, Helpers.getGroup("Org"));
        log.debug("Global list, for the guest");
    }/*from   ww w .j a v a2s.  c  o  m*/

    if (!generalPublicLists.isEmpty()) {
        hash.put("General Public Lists", generalPublicLists);
        names.add("General Public Lists");
        log.debug("General public lists");
    }

    if (currentUser.isMember("Exec")) {
        // admin level lists
        List<GroupModel> adminLists = Helpers.getNationalRepLists(true, true);
        adminLists.add(0, Helpers.getGroup("ProChaptersExec"));
        adminLists.add(0, Helpers.getGroup("UniChaptersExec"));
        adminLists.add(0, Helpers.getGroup("Exec"));

        adminLists.removeAll(currentLists);

        if (!adminLists.isEmpty()) {
            hash.put("Exec and Natl Rep Lists", adminLists);
            names.add("Exec and Natl Rep Lists");
        }
    }

    if (currentUser.isAdmin()) {

        {
            //general private lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(false)));

            List<GroupModel> generalPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
            generalPrivateLists.removeAll(currentLists);

            if (!generalPrivateLists.isEmpty()) {
                hash.put("General Private Lists", generalPrivateLists);
                names.add("General Private Lists");
                log.debug("General private lists");
            } else {
                log.debug("General private lists was empty");
            }
        }

        {
            //all chapter public lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNotNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(true)));
            crit.addOrder(Order.asc("parent"));

            List<GroupModel> chapterPublicLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPublicLists.removeAll(currentLists);

            if (!chapterPublicLists.isEmpty()) {
                hash.put("Chapter Public Lists (any chapter)", chapterPublicLists);
                names.add("Chapter Public Lists (any chapter)");
                log.debug("Chapter public lists for admin");
            } else {
                log.debug("Chapter public lists for admin; empty");
            }
        }

        {
            //all chapter private lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.isNotNull("parent"));
            crit.add(Restrictions.eq("public", new Boolean(false)));
            crit.addOrder(Order.asc("parent"));

            List<GroupModel> chapterPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPrivateLists.removeAll(currentLists);

            if (!chapterPrivateLists.isEmpty()) {
                hash.put("Chapter Private Lists (any chapter)", chapterPrivateLists);
                names.add("Chapter Private Lists (any chapter)");
                log.debug("Chapter private lists, admin");
            } else {
                log.debug("Chapter private lists, admin, empty");
            }
        }
    } else {
        if (chapter != null) {
            //chapter public lists
            crit = hibernateSession.createCriteria(GroupModel.class);
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
            crit.add(Restrictions.eq("admin", new Boolean(false)));
            crit.add(Restrictions.eq("visible", new Boolean(true)));

            crit.add(Restrictions.eq("parent", chapter));
            crit.add(Restrictions.eq("public", new Boolean(true)));

            List<GroupModel> chapterPublicLists = (new SafeHibList<GroupModel>(crit)).list();
            chapterPublicLists.removeAll(currentLists);

            if (!chapterPublicLists.isEmpty()) {
                hash.put("Chapter Public Lists", chapterPublicLists);
                names.add("Chapter Public Lists");
                log.debug("Chapter public lists");
            } else {
                log.debug("Chapter public lists was empty");
            }

            if (currentUser.isLeader(chapter, false)) {
                //own chapter's private lists
                crit = hibernateSession.createCriteria(GroupModel.class);
                crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
                crit.add(Restrictions.eq("admin", new Boolean(false)));
                crit.add(Restrictions.eq("visible", new Boolean(true)));

                crit.add(Restrictions.eq("parent", chapter));
                crit.add(Restrictions.eq("public", new Boolean(false)));

                List<GroupModel> chapterPrivateLists = (new SafeHibList<GroupModel>(crit)).list();
                chapterPrivateLists.removeAll(currentLists);

                if (!chapterPrivateLists.isEmpty()) {
                    hash.put("Chapter Private Lists", chapterPrivateLists);
                    names.add("Chapter Private Lists");
                    log.debug("Chapter private lists");
                } else {
                    log.debug("Chapter private lists was empty");
                }
            }
        }
    }

    if (!chapterLists.isEmpty()) {
        hash.put("Chapter Lists", chapterLists);
        names.add("Chapter Lists");
        log.debug("Chapter lists");
    }

    // Stick it all in the context
    ctx.put("names", names);
    ctx.put("names2", names);
    ctx.put("hash", hash);
}

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<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  .j  a va  2s . c om*/
    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);
}