Example usage for org.hibernate.criterion Projections id

List of usage examples for org.hibernate.criterion Projections id

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections id.

Prototype

public static IdentifierProjection id() 

Source Link

Document

An identifier value projection.

Usage

From source file:com.eucalyptus.tags.TagSupport.java

License:Open Source License

/**
 * Get the tags for the given resources, grouped by ID and ordered for display.
 * //from  w  ww .j  av  a2s.co m
 * @param owner The account for the tags
 * @param identifiers The resource identifiers for the tags
 * @return The tag map with an entry for each requested resource
 */
public Map<String, List<Tag>> getResourceTagMap(final OwnerFullName owner, final Iterable<String> identifiers) {
    final Map<String, List<Tag>> tagMap = Maps.newHashMap();
    for (final String id : identifiers) {
        tagMap.put(id, Lists.<Tag>newArrayList());
    }
    if (!tagMap.isEmpty()) {
        final Tag example = example(owner);
        final DetachedCriteria detachedCriteria = DetachedCriteria.forClass(resourceClass)
                .add(Restrictions.in(resourceClassIdField, Lists.newArrayList(identifiers)))
                .setProjection(Projections.id());
        final Criterion idRestriction = Property.forName(tagClassResourceField).in(detachedCriteria);
        try {
            final List<Tag> tags = Tags.list(example, Predicates.alwaysTrue(), idRestriction,
                    Collections.<String, String>emptyMap());
            for (final Tag tag : tags) {
                tagMap.get(tag.getResourceId()).add(tag);
            }
        } catch (NoSuchMetadataException e) {
            log.error(e, e);
        }
        Ordering<Tag> order = Ordering.natural().onResultOf(Tags.key());
        for (final String id : identifiers) {
            Collections.sort(tagMap.get(id), order);
        }
    }
    return tagMap;
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

License:Open Source License

/**
 * @param <T>//  ww w  .  j  a  v  a2 s.  c o m
 * @param entityManager
 * @param c
 * @param returnType
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> List<T> listUsingCache(final EntityManager entityManager, final Criteria c,
        final Class<T> returnType) {

    final List<T> resultList = new ArrayList<T>();
    final List<Long> idList = c.setProjection(Projections.id()).list();
    for (Long id : idList) {
        resultList.add(entityManager.getReference(returnType, id));
    }
    return resultList;
}

From source file:com.hypersocket.repository.AbstractRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Transactional(readOnly = true)/*from ww  w  .  j a  v a2 s.  c  o  m*/
public <T> List<T> search(Class<T> clz, final String searchColumn, final String searchPattern, final int start,
        final int length, final ColumnSort[] sorting, CriteriaConfiguration... configs) {
    Criteria criteria = createCriteria(clz);
    if (!StringUtils.isEmpty(searchPattern)) {
        criteria.add(Restrictions.ilike(searchColumn, 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.setProjection(Projections.distinct(Projections.id()));

    criteria.setFirstResult(start);
    criteria.setMaxResults(length);

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

    if (ids.isEmpty()) {
        return new ArrayList<T>();
    }

    criteria = createCriteria(clz);

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.in("id", ids));

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

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public List<Long> getResourcesIds(ExecutionContext context, String text, Class type, Class aClass,
        List<SearchFilter> filters, Map<String, SearchFilter> typeSpecificFilters, SearchSorter sorter,
        TransformerFactory transformerFactory, int firstResult, int maxResult) {
    if (transformerFactory == null) {
        throw new IllegalArgumentException("Transformer factory is null.");
    }/*  w  w w  .  j av  a2s  . c o  m*/

    SearchCriteria criteria = getResourcesListCriteria(context, text, type, aClass, filters,
            typeSpecificFilters);

    ProjectionList distinctProperties = Projections.projectionList().add(Projections.id());
    criteria.addProjection(Projections.distinct(distinctProperties));

    if (sorter != null) {
        sorter.applyOrder(type.getName(), context, criteria);
    }

    List list = getHibernateTemplate().findByCriteria(criteria, firstResult, maxResult);

    List<Long> ids = new ArrayList<Long>();
    ResultTransformer transformer = transformerFactory.createTransformer(filters, sorter);
    if (transformer != null) {
        ids.addAll(transformer.transformToIdList(list));
    } else {
        throw new IllegalArgumentException("Result transformer is null.");
    }

    return ids;
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

protected long findResourceId(String uri, Class resourceType) {
    String folderPath = RepositoryUtils.getParentPath(uri);
    if (folderPath == null) {
        throw new JSException("Invalid resource path " + uri);
    }/*w  w w .  j  a v a2  s .  c om*/

    RepoFolder folder = getFolder(folderPath, true);

    String resourceName = RepositoryUtils.getName(uri);
    Class persistentClass = resourcePersistentClass(resourceType);

    DetachedCriteria criteria = resourceNameCriteria(persistentClass, folder, resourceName);
    criteria.setProjection(Projections.id());
    List<Long> idList = getHibernateTemplate().findByCriteria(criteria);
    if (idList == null || idList.isEmpty()) {
        throw new JSException("Resource of type " + resourceType.getName() + " not found at " + uri);
    }

    if (idList.size() > 1) {
        // should not get here, but safer to check
        throw new RuntimeException("Found " + idList.size() + " IDs for resource of type "
                + resourceType.getName() + " at " + uri);
    }

    return idList.get(0);
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public List<ResourceLookup> getResources(ExecutionContext context, SearchCriteriaFactory searchCriteriaFactory,
        List<SearchFilter> filters, SearchSorter sorter, TransformerFactory transformerFactory, int current,
        int max) {
    if (transformerFactory == null) {
        throw new IllegalArgumentException("Transformer factory is null.");
    }//from  w  w  w  . j a v  a 2  s  . co m

    SearchCriteria criteria;
    List<ResourceLookup> result;
    if (queryModificationEvaluator.useFullResource(context)) {
        // we have to get ids only and make additional call, see:
        // https://community.jboss.org/wiki/HibernateFAQ-AdvancedProblems#Hibernate_does_not_return_distinct_results_for_a_query_with_outer_join_fetching_enabled_for_a_collection_even_if_I_use_the_distinct_keyword
        searchCriteriaFactory = searchCriteriaFactory.newFactory(Resource.class.getName());
        criteria = searchCriteriaFactory.create(context, filters);
        criteria.addProjection(Projections.distinct(Projections.id()));
        searchCriteriaFactory.applySorter(context, criteria, sorter);

        List list = getHibernateTemplate().findByCriteria(criteria, current, max);

        List<Long> ids = new ArrayList<Long>();
        ResultTransformer transformer = transformerFactory.createTransformer(filters, sorter);
        if (transformer != null) {
            ids.addAll(transformer.transformToIdList(list));
        } else {
            throw new IllegalArgumentException("Result transformer is null.");
        }

        result = getResourcesByIdList(ids, searchCriteriaFactory);
    } else {
        // just get the resources since no outer joins supposed to be here
        criteria = searchCriteriaFactory.create(context, filters);
        searchCriteriaFactory.applySorter(context, criteria, sorter);

        List<IdedRepoObject> list = getHibernateTemplate().findByCriteria(criteria, current, max);
        result = toLookups(list, true);
    }

    return result;
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

protected List getRoleIdList(Set roleNames) {
    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentRoleClass());

    if (roleNames != null && roleNames.size() > 0) {

        criteria.add(Restrictions.in("roleName", roleNames));
    } else {//from www  .j a va 2  s . c  o  m

        return new ArrayList();
    }

    criteria.setProjection(Projections.id());

    return getHibernateTemplate().findByCriteria(criteria);
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
protected List<Long> getUserRolesIds(RepoUser user) {
    DetachedCriteria criteria = createAssignedRolesCriteria(null, user, null, true);
    criteria.setProjection(Projections.id());

    return (List<Long>) getHibernateTemplate().findByCriteria(criteria);
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

protected DetachedCriteria createUsersWithoutRoleCriteria(String roleName, String userName, boolean order) {

    DetachedCriteria criteria = DetachedCriteria.forClass(getPersistentUserClass());

    DetachedCriteria usersWithRoleCriteria = createUsersWithRoleCriteria(roleName, "");
    usersWithRoleCriteria.setProjection(Projections.id());

    List usersWithRole = getHibernateTemplate().findByCriteria(usersWithRoleCriteria);

    String userNameField = "username";

    //        addTenantCriteria(criteria, tenantIds);
    createSearchByUserNameCriteria(criteria, userName);

    if (usersWithRole != null && usersWithRole.size() > 0) {
        criteria.add(Restrictions.not(Restrictions.in("id", usersWithRole)));
    }/* w w  w .  java  2  s  .c o  m*/

    if (order) {
        criteria.addOrder(Order.asc(userNameField));
    }

    return criteria;
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

protected List getIdByTenantIdSet(Set tenantIds) {
    DetachedCriteria idCriteria = DetachedCriteria.forClass(getPersistentTenantClass());
    idCriteria.add(Restrictions.in("tenantId", tenantIds));
    idCriteria.setProjection(Projections.id());
    return getHibernateTemplate().findByCriteria(idCriteria);
}