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:org.sakaiproject.genericdao.hibernate.HibernateBasicGenericDao.java

License:Apache License

/**
 * Build the Criteria object here to reduce code duplication
 * @param entityClass//from   w  w  w  . j  a v  a 2 s  . c  o  m
 * @param search a Search object (possibly only partially complete)
 * @return a DetachedCriteria object
 */
private DetachedCriteria buildCriteria(Class<?> entityClass, Search search) {
    // Checks to see if the required params are set and throws exception if not
    if (search == null) {
        throw new IllegalArgumentException("search cannot be null");
    }

    // Build the criteria object
    DetachedCriteria criteria = DetachedCriteria.forClass(entityClass);

    // Only add in restrictions if there are some to add
    if (search.getRestrictions() != null && search.getRestrictions().length > 0) {
        Junction junction = Expression.conjunction(); // AND
        if (!search.conjunction) {
            // set to use disjunction
            junction = Expression.disjunction(); // OR
        }
        criteria.add(junction);

        // put in the restrictions
        for (int i = 0; i < search.getRestrictions().length; i++) {
            String property = search.getRestrictions()[i].property;
            Object value = search.getRestrictions()[i].value;
            if (property == null || value == null) {
                throw new IllegalArgumentException("restrictions property and value cannot be null or empty");
            }
            if (value.getClass().isArray()) {
                // special handling for "in" type comparisons
                Object[] objectArray = (Object[]) value;
                if (objectArray.length == 1) {
                    value = objectArray[0];
                } else if (objectArray.length > 1) {
                    if (Restriction.NOT_EQUALS == search.getRestrictions()[i].comparison) {
                        junction.add(Restrictions.not(Restrictions.in(property, objectArray)));
                    } else {
                        junction.add(Restrictions.in(property, objectArray));
                    }
                } else {
                    // do nothing for now, this is slightly invalid but not worth dying over
                }
            }

            if (!value.getClass().isArray()) {
                switch (search.getRestrictions()[i].comparison) {
                case Restriction.EQUALS:
                    junction.add(Restrictions.eq(property, value));
                    break;
                case Restriction.GREATER:
                    junction.add(Restrictions.gt(property, value));
                    break;
                case Restriction.LESS:
                    junction.add(Restrictions.lt(property, value));
                    break;
                case Restriction.LIKE:
                    junction.add(Restrictions.like(property, value));
                    break;
                case Restriction.NULL:
                    junction.add(Restrictions.isNull(property));
                    break;
                case Restriction.NOT_NULL:
                    junction.add(Restrictions.isNotNull(property));
                    break;
                case Restriction.NOT_EQUALS:
                    junction.add(Restrictions.ne(property, value));
                    break;
                }
            }
        }
    }

    // handle the sorting (sort param can be null for no sort)
    if (search.getOrders() != null) {
        for (int i = 0; i < search.getOrders().length; i++) {
            if (search.getOrders()[i].ascending) {
                criteria.addOrder(org.hibernate.criterion.Order.asc(search.getOrders()[i].property));
            } else {
                criteria.addOrder(org.hibernate.criterion.Order.desc(search.getOrders()[i].property));
            }
        }
    }

    return criteria;
}

From source file:org.sakaiproject.lessonbuildertool.model.SimplePageToolDaoImpl.java

License:Educational Community License

public int getEditarDominioAshyi() {
    DetachedCriteria d = DetachedCriteria.forClass(Control.class);//.add(Restrictions.eq("toolId", toolId)).add(Restrictions.isNull("parent"));

    d.addOrder(Order.asc("idcontrol_ashyi"));
    List<Control> list = getHibernateTemplate().findByCriteria(d);

    if (list.size() > 0) {

        if (list.get(list.size() - 1).getEditar_dominio() == 1)
            return 1;
    } else if (list.size() == 0) {
        return 1;
    }//from w  w w . j a v a 2s .  c o m
    return 0;
}

From source file:org.shept.persistence.provider.hibernate.HibernateCriteriaFilter.java

License:Apache License

/**
 * @return a default criteria definition by simply return an empty entity object
 * @see http://stackoverflow.com/questions/1926618/hibernate-sort-by-properties-of-inner-bean
 * /*from  w  w  w.j  a  v  a  2s.  co  m*/
 */
public DetachedCriteria getCriteria(SortDefinition sortDefinition) {
    SortDefinition sd = defaultSortDefinition;
    DetachedCriteria crit = DetachedCriteria.forClass(getEntityClass());
    // set sort criteria from FormFilter
    if (null != sortDefinition && StringUtils.hasText(sortDefinition.getProperty())) {
        sd = sortDefinition;
    }
    if (null != sd && StringUtils.hasText(sd.getProperty())) {
        String prop = sd.getProperty();
        String[] pathArr = StringUtils.split(prop, ".");
        if (pathArr == null) {
            pathArr = new String[] { prop };
        }
        if (pathArr.length > 2) {
            throw new UnsupportedOperationException(
                    "Sort Criteria Definition '" + prop + "' may only nest one level deep");
        }
        if (pathArr.length == 2) {
            crit.createAlias(pathArr[0], pathArr[0]);
        }
        if (sortDefinition.isAscending())
            crit.addOrder(Order.asc(prop));
        else
            crit.addOrder(Order.desc(prop));
    }
    return crit;
}

From source file:org.sipfoundry.sipxconfig.common.SipxHibernateDaoSupport.java

License:Contributor Agreement License

public List<T> loadBeansByPage(Class beanClass, Integer groupId, Integer branchId, int firstRow, int pageSize,
        String[] orderBy, boolean orderAscending) {
    DetachedCriteria c = DetachedCriteria.forClass(beanClass);
    addByGroupCriteria(c, groupId);//from  ww w .j  a  v a 2  s  .com
    addByBranchCriteria(c, branchId);
    if (orderBy != null) {
        for (String o : orderBy) {
            Order order = orderAscending ? Order.asc(o) : Order.desc(o);
            c.addOrder(order);
        }
    }
    return getHibernateTemplate().findByCriteria(c, firstRow, pageSize);
}

From source file:org.sipfoundry.sipxconfig.phone.PhoneContextImpl.java

License:Contributor Agreement License

public List<Phone> loadPhonesWithNoLinesByPage(int firstRow, int pageSize, String[] orderBy,
        boolean orderAscending) {
    DetachedCriteria c = DetachedCriteria.forClass(Phone.class);
    addByNoLinesCriteria(c);/*from  ww w  . ja va  2  s.  c o m*/
    if (orderBy != null) {
        for (String o : orderBy) {
            Order order = orderAscending ? Order.asc(o) : Order.desc(o);
            c.addOrder(order);
        }
    }
    return getHibernateTemplate().findByCriteria(c, firstRow, pageSize);
}

From source file:org.squashtest.tm.internal.domain.report.common.hibernate.HibernateExecutionProgressQuery.java

License:Open Source License

@Override
public DetachedCriteria createHibernateQuery() {

    // basic request, unfiltered.
    DetachedCriteria criteria = DetachedCriteria.forClass(Campaign.class, "campaigns");

    // adds the criteria
    Collection<String> params = getCriterionNames();

    for (String name : params) {
        criteria = addCriterion(criteria, name);
    }/*from  w w  w  .j  a v  a 2 s. c o m*/

    // no matter what, order the query result.
    criteria.addOrder(Order.asc("campaigns.name"));

    // return the query
    return criteria;

}

From source file:org.web4thejob.orm.DataReaderServiceImpl.java

License:Open Source License

private DetachedCriteria toDetachedCriteria(Query query, String alias) {
    boolean hasOneToManyAssociation = false;
    DetachedCriteria detachedCriteria;
    if (alias != null) {
        detachedCriteria = DetachedCriteria.forClass(query.getTargetType(), alias);
    } else {//from  w ww .ja  va 2s  . c o  m
        detachedCriteria = DetachedCriteria.forClass(query.getTargetType());
    }

    final Map<String, String> aliases = new HashMap<String, String>();
    for (final Criterion w4tjCriterion : query.getCriteria()) {
        if (w4tjCriterion.getCondition() != null
                && (w4tjCriterion.getCondition().getOperandsNo() == 0 || (w4tjCriterion.getValue() != null
                        && StringUtils.hasText(w4tjCriterion.getValue().toString())))) {
            if (!hasOneToManyAssociation) {
                hasOneToManyAssociation = w4tjCriterion.getPropertyPath().hasOneToManySteps();
            }

            org.hibernate.criterion.Criterion hibCriterion;

            if (w4tjCriterion.isLocal()) {
                hibCriterion = toHibernateCriterion(w4tjCriterion, detachedCriteria.getAlias());
            } else {
                String aliasPath = null;
                for (final PropertyMetadata propertyMetadata : w4tjCriterion.getPropertyPath().getSteps()) {
                    if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getLastStep())) {
                        break;
                    } else if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getFirstStep())) {
                        aliasPath = propertyMetadata.getName();
                    } else {
                        aliasPath += "." + propertyMetadata.getName();
                    }

                    buildAlias(detachedCriteria, aliases, aliasPath);
                }

                hibCriterion = toHibernateCriterion(w4tjCriterion, aliases.get(aliasPath));
            }

            detachedCriteria = detachedCriteria.add(hibCriterion);
        }
    }

    if (!query.getSubqueries().isEmpty()) {
        String masterId = detachedCriteria.getAlias() + "."
                + ContextUtil.getMRS().getEntityMetadata(query.getTargetType()).getIdentifierName();
        int subqindex = 0;
        for (Subquery subquery : query.getSubqueries()) {
            subqindex += 1;
            detachedCriteria = detachedCriteria
                    .add(toHibernateSubcriterion(masterId, String.valueOf(subqindex), subquery));
        }
    }

    for (final OrderBy orderBy : query.getOrderings()) {
        PathMetadata pathMetadata = ContextUtil.getMRS().getPropertyPath(query.getTargetType(),
                StringUtils.delimitedListToStringArray(orderBy.getProperty(), Path.DELIMITER));

        String property;
        if (pathMetadata.isMultiStep()) {
            String aliasPath = null;

            if (!hasOneToManyAssociation) {
                hasOneToManyAssociation = pathMetadata.hasOneToManySteps();
            }

            for (PropertyMetadata propertyMetadata : pathMetadata.getSteps()) {
                if (propertyMetadata.equals(pathMetadata.getLastStep())) {
                    break;
                } else if (propertyMetadata.equals(pathMetadata.getFirstStep())) {
                    aliasPath = propertyMetadata.getName();
                } else {
                    aliasPath += "." + propertyMetadata.getName();
                }

                buildAlias(detachedCriteria, aliases, aliasPath);
            }
            property = aliases.get(aliasPath) + "." + pathMetadata.getLastStep().getName();
        } else {
            property = orderBy.getProperty();
        }

        if (orderBy.isDescending()) {
            detachedCriteria = detachedCriteria.addOrder(Order.desc(property));
        } else {
            detachedCriteria = detachedCriteria.addOrder(Order.asc(property));
        }
    }

    if (hasOneToManyAssociation) {
        detachedCriteria = detachedCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    }

    return detachedCriteria;
}

From source file:pe.gob.mef.gescon.hibernate.impl.ArchivoConocimientoDaoImpl.java

@Override
public List<TarchivoConocimiento> getTarchivosByTconocimiento(BigDecimal nconocimientoid) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(TarchivoConocimiento.class);
    criteria.add(Restrictions.eq("nconocimientoid", nconocimientoid));
    criteria.addOrder(Order.asc("nversion"));
    return (List<TarchivoConocimiento>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:pe.gob.mef.gescon.hibernate.impl.ArchivoDaoImpl.java

@Override
public List<Tarchivo> getTarchivosByTbaselegal(Tbaselegal tbaselegal) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(Tarchivo.class);
    criteria.add(Restrictions.eq("tbaselegal.nbaselegalid", tbaselegal.getNbaselegalid()));
    criteria.addOrder(Order.asc("nversion"));
    return (List<Tarchivo>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:pe.gob.mef.gescon.hibernate.impl.ArchivoHistorialDaoImpl.java

@Override
public List<TarchivoHist> getTarchivosHistByTbaselegalHist(TbaselegalHist tbaselegal) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(TarchivoHist.class);
    criteria.add(Restrictions.eq("nhistorialid", tbaselegal.getNhistorialid()));
    criteria.add(Restrictions.eq("nbaselegalid", tbaselegal.getNbaselegalid()));
    criteria.addOrder(Order.asc("nversion"));
    return (List<TarchivoHist>) getHibernateTemplate().findByCriteria(criteria);
}