package ibase.dashboard.scm.servlet;
import ibase.dashboard.scm.ejb.StateWiseStockRemote;
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;

public class StateWiseStockServlet 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
	{
		StateWiseStockRemote stateWiseStock = null;
		InitialContext context = null;
		AppConnectParm appConnectParm = new AppConnectParm();
		String dataSourceName = "";
		String month = "";
		String brand = "";
		String division = "";
		String linkMetadata = "";
		String viewOptionSelectd ="";
		try
		{
			response.setContentType("application/xml");
			dataSourceName    = request.getParameter("dataSourceName"); 
			month             = request.getParameter("month");
			brand             = request.getParameter("brand");
			division          = request.getParameter("division");
			linkMetadata      = request.getParameter("reportName"); 
			viewOptionSelectd = request.getParameter("viewOptionSelectd");
			
			
			System.out.println(" in StateWiseStockServlet  month--"+month+"---brand--"+brand+"----division------"+division+"-------linkMetadata---"+linkMetadata);
			
			context = new InitialContext(appConnectParm.getProperty());
			stateWiseStock = (StateWiseStockRemote) context.lookup("ibase/StateWiseStock/remote");
			JSONObject jsonObjData      = (JSONObject)stateWiseStock.getIndiaMap(dataSourceName,month,brand,division,viewOptionSelectd);
			
			/*String jsonObjData1 ="{'0':{'001':'','near_exp':'2501','long_exp':'201'},'1':{'002':'11202','near_exp':'2502','long_exp':'201'},'2':{'003':'11203','near_exp':'2503','long_exp':'203'},'3':{'004':'11247','near_exp':'2500','long_exp':'200'},'4':{'005':'11247','near_exp':'2500','long_exp':'200'},'5':{'006':'11247','near_exp':'2500','long_exp':'200'},'6':{'007':'11247','near_exp':'2500','long_exp':'200'},'7':{'008':'11247','near_exp':'2500','long_exp':'200'},'8':{'009':'11247','near_exp':'2500','long_exp':'200'},'9':{'010':'11247','near_exp':'2500','long_exp':'200'},'10':{'011':'11247','near_exp':'2500','long_exp':'200'},'11':{'012':'11247','near_exp':'2500','long_exp':'200'},'12':{'013':'11247','near_exp':'2500','long_exp':'200'},'13':{'014':'11247','near_exp':'2500','long_exp':'200'},'14':{'015':'11247','near_exp':'2500','long_exp':'200'},'15':{'016':'11247','near_exp':'2500','long_exp':'200'},'16':{'017':'11247','near_exp':'2500','long_exp':'200'},'17':{'018':'11247','near_exp':'2500','long_exp':'200'},'18':{'019':'','near_exp':'2500','long_exp':'200'},'19':{'020':'11247','near_exp':'2500','long_exp':'200'},'20':{'021':'11247','near_exp':'2500','long_exp':'200','tooltextlbl':'Maha:82781{br}Near_Exp: 420{br}Long_Exp:2'},'21':{'022':'11247','near_exp':'2500','long_exp':'200'},'22':{'023':'11247','near_exp':'2500','long_exp':'200'},'23':{'024':'11247','near_exp':'2500','long_exp':'200'},'24':{'025':'11247','near_exp':'2500','long_exp':'200'},'25':{'026':'11247','near_exp':'2500','long_exp':'200'},'26':{'027':'11247','near_exp':'2500','long_exp':'200'},'27':{'028':'11247','near_exp':'2500','long_exp':'200'},'28':{'029':'11247','near_exp':'2500','long_exp':'200'},'29':{'030':'11247','near_exp':'2500','long_exp':'200'},'30':{'031':'11247','near_exp':'2500','long_exp':'200'},'31':{'032':'11247','near_exp':'2500','long_exp':'200'},'32':{'033':'11247','near_exp':'2500','long_exp':'200'},'33':{'034':'11247','near_exp':'2500','long_exp':'200'},'34':{'035':'11247','near_exp':'2500','long_exp':'200'},'35':{'036':'11247','near_exp':'2500','long_exp':'200'}}";
			OutputStream outputStream   = response.getOutputStream();			
		    outputStream.write(jsonObjData.toString().replace('\'', '"').getBytes());
			outputStream.flush();
			outputStream.close();*/
			
			OutputStream outputStream   = response.getOutputStream();			
		    outputStream.write(jsonObjData.toString().getBytes());
			outputStream.flush();
			outputStream.close();
			
			System.out.println(" Final data set for StateWiseStockServlet build the Graph is===" + jsonObjData);	
			
		} catch (Exception e)
		{
			System.out.println("Exception : StateWiseStockServlet :doPost(HttpServletRequest request, HttpServletResponse response) :" + e);
			try
			{
			  throw new ITMException(e);
			} catch (ITMException e1)
			{
				e1.printStackTrace();
			}
		}
	}

}