Example usage for org.hibernate.criterion Projections min

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

Introduction

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

Prototype

public static AggregateProjection min(String propertyName) 

Source Link

Document

A property minimum value projection

Usage

From source file:ar.com.zauber.commons.repository.aggregate.ProjectionAggregateFunctionVisitor.java

License:Apache License

/** crea projecciones en base a {@link PropertyAggregateFunction}. */
private static Projection createPropertyProjection(final PropertyAggregateFunction paf) {
    final String propertyName = paf.getProperty();
    final Projection projection;

    if (paf instanceof AveragePropertyAggregateFunction) {
        projection = Projections.avg(propertyName);
    } else if (paf instanceof CountDistinctPropertyAggregateFunction) {
        projection = Projections.countDistinct(propertyName);
    } else if (paf instanceof CountPropertyAggregateFunction) {
        projection = Projections.count(propertyName);
    } else if (paf instanceof MaxPropertyAggregateFunction) {
        projection = Projections.max(propertyName);
    } else if (paf instanceof MinPropertyAggregateFunction) {
        projection = Projections.min(propertyName);
    } else if (paf instanceof SumPropertyAggregateFunction) {
        projection = Projections.sum(propertyName);
    } else if (paf instanceof GroupPropertyAggregateFilter) {
        projection = Projections.groupProperty(propertyName);
    } else {//from   ww w. j a v a2  s . co m
        throw new IllegalArgumentException("don't know how to process " + paf.getClass());
    }

    return projection;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<BiospecimenSummaryDataRow> getBiospecimenSummaryData(
        BiospecimenSummaryReportVO biospecimenSummaryReportVO) {
    List<BiospecimenSummaryDataRow> results = new ArrayList<BiospecimenSummaryDataRow>();

    Criteria criteria = getSession().createCriteria(BioTransaction.class, "bt");

    criteria.createAlias("status", "bts", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("biospecimen", "bs", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.study", "st", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.linkSubjectStudy", "lss", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.sampleType", "sat", JoinType.LEFT_OUTER_JOIN);

    criteria.add(Restrictions.eq("st.id", biospecimenSummaryReportVO.getStudy().getId()));
    if (biospecimenSummaryReportVO.getSubjectUID() != null) {
        criteria.add(Restrictions.eq("lss.subjectUID", biospecimenSummaryReportVO.getSubjectUID()));
    }/*from  w  w  w .j a va  2s  .co  m*/

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min("bt.id"));
    projectionList.add(Projections.groupProperty("bs.id"));

    projectionList.add(Projections.property("st.name"), "studyName");
    projectionList.add(Projections.property("lss.subjectUID"), "subjectUId");
    projectionList.add(Projections.property("bs.id"), "biospecimenId");
    projectionList.add(Projections.property("bs.biospecimenUid"), "biospecimenUid");
    projectionList.add(Projections.property("bs.parentUid"), "parentId");
    projectionList.add(Projections.property("sat.name"), "sampleType");
    projectionList.add(Projections.property("bs.quantity"), "quantity");
    projectionList.add(Projections.property("bts.name"), "initialStatus");

    criteria.setProjection(projectionList);
    criteria.setResultTransformer(Transformers.aliasToBean(BiospecimenSummaryDataRow.class));
    criteria.addOrder(Order.asc("lss.subjectUID"));
    criteria.addOrder(Order.asc("bs.biospecimenUid"));
    results = criteria.list();

    return results;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public List<BiospecimenDetailsDataRow> getBiospecimenDetailsData(
        BiospecimenDetailsReportVO biospecimenDetailReportVO) {
    List<BiospecimenDetailsDataRow> results = new ArrayList<BiospecimenDetailsDataRow>();

    Criteria criteria = getSession().createCriteria(BioTransaction.class, "bt");

    criteria.createAlias("status", "bts", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("biospecimen", "bs", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.study", "st", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.bioCollection", "bc", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.linkSubjectStudy", "lss", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.sampleType", "sat", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("bs.invCell", "inc", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("inc.invBox", "inb", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("inb.invRack", "inr", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("inr.invFreezer", "inf", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("inf.invSite", "ins", JoinType.LEFT_OUTER_JOIN);

    criteria.add(Restrictions.eq("st.id", biospecimenDetailReportVO.getStudy().getId()));
    if (biospecimenDetailReportVO.getSubjectUID() != null) {
        criteria.add(Restrictions.eq("lss.subjectUID", biospecimenDetailReportVO.getSubjectUID()));
    }/*from www. j a v  a  2  s.com*/

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.min("bt.id"));
    projectionList.add(Projections.groupProperty("bs.id"));

    projectionList.add(Projections.property("st.name"), "studyName");
    projectionList.add(Projections.property("lss.subjectUID"), "subjectUId");
    projectionList.add(Projections.property("bs.id"), "biospecimenId");
    projectionList.add(Projections.property("bc.biocollectionUid"), "biocollectionUid");
    projectionList.add(Projections.property("bs.sampleDate"), "sampleDate");
    projectionList.add(Projections.property("bs.biospecimenUid"), "biospecimenUid");
    projectionList.add(Projections.property("bs.parentUid"), "parentId");
    projectionList.add(Projections.property("sat.name"), "sampleType");
    projectionList.add(Projections.property("bs.quantity"), "quantity");
    projectionList.add(Projections.property("bts.name"), "initialStatus");

    projectionList.add(Projections.property("inb.name"), "box");
    projectionList.add(Projections.property("inr.name"), "rack");
    projectionList.add(Projections.property("inf.name"), "freezer");
    projectionList.add(Projections.property("ins.name"), "site");

    criteria.setProjection(projectionList);
    criteria.setResultTransformer(Transformers.aliasToBean(BiospecimenDetailsDataRow.class));
    criteria.addOrder(Order.asc("lss.subjectUID"));
    criteria.addOrder(Order.asc("bs.biospecimenUid"));
    results = criteria.list();

    return results;
}

From source file:com.amalto.core.storage.hibernate.StandardQueryHandler.java

License:Open Source License

@Override
public StorageResults visit(Min min) {
    FieldCondition fieldCondition = min.getExpression().accept(criterionFieldCondition);
    for (String criterionFieldName : fieldCondition.criterionFieldNames) {
        projectionList.add(Projections.min(criterionFieldName));
    }/* w  w  w .  ja v a  2  s.c om*/
    return null;
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateEventDao.java

License:Mozilla Public License

private List<Event> retrieveGrouped(List<String> eventActions, User user, Date startTime, Date stopTime,
        Set<Integer> appIds, Set<Integer> teamIds, Set<Integer> vulnIds, Set<Integer> defectIds) {
    Criteria criteria = getEventCriteria(eventActions, user, startTime, stopTime, appIds, teamIds, vulnIds,
            defectIds);/*from   w  w  w  . j  a va 2  s .c o  m*/

    criteria.setProjection(Projections.projectionList().add(Projections.count("id").as("groupCount"))
            .add(Projections.groupProperty("eventAction").as("eventAction"))
            .add(Projections.groupProperty("scan").as("scan"))
            .add(Projections.groupProperty("deletedScanId").as("deletedScanId"))
            .add(Projections.min("date"), "date").add(Projections.groupProperty("application"), "application")
            .add(Projections.groupProperty("user"), "user"));

    criteria.setResultTransformer(Transformers.aliasToBean(Event.class));

    List<Event> events = criteria.list();

    for (Event event : events) {
        EventAction eventAction = event.getEventActionEnum();
        EventAction groupedEventAction = eventAction.getGroupedEventAction();
        String groupedEventActionString = groupedEventAction.name();
        event.setEventAction(groupedEventActionString);
    }

    return events;
}

From source file:com.eucalyptus.cloudwatch.common.internal.domain.metricdata.MetricManager.java

License:Open Source License

public static List<Collection<MetricStatistics>> getManyMetricStatistics(
        List<GetMetricStatisticsParams> getMetricStatisticsParamses) {
    if (getMetricStatisticsParamses == null)
        throw new IllegalArgumentException("getMetricStatisticsParamses can not be null");
    Date now = new Date();
    Map<GetMetricStatisticsParams, Collection<MetricStatistics>> resultMap = Maps.newHashMap();
    Multimap<Class, GetMetricStatisticsParams> hashGroupMap = LinkedListMultimap.create();
    for (GetMetricStatisticsParams getMetricStatisticsParams : getMetricStatisticsParamses) {
        if (getMetricStatisticsParams == null)
            throw new IllegalArgumentException("getMetricStatisticsParams can not be null");
        getMetricStatisticsParams.validate(now);
        Class metricEntityClass = MetricEntityFactory.getClassForEntitiesGet(
                getMetricStatisticsParams.getMetricType(), getMetricStatisticsParams.getDimensionHash());
        hashGroupMap.put(metricEntityClass, getMetricStatisticsParams);
    }//from   www  .  j  a  v a2 s  .c  o m
    for (Class metricEntityClass : hashGroupMap.keySet()) {
        try (final TransactionResource db = Entities.transactionFor(metricEntityClass)) {
            // set some global criteria to start (for narrowing?)
            Date minDate = null;
            Date maxDate = null;
            Junction disjunction = Restrictions.disjunction();
            Map<GetMetricStatisticsParams, TreeMap<GetMetricStatisticsAggregationKey, MetricStatistics>> multiAggregationMap = Maps
                    .newHashMap();
            for (GetMetricStatisticsParams getMetricStatisticsParams : hashGroupMap.get(metricEntityClass)) {
                multiAggregationMap.put(getMetricStatisticsParams,
                        new TreeMap<GetMetricStatisticsAggregationKey, MetricStatistics>(
                                GetMetricStatisticsAggregationKey.COMPARATOR_WITH_NULLS.INSTANCE));
                Junction conjunction = Restrictions.conjunction();
                conjunction = conjunction
                        .add(Restrictions.lt("timestamp", getMetricStatisticsParams.getEndTime()));
                conjunction = conjunction
                        .add(Restrictions.ge("timestamp", getMetricStatisticsParams.getStartTime()));
                conjunction = conjunction
                        .add(Restrictions.eq("accountId", getMetricStatisticsParams.getAccountId()));
                conjunction = conjunction
                        .add(Restrictions.eq("metricName", getMetricStatisticsParams.getMetricName()));
                conjunction = conjunction
                        .add(Restrictions.eq("namespace", getMetricStatisticsParams.getNamespace()));
                conjunction = conjunction.add(
                        Restrictions.eq("dimensionHash", hash(getMetricStatisticsParams.getDimensionMap())));
                if (getMetricStatisticsParams.getUnits() != null) {
                    conjunction = conjunction
                            .add(Restrictions.eq("units", getMetricStatisticsParams.getUnits()));
                }
                disjunction = disjunction.add(conjunction);
                if (minDate == null || getMetricStatisticsParams.getStartTime().before(minDate)) {
                    minDate = getMetricStatisticsParams.getStartTime();
                }
                if (maxDate == null || getMetricStatisticsParams.getEndTime().after(maxDate)) {
                    maxDate = getMetricStatisticsParams.getEndTime();
                }
            }
            Criteria criteria = Entities.createCriteria(metricEntityClass);
            criteria = criteria.add(Restrictions.lt("timestamp", maxDate));
            criteria = criteria.add(Restrictions.ge("timestamp", minDate));
            criteria = criteria.add(disjunction);

            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.max("sampleMax"));
            projectionList.add(Projections.min("sampleMin"));
            projectionList.add(Projections.sum("sampleSize"));
            projectionList.add(Projections.sum("sampleSum"));
            projectionList.add(Projections.groupProperty("units"));
            projectionList.add(Projections.groupProperty("timestamp"));
            projectionList.add(Projections.groupProperty("accountId"));
            projectionList.add(Projections.groupProperty("metricName"));
            projectionList.add(Projections.groupProperty("metricType"));
            projectionList.add(Projections.groupProperty("namespace"));
            projectionList.add(Projections.groupProperty("dimensionHash"));
            criteria.setProjection(projectionList);
            criteria.addOrder(Order.asc("timestamp"));

            ScrollableResults results = criteria.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
            while (results.next()) {
                MetricEntity me = getMetricEntity(results);
                for (GetMetricStatisticsParams getMetricStatisticsParams : hashGroupMap
                        .get(metricEntityClass)) {
                    if (metricDataMatches(getMetricStatisticsParams, me)) {
                        Map<GetMetricStatisticsAggregationKey, MetricStatistics> aggregationMap = multiAggregationMap
                                .get(getMetricStatisticsParams);
                        GetMetricStatisticsAggregationKey key = new GetMetricStatisticsAggregationKey(me,
                                getMetricStatisticsParams.getStartTime(), getMetricStatisticsParams.getPeriod(),
                                getMetricStatisticsParams.getDimensionHash());
                        MetricStatistics item = new MetricStatistics(me,
                                getMetricStatisticsParams.getStartTime(), getMetricStatisticsParams.getPeriod(),
                                getMetricStatisticsParams.getDimensions());
                        if (!aggregationMap.containsKey(key)) {
                            aggregationMap.put(key, item);
                        } else {
                            MetricStatistics totalSoFar = aggregationMap.get(key);
                            totalSoFar.setSampleMax(Math.max(item.getSampleMax(), totalSoFar.getSampleMax()));
                            totalSoFar.setSampleMin(Math.min(item.getSampleMin(), totalSoFar.getSampleMin()));
                            totalSoFar.setSampleSize(totalSoFar.getSampleSize() + item.getSampleSize());
                            totalSoFar.setSampleSum(totalSoFar.getSampleSum() + item.getSampleSum());
                        }
                    }
                }
            }
            for (GetMetricStatisticsParams getMetricStatisticsParams : multiAggregationMap.keySet()) {
                resultMap.put(getMetricStatisticsParams,
                        multiAggregationMap.get(getMetricStatisticsParams).values());
            }
        }
    }
    List<Collection<MetricStatistics>> resultList = Lists.newArrayList();
    for (GetMetricStatisticsParams getMetricStatisticsParams : getMetricStatisticsParamses) {
        if (resultMap.get(getMetricStatisticsParams) == null) {
            resultList.add(new ArrayList<MetricStatistics>());
        } else {
            resultList.add(resultMap.get(getMetricStatisticsParams));
        }
    }
    return resultList;
}

From source file:com.eucalyptus.cloudwatch.common.internal.domain.metricdata.MetricManager.java

License:Open Source License

public static Collection<MetricStatistics> getMetricStatistics(
        GetMetricStatisticsParams getMetricStatisticsParams) {
    if (getMetricStatisticsParams == null)
        throw new IllegalArgumentException("getMetricStatisticsParams can not be null");
    Date now = new Date();
    getMetricStatisticsParams.validate(now);
    Class metricEntityClass = MetricEntityFactory.getClassForEntitiesGet(
            getMetricStatisticsParams.getMetricType(), getMetricStatisticsParams.getDimensionHash());
    Map<GetMetricStatisticsAggregationKey, MetricStatistics> aggregationMap = new TreeMap<GetMetricStatisticsAggregationKey, MetricStatistics>(
            GetMetricStatisticsAggregationKey.COMPARATOR_WITH_NULLS.INSTANCE);
    try (final TransactionResource db = Entities.transactionFor(metricEntityClass)) {
        Criteria criteria = Entities.createCriteria(metricEntityClass);
        criteria = criteria.add(Restrictions.eq("accountId", getMetricStatisticsParams.getAccountId()));
        criteria = criteria.add(Restrictions.eq("metricName", getMetricStatisticsParams.getMetricName()));
        criteria = criteria.add(Restrictions.eq("namespace", getMetricStatisticsParams.getNamespace()));
        criteria = criteria.add(Restrictions.lt("timestamp", getMetricStatisticsParams.getEndTime()));
        criteria = criteria.add(Restrictions.ge("timestamp", getMetricStatisticsParams.getStartTime()));
        criteria = criteria.add(Restrictions.eq("dimensionHash", getMetricStatisticsParams.getDimensionHash()));
        if (getMetricStatisticsParams.getUnits() != null) {
            criteria = criteria.add(Restrictions.eq("units", getMetricStatisticsParams.getUnits()));
        }/*www.j  a  v a  2  s  .  c o m*/

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.max("sampleMax"));
        projectionList.add(Projections.min("sampleMin"));
        projectionList.add(Projections.sum("sampleSize"));
        projectionList.add(Projections.sum("sampleSum"));
        projectionList.add(Projections.groupProperty("units"));
        projectionList.add(Projections.groupProperty("timestamp"));
        criteria.setProjection(projectionList);
        criteria.addOrder(Order.asc("timestamp"));
        ScrollableResults results = criteria.setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
        while (results.next()) {
            MetricEntity me = getMetricEntity(getMetricStatisticsParams.getAccountId(),
                    getMetricStatisticsParams.getMetricName(), getMetricStatisticsParams.getNamespace(),
                    getMetricStatisticsParams.getMetricType(), getMetricStatisticsParams.getDimensionHash(),
                    results);
            GetMetricStatisticsAggregationKey key = new GetMetricStatisticsAggregationKey(me,
                    getMetricStatisticsParams.getStartTime(), getMetricStatisticsParams.getPeriod(),
                    getMetricStatisticsParams.getDimensionHash());
            MetricStatistics item = new MetricStatistics(me, getMetricStatisticsParams.getStartTime(),
                    getMetricStatisticsParams.getPeriod(), getMetricStatisticsParams.getDimensions());
            if (!aggregationMap.containsKey(key)) {
                aggregationMap.put(key, item);
            } else {
                MetricStatistics totalSoFar = aggregationMap.get(key);
                totalSoFar.setSampleMax(Math.max(item.getSampleMax(), totalSoFar.getSampleMax()));
                totalSoFar.setSampleMin(Math.min(item.getSampleMin(), totalSoFar.getSampleMin()));
                totalSoFar.setSampleSize(totalSoFar.getSampleSize() + item.getSampleSize());
                totalSoFar.setSampleSum(totalSoFar.getSampleSum() + item.getSampleSum());
            }
        }
    }
    return Lists.newArrayList(aggregationMap.values());
}

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Adds a new projection to the current projectionList stack.
 * Mandatory Attributes:<ul>//from   w  w  w.j a v a 2s.  c o  m
 * <li><b>name</b>: The field to create a projection on.</li>
 * <li><b>type</b>: The projection type.</li>
 * <li><b>alias</b>: The name applied to the projection returned field.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @throws SAXException
 * TODO: Implement checks for mandatory attributes.
 */
public void addProjection(Attributes attrs) throws SAXException {
    ProjectionList projectionList = projectionStack.peek();
    if (projectionList == null) {
        throw new SAXException(
                "Attempted to add Projection and no ProjectionList Exists On The Stack. (Projection must be embedded inside a Projections)");
    }
    String type = attrs.getValue("type");
    String name = attrs.getValue("name");
    String alias = attrs.getValue("alias");
    if ("avg".equalsIgnoreCase(type)) {
        projectionList.add(Projections.avg(name), alias);
    } else if ("count".equalsIgnoreCase(type)) {
        projectionList.add(Projections.count(name), alias);
    } else if ("countDistinct".equalsIgnoreCase(type)) {
        projectionList.add(Projections.countDistinct(name), alias);
    } else if ("groupProperty".equalsIgnoreCase(type)) {
        projectionList.add(Projections.groupProperty(name), alias);
    } else if ("max".equalsIgnoreCase(type)) {
        projectionList.add(Projections.max(name), alias);
    } else if ("min".equalsIgnoreCase(type)) {
        projectionList.add(Projections.min(name), alias);
    } else if ("sum".equalsIgnoreCase(type)) {
        projectionList.add(Projections.sum(name), alias);
    } else if ("rowCount".equalsIgnoreCase(type)) {
        projectionList.add(Projections.rowCount(), alias);
    }

}

From source file:com.hmsinc.epicenter.model.surveillance.impl.SurveillanceRepositoryImpl.java

License:Open Source License

public DateTime getDateOfOldestAnomaly(final Geometry filter, final Geometry excludeFacilityEventsFilter) {

    final Criteria c = criteriaQuery(entityManager, Anomaly.class);

    applyAnomalyCriteria(c, null, null, false, filter, excludeFacilityEventsFilter);

    c.setProjection(Projections.min("analysisTimestamp"));

    final Object min = c.uniqueResult();
    return (min == null ? null : (DateTime) min);

}

From source file:com.hmsinc.epicenter.model.workflow.impl.WorkflowRepositoryImpl.java

License:Open Source License

public DateTime getDateOfOldestInvestigation(Geometry geometry, Collection<Organization> organizations) {

    final Criteria c = criteriaQuery(entityManager, Investigation.class, "investigation");
    applyInvestigationCriteria(c, null, null, geometry, organizations, false);

    c.setProjection(Projections.min("timestamp"));

    final Object min = c.uniqueResult();
    return (min == null ? null : (DateTime) min);
}