Example usage for org.hibernate.criterion DetachedCriteria addOrder

List of usage examples for org.hibernate.criterion DetachedCriteria addOrder

Introduction

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

Prototype

public DetachedCriteria addOrder(Order order) 

Source Link

Document

Adds an ordering

Usage

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateOrganizacionDaoImpl.java

License:Apache License

@Override
public Collection<Organizacion> findByProvincia(Provincia provincia) throws DaoException {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Organizacion with Provincia:" + provincia);
    DetachedCriteria criteria = DetachedCriteria.forClass(Organizacion.class);
    criteria.add(Restrictions.eq("provincia", provincia));
    criteria.addOrder(Order.asc("name"));
    return findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateOrganizacionDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from w  ww. j av  a  2  s. c o  m*/
public List<Organizacion> getLatest(Integer count) throws DaoException {
    DetachedCriteria criteria = DetachedCriteria.forClass(Organizacion.class);
    criteria.addOrder(Order.desc("id"));
    return (List<Organizacion>) getHibernateTemplate().findByCriteria(criteria, 0, count);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateOrganizacionSedeDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override// w  w  w. j av a 2  s .com
public Collection<OrganizacionSede> findSedeByAddressLocalityProvince(String address, String locality,
        Provincia provincia) throws DaoException {
    DetachedCriteria criteria = DetachedCriteria.forClass(OrganizacionSede.class);
    criteria.add(Restrictions.eq("direccion", address));
    criteria.add(Restrictions.eq("localidad", locality));
    criteria.add(Restrictions.eq("provincia", provincia));
    criteria.addOrder(Order.asc("id"));
    return (List<OrganizacionSede>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateProvinciaDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override// ww  w .j  a va 2 s  .c  om
public List<Provincia> findByName(String name) throws DaoException {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Provincia with name:" + name);
    DetachedCriteria criteria = DetachedCriteria.forClass(Provincia.class);
    criteria.add(Restrictions.like("name", "%" + name + "%"));
    criteria.addOrder(Order.asc("name"));
    return (List<Provincia>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateSateliteDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//  www. jav a  2  s. co  m
public Collection<Satelite> findSatelite(String name) {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Satelites with name:" + name);
    DetachedCriteria criteria = DetachedCriteria.forClass(Satelite.class);
    criteria.add(Restrictions.like("name", "%" + name + "%"));
    criteria.add(Restrictions.like("hostUrl", "%" + name + "%"));
    criteria.add(Restrictions.like("user", "%" + name + "%"));
    criteria.addOrder(Order.asc("user"));
    return (List<Satelite>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateSateliteDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from w  w  w .  j a v  a 2  s  .  c  om*/
public Collection<Satelite> findActivado() throws DaoException {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Active Satelites");
    DetachedCriteria criteria = DetachedCriteria.forClass(Satelite.class);
    criteria.add(Restrictions.eq("activado", Boolean.TRUE));
    criteria.addOrder(Order.asc("user"));
    return (List<Satelite>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateSectorDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from  w  w  w.j  av  a 2  s  . c  o  m*/
public Collection<Sector> findByName(String name) throws DaoException {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Sector with name:" + name);
    DetachedCriteria criteria = DetachedCriteria.forClass(Sector.class);
    criteria.add(Restrictions.like("name", "%" + name + "%"));
    criteria.addOrder(Order.asc("name"));
    return (List<Sector>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:net.neurowork.cenatic.centraldir.dao.impl.HibernateUserDaoImpl.java

License:Apache License

@SuppressWarnings("unchecked")
public Collection<User> findUsers(String username) throws DaoException {
    if (logger.isTraceEnabled())
        logger.trace("Hibernate Searching Users with username:" + username);
    DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
    criteria.add(Restrictions.like("username", "%" + username + "%"));
    criteria.addOrder(Order.asc("username"));
    return (List<User>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:org.ambraproject.annotation.service.AnnotationServiceImpl.java

License:Apache License

@Override
@Transactional(readOnly = true)//from   w  ww .  ja  v a  2s .  c o m
public AnnotationView[] listAnnotations(final Long articleID, final Set<AnnotationType> annotationTypes,
        final AnnotationOrder order) {
    //Basic criteria
    DetachedCriteria criteria = DetachedCriteria.forClass(Annotation.class)
            .add(Restrictions.eq("articleID", articleID)).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    //restrict by type
    if (annotationTypes != null && !annotationTypes.isEmpty()) {
        criteria.add(Restrictions.in("type", annotationTypes));
    }
    switch (order) {
    case OLDEST_TO_NEWEST:
        criteria.addOrder(Order.asc("created"));
        break;
    case MOST_RECENT_REPLY:
        //Still going to have to sort the results after creating views, because 'Most Recent Reply' isn't something that's stored on the database level
        //but ordering newest to oldest makes it more likely that the annotations will be in close to the correct order by the time we sort
        criteria.addOrder(Order.desc("created"));
        break;
    }
    List annotationResults = hibernateTemplate.findByCriteria(criteria);

    //Don't want to call buildAnnotationView() here because that would involve loading up the reply map for each annotation,
    // when we only need to do it once. So load up the info we need to build annotation views here
    Object[] articleTitleAndDoi;
    try {
        articleTitleAndDoi = (Object[]) hibernateTemplate
                .findByCriteria(DetachedCriteria.forClass(Article.class).add(Restrictions.eq("ID", articleID))
                        .setProjection(Projections.projectionList().add(Projections.property("doi"))
                                .add(Projections.property("title"))),
                        0, 1)
                .get(0);

    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("article " + articleID + " didn't exist");
    }
    String articleDoi = (String) articleTitleAndDoi[0];
    String articleTitle = (String) articleTitleAndDoi[1];

    Map<Long, List<Annotation>> replyMap = buildReplyMap(articleID);

    List<AnnotationView> viewResults = new ArrayList<AnnotationView>(annotationResults.size());

    for (Object annotation : annotationResults) {
        viewResults.add(new AnnotationView((Annotation) annotation, articleDoi, articleTitle, replyMap));
    }

    if (order == AnnotationOrder.MOST_RECENT_REPLY) {
        //Order the results by the most recent reply date
        Collections.sort(viewResults, new Comparator<AnnotationView>() {
            @Override
            public int compare(AnnotationView view1, AnnotationView view2) {
                return -1 * view1.getLastReplyDate().compareTo(view2.getLastReplyDate());
            }
        });
    }

    return viewResults.toArray(new AnnotationView[viewResults.size()]);
}

From source file:org.ambraproject.annotation.service.AnnotationServiceImpl.java

License:Apache License

@Override
@Transactional(readOnly = true)// w  w  w  .  j  av a 2s.c  om
public AnnotationView[] listAnnotationsNoReplies(final Long articleID,
        final Set<AnnotationType> annotationTypes, final AnnotationOrder order) {
    if (order == AnnotationOrder.MOST_RECENT_REPLY) {
        throw new IllegalArgumentException(
                "Cannot specify Most Recent Reply order type when replies are not being loaded up");
    }
    //Basic criteria
    DetachedCriteria criteria = DetachedCriteria.forClass(Annotation.class)
            .add(Restrictions.eq("articleID", articleID)).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    //restrict by type
    if (annotationTypes != null && !annotationTypes.isEmpty()) {
        criteria.add(Restrictions.in("type", annotationTypes));
    }
    switch (order) {
    case OLDEST_TO_NEWEST:
        criteria.addOrder(Order.asc("created"));
        break;
    }
    List annotationResults = hibernateTemplate.findByCriteria(criteria);
    //Don't want to call buildAnnotationView() here because that would involve finding the article title and doi for each annotation,
    // when we only need to do it once. So load up the info we need to build annotation views here
    Object[] articleTitleAndDoi;
    try {
        articleTitleAndDoi = (Object[]) hibernateTemplate
                .findByCriteria(DetachedCriteria.forClass(Article.class).add(Restrictions.eq("ID", articleID))
                        .setProjection(Projections.projectionList().add(Projections.property("doi"))
                                .add(Projections.property("title"))),
                        0, 1)
                .get(0);

    } catch (IndexOutOfBoundsException e) {
        throw new IllegalArgumentException("article " + articleID + " didn't exist");
    }
    String articleDoi = (String) articleTitleAndDoi[0];
    String articleTitle = (String) articleTitleAndDoi[1];

    List<AnnotationView> viewResults = new ArrayList<AnnotationView>(annotationResults.size());
    for (Object annotation : annotationResults) {
        viewResults.add(new AnnotationView((Annotation) annotation, articleDoi, articleTitle, null));
    }
    return viewResults.toArray(new AnnotationView[viewResults.size()]);
}