package org.hisp.dhis.webapi.controller;

/*
 * Copyright (c) 2004-2018, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import javax.servlet.http.HttpServletResponse;

import org.hisp.dhis.analytics.orgunit.OrgUnitQueryParams;
import org.hisp.dhis.analytics.orgunit.OrgUnitAnalyticsService;
import org.hisp.dhis.common.DhisApiVersion;
import org.hisp.dhis.common.Grid;
import org.hisp.dhis.common.cache.CacheStrategy;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Lars Helge Overland
 */
@Controller
@ApiVersion( { DhisApiVersion.DEFAULT, DhisApiVersion.ALL } )
public class OrgUnitAnalyticsController
{
    private static final String RESOURCE_PATH = "/orgUnitAnalytics";

    @Autowired
    private OrgUnitAnalyticsService distributionService;

    @Autowired
    private ContextUtils contextUtils;

    @RequestMapping( value = RESOURCE_PATH, method = RequestMethod.GET, produces = { "application/json" } )
    public @ResponseBody Grid getJson(
        @RequestParam String ou,
        @RequestParam String ougs,
        DhisApiVersion apiVersion,
        HttpServletResponse response ) throws Exception
    {
        OrgUnitQueryParams params = distributionService.getParams( ou, ougs );

        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.RESPECT_SYSTEM_SETTING );

        return distributionService.getOrgUnitDistribution( params );
    }
}

@@ -1891,16 +1891,60 @@
 rategy;%0A
+import org.hisp.dhis.system.grid.GridUtils;%0A
 import o
@@ -2675,28 +2675,25 @@
 Service 
-distribution
+analytics
 Service;
@@ -3097,28 +3097,25 @@
 arams = 
-distribution
+analytics
 Service.
@@ -3133,25 +3133,24 @@
 ou, ougs );%0A
-%0A
         cont
@@ -3263,74 +3263,1887 @@
  );%0A
+        return analyticsService.getOrgUnitDistribution( params );
 %0A    
+%7D%0A%0A
     
-return distributionService.getOrgUnitDistribution( params
+@RequestMapping( value = RESOURCE_PATH + %22.xls%22, method = RequestMethod.GET )%0A    public void getXls(%0A        @RequestParam String ou,%0A        @RequestParam String ougs,%0A        DhisApiVersion apiVersion,%0A        HttpServletResponse response ) throws Exception%0A    %7B%0A        OrgUnitQueryParams params = analyticsService.getParams( ou, ougs );%0A        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_EXCEL, CacheStrategy.RESPECT_SYSTEM_SETTING );%0A        Grid grid = analyticsService.getOrgUnitDistribution( params );%0A        GridUtils.toXls( grid, response.getOutputStream() );%0A    %7D%0A%0A    @RequestMapping( value = RESOURCE_PATH + %22.csv%22, method = RequestMethod.GET )%0A    public void getCsv(%0A        @RequestParam String ou,%0A        @RequestParam String ougs,%0A        DhisApiVersion apiVersion,%0A        HttpServletResponse response ) throws Exception%0A    %7B%0A        OrgUnitQueryParams params = analyticsService.getParams( ou, ougs );%0A        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_CSV, CacheStrategy.RESPECT_SYSTEM_SETTING );%0A        Grid grid = analyticsService.getOrgUnitDistribution( params );%0A        GridUtils.toCsv( grid, response.getWriter() );%0A    %7D%0A%0A    @RequestMapping( value = RESOURCE_PATH + %22.pdf%22, method = RequestMethod.GET )%0A    public void getPdf(%0A        @RequestParam String ou,%0A        @RequestParam String ougs,%0A        DhisApiVersion apiVersion,%0A        HttpServletResponse response ) throws Exception%0A    %7B%0A        OrgUnitQueryParams params = analyticsService.getParams( ou, ougs );%0A        contextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.RESPECT_SYSTEM_SETTING );%0A        Grid grid = analyticsService.getOrgUnitDistribution( params );%0A        GridUtils.toPdf( grid, response.getOutputStream()
  );%0A

Add various representation formats to org unit analytics (#2967)