Example usage for org.springframework.data.mongodb.core.aggregation TypeBasedAggregationOperationContext TypeBasedAggregationOperationContext

List of usage examples for org.springframework.data.mongodb.core.aggregation TypeBasedAggregationOperationContext TypeBasedAggregationOperationContext

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.aggregation TypeBasedAggregationOperationContext TypeBasedAggregationOperationContext.

Prototype

public TypeBasedAggregationOperationContext(Class<?> type,
        MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext,
        QueryMapper mapper) 

Source Link

Document

Creates a new TypeBasedAggregationOperationContext for the given type, MappingContext and QueryMapper .

Usage

From source file:com.epam.ta.reportportal.database.dao.ReportPortalRepositoryImpl.java

@Override
public long getPageNumber(String entityId, Filter filterable, Pageable pageable) {
    Class<T> javaType = this.getEntityInformation().getJavaType();
    ImmutableList.Builder<AggregationOperation> pipelineBuilder = ImmutableList.<AggregationOperation>builder()
            .add(new MatchOperation(
                    new Criteria().andOperator(toArray(toCriteriaList(filterable), Criteria.class))) {
                @Override//ww w .  j  a  va 2s .  co m
                public DBObject toDBObject(AggregationOperationContext context) {
                    return super.toDBObject(new TypeBasedAggregationOperationContext(javaType,
                            mongoOperations.getConverter().getMappingContext(), queryMapper));
                }
            });

    if (null != pageable.getSort()) {
        pipelineBuilder.add(
                /* sort results as requested */
                sort(pageable.getSort()));
    }

    pipelineBuilder.add(
            /* group items into one field pushing all results into one array */
            group("result").push("$_id").as("array"),
            /* unwind array adding index to each result */
            unwind("array", "ind", false),
            /* find needed item. How we know its index in query result */
            match(where("array").is(ObjectId.isValid(entityId) ? new ObjectId(entityId) : entityId)),
            /* grab index only */
            project("ind"));

    /* find items matching an provided filter */
    Aggregation a = Aggregation.newAggregation(toArray(pipelineBuilder.build(), AggregationOperation.class));

    final AggregationResults<Map> aggregate = mongoOperations.aggregate(a,
            getEntityInformation().getCollectionName(), Map.class);

    if (!aggregate.getUniqueMappedResult().containsKey("ind")) {
        throw new ReportPortalException(ErrorType.INCORRECT_FILTER_PARAMETERS,
                "Unable to calculate page number. Check your input parameters");
    }

    /* result returned as long. Calculate page number */
    return (long) Math.ceil((((Long) aggregate.getUniqueMappedResult().get("ind")).doubleValue() + 1d)
            / (double) pageable.getPageSize());
}