package WEB-INF.classes.wenrgise.ejb.common.utility;

import java.util.ArrayList;
import java.util.Iterator;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.ejb.common.helper.QueryRow;
import wenrgise.ejb.common.utility.DBUtilitiesBean;

public class CommonAdditionalUtility {
  public String getUserGroup(String empId) throws EnrgiseApplicationException, EnrgiseSystemException {
    String groupId = new String();
    String sQuery = String.valueOf(" select group_id from sys_user_group_map m where m.user_system_id = ").concat(String.valueOf(empId));
    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();
        groupId = oRow.get("group_id").getString();
      } 
    } 
    return groupId;
  }
  
  public String getHOD(String empId) throws EnrgiseApplicationException, EnrgiseSystemException {
    String hodId = new String();
    String sQuery = String.valueOf(String.valueOf(" select m.contact_person_id from gen_wrkgrp_mst m where m.id =  (select dtl.wkgp_mst_id from hrm_emp_wrkgrp_dtl dtl where dtl.e_per_dtl_id =").concat(String.valueOf(empId))).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();
        hodId = oRow.get("CONTACT_PERSON_ID").getString();
      } 
    } 
    return hodId;
  }
  
  public boolean checkValidEmail(String email) throws EnrgiseApplicationException, EnrgiseSystemException {
    int atCount = 0;
    int dotCount = 0;
    boolean flag = true;
    int i;
    for (i = 0; i < email.length(); i++) {
      if (email.charAt(i) == '@')
        atCount++; 
      if (email.charAt(i) == '.')
        dotCount++; 
    } 
    if (atCount == 1 && dotCount > 0 && email.indexOf("@") != 0 && email.charAt(email.length() - 1) != '.' && email.charAt(email.length() - 1) != '@') {
      dotCount = 0;
      for (i = email.indexOf("@"); i < email.length(); i++) {
        if (email.charAt(i) == '.')
          dotCount++; 
        if (email.indexOf("@") - email.indexOf(".") == -1 || email.lastIndexOf(".") - email.indexOf(".") == 1) {
          flag = false;
          break;
        } 
      } 
    } else {
      flag = false;
    } 
    if (dotCount == 0 || dotCount > 2)
      flag = false; 
    return flag;
  }
  
  public boolean checkValidPan(String panNo) throws EnrgiseApplicationException, EnrgiseSystemException {
    boolean flag = true;
    panNo = panNo.toUpperCase();
    if (panNo.length() != 10) {
      flag = false;
    } else {
      String first = panNo.substring(0, 5);
      String second = panNo.substring(5, 9);
      int i;
      for (i = 0; i < first.length(); i++) {
        if (first.charAt(i) < 'A' || first.charAt(i) > 'Z') {
          flag = false;
          break;
        } 
      } 
      for (i = 0; i < second.length(); i++) {
        if (second.charAt(i) < '0' || second.charAt(i) > '9') {
          flag = false;
          break;
        } 
      } 
      if (panNo.charAt(9) < 'A' || panNo.charAt(9) > 'Z')
        flag = false; 
    } 
    return flag;
  }
  
  public ArrayList getDelegatedEmp_Level(String empId, String HdrPrKey, String docTypeId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList outArray = new ArrayList();
    String sQuery = "select wt.level_no, wt.delegated_emp_id ";
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" from workflow_tasklist wt,workflow_dtl wd,wfl_doc_type_mst wm "));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" where status='P' "));
    sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and delegator_emp_id = "))).concat(String.valueOf(empId));
    sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and doc_dtl_id = "))).concat(String.valueOf(HdrPrKey));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" and wt.workflow_dtl_id=wd.id "));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" and wd.doc_type_mst_id=wm.id "));
    sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and wm.id = "))).concat(String.valueOf(docTypeId));
    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();
        outArray.add(oRow.get("level_no").getString());
        outArray.add(oRow.get("delegated_emp_id").getString());
      } 
    } 
    return outArray;
  }
  
  public ArrayList getEmp_Level(String HdrPrKey, String docTypeId) throws EnrgiseApplicationException, EnrgiseSystemException {
    ArrayList outArray = new ArrayList();
    String sQuery = "select wt.level_no, wt.delegated_emp_id ";
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" from workflow_tasklist wt,workflow_dtl wd,wfl_doc_type_mst wm "));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" where status='P' "));
    sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and doc_dtl_id = "))).concat(String.valueOf(HdrPrKey));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" and wt.workflow_dtl_id=wd.id "));
    sQuery = String.valueOf(sQuery).concat(String.valueOf(" and wd.doc_type_mst_id=wm.id "));
    sQuery = String.valueOf(String.valueOf(sQuery).concat(String.valueOf(" and wm.id = "))).concat(String.valueOf(docTypeId));
    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();
        outArray.add(oRow.get("level_no").getString());
        outArray.add(oRow.get("delegated_emp_id").getString());
      } 
    } 
    return outArray;
  }
}
