Example usage for org.hibernate.criterion Projections projectionList

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

Introduction

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

Prototype

public static ProjectionList projectionList() 

Source Link

Document

Create a new projection list.

Usage

From source file:net.firejack.platform.core.store.registry.EntityStore.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)//from  w w w .j av a 2 s . c o  m
public List<Object[]> findAllIdAndExtendedId() {
    return getHibernateTemplate().executeFind(new HibernateCallback<List<Object[]>>() {
        @Override
        public List<Object[]> doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(EntityModel.class);
            criteria.add(Restrictions.isNotNull("extendedEntity.id"));
            ProjectionList proList = Projections.projectionList();
            proList.add(Projections.property("id"));
            proList.add(Projections.property("extendedEntity.id"));
            criteria.setProjection(proList);
            return criteria.list();
        }
    });
}

From source file:net.firejack.platform.core.store.registry.RegistryNodeStore.java

License:Apache License

@Override
@Transactional(readOnly = true)/*from   www. java 2  s .  c  o m*/
public List<String> findAllDuplicateNamesByType(String _package, Class<?>... _class) {
    List<String> discriminators = new ArrayList<String>(_class.length);
    for (Class<?> _clas : _class) {
        DiscriminatorValue discriminator = _clas.getAnnotation(DiscriminatorValue.class);
        discriminators.add(discriminator.value());
    }

    Criteria criteria = getSession().createCriteria(getClazz());
    criteria.setProjection(Projections.projectionList().add(Projections.sqlGroupProjection("name as x",
            "x having count(x) > 1", new String[] { "x" }, new Type[] { Hibernate.STRING })));
    criteria.add(Restrictions.like("lookup", _package + ".%"));
    if (getClazz().isAnnotationPresent(DiscriminatorColumn.class) && !discriminators.isEmpty()) {
        criteria.add(Restrictions.in("class", discriminators));
    }
    return criteria.list();
}

From source file:net.firejack.platform.core.store.registry.resource.ResourceStore.java

License:Apache License

@Override
@Transactional//w  w  w.ja  va 2  s . c o m
public Integer setMaxResourceVersion(final R resource) {
    Integer maxVersion = getHibernateTemplate().execute(new HibernateCallback<Integer>() {
        public Integer doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(AbstractResourceVersionModel.class);
            criteria.add(Restrictions.eq("resource.id", resource.getId()));
            criteria.add(Restrictions.le("version", resource.getLastVersion()));

            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.max("version"));
            criteria.setProjection(projectionList);
            return (Integer) criteria.uniqueResult();
        }
    });

    resource.setLastVersion(maxVersion);
    if (resource.getPublishedVersion() != null && resource.getPublishedVersion() > maxVersion) {
        resource.setPublishedVersion(maxVersion);
    }
    update(resource);
    return maxVersion;
}

From source file:net.firejack.platform.core.store.statistics.LogTransactionStore.java

License:Apache License

@Override
@Transactional(readOnly = true)//w ww .j  a v a 2  s.  c  o  m
public List<LogTransactionModel> findAggregatedByTermAndDates(Integer offset, Integer limit, String term,
        String lookup, Date startDate, Date endDate, String sortColumn, String sortDirection,
        MetricGroupLevel level) {
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min("hourPeriod"), "startTime").add(Projections.max("hourPeriod"), "endTime")
            .add(Projections.groupProperty("packageLookup"), "packageLookup")
            .add(Projections.sum("transactions"), "transactions")
            .add(Projections.sum("entitiesLoaded"), "entitiesLoaded")
            .add(Projections.sum("entitiesUpdated"), "entitiesUpdated")
            .add(Projections.sum("entitiesInserted"), "entitiesInserted")
            .add(Projections.sum("entitiesDeleted"), "entitiesDeleted")
            .add(Projections.sum("entitiesFetched"), "entitiesFetched")
            .add(Projections.sum("collectionsLoaded"), "collectionsLoaded")
            .add(Projections.sum("collectionsUpdated"), "collectionsUpdated")
            .add(Projections.sum("collectionsRecreated"), "collectionsRecreated")
            .add(Projections.sum("collectionsRemoved"), "collectionsRemoved")
            .add(Projections.sum("collectionsFetched"), "collectionsFetched")
            .add(Projections.max("maxQueryTime"), "maxQueryTime");

    switch (level) {
    case HOUR:
        projectionList.add(Projections.groupProperty("hourPeriod").as("hourPeriod"));
        break;
    case DAY:
        projectionList.add(Projections.groupProperty("dayPeriod").as("dayPeriod"));
        break;
    case WEEK:
        projectionList.add(Projections.groupProperty("weekPeriod").as("weekPeriod"));
        break;
    case MONTH:
        projectionList.add(Projections.groupProperty("monthPeriod").as("monthPeriod"));
        break;
    }

    return findAllByProjection(offset, limit, term, lookup, startDate, endDate, sortColumn, sortDirection,
            projectionList);
}

From source file:net.firejack.platform.core.store.statistics.LogTransactionStore.java

License:Apache License

@Override
@Transactional(readOnly = true)/*from  ww w. ja v  a2s  . co m*/
public long countAggregatedByTermAndDates(String term, String lookup, Date startDate, Date endDate,
        MetricGroupLevel level) {
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("packageLookup"), "packageLookup");

    switch (level) {
    case HOUR:
        projectionList.add(Projections.groupProperty("hourPeriod").as("hourPeriod"));
        break;
    case DAY:
        projectionList.add(Projections.groupProperty("dayPeriod").as("dayPeriod"));
        break;
    case WEEK:
        projectionList.add(Projections.groupProperty("weekPeriod").as("weekPeriod"));
        break;
    case MONTH:
        projectionList.add(Projections.groupProperty("monthPeriod").as("monthPeriod"));
        break;
    }

    List<Criterion> criterions = createCriterionsForTermAndDates(term, lookup, startDate, endDate);
    return findCountWithFilter(criterions, null, null, projectionList);
}

From source file:net.firejack.platform.core.store.statistics.MetricsEntryStore.java

License:Apache License

@Override
public List<AggregatedMetricsEntryModel> findAggregatedByTermAndDates(Integer offset, Integer limit,
        String term, String lookup, Date startDate, Date endDate, String sortColumn, String sortDirection,
        MetricGroupLevel level, LogEntryType logEntryType) {

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min("hourPeriod"), "startTime").add(Projections.max("hourPeriod"), "endTime")
            .add(Projections.groupProperty("lookup"), "lookup")
            .add(Projections.groupProperty("systemAccountName"), "systemAccountName")
            .add(Projections.groupProperty("username"), "username")
            .add(Projections.sum("numberOfInvocations"), "numberOfInvocations")
            .add(Projections.avg("averageExecutionTime"), "averageExecutionTime")
            .add(Projections.min("minResponseTime"), "minResponseTime")
            .add(Projections.max("maxResponseTime"), "maxResponseTime")
            .add(Projections.avg("successRate"), "successRate");

    switch (level) {
    case HOUR:/*from w  ww.  ja v  a 2 s .co  m*/
        projectionList.add(Projections.groupProperty("hourPeriod").as("hourPeriod"));
        break;
    case DAY:
        projectionList.add(Projections.groupProperty("dayPeriod").as("dayPeriod"));
        break;
    case WEEK:
        projectionList.add(Projections.groupProperty("weekPeriod").as("weekPeriod"));
        break;
    case MONTH:
        projectionList.add(Projections.groupProperty("monthPeriod").as("monthPeriod"));
        break;
    }

    return findAllByProjection(offset, limit, term, lookup, startDate, endDate, sortColumn, sortDirection,
            logEntryType, projectionList);
}

From source file:net.firejack.platform.core.store.statistics.MetricsEntryStore.java

License:Apache License

@Override
@Transactional(readOnly = true)//from  w  w  w  .  ja  v a  2  s .  c o m
public List<AggregatedMetricsEntryModel> findAllByTermAndDates(Integer offset, Integer limit, String term,
        String lookup, Date startDate, Date endDate, String sortColumn, String sortDirection,
        LogEntryType logEntryType) {

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("hourPeriod"), "startTime")
            .add(Projections.property("hourPeriod"), "endTime")
            .add(Projections.property("lookup"), "alias_lookup")
            .add(Projections.property("systemAccountName"), "alias_systemAccountName")
            .add(Projections.property("username"), "alias_username")
            .add(Projections.property("numberOfInvocations"), "numberOfInvocations")
            .add(Projections.property("averageExecutionTime"), "averageExecutionTime")
            .add(Projections.property("minResponseTime"), "minResponseTime")
            .add(Projections.property("maxResponseTime"), "maxResponseTime")
            .add(Projections.property("successRate"), "successRate");

    return findAllByProjection(offset, limit, term, lookup, startDate, endDate, sortColumn, sortDirection,
            logEntryType, projectionList);
}

From source file:net.firejack.platform.core.store.statistics.MetricsEntryStore.java

License:Apache License

public long countAggregatedByTermAndDates(String term, String lookup, Date startDate, Date endDate,
        MetricGroupLevel level, LogEntryType logEntryType) {
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("lookup"), "lookup")
            .add(Projections.groupProperty("systemAccountName"), "systemAccountName")
            .add(Projections.groupProperty("username"), "username");

    switch (level) {
    case HOUR:/*from   w w  w .  j  av a 2  s  .  com*/
        projectionList.add(Projections.groupProperty("hourPeriod").as("hourPeriod"));
        break;
    case DAY:
        projectionList.add(Projections.groupProperty("dayPeriod").as("dayPeriod"));
        break;
    case WEEK:
        projectionList.add(Projections.groupProperty("weekPeriod").as("weekPeriod"));
        break;
    case MONTH:
        projectionList.add(Projections.groupProperty("monthPeriod").as("monthPeriod"));
        break;
    }

    List<Criterion> criterions = createCriterionsForTermAndDates(term, lookup, startDate, endDate,
            logEntryType);
    return findCountWithFilter(criterions, null, null, projectionList);
}

From source file:net.firejack.platform.service.authority.broker.role.ReleaseContextUserRolesBroker.java

License:Apache License

@Override
protected ServiceResponse perform(ServiceRequest<UserRole> request) throws Exception {
    List<UserRole> userRoles = request.getDataList();
    ServiceResponse response;//from   w  w w .  jav  a  2 s.co  m
    if (userRoles == null || userRoles.isEmpty()) {
        response = new ServiceResponse<UserRole>("No user role information specified.", false);
    } else {
        OPFContext context = OPFContext.getContext();
        if (context == null || context.getPrincipal().isGuestPrincipal()) {
            response = new ServiceResponse<UserRole>("Guest user is not authorized to release context roles.",
                    false);
        } else {
            Set<String> lookupList = new HashSet<String>();
            LinkedList<UserRole> userRolesValidated = new LinkedList<UserRole>();
            for (UserRole userRole : userRoles) {
                Role role = userRole.getRole();
                BaseUser user = userRole.getUser();
                if (role != null && StringUtils.isNotBlank(role.getLookup()) && user != null
                        && user.getId() != null && StringUtils.isNotBlank(userRole.getTypeLookup())
                        && (userRole.getModelId() != null || StringUtils.isNotBlank(userRole.getComplexPK()))) {
                    userRolesValidated.add(userRole);
                    lookupList.add(role.getLookup());
                }
            }
            if (!lookupList.isEmpty()) {
                LinkedList<Criterion> restrictions = new LinkedList<Criterion>();
                restrictions.add(Restrictions.in("lookup", lookupList));
                List<Object[]> roles = roleStore.searchWithProjection(restrictions,
                        Projections.projectionList().add(Projections.property("lookup")).add(Projections.id()),
                        null);
                restrictions.clear();
                Map<String, Long> rolesMap = new HashMap<String, Long>();
                for (Object[] roleModelData : roles) {
                    rolesMap.put((String) roleModelData[0], (Long) roleModelData[1]);
                }
                LinkedList<UserRoleModel> userRoleList = new LinkedList<UserRoleModel>();
                for (UserRole userRole : userRolesValidated) {
                    Role role = userRole.getRole();
                    if (role != null && StringUtils.isNotBlank(role.getLookup())) {
                        Long roleId = rolesMap.get(role.getLookup());
                        if (roleId != null) {
                            restrictions.add(Restrictions.eq("user.id", userRole.getUser().getId()));
                            restrictions.add(Restrictions.eq("role.id", roleId));
                            restrictions.add(Restrictions.eq("type", userRole.getTypeLookup()));
                            if (userRole.getModelId() == null) {
                                restrictions.add(Restrictions.eq("externalId", userRole.getComplexPK()));
                            } else {
                                restrictions.add(Restrictions.eq("internalId", userRole.getModelId()));
                            }
                            List<UserRoleModel> foundUserRoles = userRoleStore.search(restrictions, null, null,
                                    false);
                            if (!foundUserRoles.isEmpty()) {
                                userRoleList.add(foundUserRoles.get(0));
                            }
                            restrictions.clear();
                        }
                    }
                }
                userRoleStore.deleteAll(userRoleList);
            }
            response = new ServiceResponse<UserRole>("Success", true);
        }
    }
    return response;
}

From source file:net.firejack.platform.service.registry.broker.entity.ReadDirectChildrenTypesBroker.java

License:Apache License

@Override
protected ServiceResponse<Entity> perform(ServiceRequest<SimpleIdentifier<String>> request) throws Exception {
    String typeLookup = request.getData().getIdentifier();
    ServiceResponse<Entity> response;
    if (StringUtils.isBlank(typeLookup)) {
        response = new ServiceResponse<Entity>("Type Lookup is not specified.", false);
    } else {//from   www .  j a v a 2s  .com
        Map<String, String> aliases = new HashMap<String, String>();
        aliases.put("sourceEntity", "child");
        aliases.put("targetEntity", "target");
        LinkedList<Criterion> restrictions = new LinkedList<Criterion>();
        restrictions.add(Restrictions.eq("relationshipType", RelationshipType.PARENT_CHILD));
        restrictions.add(Restrictions.eq("target.lookup", typeLookup));

        ProjectionList projections = Projections.projectionList().add(Projections.property("child.id"))
                .add(Projections.property("child.lookup"));
        List<Object[]> childrenTypesData = relationshipStore.searchWithProjection(restrictions, projections,
                aliases, null, false);
        List<Entity> childrenTypes = new ArrayList<Entity>(childrenTypesData.size());
        for (Object[] typeData : childrenTypesData) {
            Long entityId = (Long) typeData[0];
            String entityLookup = (String) typeData[1];
            Entity entity = new Entity();
            entity.setId(entityId);
            entity.setLookup(entityLookup);
            childrenTypes.add(entity);
        }
        response = new ServiceResponse<Entity>(childrenTypes, "Success", true);
    }
    return response;
}