/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JsonGraphs.Controller;

import JsonGraphs.Bean.BucketWiseLoanOutstandingBean;
import JsonGraphs.Util.JsonDbHandler;
//import LoginDb.JsonDbHandler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import oracle.jdbc.OracleTypes;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author 1320035
 */
public class JsonDashboardServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        barChartData barChartDataObjOut = null;
        barChartData barChartDataOb = null;
        ArrayList<String> labels = null;
        ArrayList<datasets> datasets = null;
        datasets datasetsObj = null;
        ArrayList<Integer> data = null;
        ArrayList<String> randColors = null;
        String color = null;
        //List<String> beneficiaryList = null;
        // 1. get received JSON data from request
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String json = "";
        if (br != null) {
            json = br.readLine();
        }

        // 2. initiate jackson mapper
        ObjectMapper mapper = new ObjectMapper();

        // 3. Convert received JSON to SubgroupBean
        BucketWiseLoanOutstandingBean BucketWiseLoanOutstandingBeanaObj = mapper.readValue(json, BucketWiseLoanOutstandingBean.class);
        // 4. Set response type to JSON
        response.setContentType("application/json");



        // 5. Add article to List<SubgroupBean>
        try {
            barChartDataOb = new barChartData();
            String toDate = BucketWiseLoanOutstandingBeanaObj.getToDate();
            String fromDate = BucketWiseLoanOutstandingBeanaObj.getFromDate();
            String distCode = BucketWiseLoanOutstandingBeanaObj.getDistCode();
            String reportSelection = BucketWiseLoanOutstandingBeanaObj.getReportSelection();

            
            barChartDataOb = new JsonDbHandler().selectionGraph(toDate, fromDate, distCode, reportSelection);

        } catch (Exception ex) {
           // System.out.println(ex.toString());
           System.out.println("Error Occurred during processing.");
        
        } finally {
            mapper.writeValue(response.getOutputStream(), barChartDataOb);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
