class TenderPercentagesController extends GenericOCDSController {

    @ApiOperation("Returns the percent of tenders with active awards, "
            + "with tender.submissionMethod='electronicSubmission'."
            + "The endpoint also returns the total tenderds with active awards and the count of tenders with "
            + "tender.submissionMethod='electronicSubmission")
    @RequestMapping(value = "/api/percentTendersUsingEBid", method = {RequestMethod.POST, RequestMethod.GET},
            produces = "application/json")
    public List<Document> percentTendersUsingEBid(@ModelAttribute @Valid final YearFilterPagingRequest filter) {

        DBObject project1 = new BasicDBObject();
        addYearlyMonthlyProjection(filter, project1, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE));
        project1.put(Fields.UNDERSCORE_ID, "$tender._id");
        project1.put("electronicSubmission", new BasicDBObject("$cond", Arrays.asList(new BasicDBObject(
                        "$eq",
                        Arrays.asList(
                                "$tender.submissionMethod",
                                Tender.SubmissionMethod.electronicSubmission.toString()
                        )
                ), 1,
                0
        )));

        DBObject group1 = new BasicDBObject();
        group1.put(
                Fields.UNDERSCORE_ID, org.springframework.data
                        .mongodb.core.aggregation.Fields.UNDERSCORE_ID_REF);
        addYearlyMonthlyGroupingOperationFirst(filter, group1);
        group1.put("electronicSubmission", new BasicDBObject("$max", "$electronicSubmission"));

        DBObject group2 = new BasicDBObject();
        addYearlyMonthlyReferenceToGroup(filter, group2);
        group2.put(Keys.TOTAL_TENDERS, new BasicDBObject("$sum", 1));
        group2.put(Keys.TOTAL_TENDERS_USING_EBID, new BasicDBObject("$sum", "$electronicSubmission"));

        DBObject project2 = new BasicDBObject();
        project2.put(Keys.TOTAL_TENDERS, 1);
        project2.put(Keys.TOTAL_TENDERS_USING_EBID, 1);
        project2.put(Keys.PERCENTAGE_TENDERS_USING_EBID, new BasicDBObject("$multiply", Arrays
                .asList(new BasicDBObject("$divide", Arrays.asList("$totalTendersUsingEbid", "$totalTenders")), 100)));

        Aggregation agg = newAggregation(
                match(where(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true)
                        .and("tender.submissionMethod.0").exists(true).
                                and(MongoConstants.FieldNames.AWARDS_STATUS).is(Award.Status.active.toString())
                        .andOperator(getYearDefaultFilterCriteria(
                                filter,
                                MongoConstants.FieldNames.TENDER_PERIOD_START_DATE
                        ))),
                unwind("$tender.submissionMethod"),
                new CustomProjectionOperation(project1), new CustomGroupingOperation(group1),
                new CustomGroupingOperation(group2),
                new CustomProjectionOperation(project2),
                transformYearlyGrouping(filter).andInclude(Keys.TOTAL_TENDERS,
                        Keys.TOTAL_TENDERS_USING_EBID, Keys.PERCENTAGE_TENDERS_USING_EBID
                ),
                getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize())
        );

        return releaseAgg(agg);
    }

}