package ibase.webitm.ejb.wms;

import ibase.ejb.*;
import ibase.system.config.*;

import ibase.webitm.ejb.*;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.io.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.*;
import org.w3c.dom.*;
import java.net.InetAddress;
import org.xml.sax.InputSource;
import java.util.Calendar;
import java.util.HashMap;
import java.text.SimpleDateFormat;

@javax.ejb.Stateless
public class PickAssignIC extends ValidatorEJB implements PickAssignICRemote,PickAssignICLocal
{
	public String itemChanged() throws RemoteException, ITMException
	{
		return "";
	}
	public String itemChanged(String xmlString, String xmlString1, String xmlString2, String objContext, String currentColumn, String editFlag, String xtraParams) throws RemoteException, ITMException
	{
		Document dom = null;
		Document dom1 = null;
		Document dom2 = null;
		String errString = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			if (xmlString != null && xmlString.trim().length()!=0)
			{
				dom = genericUtility.parseString(xmlString); 
			}
			if (xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = genericUtility.parseString(xmlString1); 
			}
			if (xmlString2 != null && xmlString2.trim().length()!=0)
			{
				dom2 = genericUtility.parseString(xmlString2); 
			}
			errString = itemChanged( dom, dom1, dom2, objContext, currentColumn, editFlag, xtraParams );
			System.out.println ( "ErrString :" + errString);
		}
		catch (Exception e)
		{
			System.out.println ( "Exception :PickAssignIC :itemChanged(String,String):" + e.getMessage() + ":" );
			errString = genericUtility.createErrorString(e);
		}
		System.out.println ( "returning from PickAssignIC itemChanged" );
		return errString;
	}

	public String itemChanged( Document dom, Document dom1, Document dom2, String objContext, String currentColumn, String editFlag, String xtraParams ) throws RemoteException,ITMException
	{

		String sql = "";
		String columnValue = "";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		StringBuffer valueXmlString = new StringBuffer();
		String orddatFrom = "";
		String orddatTo = "";
		String pickOrderFrom = "";
		String pickOrderTo = "";
		int currentFormNo = 0;
		GenericUtility genericUtility = GenericUtility.getInstance();
		java.sql.Date dateFrom = null;
		java.sql.Date dateTo = null;
		HashMap dateMap = new HashMap();

		try
		{
			String applDateFmt = genericUtility.getApplDateFormat();
			String dbDateFmt = genericUtility.getDBDateFormat();
			System.out.println("**********ITEM CHANGE CALLED************");
			columnValue = genericUtility.getColumnValue( currentColumn, dom );
			ConnDriver connDriver = null;
			connDriver = new ConnDriver();
			conn = connDriver.getConnectDB("DriverValidator");
			if( objContext != null && objContext.trim().length() > 0 )
			{
				currentFormNo = Integer.parseInt( objContext );
			}
			valueXmlString = new StringBuffer( "<?xml version=\"1.0\"?>\r\n<Root>\r\n<Header>\r\n<editFlag>" );
			valueXmlString.append( editFlag ).append( "</editFlag>\r\n</Header>\r\n" );

			switch ( currentFormNo )
			{
			case 1:
			{
				valueXmlString.append( "<Detail1>\r\n" );

				if( currentColumn.trim().equalsIgnoreCase( "pick_order_to" ) )
				{
					pickOrderFrom = checkNull( genericUtility.getColumnValue("pick_order_from",dom) );
					System.out.println("PICKORDERFROM====="+pickOrderFrom);
					pickOrderTo = checkNull( genericUtility.getColumnValue("pick_order_to",dom) );
					System.out.println("PICKORDERTO======="+pickOrderTo);
					dateMap = getOrderDate(  pickOrderFrom,pickOrderTo,conn);					
					dateFrom = ((java.sql.Date)dateMap.get("MIN_DATE"));
					System.out.println("dateFrom=="+dateFrom);
		
					if( dateFrom == null )
					{	
						valueXmlString.append( "<from_date><![CDATA[" ).append("").append( "]]></from_date>\r\n" );
					}
					else
					{
						orddatFrom = genericUtility.getValidDateString( dateFrom.toString() , dbDateFmt, applDateFmt );	
					}
				
					dateTo =(java.sql.Date)dateMap.get("MAX_DATE");
					System.out.println("dateTo=="+dateTo);
					if( dateTo == null )
					{
						valueXmlString.append( "<to_date><![CDATA[" ).append("").append( "]]></to_date>\r\n" );
					}
					else
					{
					    orddatTo = genericUtility.getValidDateString( dateTo.toString() , dbDateFmt, applDateFmt );	 
					}
					
					System.out.println("orddatFrom"+orddatFrom);
					System.out.println("orddatTo"+orddatTo);
					if( pickOrderTo != null && !pickOrderTo.equalsIgnoreCase("ZZ"))
					{
						if( dateFrom != null )
						{	
							valueXmlString.append( "<from_date><![CDATA[" ).append( orddatFrom ).append( "]]></from_date>\r\n" );
							valueXmlString.append( "<to_date><![CDATA[" ).append( orddatTo ).append( "]]></to_date>\r\n" );
						}
						else
						{
							valueXmlString.append( "<from_date><![CDATA[" ).append( "" ).append( "]]></from_date>\r\n" );
							valueXmlString.append( "<to_date><![CDATA[" ).append( "" ).append( "]]></to_date>\r\n" );
						}
					}
					else
					{
						valueXmlString.append( "<from_date><![CDATA[" ).append( orddatFrom ).append( "]]></from_date>\r\n" );
						valueXmlString.append( "<to_date><![CDATA[" ).append( orddatTo ).append( "]]></to_date>\r\n" );
					}
				}
			}
			valueXmlString.append( "</Detail1>\r\n" );
			break;
			}//switch statement ends here
		}//try block ends here
		catch(Exception e)
		{
			System.out.println( "Exception :PickAssignIC :itemChanged(Document,String):" + e.getMessage() + ":" );
			valueXmlString = valueXmlString.append( genericUtility.createErrorString( e ) );
		}
		finally
		{
			try
			{
				if( conn != null && ! conn.isClosed() )
				{
					if( rs != null )
					{
						rs.close();
						rs = null;
					}
					if( pstmt != null )
					{
						pstmt.close();
						pstmt = null;
					}

					conn.close();
				}
			}
			catch(Exception e)
			{
				System.out.println( "Exception :PickAssignIC:itemChanged :==>\n"+e.getMessage());
				throw new ITMException(e);
			}
		}
		valueXmlString.append( "</Root>\r\n" );	
		System.out.println( "\n****ValueXmlString :" + valueXmlString.toString() + ":********" );
		return valueXmlString.toString();
	}

	public HashMap getOrderDate( String pickOrderFrom, String pickOrderTo,Connection conn )throws RemoteException, ITMException
	{
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		java.sql.Date minOrderDate = null;
		java.sql.Date maxOrderDate = null;
		HashMap dateMap = new HashMap();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{		 
			sql = " SELECT MIN( ORDER_DATE ), MAX( ORDER_DATE ) FROM PICK_ORD_HDR WHERE PICK_ORDER BETWEEN ? AND ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, pickOrderFrom);
			pstmt.setString(2, pickOrderTo); 
			rs = pstmt.executeQuery(); 
			if(rs.next())
			{
				minOrderDate = rs.getDate(1);
				maxOrderDate = rs.getDate(2);
				dateMap.put("MIN_DATE",minOrderDate);
				dateMap.put("MAX_DATE",maxOrderDate);
			}
			if(pstmt != null)
			{
				pstmt.close();
				pstmt = null;

			}
			if(rs != null)
			{
				rs.close();
				rs = null;

			}
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new ITMException(ex);
		}
		finally
		{
			try
			{
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;

				}
				if ( rs != null )
				{
					rs.close();
					rs = null;
				}

			}
			catch ( Exception e )
			{
				e.printStackTrace();
				throw new ITMException(e);
			}
		}	
		return dateMap; 	 	 
	}

	private String checkNull( String input )
	{
		if ( input == null )
		{
			input = "";
		}
		return input;
	}
}