package ibase.dashboard.mfg.servlet;
import ibase.dashboard.mfg.ejb.HalolMFGReleaseRemote;
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 HalolMonthWisePendingReleaseServlet 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
	{
		HalolMFGReleaseRemote halolMFGReleaseRemote = null;
		InitialContext context = null;
		AppConnectParm appConnectParm = new AppConnectParm();
		String dataSourceName = "";
		String month = "";
		String prd_code ="",geoLoc="";
		String metadataName="";
		//JSONObject jsonObjData  = null;
		try
		{
			response.setContentType("application/xml");
			dataSourceName    = request.getParameter("dataSourceName"); 
			//month             = (request.getParameter("month"));
			month              = (request.getParameter("prd_code"));
			geoLoc  = (request.getParameter("geolocation"));
			metadataName = (request.getParameter("reportName"));
			System.out.println("metadataName--in Pending Servlet-------->"+metadataName);
			System.out.println(" month and week ===" + month+"prd_code===="+prd_code);	
			context = new InitialContext(appConnectParm.getProperty());
			halolMFGReleaseRemote       = (HalolMFGReleaseRemote) context.lookup("ibase/HalolMFGRelease/remote");
			if(metadataName.equals("monthwisepending"))
			{
				System.out.println("in monthwisepending");
				
				JSONObject jsonObjData      = (JSONObject)halolMFGReleaseRemote.getMonthWisePendingRelease (dataSourceName,month,geoLoc);
				OutputStream outputStream   = response.getOutputStream();			
				outputStream.write(jsonObjData.toString().getBytes());
				outputStream.flush();
				outputStream.close();
				System.out.println(" Final data set inside HalolMonthWiseActReleaseServlet build the Graph is===" + jsonObjData);	
			}else if(metadataName.equals("tbdbatch"))
			{
				System.out.println("in tbdbatch");
				JSONObject jsonObjData  = (JSONObject)halolMFGReleaseRemote.getTBDBatch(dataSourceName, month,geoLoc);
				OutputStream outputStream   = response.getOutputStream();			
				outputStream.write(jsonObjData.toString().getBytes());
				outputStream.flush();
				outputStream.close();
				System.out.println(" Final data set inside HalolMonthWiseActReleaseServlet build the Graph is===" + jsonObjData);	
			}
			
		} catch (Exception e)
		{
			System.out.println("Exception : HalolMonthWiseActReleaseServlet :doPost(HttpServletRequest request, HttpServletResponse response) :" + e.getMessage());
			try
			{
			  throw new ITMException(e);
			} catch (ITMException e1)
			{
				e1.printStackTrace();
			}
		}
	}
}