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:com.ut.tekir.tender.TenderBrowseBean.java

License:LGPL

@Override
public DetachedCriteria buildCriteria() {
    DetachedCriteria crit = DetachedCriteria.forClass(Tender.class);

    addProjections(crit);/*from w w w .j a va  2 s  . c  o  m*/

    addRestrictions(crit);

    crit.addOrder(Order.desc("serial"));

    return crit;
}

From source file:com.yuga.ygplatform.modules.sys.utils.UserUtils.java

License:Open Source License

public static List<Office> getOfficeList() {
    @SuppressWarnings("unchecked")
    List<Office> officeList = (List<Office>) getCache(CACHE_OFFICE_LIST);
    if (officeList == null) {
        User user = getUser();/*from   w ww.  j  a  va  2 s . c  om*/
        // if (user.isAdmin()){
        // officeList = officeDao.findAllList();
        // }else{
        // officeList = officeDao.findAllChild(user.getOffice().getId(), "%,"+user.getOffice().getId()+",%");
        // }
        DetachedCriteria dc = officeDao.createDetachedCriteria();
        dc.add(dataScopeFilter(user, dc.getAlias(), ""));
        dc.add(Restrictions.eq("delFlag", Office.DEL_FLAG_NORMAL));
        dc.addOrder(Order.asc("code"));
        officeList = officeDao.find(dc);
        putCache(CACHE_OFFICE_LIST, officeList);
    }
    return officeList;
}

From source file:corner.service.tree.TreeService.java

License:Apache License

/**
 * //from   w w  w. ja v  a 2 s  .c o m
 * @param page 
 * 
 * @param clazz class
 * @param depend 
 * @param depth   ?
 */
public List getDepthTree(IPage page, Class clazz, String[] depends, int depth, int left, int right) {
    DetachedCriteria criteria = DetachedCriteria.forClass(clazz);

    if (depth == 1) {
        //         select * from MP_S_ACCOUNT_ITEM_CODE where tree_depth = 1;
        criteria.add(Restrictions.eq(ITreeAdaptor.DEPTH_PRO_NAME, depth));
    } else {
        //         select * from MP_S_ACCOUNT_ITEM_CODE where tree_depth = 2 and tree_left_code > 1 and tree_right_code < 8;
        Criterion leftright = Restrictions.and(Restrictions.gt(ITreeAdaptor.LEFT_PRO_NAME, left),
                Restrictions.lt(ITreeAdaptor.RIGHT_PRO_NAME, right));
        criteria.add(Restrictions.and(Restrictions.eq(ITreeAdaptor.DEPTH_PRO_NAME, depth), leftright));
    }

    //
    if (page instanceof ITreeQueryPage) {
        ((ITreeQueryPage) page).appendCriteria(criteria, depends);
    }

    criteria.addOrder(Order.asc(ITreeAdaptor.LEFT_PRO_NAME));

    return findByCriteria(criteria);
}

From source file:corner.service.tree.TreeService.java

License:Apache License

/**
 * //from  w  w w  . j av a  2  s  .  c  o m
 * 
 * @param clazz
 *            
 */
@SuppressWarnings("unchecked")
public List<? extends ITreeAdaptor> getTree(Class<? extends ITreeAdaptor> clazz) {
    DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
    criteria.addOrder(Order.asc(ITreeAdaptor.LEFT_PRO_NAME));

    return findByCriteria(criteria);
}

From source file:cz.jirutka.rsql.hibernate.RSQL2CriteriaConverterImpl.java

License:Open Source License

@Override
public DetachedCriteria createCriteria(String query, String orderBy, boolean ascending, Class<?> entityClass)
        throws RSQLException {
    assert orderBy != null : "orderBy must not be null!";

    DetachedCriteria result = createCriteria(query, entityClass);

    orderBy = ROOT_ALIAS + '.' + mapper.translate(orderBy, entityClass);
    result.addOrder(ascending ? Order.asc(orderBy) : Order.desc(orderBy));

    return result;
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleGenericDao.java

License:Apache License

@Override
public List<PK> getAllIds() {
    DetachedCriteria criteria = DetachedCriteria.forClass(type);
    criteria.setProjection(Projections.id());
    String idName = getHibernateTemplate().getSessionFactory().getClassMetadata(type)
            .getIdentifierPropertyName();
    criteria.addOrder(Order.asc(idName));
    return (List<PK>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateEscidocRoleDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *
 * @see EscidocRoleDaoInterface #retrieveRoles(java.util.Map, int, int, java.lang.String,
 *      de.escidoc.core.common.util.list.ListSorting)
 *///from w  w w  . j  a v  a2s.co m
@Override
public List<EscidocRole> retrieveRoles(final Map<String, Object> criterias, final int offset,
        final int maxResults, final String orderBy, final ListSorting sorting)
        throws SqlDatabaseSystemException {

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(EscidocRole.class, "r");
    detachedCriteria.add(Restrictions.ne("id", EscidocRole.DEFAULT_USER_ROLE_ID));

    if (criterias != null && !criterias.isEmpty()) {
        // ids
        final Set<String> roleIds = mergeSets((Set<String>) criterias.remove(Constants.DC_IDENTIFIER_URI),
                (Set<String>) criterias.remove(Constants.FILTER_PATH_ID));
        if (roleIds != null && !roleIds.isEmpty()) {
            detachedCriteria.add(Restrictions.in("id", roleIds.toArray()));
        }

        // limited
        final String limited = (String) criterias.remove("limited");
        if (limited != null) {
            if (Boolean.parseBoolean(limited)) {
                detachedCriteria.add(Restrictions.isNotEmpty("scopeDefs"));
            } else {
                detachedCriteria.add(Restrictions.isEmpty("scopeDefs"));
            }
        }

        // granted
        final String granted = (String) criterias.remove("granted");
        if (granted != null) {
            final DetachedCriteria subQuery = DetachedCriteria.forClass(RoleGrant.class, "rg");
            subQuery.setProjection(Projections.rowCount());
            subQuery.add(Restrictions.eqProperty("escidocRole.id", "r.id"));

            if (Boolean.parseBoolean(granted)) {
                detachedCriteria.add(Subqueries.lt(0, subQuery));
            } else {
                detachedCriteria.add(Subqueries.eq(0, subQuery));
            }
        }

        for (final Entry<String, Object[]> stringEntry : criteriaMap.entrySet()) {
            final Object criteriaValue = criterias.remove(stringEntry.getKey());
            if (criteriaValue != null) {
                final Object[] parts = stringEntry.getValue();
                if (parts[0].equals(COMPARE_EQ)) {
                    detachedCriteria.add(Restrictions.eq((String) parts[1], criteriaValue));
                } else {
                    detachedCriteria.add(Restrictions.like((String) parts[1], criteriaValue));
                }
            }
        }
    }

    if (orderBy != null) {
        if (sorting == ListSorting.ASCENDING) {
            detachedCriteria.addOrder(Order.asc(propertiesNamesMap.get(orderBy)));
        } else if (sorting == ListSorting.DESCENDING) {
            detachedCriteria.addOrder(Order.desc(propertiesNamesMap.get(orderBy)));
        }
    }

    if (criterias != null && criterias.isEmpty()) {

        final List<EscidocRole> result;
        try {
            result = getHibernateTemplate().findByCriteria(detachedCriteria, offset, maxResults);
        } catch (final DataAccessException e) {
            throw new SqlDatabaseSystemException(e);
        }

        return result;
    } else {
        // unsupported filter criteria has been found, therefore the result
        // list must be empty.
        return new ArrayList<EscidocRole>(0);
    }
}

From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateUserAccountDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *
 * @see UserAccountDaoInterface #retrieveUserAccounts(java.util.Map, int, int, String, ListSorting)
 *//*ww w. j  a  v  a  2s.  com*/
@Override
public List<UserAccount> retrieveUserAccounts(final Map<String, Object> criterias, final int offset,
        final int maxResults, final String orderBy, final ListSorting sorting)
        throws SqlDatabaseSystemException {

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(UserAccount.class, "user");

    final Map<String, Object> clonedCriterias = new HashMap<String, Object>(criterias);

    // ids
    final Set<String> userAccountIds = mergeSets(
            (Set<String>) clonedCriterias.remove(Constants.DC_IDENTIFIER_URI),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_ID));
    if (userAccountIds != null && !userAccountIds.isEmpty()) {
        detachedCriteria.add(Restrictions.in("id", userAccountIds.toArray()));
    }

    // active flag
    final String active = (String) clonedCriterias.remove(Constants.FILTER_ACTIVE);
    final String active1 = (String) clonedCriterias.remove(Constants.FILTER_PATH_ACTIVE);
    if (active != null) {
        detachedCriteria.add(Restrictions.eq("active", Boolean.valueOf(active)));
    } else if (active1 != null) {
        detachedCriteria.add(Restrictions.eq("active", Boolean.valueOf(active1)));
    }

    for (final Entry<String, Object[]> stringEntry : criteriaMap.entrySet()) {
        if (stringEntry.getKey().equals(Constants.FILTER_ORGANIZATIONAL_UNIT)
                || stringEntry.getKey().equals(Constants.FILTER_PATH_ORGANIZATIONAL_UNIT)) {
            continue;
        }
        final Object criteriaValue = clonedCriterias.remove(stringEntry.getKey());
        if (criteriaValue != null) {
            final Object[] parts = stringEntry.getValue();
            if (parts[0].equals(COMPARE_EQ)) {
                detachedCriteria.add(Restrictions.eq((String) parts[1], criteriaValue));
            } else {
                detachedCriteria.add(Restrictions.like((String) parts[1], criteriaValue));
            }
        }
    }

    // organizational units
    final String organizationalUnit1 = (String) clonedCriterias.remove(Constants.FILTER_ORGANIZATIONAL_UNIT);
    final String organizationalUnit2 = (String) clonedCriterias
            .remove(Constants.FILTER_PATH_ORGANIZATIONAL_UNIT);
    final String organizationalUnit = organizationalUnit1 != null ? organizationalUnit1 : organizationalUnit2;
    if (organizationalUnit != null) {

        final String ouAttributeName = EscidocConfiguration.getInstance()
                .get(EscidocConfiguration.ESCIDOC_CORE_AA_OU_ATTRIBUTE_NAME);

        if (ouAttributeName == null || ouAttributeName.length() == 0) {
            throw new SqlDatabaseSystemException("ou-attribute-name not found in configuration");
        }
        detachedCriteria
                .add(Restrictions.sqlRestriction("this_.id in (" + "select ua.id from aa.user_account ua, "
                        + "aa.user_attribute atts " + "where ua.id = atts.user_id " + "and atts.name = '"
                        + ouAttributeName + "' and atts.value = ?)", organizationalUnit, Hibernate.STRING));
        // detachedCriteria.add(Restrictions.like("ous", StringUtility
        // .concatenateToString("%", organizationalUnit, "|||%")));
    }

    if (orderBy != null) {
        if (sorting == ListSorting.ASCENDING) {
            detachedCriteria.addOrder(Order.asc(propertiesNamesMap.get(orderBy)));
        } else if (sorting == ListSorting.DESCENDING) {
            detachedCriteria.addOrder(Order.desc(propertiesNamesMap.get(orderBy)));
        }
    }

    if (clonedCriterias.isEmpty()) {
        final List<UserAccount> result;

        try {
            result = getHibernateTemplate().findByCriteria(detachedCriteria, offset, maxResults);
        } catch (final DataAccessException e) {
            throw new SqlDatabaseSystemException(e);
        }
        return result;
    } else {
        // unsupported filter criteria has been found, therefore the result
        // list must be empty.
        return new ArrayList<UserAccount>(0);
    }

}

From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateUserAccountDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *
 * @see UserAccountDaoInterface #retrieveGrants(java.util.Map, int, int, String, ListSorting)
 *//*from  www  . j  ava 2s.c  om*/
@Override
public List<RoleGrant> retrieveGrants(final Map<String, HashSet<String>> criterias, final String orderBy,
        final ListSorting sorting) throws SqlDatabaseSystemException {

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(RoleGrant.class, "roleGrant");

    final Map<String, Object> clonedCriterias = new HashMap<String, Object>(criterias);

    // users
    final Set<String> userIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_USER),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_USER_ID));
    Criterion userCriterion = null;
    if (userIds != null && !userIds.isEmpty()) {
        userCriterion = getInRestrictions(userIds, "userId");
    }

    // groups
    final Set<String> groupIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_GROUP),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_GROUP_ID));
    Criterion groupCriterion = null;
    if (groupIds != null && !groupIds.isEmpty()) {
        groupCriterion = getInRestrictions(groupIds, "groupId");
    }

    // concatenate users and groups with OR
    if (userCriterion != null || groupCriterion != null) {
        if (userCriterion == null) {
            detachedCriteria.add(groupCriterion);
        } else if (groupCriterion == null) {
            detachedCriteria.add(userCriterion);
        } else {
            detachedCriteria.add(Restrictions.or(userCriterion, groupCriterion));
        }
    }

    // roles
    final Set<String> roleIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_ROLE),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_ROLE_ID));
    if (roleIds != null && !roleIds.isEmpty()) {
        detachedCriteria.add(getInRestrictions(roleIds, "roleId"));
    }

    // assigned-on
    final Set<String> objectIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_ASSIGNED_ON),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_ASSIGNED_ON_ID));
    if (objectIds != null && !objectIds.isEmpty()) {
        detachedCriteria.add(getInRestrictions(objectIds, "objectId"));
    }

    // created-by
    final Set<String> creatorIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_CREATED_BY),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_CREATED_BY_ID));
    if (creatorIds != null && !creatorIds.isEmpty()) {
        detachedCriteria.add(getInRestrictions(creatorIds, "creatorId"));
    }

    // revoked-by
    final Set<String> revokerIds = mergeSets((Set<String>) clonedCriterias.remove(Constants.FILTER_REVOKED_BY),
            (Set<String>) clonedCriterias.remove(Constants.FILTER_PATH_REVOKED_BY_ID));
    if (revokerIds != null && !revokerIds.isEmpty()) {
        detachedCriteria.add(getInRestrictions(revokerIds, "revokerId"));
    }

    if (orderBy != null) {
        if (sorting == ListSorting.ASCENDING) {
            detachedCriteria.addOrder(Order.asc(grantPropertiesNamesMap.get(orderBy)));
        } else if (sorting == ListSorting.DESCENDING) {
            detachedCriteria.addOrder(Order.desc(grantPropertiesNamesMap.get(orderBy)));
        }
    }

    if (clonedCriterias.isEmpty()) {
        final List<RoleGrant> result;

        try {
            result = getHibernateTemplate().findByCriteria(detachedCriteria);
        } catch (final DataAccessException e) {
            throw new SqlDatabaseSystemException(e);
        }
        return result;
    } else {
        // unsupported filter criteria has been found, therefore the result
        // list must be empty.
        return new ArrayList<RoleGrant>(0);
    }

}

From source file:de.escidoc.core.aa.business.persistence.hibernate.HibernateUserGroupDao.java

License:Open Source License

/**
 * See Interface for functional description.
 *
 * @see UserGroupDaoInterface #retrieveGrants(List)
 */// w w  w .  j  a v  a 2s. c om
@Override
public Map<String, List<RoleGrant>> retrieveCurrentGrants(final List<String> groupIds)
        throws SqlDatabaseSystemException {

    final Map<String, List<RoleGrant>> orderedResult = new HashMap<String, List<RoleGrant>>();

    final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(RoleGrant.class);
    detachedCriteria.add(Restrictions.in("groupId", groupIds.toArray()));
    detachedCriteria.add(Restrictions.isNull("revocationDate"));
    detachedCriteria.addOrder(Order.desc("objectId"));

    final List<RoleGrant> roleGrants;
    try {
        roleGrants = getHibernateTemplate().findByCriteria(detachedCriteria);
    } catch (final DataAccessException e) {
        throw new SqlDatabaseSystemException(e);
    }
    if (roleGrants != null) {
        for (final RoleGrant roleGrant : roleGrants) {
            if (orderedResult.get(roleGrant.getGroupId()) == null) {
                orderedResult.put(roleGrant.getGroupId(), new ArrayList<RoleGrant>());
            }
            orderedResult.get(roleGrant.getGroupId()).add(roleGrant);
        }
    }

    return orderedResult;
}