Example usage for org.hibernate.criterion Projections avg

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

Introduction

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

Prototype

public static AggregateProjection avg(String propertyName) 

Source Link

Document

A property average value projection

Usage

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * Adds a projection that allows the criteria to return the property average value
 *
 * @param propertyName The name of the property
 * @param alias The alias to use/*from w w w  . j  a v a  2  s  . c  o m*/
 */
public org.grails.datastore.mapping.query.api.Projections avg(String propertyName, String alias) {
    final AggregateProjection aggregateProjection = Projections.avg(calculatePropertyName(propertyName));
    addProjectionToList(aggregateProjection, alias);
    return this;
}

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  ww w.  j  a  v a2 s.c o 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:org.balisunrise.daa.hibernate.HQuery.java

License:Open Source License

@Override
public <V> V avg(String property) {
    return (V) hcrit.setProjection(Projections.avg(property)).uniqueResult();
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.jpahibernate.JpaHibFindByConditionStatAccessImpl.java

License:Apache License

private void addStatProjection(Criteria criteria) throws PersistenceException {
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.rowCount());
    for (ColumnStat<T> column : statResult) {
        if (column.isCountNotNullFlag()) {
            projList.add(Projections.count(column.getColumn().getName()));
        }//w  w  w.  j a v a2s .c om
        if (column.isMinFlag()) {
            projList.add(Projections.min(column.getColumn().getName()));
        }
        if (column.isMaxFlag()) {
            projList.add(Projections.max(column.getColumn().getName()));
        }
        if (column.isAverageFlag()) {
            projList.add(Projections.avg(column.getColumn().getName()));
        }
        if (column.isSumFlag()) {
            projList.add(Projections.sum(column.getColumn().getName()));
        }
    }

    criteria.setProjection(projList);
}

From source file:org.iternine.jeppetto.dao.hibernate.HibernateQueryModelDAO.java

License:Apache License

@Override
public Projection buildProjection(String projectionField, ProjectionType projectionType,
        Iterator argsIterator) {/* w ww.  j av a2s . co  m*/
    Projection projection = new Projection();

    projection.setField(projectionField);

    switch (projectionType) {
    case RowCount:
        projection.setDetails(Projections.rowCount());
        break;

    case Count:
        projection.setDetails(Projections.count(projectionField));
        break;

    case CountDistinct:
        projection.setDetails(Projections.countDistinct(projectionField));
        break;

    case Maximum:
        projection.setDetails(Projections.max(projectionField));
        break;

    case Minimum:
        projection.setDetails(Projections.min(projectionField));
        break;

    case Average:
        projection.setDetails(Projections.avg(projectionField));
        break;

    case Sum:
        projection.setDetails(Projections.sum(projectionField));
        break;

    default:
        throw new RuntimeException("Unexpected projection type: " + projectionType);
    }

    return projection;
}

From source file:org.sculptor.framework.accessimpl.jpahibernate.JpaHibFindByConditionStatAccessImpl.java

License:Apache License

private void addStatProjection(Criteria criteria) throws PersistenceException {
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.rowCount());
    for (ColumnStatRequest<T> column : statRequest) {
        if (column.isFlag(COUNT)) {
            projList.add(Projections.count(column.getColumn().getName()));
        }// w w w.j a v a  2s. co m
        if (column.isFlag(MIN)) {
            projList.add(Projections.min(column.getColumn().getName()));
        }
        if (column.isFlag(MAX)) {
            projList.add(Projections.max(column.getColumn().getName()));
        }
        if (column.isFlag(AVERAGE)) {
            projList.add(Projections.avg(column.getColumn().getName()));
        }
        if (column.isFlag(SUM)) {
            projList.add(Projections.sum(column.getColumn().getName()));
        }
        if (column.isFlag(GROUP_BY_VAL)) {
            projList.add(Projections.groupProperty(column.getColumn().getName()));
        }

        // Time groups
        for (ColumnStatType flag : TIME_GROUPS) {
            if (column.isFlag(flag)) {
                projList.add(makeTimeGroupBy(column, flag, criteria));
            }
        }
    }

    criteria.setProjection(projList);
}

From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java

License:Open Source License

@Override
public void visitPropertySelection(QPropertySelection n) throws Exception {
    String name = parseSubcriteria(n.getProperty());

    switch (n.getFunction()) {
    default:/* w ww.  j a va 2 s  .c o  m*/
        throw new IllegalStateException("Unexpected selection item function: " + n.getFunction());
    case AVG:
        m_lastProj = Projections.avg(name);
        break;
    case MAX:
        m_lastProj = Projections.max(name);
        break;
    case MIN:
        m_lastProj = Projections.min(name);
        break;
    case SUM:
        m_lastProj = Projections.sum(name);
        break;
    case COUNT:
        m_lastProj = Projections.count(name);
        break;
    case COUNT_DISTINCT:
        m_lastProj = Projections.countDistinct(name);
        break;
    case ID:
        m_lastProj = Projections.id();
        break;
    case PROPERTY:
        m_lastProj = Projections.groupProperty(name);
        break;
    case ROWCOUNT:
        m_lastProj = Projections.rowCount();
        break;
    case DISTINCT:
        m_lastProj = Projections.distinct(Projections.property(name));
        break;
    }
}