package wenrgise.workflow.ejb.business;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import wenrgise.common.bean.BaseHeaderBean;
import wenrgise.common.bean.LOVBean;
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.common.vo.BaseHeaderVO;
import wenrgise.common.vo.LovQueryVO;
import wenrgise.common.vo.LovVO;
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.WflDOPGrpDtlBean;
import wenrgise.workflow.bean.WflDOPGrpHdrBean;
import wenrgise.workflow.vo.WflDOPGrpQVO;

public class WflDOPGrpBO extends BaseBO {
  public WflDOPGrpBO() {}
  
  public WflDOPGrpBO(UserInfo oUserInfo) {
    super(oUserInfo);
  }
  
  public void updateHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {}
  
  public String saveNewHeaderImpl(BaseHeaderBean oBaseHeaderBean) throws EnrgiseSystemException {
    return "saveNewHeaderImpl ille!";
  }
  
  public void additionalFieldValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    WflDOPGrpHdrBean oWflDOPGrpHdrBean = (WflDOPGrpHdrBean)oBaseHeaderBean;
    ArrayList oErrorList = new ArrayList();
    if (bHeaderDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        checkMandatoryHeader(oWflDOPGrpHdrBean);  
    if (bDetailDataChanged)
      if (sScreenName.equalsIgnoreCase("WflDOPGrp")) {
        Iterator oIt = oDetailBeanArray.iterator();
        int count = 1;
        while (oIt.hasNext()) {
          WflDOPGrpDtlBean oWflDOPGrpDtlBean = oIt.next();
          if (!oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("D"))
            checkMandatoryDtls(oWflDOPGrpDtlBean, count, oDetailBeanArray); 
          count++;
        } 
      }  
    reportError(oErrorList);
  }
  
  private void checkMandatoryHeader(WflDOPGrpHdrBean oWflDOPGrpHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    if (!EnrgiseUtil.checkString(oWflDOPGrpHdrBean.getGrpCode())) {
      MessageKey oMessageKey = new MessageKey("WFL.WflDOPGrp.grpCode");
      ArrayList oParam = new ArrayList();
      oParam.add(oMessageKey);
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.header.mandatoryFieldMissing", oParam, "E"));
    } 
    reportError(oList);
  }
  
  public void additionalTimestampValidationImpl(BaseHeaderBean param1, Timestamp param2, String param3, String param4, boolean param5, ArrayList param6, boolean param7, Timestamp param8) {}
  
  public void additionalBusinessValidationImpl(BaseHeaderBean oBaseHeaderBean, Timestamp oWhenPicked, String sScreenName, String sScreenMode, boolean bHeaderDataChanged, ArrayList oDetailBeanArray, boolean bDetailDataChanged, Timestamp oDetailPicked) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oList = new ArrayList();
    if (bHeaderDataChanged)
      checkUniqueHeader((WflDOPGrpHdrBean)oBaseHeaderBean); 
    if (bDetailDataChanged)
      if (!sScreenMode.equalsIgnoreCase("D"))
        if (sScreenName.equalsIgnoreCase("WflDOPGrp")) {
          Iterator oIt1 = oDetailBeanArray.iterator();
          EnrgiseUtil.checkDuplicate(oDetailBeanArray, "txtempNo", "WFL.WflDOPGrp.empNo", oList, true);
          checkUniqueDOPGrpDtl(oBaseHeaderBean.getHeaderPrimaryKey(), oDetailBeanArray, oList);
          checkDefaultMember(oBaseHeaderBean.getHeaderPrimaryKey(), oDetailBeanArray, oList);
        }   
    reportError(oList);
  }
  
  public void initializeBOImpl() {
    this.headerTable = "wfl_dop_groups";
  }
  
  public void saveDetailImpl(String sHeaderPrimaryKey, String sScreenName, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    if (sScreenName.equalsIgnoreCase("WflDOPGrp"))
      saveWflDOPGrpDtls(sHeaderPrimaryKey, oDetailBeanArray); 
  }
  
  public String saveHeaderImpl(BaseHeaderBean oBaseHeaderBean, String ScreenMode) throws EnrgiseSystemException {
    String returnString = null;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    WflDOPGrpHdrBean oWflDOPGrpHdrBean = (WflDOPGrpHdrBean)oBaseHeaderBean;
    String sGrpFlag = String.valueOf(oWflDOPGrpHdrBean.getGrpFlag()).equalsIgnoreCase("on") ? "Y" : "N";
    if (ScreenMode.equalsIgnoreCase("N")) {
      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, oWflDOPGrpHdrBean.getGrpCode()));
      oParameters.add(new DBObject(4, 1, 12, oWflDOPGrpHdrBean.getGrpDesc()));
      oParameters.add(new DBObject(5, 1, 12, oWflDOPGrpHdrBean.getRemarks()));
      oParameters.add(new DBObject(6, 1, 12, sGrpFlag));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "WflDOPGroupMst.proc_UpsertWflDopGrpHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("U")) {
      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, oWflDOPGrpHdrBean.getGrpCode()));
      oParameters.add(new DBObject(4, 1, 12, oWflDOPGrpHdrBean.getGrpDesc()));
      oParameters.add(new DBObject(5, 1, 12, oWflDOPGrpHdrBean.getRemarks()));
      oParameters.add(new DBObject(6, 1, 12, sGrpFlag));
      oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getUserId()));
      oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
      oParameters.add(new DBObject(10, 2, 12));
      oParameters.add(new DBObject(11, 2, 12));
      oParameters.add(new DBObject(12, 2, 12));
      oParameters.add(new DBObject(13, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "WflDOPGroupMst.proc_UpsertWflDopGrpHdr(?,?,?,?,?,?,?,?,?,?,?,?,?)");
      DBObject oOutObject = oOutArray.get(0);
      returnString = (String)oOutObject.getObject();
    } else if (ScreenMode.equalsIgnoreCase("D")) {
      oParameters.add(new DBObject(1, 1, 12, oWflDOPGrpHdrBean.getHeaderPrimaryKey()));
      oParameters.add(new DBObject(2, 2, 12));
      oParameters.add(new DBObject(3, 2, 12));
      oParameters.add(new DBObject(4, 2, 4));
      ArrayList oOutArray = oBean.callProc(oParameters, "WflDOPGroupMst.proc_DeleteWflDopGrpHdr(?,?,?,?)");
    } 
    return returnString;
  }
  
  public void saveWflDOPGrpDtls(String sHeaderPrimaryKey, ArrayList oDetailBeanArray) throws EnrgiseSystemException {
    boolean bInsert = false;
    boolean bDelete = false;
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    DBUtilitiesBean oBean1 = null;
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      WflDOPGrpDtlBean oWflDOPGrpDtlBean = oIt.next();
      String sDfliMem = String.valueOf(oWflDOPGrpDtlBean.getDfltMembr()).equalsIgnoreCase("on") ? "Y" : "N";
      if (oWflDOPGrpDtlBean.getStatus().equals("N")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("WflDOPGroupMst.proc_UpsertWflDopGrpMembers(?,?,?,?,?,?,?,?,?)");
          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, sHeaderPrimaryKey));
        oParameters.add(new DBObject(4, 1, 12, oWflDOPGrpDtlBean.getEmpId()));
        oParameters.add(new DBObject(5, 1, 12, oWflDOPGrpDtlBean.getTxtHiLevel()));
        oParameters.add(new DBObject(6, 1, 12, sDfliMem));
        oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getUserId()));
        oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oWflDOPGrpDtlBean.getStatus().equals("U")) {
        if (!bInsert) {
          oBean = new DBUtilitiesBean();
          oBean.createBatch("WflDOPGroupMst.proc_UpsertWflDopGrpMembers(?,?,?,?,?,?,?,?,?)");
          bInsert = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, "U"));
        oParameters.add(new DBObject(2, 1, 12, oWflDOPGrpDtlBean.getDetailId()));
        oParameters.add(new DBObject(3, 1, 12, sHeaderPrimaryKey));
        oParameters.add(new DBObject(4, 1, 12, oWflDOPGrpDtlBean.getEmpId()));
        oParameters.add(new DBObject(5, 1, 12, oWflDOPGrpDtlBean.getTxtHiLevel()));
        oParameters.add(new DBObject(6, 1, 12, sDfliMem));
        oParameters.add(new DBObject(7, 1, 12, this.oUserInfo.getSiteId()));
        oParameters.add(new DBObject(8, 1, 12, this.oUserInfo.getUserId()));
        oParameters.add(new DBObject(9, 1, 12, this.oUserInfo.getSiteId()));
        oBean.addToBatch(oParameters);
        continue;
      } 
      if (oWflDOPGrpDtlBean.getStatus().equals("D")) {
        if (!bDelete) {
          oBean1 = new DBUtilitiesBean();
          oBean1.createBatch("WflDOPGroupMst.proc_DeleteWflDopGrpMembers(?)");
          bDelete = true;
        } 
        oParameters = new ArrayList();
        oParameters.add(new DBObject(1, 1, 12, oWflDOPGrpDtlBean.getDetailId()));
        oBean1.addToBatch(oParameters);
      } 
    } 
    if (bInsert)
      oBean.executeBatch(); 
    if (bDelete)
      oBean1.executeBatch(); 
  }
  
  public RecordMetaInfo getWflDOPGrpHdrMetaInfo(WflDOPGrpQVO oWflDOPGrpQVO) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    Timestamp oWhenPicked = null;
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, oWflDOPGrpQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    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, "WflDOPGroupMst.proc_GetWFlDOPHdrMetaInfo(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTimeObject = oOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    DBObject oTotalRecord = oOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    return oRecordMetaInfo;
  }
  
  public ArrayList getWflDOPGrpHdrInfo(WflDOPGrpQVO oWflDOPGrpQVO, long lStartPosition, long lLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    int count = 0;
    BaseHeaderVO oBaseHeaderVO = new BaseHeaderVO();
    ArrayList oHeaderList = null;
    if (oWflDOPGrpQVO == null)
      oWflDOPGrpQVO = new WflDOPGrpQVO(); 
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, -5, new Long(lStartPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, oWflDOPGrpQVO.getHeaderPrimaryKey()));
    oParameters.add(new DBObject(4, 2, -10));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "WflDOPGroupMst.proc_GetWFlDOPHdrInfo(?,?,?,?,?,?,?)");
    DBObject oOutObject = oOutArray.get(0);
    ArrayList oList = (ArrayList)oOutObject.getObject();
    if (oList.size() == 0)
      throw new EnrgiseApplicationException("wenrgise.workflow.norecordfound", "M"); 
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oHeaderList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      WflDOPGrpHdrBean oWflDOPGrpHdrBean = new WflDOPGrpHdrBean();
      oWflDOPGrpHdrBean.setHeaderPrimaryKey(oRow.get("ID").getString());
      oWflDOPGrpHdrBean.setGrpCode(oRow.get("GrCode").getString());
      oWflDOPGrpHdrBean.setGrpDesc(oRow.get("GrType").getString());
      oWflDOPGrpHdrBean.setRemarks(oRow.get("Remarks").getString());
      oWflDOPGrpHdrBean.setGrpFlag(String.valueOf(oRow.get("ValFlag").getString()).equalsIgnoreCase("Y") ? "on" : "off");
      oHeaderList.add(oWflDOPGrpHdrBean);
    } 
    return oHeaderList;
  }
  
  public RecordMetaInfo getWflDOPGrpDtlMetaInfo(String sPrimaryKey) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, new String(sPrimaryKey)));
    oParameters.add(new DBObject(2, 2, -5));
    oParameters.add(new DBObject(3, 2, 93));
    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, "WflDOPGroupMst.proc_GetWFlDOPGrpDtlMetaInfo(?,?,?,?,?,?)");
    RecordMetaInfo oRecordMetaInfo = new RecordMetaInfo();
    DBObject oTotalRecord = oOutArray.get(0);
    oRecordMetaInfo.setRecordCount(((Long)oTotalRecord.getObject()).longValue());
    DBObject oTimeObject = oOutArray.get(1);
    oRecordMetaInfo.setOWhenPicked((Timestamp)oTimeObject.getObject());
    System.out.println(oRecordMetaInfo);
    return oRecordMetaInfo;
  }
  
  public ArrayList getWflDOPGrpDtlInfo(String lPrimaryKey, long lDetailFirstPosition, long lDetailLastPosition) throws EnrgiseApplicationException, EnrgiseSystemException {
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    QueryRow oRow = null;
    ArrayList oList = null;
    Iterator oIt = null;
    ArrayList oDtlList = new ArrayList();
    ArrayList oParameters = new ArrayList();
    oParameters.add(new DBObject(1, 1, 12, new Long(lDetailFirstPosition)));
    oParameters.add(new DBObject(2, 1, -5, new Long(lDetailLastPosition)));
    oParameters.add(new DBObject(3, 1, 12, lPrimaryKey));
    oParameters.add(new DBObject(4, 2, -10));
    oParameters.add(new DBObject(5, 2, 12));
    oParameters.add(new DBObject(6, 2, 12));
    oParameters.add(new DBObject(7, 2, 4));
    ArrayList oOutArray = oBean.callProc(oParameters, "WflDOPGroupMst.proc_GetWFlDOPGrpDtlInfo(?,?,?,?,?,?,?)");
    oList = (ArrayList)((DBObject)oOutArray.get(0)).getObject();
    oIt = oList.iterator();
    while (oIt.hasNext()) {
      oRow = oIt.next();
      WflDOPGrpDtlBean oWflDOPGrpDtlBean = new WflDOPGrpDtlBean();
      oWflDOPGrpDtlBean.setDetailId(oRow.get("Id").getString());
      oWflDOPGrpDtlBean.setTxtempNo(oRow.get("EmpNo").getString());
      oWflDOPGrpDtlBean.setEmpId(oRow.get("e_per_dtl_id").getString());
      oWflDOPGrpDtlBean.setTxtEmpName(oRow.get("MEMNAME").getString());
      oWflDOPGrpDtlBean.setTxtHiLevel(oRow.get("Hlevel").getString());
      oWflDOPGrpDtlBean.setDfltMembr(String.valueOf(oRow.get("DfltMem").getString()).equalsIgnoreCase("Y") ? "on" : "off");
      oDtlList.add(oWflDOPGrpDtlBean);
    } 
    return oDtlList;
  }
  
  public LovVO getWflDOPGrpCodeQLOV(LovQueryVO oLovQueryVO) throws EnrgiseSystemException {
    String sQuery = "select distinct wdg.id as id,wdg.group_code as GrCode from wfl_dop_groups wdg";
    if (oLovQueryVO.getSearchField1() != null)
      sQuery = String.valueOf(String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" where  upper(wdg.group_code) LIKE upper('%"))).concat(String.valueOf(oLovQueryVO.getSearchField1()))).concat(String.valueOf("%') ")); 
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    LovVO oLovVO = new LovVO();
    ArrayList oHeaderList = new ArrayList();
    oHeaderList.add("");
    oHeaderList.add("WFL.WflDOPGrp.grpCode");
    oLovVO.setHeaderList(oHeaderList);
    ArrayList arylstVisibility = new ArrayList();
    arylstVisibility.add("H");
    arylstVisibility.add("V");
    oLovVO.setVisibilityList(arylstVisibility);
    int count = 0;
    QueryRow oRow = null;
    Iterator oIt = oList.iterator();
    while (oIt.hasNext()) {
      if (count == 0)
        oList = new ArrayList(); 
      count++;
      oRow = oIt.next();
      LOVBean oLOVBean = new LOVBean();
      oLOVBean.setDetailField1(oRow.get("id").getString());
      oLOVBean.setDetailField2(oRow.get("GrCode").getString());
      oList.add(oLOVBean);
    } 
    oLovVO.setDetailList(oList);
    return oLovVO;
  }
  
  private void checkDefaultMember(String sHeaderPrimKey, ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList oParameters = new ArrayList();
    DBUtilitiesBean oBean = null;
    ArrayList oOutArray = null;
    Iterator oDetailBeanIterator = oDetailBeanArray.iterator();
    String oBeanUpdate = new String();
    boolean flag = false;
    while (oDetailBeanIterator.hasNext()) {
      WflDOPGrpDtlBean oWflDOPGrpDtlBean = oDetailBeanIterator.next();
      if (oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("U") || oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("D")) {
        if (flag == true)
          oBeanUpdate = String.valueOf(oBeanUpdate).concat(String.valueOf(",")); 
        oBeanUpdate = String.valueOf(oBeanUpdate).concat(String.valueOf(oWflDOPGrpDtlBean.getDetailId()));
        flag = true;
      } 
    } 
    String sQuery = new String();
    if (!oBeanUpdate.equalsIgnoreCase("")) {
      sQuery = String.valueOf(String.valueOf(String.valueOf(String.valueOf(" select t.id from wfl_dop_group_members t where t.dop_group_id=").concat(String.valueOf(sHeaderPrimKey))).concat(String.valueOf(" and t.id not in ("))).concat(String.valueOf(oBeanUpdate))).concat(String.valueOf(") and t.default_member='Y' "));
    } else {
      sQuery = String.valueOf(String.valueOf(" select t.id from wfl_dop_group_members t where t.dop_group_id=").concat(String.valueOf(sHeaderPrimKey))).concat(String.valueOf(" and t.default_member='Y' "));
    } 
    System.out.println(sQuery);
    DBUtilitiesBean oBean1 = new DBUtilitiesBean();
    ArrayList arylstDefaultMemberNo = oBean1.executeQuery(sQuery);
    ArrayList arylstCombMatIds = new ArrayList();
    if (arylstDefaultMemberNo.size() > 1)
      throw new EnrgiseApplicationException("WFL.WflDOPGrp.noExceedingOne", "E"); 
    int iPersons = arylstDefaultMemberNo.size();
    Iterator oIt = oDetailBeanArray.iterator();
    while (oIt.hasNext()) {
      WflDOPGrpDtlBean oWflDOPGrpDtlBean = oIt.next();
      if (oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("U"))
        if (oWflDOPGrpDtlBean.getDfltMembr().equalsIgnoreCase("on"))
          iPersons++;  
      if (oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("D"))
        if (oWflDOPGrpDtlBean.getDfltMembr().equalsIgnoreCase("off"))
          iPersons--;  
      if (oWflDOPGrpDtlBean.getStatus().equalsIgnoreCase("N"))
        if (oWflDOPGrpDtlBean.getDfltMembr().equalsIgnoreCase("on"))
          iPersons++;  
    } 
    if (iPersons > 1)
      throw new EnrgiseApplicationException("WFL.WflDOPGrp.noExceedingOne", "E"); 
    if (iPersons < 1)
      throw new EnrgiseApplicationException("WFL.WflDOPGrp.noLessthanOne", "E"); 
  }
  
  private void checkUniqueDOPGrpDtl(String sHeaderPrimaryKey, ArrayList oDetailBeanArray, ArrayList oErrorList) throws EnrgiseApplicationException, EnrgiseSystemException {
    Iterator oIt1 = oDetailBeanArray.iterator();
    int rowCount = 1;
    while (oIt1.hasNext()) {
      WflDOPGrpDtlBean oWflDOPGrpDtlBean = oIt1.next();
      if (!EnrgiseUtil.checkString(oWflDOPGrpDtlBean.getEmpId())) {
        String sQuery = String.valueOf(" Select id as ID from wfl_dop_group_members a  where a.dop_group_id= ").concat(String.valueOf(sHeaderPrimaryKey));
        sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and a.e_per_dtl_id= "))).concat(String.valueOf(oWflDOPGrpDtlBean.getEmpId()));
        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.WflDOPGrp.empNo");
          oParams.add(oMessageKey);
          oParams.add(new Integer(rowCount));
          oErrorList.add(new EnrgiseMessageKeyException("wenrgise.workflow.detail.UniqueKeyConstraint", oParams));
        } 
      } 
      rowCount++;
    } 
  }
  
  private void checkMandatoryDtls(WflDOPGrpDtlBean oWflDOPGrpDtlBean, int count, ArrayList oDetailBeanArray) throws EnrgiseSystemException, EnrgiseApplicationException {
    ArrayList oList = new ArrayList();
    if (!EnrgiseUtil.checkString(oWflDOPGrpDtlBean.getTxtempNo())) {
      ArrayList oParam = new ArrayList();
      MessageKey oMessageKey = new MessageKey("WFL.WflDOPGrp.empNo");
      oParam.add(oMessageKey);
      oList.add(new EnrgiseMessageKeyException("wenrgise.common.detail.mandatoryfieldmissing", oParam, "E"));
    } 
    reportError(oList);
  }
  
  private void checkUniqueHeader(WflDOPGrpHdrBean oWflDOPGrpHdrBean) throws EnrgiseApplicationException, EnrgiseSystemException {
    String sQuery = String.valueOf(String.valueOf(" select t.id from wfl_dop_groups t where t.group_code='").concat(String.valueOf(oWflDOPGrpHdrBean.getGrpCode()))).concat(String.valueOf("'"));
    System.out.println(sQuery);
    DBUtilitiesBean oBean = new DBUtilitiesBean();
    ArrayList oList = oBean.executeQuery(sQuery);
    Iterator oIt = oList.iterator();
    if (oIt.hasNext()) {
      QueryRow oRow = oIt.next();
      if (!oRow.get("ID").getString().equalsIgnoreCase(oWflDOPGrpHdrBean.getHeaderPrimaryKey()))
        throw new EnrgiseApplicationException("wenrgise.common.header.uniqueConstraintViolated", "E"); 
    } 
  }
}
