package ibase.dashboard.scm.servlet;
import ibase.dashboard.scm.ejb.SalesInventoryRemote;
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 SalesInventoryBrandWiseServlet 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
	{
		SalesInventoryRemote  salesInventoryRemote = null;
		InitialContext context = null;
		AppConnectParm appConnectParm = new AppConnectParm();
		String dataSourceName = "";
		String month = "",urlStr="",brandName="";
		try
		{
			response.setContentType("application/xml");
			dataSourceName          =   request.getParameter("dataSourceName"); 
			month                   =  (request.getParameter("month"));		
			/*urlStr                  =  (request.getParameter("P1"));
			String[] splitArr = urlStr.trim().split("\\s+");
			System.out.println(" month======" + month);
			System.out.println(" brand======" + splitArr[1]);
			brandName = splitArr[1];*/
		   	brandName                = (request.getParameter("CELL_VAL"));
			context                  = new InitialContext(appConnectParm.getProperty());
			salesInventoryRemote    = (SalesInventoryRemote) context.lookup("ibase/SalesInventory/remote");
			//Here invoke EJB define method 
			JSONObject jsonObjData      = (JSONObject)salesInventoryRemote.getSalesInventoryBrandWise(dataSourceName, month,brandName);
			OutputStream outputStream   = response.getOutputStream();			
		    outputStream.write(jsonObjData.toString().getBytes());
			outputStream.flush();
			outputStream.close();
			System.out.println(" Final data set for SalesInventoryBrandWiseServlet build to Graph and Grid===" + jsonObjData);	
			
		} catch (Exception e)
		{
			System.out.println("Exception in: SalesInventoryBrandWiseServlet :doPost(HttpServletRequest request, HttpServletResponse response) :" + e.getMessage());
			try
			{
			  throw new ITMException(e);
			} catch (ITMException e1)
			{
				e1.printStackTrace();
			}
		}
	}

}