package ibase.dashboard.mfg.servlet;

import ibase.dashboard.mfg.ejb.ProfitLossBalSheetRemote;
import ibase.system.config.AppConnectParm;
import ibase.webitm.utility.ITMException;

import java.io.IOException;
import java.io.OutputStream;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

@SuppressWarnings("serial")
public class ProfitLossBalSheetServlet extends HttpServlet
{
	//private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		doPost(request, response);
	}

	@SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		ProfitLossBalSheetRemote profitLossBalSheetRemote = null;
		InitialContext context = null;
		AppConnectParm appConnectParm = new AppConnectParm();
		String dataSourceName = "";
		String prdfrom = "", acctprd = "",sitecode="",prdto="",formatcode="",reporttype="";
		try
		{
			response.setContentType("application/xml");
			sitecode= request.getParameter("sitecode");
			prdfrom = request.getParameter("prdfrom");
			prdto= request.getParameter("prdto");
			acctprd= request.getParameter("acctprd");
			formatcode= request.getParameter("formatcode");
			reporttype= request.getParameter("reporttype");
			dataSourceName    = request.getParameter("dataSourceName"); 
			System.out.println("in doPost" );
			System.out.println("days is =" + sitecode );
			System.out.println("days is =" + prdfrom );
			System.out.println("days is =" + prdto );
			System.out.println("days is =" + acctprd );
			System.out.println("days is =" + formatcode );
			System.out.println("days is =" + reporttype );
			
			context = new InitialContext(appConnectParm.getProperty());
			profitLossBalSheetRemote = (ProfitLossBalSheetRemote) context.lookup("ibase/ProfitLossBalSheet/remote");
			JSONObject jsonObjData      = (JSONObject)profitLossBalSheetRemote.getProfitBalanceSheetDetails(sitecode,prdfrom,prdto,acctprd,formatcode,reporttype,dataSourceName);
			OutputStream outputStream   = response.getOutputStream();			
		    outputStream.write(jsonObjData.toString().getBytes());
			outputStream.flush();
			outputStream.close();
			System.out.println(" Final data set for ProfitLossBalSheetServlet build the Graph is===" + jsonObjData);	
			
		} catch (Exception e)
		{
			System.out.println("Exception : ProfitLossBalSheetServlet :doPost(HttpServletRequest request, HttpServletResponse response) :" + e);
			try
			{
			  throw new ITMException(e);
			} catch (ITMException e1)
			{
				e1.printStackTrace();
			}
		}
	}

}