package wenrgise.hrms.ejb.business;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.sql.DataSource;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.DateUtility;
import wenrgise.common.utility.RecordMetaInfo;
import wenrgise.common.utility.ServiceLocator;
import wenrgise.common.utility.UserInfo;
import wenrgise.common.vo.BaseHeaderVO;
import wenrgise.common.vo.LovQueryVO;
import wenrgise.common.vo.LovVO;
import wenrgise.ejb.common.helper.DBObject;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;
import wenrgise.hrms.bean.HrmAdhocReportHdrBean;
import wenrgise.hrms.bean.HrmLvMstDtlBean;
import wenrgise.hrms.bean.HrmLvMstHdrBean;
import wenrgise.hrms.vo.HrmAdhocReportQVO;

public class HrmAdhocReportBO extends HrmBaseBO {
  public HrmAdhocReportBO() {}
  
  public HrmAdhocReportBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getHrmAdhocReportHdrMetaInfo(HrmAdhocReportQVO oHrmAdhocReportQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmAdhocReportQVO.getHeaderPrimaryKey()));
    arylstParam.add(new DBObject(2, 1, 12, oHrmAdhocReportQVO.getCode()));
    arylstParam.add(new DBObject(3, 1, 12, oHrmAdhocReportQVO.getCategory_tag()));
    arylstParam.add(new DBObject(4, 2, -5));
    arylstParam.add(new DBObject(5, 2, 93));
    arylstParam.add(new DBObject(6, 2, 12));
    arylstParam.add(new DBObject(7, 2, 12));
    arylstParam.add(new DBObject(8, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_hrmadhocreportCount(?,?,?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTimeObject = arylstOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    DBObject oTotalRecord = arylstOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    return oRecordMetaInfo;
  }
  
  public ArrayList getHrmAdhocReportHeaderInfo(HrmAdhocReportQVO oHrmAdhocReportQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList arylstHeaderList = null;
    if (oHrmAdhocReportQVO == null)
      oHrmAdhocReportQVO = new HrmAdhocReportQVO(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    arylstParam.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    arylstParam.add(new DBObject(3, 1, 12, oHrmAdhocReportQVO.getHeaderPrimaryKey()));
    arylstParam.add(new DBObject(4, 1, 12, oHrmAdhocReportQVO.getCode()));
    arylstParam.add(new DBObject(5, 1, 12, oHrmAdhocReportQVO.getCategory_tag()));
    arylstParam.add(new DBObject(6, 2, -10));
    arylstParam.add(new DBObject(7, 2, 12));
    arylstParam.add(new DBObject(8, 2, 12));
    arylstParam.add(new DBObject(9, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_hrmadhocreportDtl(?,?,?,?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.hrms.norecordfound", "M"); 
    QueryRow oRow = null;
    HashMap oColumns = null;
    Iterator itrBean = arylstList.iterator();
    while (itrBean.hasNext()) {
      if (count == 0)
        arylstHeaderList = new ArrayList(); 
      count++;
      oRow = itrBean.next();
      HrmAdhocReportHdrBean oReportHeader = new HrmAdhocReportHdrBean();
      oReportHeader.setHeaderPrimaryKey(oRow.get("ID").getString());
      oReportHeader.setId(oRow.get("ID").getString());
      oReportHeader.setCode(oRow.get("CODE").getString());
      oReportHeader.setQuery_desc(oRow.get("query_desc").getString());
      oReportHeader.setCategory_tag(oRow.get("category_tag").getString());
      oReportHeader.setQuery(oRow.get("query_dtl").getString());
      oReportHeader.setParam1(oRow.get("param1").getString());
      oReportHeader.setParam2(oRow.get("param2").getString());
      oReportHeader.setParam3(oRow.get("param3").getString());
      oReportHeader.setParam4(oRow.get("param4").getString());
      oReportHeader.setParam5(oRow.get("param5").getString());
      oReportHeader.setParam6(oRow.get("param6").getString());
      oReportHeader.setParam7(oRow.get("param7").getString());
      oReportHeader.setParam8(oRow.get("param8").getString());
      oReportHeader.setParam9(oRow.get("param9").getString());
      oReportHeader.setParam10(oRow.get("param10").getString());
      arylstHeaderList.add(oReportHeader);
    } 
    return arylstHeaderList;
  }
  
  public void initializeBOImpl() {
    this.headerTable = "HRM_LEAVE_MST";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList arylstDetailBeanArray) throws EnrgiseSystemException {}
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    DateUtility d = new DateUtility();
    HrmAdhocReportHdrBean oHrmAdhocReportHdrBean = (HrmAdhocReportHdrBean)oBaseHeaderBean;
    if (ScreenMode.equalsIgnoreCase("N")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "i"));
      arylstParam.add(new DBObject(2, 1, 12, oHrmAdhocReportHdrBean.getId()));
      arylstParam.add(new DBObject(3, 1, 12, oHrmAdhocReportHdrBean.getCode()));
      arylstParam.add(new DBObject(4, 1, 12, oHrmAdhocReportHdrBean.getQuery_desc()));
      arylstParam.add(new DBObject(5, 1, 12, oHrmAdhocReportHdrBean.getCategory_tag()));
      arylstParam.add(new DBObject(6, 1, 12, oHrmAdhocReportHdrBean.getQuery()));
      arylstParam.add(new DBObject(7, 1, 12, oHrmAdhocReportHdrBean.getParam1()));
      arylstParam.add(new DBObject(8, 1, 12, oHrmAdhocReportHdrBean.getParam2()));
      arylstParam.add(new DBObject(9, 1, 12, oHrmAdhocReportHdrBean.getParam3()));
      arylstParam.add(new DBObject(10, 1, 12, oHrmAdhocReportHdrBean.getParam4()));
      arylstParam.add(new DBObject(11, 1, 12, oHrmAdhocReportHdrBean.getParam5()));
      arylstParam.add(new DBObject(12, 1, 12, oHrmAdhocReportHdrBean.getParam6()));
      arylstParam.add(new DBObject(13, 1, 12, oHrmAdhocReportHdrBean.getParam7()));
      arylstParam.add(new DBObject(14, 1, 12, oHrmAdhocReportHdrBean.getParam8()));
      arylstParam.add(new DBObject(15, 1, 12, oHrmAdhocReportHdrBean.getParam9()));
      arylstParam.add(new DBObject(16, 1, 12, oHrmAdhocReportHdrBean.getParam10()));
      arylstParam.add(new DBObject(17, 2, 12));
      arylstParam.add(new DBObject(18, 2, 12));
      arylstParam.add(new DBObject(19, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_Upserthrmadhocreport(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      arylstParam = new ArrayList();
      arylstParam.add(new DBObject(1, 1, 12, "u"));
      arylstParam.add(new DBObject(2, 1, 12, oHrmAdhocReportHdrBean.getId()));
      arylstParam.add(new DBObject(3, 1, 12, oHrmAdhocReportHdrBean.getCode()));
      arylstParam.add(new DBObject(4, 1, 12, oHrmAdhocReportHdrBean.getQuery_desc()));
      arylstParam.add(new DBObject(5, 1, 12, oHrmAdhocReportHdrBean.getCategory_tag()));
      arylstParam.add(new DBObject(6, 1, 12, oHrmAdhocReportHdrBean.getQuery()));
      arylstParam.add(new DBObject(7, 1, 12, oHrmAdhocReportHdrBean.getParam1()));
      arylstParam.add(new DBObject(8, 1, 12, oHrmAdhocReportHdrBean.getParam2()));
      arylstParam.add(new DBObject(9, 1, 12, oHrmAdhocReportHdrBean.getParam3()));
      arylstParam.add(new DBObject(10, 1, 12, oHrmAdhocReportHdrBean.getParam4()));
      arylstParam.add(new DBObject(11, 1, 12, oHrmAdhocReportHdrBean.getParam5()));
      arylstParam.add(new DBObject(12, 1, 12, oHrmAdhocReportHdrBean.getParam6()));
      arylstParam.add(new DBObject(13, 1, 12, oHrmAdhocReportHdrBean.getParam7()));
      arylstParam.add(new DBObject(14, 1, 12, oHrmAdhocReportHdrBean.getParam8()));
      arylstParam.add(new DBObject(15, 1, 12, oHrmAdhocReportHdrBean.getParam9()));
      arylstParam.add(new DBObject(16, 1, 12, oHrmAdhocReportHdrBean.getParam10()));
      arylstParam.add(new DBObject(17, 2, 12));
      arylstParam.add(new DBObject(18, 2, 12));
      arylstParam.add(new DBObject(19, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_Upserthrmadhocreport(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = (String)oOutObject.getObject();
      System.out.println(returnString);
    } else if (ScreenMode.equalsIgnoreCase("D")) {
      arylstParam.add(new DBObject(1, 1, 12, oHrmAdhocReportHdrBean.getHeaderPrimaryKey()));
      arylstParam.add(new DBObject(2, 2, 12));
      arylstParam.add(new DBObject(3, 2, 12));
      arylstParam.add(new DBObject(4, 2, 4));
      ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_Deletehrmadhocreport(?,?,?,?)");
      DBObject oOutObject = arylstOutArray.get(0);
      returnString = null;
    } 
    return returnString;
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update ille!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public LovVO getQueryLOVData(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    LovVO oLovVO = new LovVO();
    ArrayList arylstHeaderList = new ArrayList();
    arylstHeaderList.add("hrm.HrmAdhocReport.id");
    arylstHeaderList.add("hrm.HrmAdhocReport.code");
    arylstHeaderList.add("hrm.HrmAdhocReport.tag");
    oLovVO.setHeaderList(arylstHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    String sQuerySearch1 = new String();
    String sQuerySearch2 = new String();
    if (oLovQueryVO.getSearchField1() != null)
      sQuerySearch1 = oLovQueryVO.getSearchField1(); 
    if (oLovQueryVO.getSearchField2() != null)
      sQuerySearch2 = oLovQueryVO.getSearchField2(); 
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, sQuerySearch1));
    arylstParam.add(new DBObject(2, 1, 12, sQuerySearch2));
    arylstParam.add(new DBObject(3, 2, -10));
    arylstParam.add(new DBObject(4, 2, 12));
    arylstParam.add(new DBObject(5, 2, 12));
    arylstParam.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_GetQueryCodeLOV(?,?,?,?,?,?)");
    DBObject oOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)oOutObject.getObject();
    QueryRow oRow = null;
    Iterator iter = arylstList.iterator();
    while (iter.hasNext()) {
      if (count == 0)
        arylstList = new ArrayList(); 
      count++;
      oRow = iter.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("ID").getString());
      oLOVBean.setDetailField2(oRow.get("CODE").getString());
      oLOVBean.setDetailField3(oRow.get("CATEGORY_TAG").getString());
      arylstList.add(oLOVBean);
    } 
    oLovVO.setDetailList(arylstList);
    return oLovVO;
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    reportError(arylstErrorList);
  }
  
  private void checkMandatoryHeader(HrmLvMstHdrBean oHrmLvMstHdrBean) throws EnrgiseSystemException, EnrgiseApplicationException {}
  
  private void checkMandatory(HrmLvMstDtlBean oHrmLvMstDtlBean, int iCount, ArrayList arylstErrorList) throws EnrgiseSystemException, EnrgiseApplicationException {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList arylstDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstErrorList = new ArrayList();
    reportError(arylstErrorList);
  }
  
  public void downloadAdhocReport(HrmAdhocReportHdrBean oHrmAdhocReportHdrBean, WritableSheet s) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParam = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    arylstParam = new ArrayList();
    arylstParam.add(new DBObject(1, 1, 12, oHrmAdhocReportHdrBean.getId()));
    arylstParam.add(new DBObject(2, 1, 12, oHrmAdhocReportHdrBean.getParamval1()));
    arylstParam.add(new DBObject(3, 1, 12, oHrmAdhocReportHdrBean.getParamval2()));
    arylstParam.add(new DBObject(4, 1, 12, oHrmAdhocReportHdrBean.getParamval3()));
    arylstParam.add(new DBObject(5, 1, 12, oHrmAdhocReportHdrBean.getParamval4()));
    arylstParam.add(new DBObject(6, 1, 12, oHrmAdhocReportHdrBean.getParamval5()));
    arylstParam.add(new DBObject(7, 1, 12, oHrmAdhocReportHdrBean.getParamval6()));
    arylstParam.add(new DBObject(8, 1, 12, oHrmAdhocReportHdrBean.getParamval7()));
    arylstParam.add(new DBObject(9, 1, 12, oHrmAdhocReportHdrBean.getParamval8()));
    arylstParam.add(new DBObject(10, 1, 12, oHrmAdhocReportHdrBean.getParamval9()));
    arylstParam.add(new DBObject(11, 1, 12, oHrmAdhocReportHdrBean.getParamval10()));
    arylstParam.add(new DBObject(12, 2, 12));
    arylstParam.add(new DBObject(13, 2, 12));
    arylstParam.add(new DBObject(14, 2, 12));
    arylstParam.add(new DBObject(15, 2, 4));
    ArrayList arylstOutArray = oBean.callProc(arylstParam, "hrmadhocreport.proc_Executehrmadhocreport(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    DBObject outObject = arylstOutArray.get(0);
    String query = (String)outObject.getObject();
    DataSource oDataSource = null;
    Statement stmt = null;
    Connection conn = null;
    ResultSet rs = null;
    try {
      oDataSource = (DataSource)ServiceLocator.getLocator().getService("jdbc/conDS");
      conn = oDataSource.getConnection();
      stmt = conn.createStatement();
      rs = stmt.executeQuery(query);
      ResultSetMetaData rsmd = rs.getMetaData();
      int numColumns = rsmd.getColumnCount();
      int rowPos = 0;
      WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
      WritableCellFormat cf = new WritableCellFormat(wf);
      cf.setWrap(false);
      ArrayList heading = new ArrayList(numColumns);
      for (int i = 1; i < numColumns + 1; i++) {
        String columnName = rsmd.getColumnName(i);
        s.addCell((WritableCell)new Label(i - 1, rowPos, columnName, (CellFormat)cf));
        heading.add(i - 1, columnName);
      } 
      wf = new WritableFont(WritableFont.ARIAL, 8, WritableFont.NO_BOLD);
      while (rs.next()) {
        rowPos++;
        int colPos = -1;
        Iterator itrHeading = heading.iterator();
        while (colPos + 1 < heading.size()) {
          colPos++;
          String sHeading = heading.get(colPos).toString();
          cf = new WritableCellFormat(wf);
          cf.setWrap(false);
          s.addCell((WritableCell)new Label(colPos, rowPos, rs.getString(sHeading), (CellFormat)cf));
        } 
      } 
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      try {
        if (stmt != null)
          stmt.close(); 
        if (rs != null)
          rs.close(); 
        if (conn != null)
          conn.close(); 
      } catch (Exception e) {
        e.printStackTrace();
      } 
    } 
  }
}
