Example usage for javax.persistence.criteria CriteriaBuilder avg

List of usage examples for javax.persistence.criteria CriteriaBuilder avg

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder avg.

Prototype

<N extends Number> Expression<Double> avg(Expression<N> x);

Source Link

Document

Create an aggregate expression applying the avg operation.

Usage

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * @param criteriaBuilder/*  www  . j  av  a2 s.c  om*/
 * @param out
 */
@SuppressWarnings("unchecked")
protected void generateHaving(final CriteriaBuilder criteriaBuilder, final List<Predicate> out) {
    // log.debug("generateHaving :");

    Preconditions.checkNotNull(out);
    Preconditions.checkNotNull(criteriaBuilder);
    Preconditions.checkNotNull(root);

    if (_joins != null && _joins.size() > 0) {
        Set<Entry<JoinContainer<E>, CriteriaComposer<?>>> allSetJoins = _joins.entrySet();

        for (Entry<JoinContainer<E>, CriteriaComposer<?>> joinEntry : allSetJoins) {
            CriteriaComposer<?> subCriteria = joinEntry.getValue();

            if (subCriteria != null)
                subCriteria.generateHaving(criteriaBuilder, out);
        }
    }

    if (_havings != null) {
        List<Predicate> andPredicates = new ArrayList<Predicate>(0);
        List<Predicate> orPredicates = new ArrayList<Predicate>(0);

        LogicOperator lastOperator = LogicOperator.NONE;

        for (HavingContainer<E> havingContainer : _havings) {

            @SuppressWarnings("rawtypes")
            Expression expression = null;

            switch (havingContainer.function) {
            case COUNT:
                expression = criteriaBuilder.count(root.get(havingContainer.attribute));
                break;

            case AVG:
                expression = criteriaBuilder.avg(root.get(havingContainer.attribute));
                break;

            case SUM:
                expression = criteriaBuilder.sum(root.get(havingContainer.attribute));
                break;

            case MAX:
                expression = criteriaBuilder.max(root.get(havingContainer.attribute));
                break;

            case MIN:
                expression = criteriaBuilder.min(root.get(havingContainer.attribute));
                break;
            }

            Predicate predicate = ComparisonOperatorProcessor.process(criteriaBuilder, expression,
                    havingContainer.comparisionOperator, havingContainer.value);

            if (havingContainer.notOperator != null)
                predicate = ComparisonOperatorProcessor.negate(criteriaBuilder, predicate);

            LogicOperator nextOperator = havingContainer.logicOperator;

            if (nextOperator == LogicOperator.NONE && !nextOperator.equals(lastOperator))
                nextOperator = lastOperator;

            switch (nextOperator) {
            case AND:
            case NONE:
                andPredicates.add(predicate);
                break;
            case OR:
                orPredicates.add(predicate);
                break;
            default:
                throw new java.lang.IllegalStateException(
                        "Unknown operator found " + havingContainer.comparisionOperator.toString());
            }

            lastOperator = nextOperator;

        } // end for

        if (andPredicates != null && andPredicates.size() > 0)
            out.add(criteriaBuilder.and(andPredicates.toArray(new Predicate[andPredicates.size()])));

        if (orPredicates != null && orPredicates.size() > 0)
            out.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));

    } // end if
}

From source file:org.easy.criteria.CriteriaComposer.java

/**
 * @param out/*from   ww w  . jav a 2s  .c o m*/
 */
@SuppressWarnings("unchecked")
protected void generateSelect(final CriteriaBuilder criteriaBuilder, final List<Selection<?>> out) {
    Preconditions.checkNotNull(out);

    if (multiselect != null && multiselect.size() > 0) {
        out.addAll(multiselect);
    }

    if (_selects != null && _selects.size() > 0) {
        for (SelectContainer<E> selectContainer : _selects) {
            if (selectContainer.function == null) {
                Selection<?> selection = root.get(selectContainer.singularAttribute);
                selection.alias(selectContainer.alias);
                out.add(selection);
            } else {
                @SuppressWarnings("rawtypes")
                Expression numExp = null;
                switch (selectContainer.function) {
                case COUNT:
                    numExp = criteriaBuilder.count(root.get(selectContainer.singularAttribute));
                    numExp.alias(selectContainer.alias);
                    break;

                case AVG:
                    numExp = criteriaBuilder.avg(root.get(selectContainer.singularAttribute));
                    numExp.alias(selectContainer.alias);
                    break;

                case SUM:
                    numExp = criteriaBuilder.sum(root.get(selectContainer.singularAttribute));
                    numExp.alias(selectContainer.alias);
                    break;

                case MAX:
                    numExp = criteriaBuilder.max(root.get(selectContainer.singularAttribute));
                    numExp.alias(selectContainer.alias);
                    break;

                case MIN:
                    numExp = criteriaBuilder.min(root.get(selectContainer.singularAttribute));
                    numExp.alias(selectContainer.alias);
                    break;
                }

                if (numExp != null)
                    out.add(numExp);
            }

        }
    }

    if (_joins != null) {
        Set<Entry<JoinContainer<E>, CriteriaComposer<?>>> allSetJoins = _joins.entrySet();

        for (Entry<JoinContainer<E>, CriteriaComposer<?>> join : allSetJoins) {
            CriteriaComposer<?> subCriteria = join.getValue();

            if (subCriteria != null)
                subCriteria.generateSelect(criteriaBuilder, out);
        }
    }
}