package wenrgise.workflow.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseMessageKeyException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.utility.MessageKey;
import wenrgise.common.utility.RecordMetaInfo;
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.utility.DBUtilitiesBean;
import wenrgise.workflow.bean.WflActMstDtlBean;

public class WflActivityMstBO extends BaseBO {
  public WflActivityMstBO() {}
  
  public WflActivityMstBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public RecordMetaInfo getWflActivityDtlMetaInfo() throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean objBean = new DBUtilitiesBean();
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 2, -5));
    arylstParameters.add(new DBObject(2, 2, 93));
    arylstParameters.add(new DBObject(3, 2, 12));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 4));
    ArrayList arylstOutArray = objBean.callProc(arylstParameters, "WflActivityMst.proc_GetActMstDtlMetaInfo(?,?,?,?,?)");
    RecordMetaInfo objRecordMetaInfo = new RecordMetaInfo();
    DBObject objTotalRecord = arylstOutArray.get(0);
    objRecordMetaInfo.setRecordCount(((Long)objTotalRecord.getObject()).longValue());
    DBObject oTimeObject = arylstOutArray.get(1);
    objRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    return objRecordMetaInfo;
  }
  
  public ArrayList getWflActivityDtlInfo(long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList arylstParameters = new ArrayList();
    DBUtilitiesBean objBean = new DBUtilitiesBean();
    Timestamp tmstWhenPicked = null;
    int count = 0;
    ArrayList arylstDetailList = null;
    arylstParameters = new ArrayList();
    arylstParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    arylstParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    arylstParameters.add(new DBObject(3, 2, -10));
    arylstParameters.add(new DBObject(4, 2, 12));
    arylstParameters.add(new DBObject(5, 2, 12));
    arylstParameters.add(new DBObject(6, 2, 4));
    ArrayList arylstOutArray = objBean.callProc(arylstParameters, "WflActivityMst.proc_GetActMstDtlInfo(?,?,?,?,?,?)");
    DBObject objOutObject = arylstOutArray.get(0);
    ArrayList arylstList = (ArrayList)objOutObject.getObject();
    if (arylstList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.workflow.norecordfound", "M"); 
    QueryRow objRow = null;
    HashMap objColumns = null;
    Iterator itrIt = arylstList.iterator();
    while (itrIt.hasNext()) {
      if (count == 0)
        arylstDetailList = new ArrayList(); 
      count++;
      objRow = itrIt.next();
      WflActMstDtlBean oWflActMstDtlBean = new WflActMstDtlBean();
      oWflActMstDtlBean.setDetailId(objRow.get("id").getString());
      oWflActMstDtlBean.setTxtAttrCode(objRow.get("activity_code").getString());
      oWflActMstDtlBean.setTxtAttrDesc(objRow.get("activity_desc").getString());
      arylstDetailList.add(oWflActMstDtlBean);
    } 
    return arylstDetailList;
  }
  
  private void saveWflActivityDtls(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bUpdate = false;
    boolean bDelete = false;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      WflActMstDtlBean oWflActMstDtlBean = oIt.next();
      if (oWflActMstDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("WflActivityMst.proc_UpsertWflActivityMstDtl(?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "N"));
        oParameters.add(new DBObject(2, 1, 12, null));
        oParameters.add(new DBObject(3, 1, 12, oWflActMstDtlBean.getTxtAttrCode()));
        oParameters.add(new DBObject(4, 1, 12, oWflActMstDtlBean.getTxtAttrDesc()));
        oParameters.add(new DBObject(5, 1, 12, this.oUserInfo.getSiteId()));
        oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserId()));
        oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oWflActMstDtlBean.getStatus().equals("U")) {
        if (!bUpdate) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("WflActivityMst.proc_UpsertWflActivityMstDtl(?,?,?,?,?,?,?)");
          bUpdate = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "U"));
        oParameters.add(new DBObject(2, 1, 12, oWflActMstDtlBean.getDetailId()));
        oParameters.add(new DBObject(3, 1, 12, oWflActMstDtlBean.getTxtAttrCode()));
        oParameters.add(new DBObject(4, 1, 12, oWflActMstDtlBean.getTxtAttrDesc()));
        oParameters.add(new DBObject(5, 1, 12, this.oUserInfo.getSiteId()));
        oParameters.add(new DBObject(6, 1, 12, this.oUserInfo.getUserId()));
        oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oWflActMstDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("WflActivityMst.proc_DeleteWflActivityMstDtl(?)");
          bDelete = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, oWflActMstDtlBean.getDetailId()));
        oBean1.addToBatch(oParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bUpdate)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public void initializeBOImpl() {
    this.headerTable = "WFL_ACTIVITY_MST";
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    return returnString;
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("WflActivityMst"))
      saveWflActivityDtls(sHeaderPrimaryKey, oDetailBeanArray); 
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    System.out.println("Update Nischt!");
  }
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl Nischt!";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("WflActivityMst")) {
        Iterator oIt = oDetailBeanArray.iterator();
        int count = 1;
        while (oIt.hasNext()) {
          WflActMstDtlBean oWflActMstDtlBean = oIt.next();
          if (!oWflActMstDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatoryDtls(oWflActMstDtlBean, count, oDetailBeanArray); 
          count++;
        } 
      }  
    reportError(oErrorList);
  }
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oErrorList = new ArrayList();
    if (bDetailDataChanged) {
      if (!sScreenMode.equalsIgnoreCase("D"))
        if (sScreenName.equalsIgnoreCase("WflActivityMst")) {
          Iterator oIt1 = oDetailBeanArray.iterator();
          checkUniqueMaterialDtl(oBaseHeaderBean.getHeaderPrimaryKey(), oDetailBeanArray, oErrorList);
          EnrgiseUtil.checkDuplicate(oDetailBeanArray, "txtAttrCode", "wfl.WflActivityMst.attrCode", oErrorList, true);
        }  
      reportError(oErrorList);
    } 
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  private void checkMandatoryDtls(WflActMstDtlBean oWflActMstDtlBean, int count, ArrayList oDetailBeanArray) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oList = new ArrayList();
    if (!EnrgiseUtil.checkString(oWflActMstDtlBean.getTxtAttrCode())) {
      ArrayList oParam = new ArrayList();
      MessageKey oMessageKey = new MessageKey("wfl.WflActivityMst.attrCode");
      oParam.add(oMessageKey);
      oParam.add(new Integer(count));
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParam, "E"));
    } 
    if (!EnrgiseUtil.checkString(oWflActMstDtlBean.getTxtAttrDesc())) {
      ArrayList oParam = new ArrayList();
      MessageKey oMessageKey = new MessageKey("wfl.WflActivityMst.attrDesc");
      oParam.add(oMessageKey);
      oParam.add(new Integer(count));
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryFieldMissing", oParam, "E"));
    } 
    reportError(oList);
  }
  
  private void checkUniqueMaterialDtl(String sHeaderPrimaryKey, ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    Iterator oIt1 = oDetailBeanArray.iterator();
    int rowCount = 1;
    while (oIt1.hasNext()) {
      WflActMstDtlBean oWflActMstDtlBean = oIt1.next();
      if (!EnrgiseUtil.checkString(oWflActMstDtlBean.getDetailId())) {
        String sQuery = String.valueOf(String.valueOf(" Select id as ID from WFL_ACTIVITY_MST where activity_code='").concat(String.valueOf(oWflActMstDtlBean.getTxtAttrCode()))).concat(String.valueOf("' "));
        DBUtilitiesBean oBean = new DBUtilitiesBean();
        ArrayList oList = oBean.executeQuery(sQuery);
        Iterator oIt = oList.iterator();
        if (oIt.hasNext()) {
          ArrayList oParams = new ArrayList();
          MessageKey oMessageKey = new MessageKey("wfl.WflActivityMst.attrCode");
          oParams.add(oMessageKey);
          oParams.add(new Integer(rowCount));
          oErrorList.add(new EnrgiseMessageKeyException("wenrgise.workflow.detail.UniqueKeyConstraint", oParams));
        } 
      } 
      rowCount++;
    } 
  }
}
