package ibase.webitm.ejb.sys;

import java.sql.*;
import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
//import ibase.webitm.utility.GenericUtility;
import ibase.system.config.*;
import ibase.webitm.ejb.sys.UtilMethods;
import ibase.webitm.ejb.*;
public class GenValBean
{
	//GenericUtility genericUtility = GenericUtility.getInstance();
	public void CheckCredential() throws Exception
	{
		System.out.println("Check status [" + status + "]");
		if ((status != null && "Y".equalsIgnoreCase(status)) || e_days > 360 )
		{
			setStatus();
			conn.close();
			conn = null;
			throw new Exception("340 Invalid system, cannot proceed");
		}
			
	}
    private static String encodeString (String stringVal)
    {
        StringBuffer buf = new StringBuffer();
        if (stringVal != null) {
            byte[] bytes = (stringVal).getBytes();
            int    b;

            for (int n = 0; n < bytes.length; n++) {
                b = bytes[n] ^ (ENCODE_XORMASK + n);
                buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
                buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
            }
        }
        return buf.toString();
    }
    private static String decodeString( String stringVal ) {

        // check that the string value isn't null or zero-length
        if( stringVal == null || stringVal.length() <= 0 ) {
            return null;
        }

        // unrafel the string value
        char[] chars = stringVal.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        int b;
        for (int n = 0, m = 0; n < bytes.length; n++) {
            b = chars[m++] - ENCODE_CHAR_OFFSET1;
            b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
            bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n));
        }
        stringVal = new String(bytes);

        return stringVal;
    }
    private void setStd() throws Exception
    {
    	std = sdf.parse(decodeString(CHK_STRING1));
    	setCrd();
    }
    private void setCrd() throws Exception
    {
    	
    	crd = new java.util.Date();
    	
    	crd.setTime(System.currentTimeMillis());
    	crd = sdf.parse(sdf.format(crd));
    	setDays();
    }
  	private void setDays() throws Exception
  	{
		Statement stmt = null;
		ResultSet rs = null;

  		e_days = um.DaysAfter(java.sql.Timestamp.valueOf(sdf.format(std) + " 00:00:00.000"), java.sql.Timestamp.valueOf(sdf.format(crd) + " 00:00:00.000") );
  		stmt = conn.createStatement();
  		rs = stmt.executeQuery("SELECT STATUS FROM PURCCTRL");
  		if (rs.next())
  		{
  			status = rs.getString("STATUS");
  		}
  		else
  		{
  			status = null;
  		}
  		rs.close();
  		rs = null;
  		stmt.close();
  		stmt = null;
  	}
  	private void setStatus() throws Exception
  	{
		Statement stmt = null;
  		
  		stmt = conn.createStatement();
  		stmt.executeUpdate("DELETE FROM PURCCTRL");
  		stmt.executeUpdate("INSERT INTO PURCCTRL() VALUES ()" );
  		stmt.close();
  		stmt = null;
		conn.commit();
  	}
	public GenValBean() throws Exception
	{
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver conndriver = new ConnDriver();
 		conn = conndriver.getConnectDB("DriverITM");
 		conn.setAutoCommit(false);
 		itmDBAccessEJB = null;
 		conndriver = null;
 		
		setStd();
	}
  	private UtilMethods um = UtilMethods.getInstance();
    private final static int    ENCODE_XORMASK = 0x5A;
    private final static char	ENCODE_DELIMETER = '\002';
    private final static char	ENCODE_CHAR_OFFSET1 = 'A';
    private final static char	ENCODE_CHAR_OFFSET2 = 'h';
    private final static String	CHK_STRING1 = "InMnBoAmAmLlGmAm";
    private final static String	CHK_STRING2 = "JnKnOn";
    private final int maxNum = (int) Integer.parseInt(decodeString(CHK_STRING2));
	private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private long e_days = 0;
	private java.util.Date std = null;
	private java.util.Date crd = null;
	private String status = null;
	private Connection conn = null;
}//class