package WEB-INF.classes.wenrgise.hrms.webtier.action;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import wenrgise.common.exception.EnrgiseApplicationException;
import wenrgise.common.exception.EnrgiseSystemException;
import wenrgise.common.utility.EnrgiseUtil;
import wenrgise.common.webtier.action.BaseAction;
import wenrgise.hrms.businessdelegate.HrmGenEligRuleBD;
import wenrgise.hrms.webtier.form.HrmGenEligRuleForm;

public class HrmGenEligRuleAction extends BaseAction {
  private HashMap mapEligClass;
  
  private Stack stkFormula = null;
  
  private Map mapAlias = new HashMap();
  
  private int iAlias = 1;
  
  private boolean bOrFlag = false;
  
  public ActionForward executeImpl(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws EnrgiseApplicationException, EnrgiseSystemException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException, ServletException, IOException {
    HrmGenEligRuleForm oEligForm = (HrmGenEligRuleForm)form;
    if (oEligForm.getHidAction() == null)
      oEligForm.setHidAction("classinfo"); 
    if (oEligForm.getHidAction().equalsIgnoreCase("classinfo"))
      getEligClass(oEligForm); 
    if (oEligForm.getHidAction().equalsIgnoreCase("and") || oEligForm.getHidAction().equalsIgnoreCase("or"))
      setFormula(oEligForm); 
    if (oEligForm.getHidAction().equalsIgnoreCase("show")) {
      if (!oEligForm.getValues().equals(""))
        setFormula(oEligForm); 
      generateSql(oEligForm);
    } 
    if (oEligForm.getHidAction().equalsIgnoreCase("refresh")) {
      oEligForm.setHidAction("");
      oEligForm.setHidDocumentCodeId("");
      oEligForm.setHidFormula("");
      oEligForm.setHidValuesId("");
      oEligForm.setDocumentCode("");
      oEligForm.setDocumentType("");
      oEligForm.setFormula("");
      oEligForm.setValues("");
      oEligForm.setEligSql("");
      oEligForm.setEligibilityClass("");
      oEligForm.setDisabeligibilityClass("true");
      oEligForm.setDisabdocumentType("false");
      oEligForm.setDisaboperator("false");
      oEligForm.setDisabbutValues("false");
      oEligForm.setHeaderDataChanged(false);
    } 
    return mapping.findForward("success");
  }
  
  private void getEligClass(HrmGenEligRuleForm oEligForm) throws EnrgiseApplicationException, EnrgiseSystemException {
    HrmGenEligRuleBD oEligBD = new HrmGenEligRuleBD();
    this.mapEligClass = oEligBD.getEligClassInfo();
    oEligForm.setEligibilityClasses(this.mapEligClass);
    oEligForm.setTxtHidSubmitFirst("false");
  }
  
  private void setFormula(HrmGenEligRuleForm oEligForm) throws EnrgiseApplicationException, EnrgiseSystemException {
    StringBuffer sNewFormula = new StringBuffer();
    StringBuffer sNewHidFormula = new StringBuffer();
    if (!EnrgiseUtil.checkString(oEligForm.getEligibilityClass()))
      return; 
    if (!EnrgiseUtil.checkString(oEligForm.getOperator()))
      return; 
    if (!EnrgiseUtil.checkString(oEligForm.getValues()))
      return; 
    if (!this.mapAlias.containsKey(oEligForm.getEligibilityClass()))
      this.mapAlias.put(oEligForm.getEligibilityClass(), new String(String.valueOf("tab").concat(String.valueOf(this.iAlias++)))); 
    StringBuffer sFormula = new StringBuffer(oEligForm.getFormula());
    StringBuffer sHidFormula = new StringBuffer(oEligForm.getHidFormula());
    if (sFormula.toString().equalsIgnoreCase("")) {
      sNewFormula.append("( ");
      sNewHidFormula.append("( ");
    } 
    sNewFormula.append(this.mapEligClass.get(oEligForm.getEligibilityClass()).toString());
    sNewHidFormula.append(String.valueOf(this.mapAlias.get(oEligForm.getEligibilityClass()).toString()).concat(String.valueOf(".CLS_DT_ID")));
    sNewFormula.append(" ");
    sNewHidFormula.append(" ");
    sNewFormula.append(oEligForm.getOperator());
    sNewHidFormula.append(oEligForm.getOperator());
    sNewFormula.append(" ");
    sNewHidFormula.append(" ");
    sNewFormula.append("( ");
    sNewHidFormula.append("( ");
    sNewFormula.append(oEligForm.getValues());
    sNewHidFormula.append(oEligForm.getHidValuesId());
    sNewFormula.append(" )");
    sNewHidFormula.append(" )");
    if (oEligForm.getHidAction().equalsIgnoreCase("and")) {
      sNewFormula.append(" and ");
      sNewHidFormula.append(" and ");
      oEligForm.setDisabdocumentType("true");
      oEligForm.setDisabbutDocumentCode("true");
    } 
    if (oEligForm.getHidAction().equalsIgnoreCase("or")) {
      sNewFormula.append(" ) ");
      sNewHidFormula.append(" ) ");
      sNewFormula.append(" or ( ");
      sNewHidFormula.append(" or ( ");
      oEligForm.setDisabdocumentType("true");
      oEligForm.setDisabbutDocumentCode("true");
    } 
    if (oEligForm.getHidAction().equalsIgnoreCase("show")) {
      sNewFormula.append(" ) ");
      sNewHidFormula.append(" ) ");
      sFormula.append(sNewFormula);
      sHidFormula.append(sNewHidFormula);
      oEligForm.setEligibilityClass("");
      oEligForm.setOperator("");
      oEligForm.setValues("");
      oEligForm.setFormula(sFormula.toString());
      oEligForm.setHidFormula(unescape(sHidFormula.toString()));
      oEligForm.setEligibilityClass("");
      oEligForm.setDisaboperator("true");
      oEligForm.setDisabeligibilityClass("true");
      oEligForm.setDisabvalues("true");
      oEligForm.setDisabbutOr("true");
      oEligForm.setDisabbutAnd("true");
      oEligForm.setDisabbutShowFormula("true");
      oEligForm.setDisabbutValues("true");
      oEligForm.setDisabbutDocumentCode("true");
      oEligForm.setDisabdocumentType("true");
      oEligForm.setDisabbutDocumentCode("true");
    } else {
      sFormula.append(sNewFormula);
      sHidFormula.append(sNewHidFormula);
      oEligForm.setEligibilityClass("");
      oEligForm.setOperator("");
      oEligForm.setValues("");
      oEligForm.setFormula(sFormula.toString());
      oEligForm.setHidFormula(sHidFormula.toString());
      oEligForm.setEligibilityClass("");
    } 
  }
  
  private void generateSql(HrmGenEligRuleForm oEligForm) {
    StringBuffer sbEligSql = new StringBuffer();
    StringBuffer sbHidFormula = new StringBuffer(oEligForm.getHidFormula());
    StringBuffer sbFormula = new StringBuffer(oEligForm.getFormula());
    sbEligSql.append("select distinct * from ");
    for (int iAlias = 1; iAlias <= this.mapAlias.size(); iAlias++) {
      if (iAlias == 1) {
        sbEligSql.append("HRM_EMP_CLASS_MAP tab");
      } else {
        sbEligSql.append(", HRM_EMP_CLASS_MAP tab");
      } 
      sbEligSql.append(iAlias);
    } 
    sbEligSql.append(" where ");
    if (sbHidFormula.toString().endsWith("or ( ")) {
      sbHidFormula.setLength(sbHidFormula.length() - 5);
      sbFormula.setLength(sbFormula.length() - 5);
    } 
    if (sbHidFormula.toString().endsWith("and ")) {
      sbHidFormula.setLength(sbHidFormula.length() - 4);
      sbFormula.setLength(sbFormula.length() - 4);
    } 
    sbEligSql.append(sbHidFormula);
    if (this.bOrFlag)
      sbEligSql.append(" )"); 
    sbEligSql.append("and tab1.E_PER_DTL_ID = ?");
    oEligForm.setHidFormula(sbHidFormula.toString());
    oEligForm.setFormula(sbFormula.toString());
    oEligForm.setEligSql(sbEligSql.toString());
    System.out.println(sbEligSql.toString());
  }
  
  private String unescape(String sSQL) {
    if (sSQL.indexOf("&gt;") != -1)
      sSQL.replaceAll("&gt;", ">"); 
    if (sSQL.indexOf("&lt;") != -1)
      sSQL.replaceAll("&lt;", "<"); 
    return sSQL;
  }
}
