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:com.abiquo.server.core.infrastructure.management.RasdManagementDAO.java

License:Open Source License

public List<RasdManagement> findDisksAndVolumesByVirtualMachine(final VirtualMachine virtualMachine) {
    Criteria crit = createCriteria();
    crit.createAlias(RasdManagement.RASD_PROPERTY, "rasd");

    // Add disk resource type filter
    crit.add(Restrictions.in(RasdManagement.ID_RESOURCE_TYPE_PROPERTY,
            new String[] { VolumeManagement.DISCRIMINATOR, DiskManagement.DISCRIMINATOR }));

    // Add virtual machine filter
    crit.add(sameVirtualMachine(virtualMachine));

    // Order by generation (attachment order)
    crit.addOrder(Order.asc("sequence"));

    return getResultList(crit);
}

From source file:com.abiquo.server.core.infrastructure.network.VLANNetworkDAO.java

License:Open Source License

public List<VLANNetwork> findVlanNetworks(final VirtualDatacenter virtualDatacenter) {
    assert virtualDatacenter != null;

    Criteria criteria = createCriteria(sameNetwork(virtualDatacenter));
    criteria.addOrder(Order.asc(VLANNetwork.NAME_PROPERTY));
    List<VLANNetwork> result = getResultList(criteria);

    return result;
}

From source file:com.abiquo.server.core.infrastructure.RackDAO.java

License:Open Source License

public List<Rack> findRacks(final Datacenter datacenter, final String filter) {
    assert datacenter != null;
    assert isManaged2(datacenter);

    if (filter != null && !filter.isEmpty()) {
        Query query = getSession().createSQLQuery(QUERY_GET_FILTERED_RACKS);
        query.setParameter("idDatacenter", datacenter.getId());
        query.setString("filter", "%" + filter + "%");

        List<Rack> racks = getSQLQueryResults(getSession(), query, Rack.class, 0);
        return racks;

    }//ww  w  .j  a  v a2s .co  m
    Criteria criteria = createCriteria(sameDatacenter(datacenter));
    criteria.addOrder(Order.asc(Rack.NAME_PROPERTY));
    List<Rack> result = getResultList(criteria);
    return result;
}

From source file:com.abiquo.server.core.infrastructure.RackDAO.java

License:Open Source License

public List<Rack> findRacksWithHAEnabled(final Datacenter datacenter) {
    Criteria criteria = createCriteria(sameDatacenter(datacenter));
    criteria.add(Restrictions.eq(Rack.HAENABLED_PROPERTY, true));
    criteria.addOrder(Order.asc(Rack.NAME_PROPERTY));

    List<Rack> result = getResultList(criteria);

    return result;
}

From source file:com.abiquo.server.core.infrastructure.UcsRackDAO.java

License:Open Source License

public List<UcsRack> findAllUcsRacksByDatacenter(final Datacenter datacenter, final String filter) {
    Criteria criteria = createCriteria(sameDatacenter(datacenter));

    if (filter != null && !filter.isEmpty()) {
        criteria.add(filterBy(filter));//from   w  w w .j  a  va2s .  c o  m
    }

    criteria.addOrder(Order.asc(Rack.NAME_PROPERTY));

    return criteria.list();
}

From source file:com.abiquo.server.core.pricing.PricingTemplateDAO.java

License:Open Source License

private Criteria createCriteria(final String filter, final String orderBy, final boolean desc) {
    Criteria criteria = createCriteria();

    if (!StringUtils.isEmpty(filter)) {
        criteria.add(filterBy(filter));//from  w w  w. j a  v  a 2s .  co  m
    }

    if (!StringUtils.isEmpty(orderBy)) {
        Order order = Order.asc(orderBy);
        if (desc) {
            order = Order.desc(orderBy);
        }
        if (!orderBy.equals(PricingTemplate.NAME_PROPERTY)) {
            criteria.addOrder(Order.asc(PricingTemplate.CURRENCY_PROPERTY));
        }
        criteria.addOrder(order);
        criteria.addOrder(Order.asc(PricingTemplate.NAME_PROPERTY));
    }

    return criteria;
}

From source file:com.abiquo.server.core.pricing.PricingTemplateDAO.java

License:Open Source License

public List<PricingTemplate> findAllPricingTemplateByName(final String name) {
    Criteria criteria = createCriteria(sameName(name));
    criteria.addOrder(Order.asc(PricingTemplate.NAME_PROPERTY));

    return criteria.list();
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ?Criteria,.//from ww  w .j  ava2 s.  c om
 */
protected Criteria setPageParameter(final Criteria c, final Page<T> page) {
    // hibernatefirstResult??0
    if (page.getPageSize() > 0) {
        c.setFirstResult(page.getFirst() - 1);
        c.setMaxResults(page.getPageSize());
    }

    if (page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (Page.ASC.equals(orderArray[i])) {
                c.addOrder(Order.asc(orderByArray[i]));
            } else {
                c.addOrder(Order.desc(orderByArray[i]));
            }
        }
    }
    return c;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

@SuppressWarnings("unchecked")
public Page<T> findPageDynamicFetch(final Page<T> page, final List<PropertyFilter> filters,
        final String... lazyObjects) {
    Criteria criteria = getSession().createCriteria(entityClass);
    Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>();
    for (PropertyFilter filter : filters) {
        if (!MatchType.INS.equals(filter.getMatchType())) {
            if (!filter.isMultiProperty()) {
                String propertyName = filter.getPropertyName();
                Object[] propertyValue = filter.getPropertyValue();
                MatchType matchType = filter.getMatchType();
                Criteria parent = findParentCriteria(criteria, propertyName, criteriaMap);
                String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
                parent.add(getCriterion(tmp[tmp.length - 1], propertyValue, matchType));
            } else {
                Disjunction disjunction = Restrictions.disjunction();
                Object[] propertyValue = filter.getPropertyValue();
                MatchType matchType = filter.getMatchType();
                String[] propertyNames = filter.getPropertyNames();
                for (String propertyName : propertyNames) {
                    // Criteria parent = findParentCriteria(criteria,
                    // propertyName, criteriaMap);
                    String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
                    // parent.add(getCriterion(tmp[tmp.length - 1],
                    // propertyValue, matchType));
                    for (int i = 0; i <= tmp.length - 2; i++) {
                        criteria.createAlias(tmp[i], tmp[i], CriteriaSpecification.LEFT_JOIN);
                    }//  ww  w. j a  v  a 2  s.  c  om
                    // disjunction.ad
                }
                criteria.add(Restrictions.or(
                        Restrictions.like(propertyNames[0], propertyValue[0].toString(), MatchMode.ANYWHERE),
                        Restrictions.like(propertyNames[1], propertyValue[0].toString(), MatchMode.ANYWHERE)));

                // criteria.add(disjunction);
            }
        } else {
            criteria.add(org.hibernate.criterion.Expression.sql("this_." + filter.getPropertyName() + " in "
                    + String.valueOf(filter.getPropertyValue()[0])));
        }
    }
    if (lazyObjects != null) {
        for (int i = 0; i < lazyObjects.length; i++) {
            criteria.setFetchMode(lazyObjects[i], FetchMode.EAGER);
        }
    }
    if (page != null && page.isAutoCount()) {
        int totalCount = countCriteriaResult(criteria);
        page.setTotalCount(totalCount);
    }
    if (page != null && page.getPageSize() > 0) {
        if (page.getTotalPages() < page.getPageNo()) {
            page.setPageNo(1L);
        }
        criteria.setFirstResult(page.getFirst() - 1);
        criteria.setMaxResults(page.getPageSize());
    }
    if (page != null && page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (orderByArray[i].indexOf(".") > 0) {
                // ???
                if (Page.ASC.equals(orderArray[i])) {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                } else {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                }
            } else {
                if (Page.ASC.equals(orderArray[i])) {
                    criteria.addOrder(Order.asc(orderByArray[i]));
                } else {
                    criteria.addOrder(Order.desc(orderByArray[i]));
                }
            }
        }
    }

    List result = criteria.list();
    if (page == null) {
        Page p = new Page<T>();
        p.setResult(result);
        p.setTotalCount(result.size());
        p.setPageNo(1L);
        p.setPageSize(result.size());
        return p;
    }
    page.setResult(result);
    return page;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 *   OR//from  ww  w.j  av  a 2  s .c o  m
 * 
 * @new date 2012-04-23
 * @param page
 * @param filters
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> findPageWithOR(final Page<T> page, final List<PropertyFilter> filters) {
    Criteria criteria = getSession().createCriteria(entityClass);
    Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>();

    for (PropertyFilter filter : filters) {
        if (!filter.isMultiProperty()) {
            String propertyName = filter.getPropertyName();
            createSubCriteria(criteria, propertyName, criteriaMap);
        } else {
            for (String propertyName : filter.getPropertyNames()) {
                createSubCriteria(criteria, propertyName, criteriaMap);
            }
        }
    }

    boundCriterion(criteria, buildPropertyFilterCriterions(filters));

    if (page != null && page.isAutoCount()) {
        int totalCount = countCriteriaResult(criteria);
        page.setTotalCount(totalCount);
    }
    if (page != null && page.getPageSize() > 0) {
        if (page.getTotalPages() < page.getPageNo()) {
            page.setPageNo(1L);
        }
        criteria.setFirstResult(page.getFirst() - 1);
        criteria.setMaxResults(page.getPageSize());
    }
    if (page != null && page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (orderByArray[i].indexOf(".") > 0) {
                // ???
                if (Page.ASC.equals(orderArray[i])) {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                } else {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                }
            } else {
                if (Page.ASC.equals(orderArray[i])) {
                    criteria.addOrder(Order.asc(orderByArray[i]));
                } else {
                    criteria.addOrder(Order.desc(orderByArray[i]));
                }
            }
        }
    }

    List result = criteria.list();
    if (page == null) {
        Page p = new Page<T>();
        p.setResult(result);
        p.setTotalCount(result.size());
        p.setPageNo(1L);
        p.setPageSize(result.size());
        return p;
    }
    page.setResult(result);
    return page;
}