Example usage for org.hibernate.criterion DetachedCriteria setProjection

List of usage examples for org.hibernate.criterion DetachedCriteria setProjection

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria setProjection.

Prototype

public DetachedCriteria setProjection(Projection projection) 

Source Link

Document

Set the projection to use.

Usage

From source file:com.jaspersoft.jasperserver.api.logging.access.service.impl.AccessServiceImpl.java

License:Open Source License

public int getAccessEventsCount() {
    DetachedCriteria criteria = DetachedCriteria
            .forClass(persistentClassFactory.getImplementationClass(AccessEvent.class));
    criteria.setProjection(Projections.rowCount());
    return (Integer) getHibernateTemplate().findByCriteria(criteria).get(0);
}

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

License:Open Source License

public boolean resourceExists(ExecutionContext executionContext, FilterCriteria filterCriteria) {
    DetachedCriteria criteria = translateFilterToCriteria(filterCriteria);
    boolean exists;
    if (criteria == null) {
        exists = false;/*  www.  j av  a2  s . c o  m*/
    } else {
        criteria.setProjection(Projections.rowCount());
        criteria.getExecutableCriteria(getSession()).setCacheable(true);
        List countList = getHibernateTemplate().findByCriteria(criteria);
        int count = ((Integer) countList.get(0)).intValue();
        exists = count > 0;
    }
    return exists;
}

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

License:Open Source License

protected boolean resourceExists(RepoFolder folder, String name, Class resourceType) {
    Class persistentClass = resourcePersistentClass(resourceType);
    DetachedCriteria criteria = resourceNameCriteria(persistentClass, folder, name);
    criteria.setProjection(Projections.rowCount());
    criteria.getExecutableCriteria(getSession()).setCacheable(true);
    List countList = getHibernateTemplate().findByCriteria(criteria);
    int count = ((Integer) countList.get(0)).intValue();
    return count > 0;
}

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  . ja  va 2 s  .  co  m

    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 int getFoldersCount(final String parentURI) {
    return (Integer) executeCallback(new DaoCallback() {
        public Object execute() {
            DetachedCriteria criteria = DetachedCriteria.forClass(RepoFolder.class);
            criteria.add(Restrictions.eq("hidden", Boolean.FALSE));
            criteria.setProjection(Projections.rowCount());
            if (parentURI != null && !Folder.SEPARATOR.equals(parentURI)) {
                criteria.add(Restrictions.or(
                        (parentURI.contains("%")) ? Restrictions.eq("URI", parentURI)
                                : Restrictions.like("URI", parentURI),
                        Restrictions.like("URI", parentURI + "/%")));
            }/*from   w ww  .  j a  va 2 s .co m*/
            return new BasicTransformer().transformToCount(getHibernateTemplate().findByCriteria(criteria));
        }
    });
}

From source file:com.jaspersoft.jasperserver.api.metadata.tenant.service.impl.TenantServiceImpl.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
@Transactional(propagation = Propagation.REQUIRED)
public List<String> getAllSubTenantIdList(ExecutionContext context, final String parentTenantId) {
    List<String> subTenantIdList = new ArrayList<String>();

    /* Retrieving parent tenant. */
    RepoTenant parent = getRepoTenant(parentTenantId, false);

    if (parent != null) {
        DetachedCriteria criteria = createSubTenantsCriteria(parent, null, -1);
        criteria.getExecutableCriteria(getSession()).setCacheable(true);
        criteria.setProjection(Projections.property("tenantId"));
        subTenantIdList.addAll(getHibernateTemplate().findByCriteria(criteria));
    }//from w ww  . j a  v a 2 s. c o m

    return subTenantIdList;
}

From source file:com.jaspersoft.jasperserver.api.metadata.tenant.service.impl.TenantServiceImpl.java

License:Open Source License

private int createNumberOfUsersOrRolesCriteria(String tenantId, Class aClass) {
    Integer rowCount = 0;/* w  ww  . j  a  v  a  2s .  c o m*/

    RepoTenant tenant = getRepoTenant(tenantId, false);
    if (tenant != null) {
        DetachedCriteria criteria = DetachedCriteria.forClass(aClass);
        criteria.createAlias("tenant", "t");

        criteria.add(Restrictions.or(Restrictions.eq("t.tenantUri", tenant.getTenantUri()), Restrictions
                .like("t.tenantUri", tenant.getTenantUri().equals("/") ? "/%" : tenant.getTenantUri() + "/%")));

        criteria.setProjection(Projections.count("id"));
        criteria.getExecutableCriteria(getSession()).setCacheable(true);

        List results = getHibernateTemplate().findByCriteria(criteria);
        if (results != null && !results.isEmpty()) {

            rowCount = (Integer) results.get(0);
        }
    }

    return rowCount;
}

From source file:com.jaspersoft.jasperserver.api.metadata.tenant.service.impl.TenantServiceImpl.java

License:Open Source License

public int getSubTenantsCount(ExecutionContext context, String parentTenantId, String text) {
    DetachedCriteria criteria = createSearchTenantsCriteria(parentTenantId, text);

    criteria.setProjection(Projections.rowCount());
    criteria.getExecutableCriteria(getSession()).setCacheable(true);

    List results = getHibernateTemplate().findByCriteria(criteria);

    if (results != null && !results.isEmpty()) {
        return (Integer) results.get(0);
    }//from   w  w w  . j a v  a2  s .  c  o m

    return 0;
}

From source file:com.jaspersoft.jasperserver.api.metadata.tenant.service.impl.TenantServiceImpl.java

License:Open Source License

public Map<String, Integer> getSubTenantsCountMap(List<String> tenantIds) {
    if (tenantIds == null || tenantIds.size() == 0) {
        return Collections.emptyMap();
    }/*w ww.j  a  va2  s.  c o m*/

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

    criteria.createAlias("parent", "p");
    criteria.add(Restrictions.in("p.tenantId", tenantIds));

    criteria.setProjection(Projections.projectionList().add(Projections.rowCount())
            .add(Projections.groupProperty("p.tenantId")));
    criteria.getExecutableCriteria(getSession()).setCacheable(true);

    List results = getHibernateTemplate().findByCriteria(criteria);

    Map<String, Integer> subTenantCounts = new HashMap<String, Integer>(tenantIds.size(), 1);
    if (results != null && results.size() > 0) {
        for (Object result : results) {
            String tenantId = (String) ((Object[]) result)[1];
            Integer count = (Integer) ((Object[]) result)[0];

            subTenantCounts.put(tenantId, count);
        }
    }

    for (String tenantId : tenantIds) {
        if (!subTenantCounts.containsKey(tenantId)) {
            subTenantCounts.put(tenantId, 0);
        }
    }

    return subTenantCounts;
}

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

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public int getTenantVisibleRolesCount(ExecutionContext context, Set tenantIds, String name) {
    DetachedCriteria criteria = createTenantVisibleRolesCriteria(tenantIds, name, false);

    criteria.setProjection(Projections.rowCount());

    List results = getHibernateTemplate().findByCriteria(criteria);

    Integer rowCount = new Integer(0);
    if (results != null && !results.isEmpty()) {
        rowCount = (Integer) results.get(0);
    }//ww w. j av  a 2  s.c o  m

    return rowCount.intValue();
}