package wenrgise.common.ejb.business;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.DateUtility;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.UserInfo;
import wenrgise.ejb.common.business.BaseBO;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.helper.QueryValue;
import wenrgise.ejb.common.utility.DBUtilitiesBean;

public class HrmCommonBO extends BaseBO {
  public static final int AFTER_TIME = 1;
  
  public static final int TIME_ERROR = -2;
  
  public static final int EQUAL_TIME = 0;
  
  public static final int BEFORE_TIME = -1;
  
  public HrmCommonBO() {}
  
  public HrmCommonBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public String getRefList(String sTabName, String sRefKey, String sRefValue, String sPrmKey) throws EnrgiseSystemException {
    String sRefList = new String();
    String sQuery = null;
    sQuery = String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("SELECT distinct ").concat(String.valueOf(sPrmKey))).concat(String.valueOf(" as keyId from "))).concat(String.valueOf(sTabName))).concat(String.valueOf(" where "))).concat(String.valueOf(sRefKey))).concat(String.valueOf(" = "))).concat(String.valueOf(sRefValue));
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    if (oList.size() != 0) {
      QueryRow oRow = null;
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        oRow = oIt.next();
        sRefList = String.valueOf(sRefList).concat(String.valueOf(oRow.get("keyId").getString()));
        sRefList = String.valueOf(sRefList).concat(String.valueOf(","));
      } 
    } 
    return sRefList;
  }
  
  public String getRefList1(String sTabName, String sRefKey, String sRefValue, String sPrmKey) throws EnrgiseSystemException {
    String sRefList = new String();
    String sQuery = null;
    sQuery = String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("SELECT distinct ").concat(String.valueOf(sPrmKey))).concat(String.valueOf(" as keyId from "))).concat(String.valueOf(sTabName))).concat(String.valueOf(" where "))).concat(String.valueOf(sRefKey))).concat(String.valueOf(" ='"))).concat(String.valueOf(sRefValue))).concat(String.valueOf("'"));
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    if (oList.size() != 0) {
      QueryRow oRow = null;
      Iterator oIt = oList.iterator();
      while (oIt.hasNext()) {
        oRow = oIt.next();
        sRefList = String.valueOf(sRefList).concat(String.valueOf(oRow.get("keyId").getString()));
      } 
    } 
    return sRefList;
  }
  
  public void deleteChildDetailRec(String sChildTabName, String sRefKey, String sRefValue, String sRefIdList) throws EnrgiseSystemException {
    String sQuery = null;
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    if (sRefIdList != null) {
      sQuery = String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("Delete from ").concat(String.valueOf(sChildTabName))).concat(String.valueOf(" where "))).concat(String.valueOf(sRefKey))).concat(String.valueOf(" in ( "))).concat(String.valueOf(sRefIdList))).concat(String.valueOf(" )"));
    } else {
      sQuery = String.valueOf(String.valueOf(String.valueOf(String.valueOf(String.valueOf("Delete from ").concat(String.valueOf(sChildTabName))).concat(String.valueOf(" where "))).concat(String.valueOf(sRefKey))).concat(String.valueOf(" ="))).concat(String.valueOf(sRefValue));
    } 
    int flag = oBean.executeUpsert(sQuery);
  }
  
  public String getNextCnclDtlToDate(String sDate, String sTxnId) throws EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    String sNextDate = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 93, EnrgiseUtil.convertToSqlDate(sDate)));
    oParameters.add(new DBObject(2, 1, 12, sTxnId));
    oParameters.add(new DBObject(3, 2, -10));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMLVCANQUERY.proc_GetNextCnclDtlToDate(?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      sNextDate = EnrgiseUtil.convertToString(oRow.get("NEXTCNCLDTLTODATE").getDate());
    } 
    return sNextDate;
  }
  
  public String getPrevCnclDtlFromDate(String sDate, String sTxnId) throws EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    String sPrevDate = null;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 93, EnrgiseUtil.convertToSqlDate(sDate)));
    oParameters.add(new DBObject(2, 1, 12, sTxnId));
    oParameters.add(new DBObject(3, 2, -10));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMLVCANQUERY.proc_GetPrevCnclDtlFromDate(?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    QueryValue oValue = null;
    HashMap oColumns = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      sPrevDate = EnrgiseUtil.convertToString(oRow.get("PREVCNCLDTLTODATE").getDate());
    } 
    return sPrevDate;
  }
  
  public String getStartDate(String sCalId, String sDate) throws EnrgiseApplicationException, EnrgiseSystemException {
    sDate = getddmmyyyyDateFormat(sDate);
    DateUtility d = new DateUtility();
    ArrayList oParameters = new ArrayList();
    String startDate = null;
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sCalId));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMORGCALENDARMST.proc_GetHrCalHdrInfoById(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0);
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      String beginMonth = oRow.get("BEGIN_MONTH").getString();
      String durationMonth = oRow.get("DURATION_MONTH").getString();
      int beMon = Integer.parseInt(beginMonth);
      String monOfDate = sDate.substring(3, 5);
      String yearOfDate = sDate.substring(6, 10);
      int monDate = Integer.parseInt(monOfDate);
      int yrDate = Integer.parseInt(yearOfDate);
      if (beMon <= monDate) {
        if (beMon < 10)
          beginMonth = String.valueOf("0").concat(String.valueOf(beginMonth)); 
        startDate = String.valueOf(String.valueOf(String.valueOf("01/").concat(String.valueOf(beginMonth))).concat(String.valueOf("/"))).concat(String.valueOf(yearOfDate));
        continue;
      } 
      if (beMon > monDate) {
        if (beMon < 10)
          beginMonth = String.valueOf("0").concat(String.valueOf(beginMonth)); 
        startDate = String.valueOf(String.valueOf(String.valueOf("01/").concat(String.valueOf(beginMonth))).concat(String.valueOf("/"))).concat(String.valueOf(yrDate - 1));
      } 
    } 
    startDate = getddmmmyyyyDateFormat(startDate);
    return startDate;
  }
  
  public String getEndDate(String sCalId, String sDate) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sStartDate = getStartDate(sCalId, sDate);
    sStartDate = getddmmyyyyDateFormat(sStartDate);
    String sEndDate = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, sCalId));
    oParameters.add(new DBObject(2, 2, -10));
    oParameters.add(new DBObject(3, 2, 12));
    oParameters.add(new DBObject(4, 2, 12));
    oParameters.add(new DBObject(5, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "HRMORGCALENDARMST.proc_GetHrCalHdrInfoById(?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0);
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      int yrEndDate, ddEndDate;
      oRow = oIt.next();
      String durationMonth = oRow.get("DURATION_MONTH").getString();
      int durMon = Integer.parseInt(durationMonth);
      String monOfStartDate = sStartDate.substring(3, 5);
      String yearOfStartDate = sStartDate.substring(6, 10);
      int monStartDate = Integer.parseInt(monOfStartDate);
      int yrStartDate = Integer.parseInt(yearOfStartDate);
      int monEndDate = monStartDate + durMon - 1;
      if (monEndDate > 12) {
        monEndDate -= 12;
        yrEndDate = yrStartDate + 1;
      } else {
        yrEndDate = yrStartDate;
      } 
      if (monEndDate == 2) {
        int quo = yrEndDate / 4;
        int rem = yrEndDate - 4 * quo;
        if (rem == 0) {
          ddEndDate = 29;
        } else {
          ddEndDate = 28;
        } 
      } else if (monEndDate == 4 || monEndDate == 6 || monEndDate == 9 || monEndDate == 11) {
        ddEndDate = 30;
      } else {
        ddEndDate = 31;
      } 
      if (monEndDate < 10) {
        sEndDate = String.valueOf(String.valueOf(String.valueOf(String.valueOf(ddEndDate).concat(String.valueOf("/0"))).concat(String.valueOf(monEndDate))).concat(String.valueOf("/"))).concat(String.valueOf(yrEndDate));
        continue;
      } 
      sEndDate = String.valueOf(String.valueOf(String.valueOf(String.valueOf(ddEndDate).concat(String.valueOf("/"))).concat(String.valueOf(monEndDate))).concat(String.valueOf("/"))).concat(String.valueOf(yrEndDate));
    } 
    sEndDate = getddmmmyyyyDateFormat(sEndDate);
    return sEndDate;
  }
  
  public String getddmmyyyyDateFormat(String sDate) throws EnrgiseSystemException {
    String sRetDate = null;
    String monOfDate = sDate.substring(3, 6);
    String yrOfdate = sDate.substring(7, 11);
    String ddOfDate = sDate.substring(0, 2);
    String[] e = { 
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
        "Nov", "Dec" };
    for (int i = 1; i < e.length + 1; i++) {
      if (e[i - 1].equalsIgnoreCase(monOfDate))
        monOfDate = (i <= 9) ? String.valueOf("0").concat(String.valueOf(String.valueOf(i))) : String.valueOf(i); 
    } 
    sRetDate = String.valueOf(String.valueOf(String.valueOf(String.valueOf(ddOfDate).concat(String.valueOf("/"))).concat(String.valueOf(monOfDate))).concat(String.valueOf("/"))).concat(String.valueOf(yrOfdate));
    return sRetDate;
  }
  
  public String getddmmmyyyyDateFormat(String sDate) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sRetDate = null;
    String monOfDate = sDate.substring(3, 5);
    int monDate = Integer.parseInt(monOfDate);
    String yrOfdate = sDate.substring(6, 10);
    String ddOfDate = sDate.substring(0, 2);
    String[] e = { 
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
        "Nov", "Dec" };
    for (int i = 1; i < e.length + 1; i++) {
      if (i == monDate)
        monOfDate = e[i - 1]; 
    } 
    sRetDate = String.valueOf(String.valueOf(String.valueOf(String.valueOf(ddOfDate).concat(String.valueOf("-"))).concat(String.valueOf(monOfDate))).concat(String.valueOf("-"))).concat(String.valueOf(yrOfdate));
    return sRetDate;
  }
  
  public int getDayOfWeek(String sDate) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String sDdmmyyyy = oBO.getddmmyyyyDateFormat(sDate);
    int iDay = Integer.parseInt(sDdmmyyyy.substring(0, 2));
    int iMonth = Integer.parseInt(sDdmmyyyy.substring(3, 5));
    int iYear = Integer.parseInt(sDdmmyyyy.substring(6, 10));
    iMonth--;
    GregorianCalendar c = new GregorianCalendar(iYear, iMonth, iDay);
    int iDayOfWeek = c.get(7);
    iDayOfWeek = (iDayOfWeek == 1) ? 7 : (iDayOfWeek - 1);
    return iDayOfWeek;
  }
  
  public int getWeekOfMonth(String sDate) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String sDdmmyyyy = oBO.getddmmyyyyDateFormat(sDate);
    int iDay = Integer.parseInt(sDdmmyyyy.substring(0, 2));
    int iMonth = Integer.parseInt(sDdmmyyyy.substring(3, 5));
    int iYear = Integer.parseInt(sDdmmyyyy.substring(6, 10));
    iMonth--;
    GregorianCalendar c = new GregorianCalendar(iYear, iMonth, iDay);
    int iWeekOfMonth = c.get(4);
    return iWeekOfMonth;
  }
  
  public Date getNextDay(String sDate, int iDays) throws EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String sDdmmyyyy = oBO.getddmmyyyyDateFormat(sDate);
    int iDay = Integer.parseInt(sDdmmyyyy.substring(0, 2));
    int iMonth = Integer.parseInt(sDdmmyyyy.substring(3, 5));
    int iYear = Integer.parseInt(sDdmmyyyy.substring(6, 10));
    iMonth--;
    GregorianCalendar c = new GregorianCalendar(iYear, iMonth, iDay);
    while (iDays-- > 0) {
      if (iMonth == 3 || iMonth == 5 || iMonth == 8 || iMonth == 10) {
        if (iDay == 30) {
          iDay = 1;
          iMonth++;
        } else {
          iDay++;
        } 
      } else if (iMonth == 1) {
        if (iDay == 28) {
          if (c.isLeapYear(iYear)) {
            iDay++;
          } else {
            iDay = 1;
            iMonth++;
          } 
        } else if (iDay == 29) {
          iDay = 1;
          iMonth++;
        } else {
          iDay++;
        } 
      } else if (iDay == 31) {
        iDay = 1;
        if (iMonth == 11) {
          iMonth = 0;
          iYear++;
        } else {
          iMonth++;
        } 
      } else {
        iDay++;
      } 
      c.set(iYear, iMonth, iDay);
    } 
    Date dDate = new Date(c.getTimeInMillis());
    return dDate;
  }
  
  public Date getPreviousDay(String sDate, int iDays) throws EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String ddmmyyyy = oBO.getddmmyyyyDateFormat(sDate);
    int iDay = Integer.parseInt(ddmmyyyy.substring(0, 2));
    int iMonth = Integer.parseInt(ddmmyyyy.substring(3, 5));
    int iYear = Integer.parseInt(ddmmyyyy.substring(6, 10));
    iMonth--;
    GregorianCalendar c = new GregorianCalendar(iYear, iMonth, iDay);
    while (iDays-- > 0) {
      if (iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 8 || iMonth == 10) {
        if (iDay == 1) {
          iDay = 31;
          iMonth--;
        } else {
          iDay--;
        } 
      } else if (iMonth == 3) {
        if (iDay == 1) {
          if (c.isLeapYear(iYear)) {
            iDay = 29;
            iMonth--;
          } else {
            iDay = 28;
            iMonth--;
          } 
        } else {
          iDay--;
        } 
      } else if (iDay == 1) {
        iDay = 30;
        if (iMonth == 1) {
          iMonth = 12;
          iYear--;
        } else {
          iMonth--;
        } 
      } else {
        iDay--;
      } 
      c.set(iYear, iMonth, iDay);
    } 
    Date dDate = new Date(c.getTimeInMillis());
    return dDate;
  }
  
  public boolean checkTimeFormat(String sTime) throws EnrgiseSystemException {
    boolean sFlag = true;
    if (sTime.length() != 5 || !sTime.substring(2, 3).equalsIgnoreCase(":"))
      sFlag = false; 
    return sFlag;
  }
  
  public boolean checkTimeValue(String sTime) throws EnrgiseSystemException {
    boolean returnVal = true;
    String sHour = sTime.substring(0, 2);
    String sMin = sTime.substring(3, 5);
    int stHour = Integer.parseInt(sHour);
    int stMin = Integer.parseInt(sMin);
    if (stHour >= 24 || stHour < 0)
      returnVal = false; 
    if (stMin >= 60 || stMin < 0)
      returnVal = false; 
    return returnVal;
  }
  
  public int compareTime(String sRefTime, String sOrigTime) throws EnrgiseSystemException {
    if (null == sOrigTime || null == sRefTime)
      return -2; 
    String sRefHour = sRefTime.substring(0, 2);
    String sRefMin = sRefTime.substring(3, 5);
    String refTime = String.valueOf(sRefHour).concat(String.valueOf(sRefMin));
    String sOrigHour = sOrigTime.substring(0, 2);
    String sOrigMin = sOrigTime.substring(3, 5);
    String origTime = String.valueOf(sOrigHour).concat(String.valueOf(sOrigMin));
    int iRefTime = Integer.parseInt(refTime);
    int iOrigTime = Integer.parseInt(origTime);
    if (iRefTime == iOrigTime)
      return 0; 
    if (iOrigTime < iRefTime)
      return -1; 
    return 1;
  }
  
  public String getSysDate() throws EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    String sSysdate = EnrgiseUtil.convertToString(Calendar.getInstance().getTime());
    String ddmmyyyy = oBO.getddmmyyyyDateFormat(sSysdate);
    return ddmmyyyy;
  }
  
  public void checkMMYYYY(String yymm, ArrayList oErrorList) {
    if (yymm.length() != 6) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
    if (yymm.substring(2, 3).equals("0")) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
    int mon = Integer.parseInt(yymm.substring(0, 2));
    if (mon > 12 || mon < 1) {
      oErrorList.add(new EnrgiseApplicationException("hrms.common.yyyymm"));
      return;
    } 
  }
  
  public String getTimeDiff(String sRefTime, String sOrigTime) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmCommonBO oBO = new HrmCommonBO();
    return new String();
  }
  
  public void initializeBOImpl() {
    this.headerTable = "PUM_PO_HDR";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {}
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String sScreenMode) throws EnrgiseSystemException {
    return " ";
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return " ";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
}
