Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w  w  w.  ja v a  2 s  .  com*/
public Collection<T> searchAssignedResources(List<Principal> principals, final String searchPattern,
        final int start, final int length, final ColumnSort[] sorting, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    Set<T> everyone = new HashSet<T>(criteria.list());

    criteria = createCriteria(getResourceClass());

    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("id"));
    projList.add(Projections.property("name"));

    criteria.setProjection(Projections.distinct(projList));
    criteria.setFirstResult(start);
    criteria.setMaxResults(length);

    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    for (ColumnSort sort : sorting) {
        criteria.addOrder(sort.getSort() == Sort.ASC ? Order.asc(sort.getColumn().getColumnName())
                : Order.desc(sort.getColumn().getColumnName()));
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));
    criteria = criteria.createCriteria("principals");

    List<Long> ids = new ArrayList<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }
    criteria.add(Restrictions.in("id", ids));

    List<Object[]> results = (List<Object[]>) criteria.list();

    if (results.size() > 0) {
        Long[] entityIds = new Long[results.size()];
        int idx = 0;
        for (Object[] obj : results) {
            entityIds[idx++] = (Long) obj[0];
        }

        criteria = createCriteria(getResourceClass());
        criteria.add(Restrictions.in("id", entityIds));

        everyone.addAll((List<T>) criteria.list());
    }
    return everyone;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public boolean hasAssignedEveryoneRole(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.property("id"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);//  www .  ja  v  a 2s  .  c o  m
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    List<?> everyoneRoles = criteria.list();

    return everyoneRoles.size() > 0;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  w  w .ja  va2 s  .com
public Collection<Principal> getAssignedPrincipals(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));

    criteria = criteria.createCriteria("principals");
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    List<?> uniquePrincipals = criteria.list();

    if (uniquePrincipals.isEmpty()) {
        return new HashSet<Principal>();
    }
    criteria = createCriteria(Principal.class);
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.in("id", uniquePrincipals));
    List<?> res = criteria.list();

    return (Collection<Principal>) res;
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public Long getAssignedPrincipalCount(Realm realm, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);//from  ww  w  .  j av  a 2s.com
    }

    criteria.add(Restrictions.eq("realm", realm));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));

    criteria = criteria.createCriteria("principals");
    criteria.setProjection(Projections.distinct(Projections.property("id")));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);

    List<?> uniquePrincipals = criteria.list();

    return (long) uniquePrincipals.size();

}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
public Long getAssignedResourceCount(List<Principal> principals, final String searchPattern,
        CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.property("id"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);
    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }/*ww  w  .j  av  a2  s.co m*/

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", true));

    List<?> ids = criteria.list();

    criteria = createCriteria(getResourceClass());
    criteria.setProjection(Projections.countDistinct("name"));
    criteria.setResultTransformer(CriteriaSpecification.PROJECTION);
    if (StringUtils.isNotBlank(searchPattern)) {
        criteria.add(Restrictions.ilike("name", searchPattern));
    }

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);
    }

    criteria.add(Restrictions.eq("realm", principals.get(0).getRealm()));
    if (ids.size() > 0) {
        criteria.add(Restrictions.not(Restrictions.in("id", ids)));
    }

    criteria = criteria.createCriteria("roles");
    criteria.add(Restrictions.eq("allUsers", false));
    criteria = criteria.createCriteria("principals");
    List<Long> pids = new ArrayList<Long>();
    for (Principal p : principals) {
        pids.add(p.getId());
    }
    criteria.add(Restrictions.in("id", pids));

    Long count = (Long) criteria.uniqueResult();
    return count + ids.size();

}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w w  w. j  a v  a2 s  . co m*/
public List<AssignableResource> getAllAssignableResources(List<Principal> principals) {

    Set<Long> ids = new HashSet<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }
    Criteria crit = createCriteria(AssignableResource.class);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit = crit.createCriteria("roles");
    crit = crit.createCriteria("principals");
    crit.add(Restrictions.in("id", ids));

    return (List<AssignableResource>) crit.list();
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w w.  j ava 2s.  c  om*/
public T getResourceByIdAndPrincipals(Long resourceId, List<Principal> principals) {

    Set<Long> ids = new HashSet<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }
    Criteria crit = createCriteria(getResourceClass());
    crit.add(Restrictions.eq("id", resourceId));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit = crit.createCriteria("roles");
    crit = crit.createCriteria("principals");
    crit.add(Restrictions.in("id", ids));

    return (T) crit.uniqueResult();
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*www  .  j  ava 2  s  .c  o  m*/
public List<T> getResources(Realm realm) {

    Criteria crit = createCriteria(getResourceClass());
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.setFetchMode("roles", FetchMode.SELECT);
    crit.add(Restrictions.eq("deleted", false));
    crit.add(Restrictions.eq("realm", realm));

    return (List<T>) crit.list();
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public List<T> allResources() {

    Criteria crit = createCriteria(getResourceClass());
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.setFetchMode("roles", FetchMode.SELECT);
    crit.add(Restrictions.eq("deleted", false));

    return (List<T>) crit.list();
}

From source file:com.hypersocket.session.SessionRepositoryImpl.java

License:Open Source License

@Override
public Long getActiveSessionCount(boolean distinctUsers) {
    Criteria criteria = createCriteria(Session.class);

    criteria.add(Restrictions.isNull("signedOut"));
    criteria.add(Restrictions.eq("system", false));

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (distinctUsers) {
        criteria.setProjection(Projections.countDistinct("principal"));
    } else {//from w w  w  .  j av a  2s . co m
        criteria.setProjection(Projections.rowCount());
    }
    return (long) criteria.uniqueResult();
}