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

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
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.ejb.common.session.UserSession;
import wenrgise.workflow.bean.WflDocMstAttrDtlBean;
import wenrgise.workflow.businessdelegate.WflConditionsBD;
import wenrgise.workflow.webtier.form.WflConditionForm;

public class WflWorkFlowCondAction extends BaseAction {
  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 {
    WflConditionForm oWflConditionForm = (WflConditionForm)form;
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("classinfo")) {
      getDocAttr(oWflConditionForm, request);
      refreshForm(oWflConditionForm);
    } 
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("validate"))
      getLovKeyValue(oWflConditionForm); 
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("and") || oWflConditionForm.getHidAction().equalsIgnoreCase("or"))
      setFormula(oWflConditionForm); 
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("show"))
      if (!oWflConditionForm.getValues().equals(""))
        setFormula(oWflConditionForm);  
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("refresh"))
      refreshForm(oWflConditionForm); 
    return mapping.findForward("success");
  }
  
  private void refreshForm(WflConditionForm oWflConditionForm) {
    oWflConditionForm.setHidAction("");
    oWflConditionForm.setFormula("");
    oWflConditionForm.setValues("");
    oWflConditionForm.setOperator("");
    oWflConditionForm.setDocAttr("");
    oWflConditionForm.setDisaboperator("false");
    oWflConditionForm.setDisabdocAttr("false");
    oWflConditionForm.setDisabvalues("false");
    oWflConditionForm.setDisabbutOr("false");
    oWflConditionForm.setDisabbutAnd("false");
    oWflConditionForm.setDisabbutShowFormula("false");
    oWflConditionForm.setDisabbutValues("true");
    oWflConditionForm.setDisabbutSelect("true");
  }
  
  private void getDocAttr(WflConditionForm oWflConditionForm, HttpServletRequest request) throws RemoteException, EnrgiseApplicationException, EnrgiseSystemException {
    WflConditionsBD oWflConditionsBD = new WflConditionsBD();
    UserSession oUser = getUserSessionBean(request);
    oWflConditionsBD.setOUserInfo(oUser.getUserInfo());
    ArrayList docAttributes = oWflConditionsBD.getDocAttributes(oWflConditionForm.getDocId());
    Iterator oIt = docAttributes.iterator();
    HashMap oDocAttrTypeMap = new HashMap();
    HashMap oDocAttrNameMap = new HashMap();
    HashMap oDocLovKeyMap = new HashMap();
    while (oIt.hasNext()) {
      WflDocMstAttrDtlBean oWflDocMstAttrDtlBean = oIt.next();
      oDocAttrTypeMap.put(oWflDocMstAttrDtlBean.getTxtAttrName(), oWflDocMstAttrDtlBean.getTxtType());
      oDocAttrNameMap.put(oWflDocMstAttrDtlBean.getTxtAttrName(), oWflDocMstAttrDtlBean.getTxtAttrName());
      oDocLovKeyMap.put(oWflDocMstAttrDtlBean.getTxtAttrName(), oWflDocMstAttrDtlBean.getTxtLovKey());
    } 
    oWflConditionForm.setDocAttributes(oDocAttrNameMap);
    oWflConditionForm.setDocTypes(oDocAttrTypeMap);
    oWflConditionForm.setDocLovKey(oDocLovKeyMap);
    oWflConditionForm.setFormStatus("false");
  }
  
  private void getLovKeyValue(WflConditionForm oWflConditionForm) throws EnrgiseApplicationException, EnrgiseSystemException {
    HashMap oMap = oWflConditionForm.getDocLovKey();
    if (oMap.get(oWflConditionForm.getDocAttr()) != null) {
      oWflConditionForm.setConditionLovKey(oMap.get(oWflConditionForm.getDocAttr()).toString());
      oWflConditionForm.setDisabbutValues("false");
    } else {
      oWflConditionForm.setDisabbutValues("true");
    } 
  }
  
  private void setFormula(WflConditionForm oWflConditionForm) throws EnrgiseApplicationException, EnrgiseSystemException {
    StringBuffer sNewFormula = new StringBuffer();
    if (!EnrgiseUtil.checkString(oWflConditionForm.getDocAttr()))
      return; 
    if (!EnrgiseUtil.checkString(oWflConditionForm.getOperator()))
      return; 
    if (!EnrgiseUtil.checkString(oWflConditionForm.getValues()))
      return; 
    StringBuffer sFormula = new StringBuffer(oWflConditionForm.getFormula());
    if (sFormula.toString().equalsIgnoreCase(""))
      sNewFormula.append("( "); 
    HashMap oDocAttributes = oWflConditionForm.getDocAttributes();
    HashMap oDocType = oWflConditionForm.getDocTypes();
    String operandName = oDocAttributes.get(oWflConditionForm.getDocAttr()).toString();
    String operandType = oDocType.get(operandName).toString().trim();
    sNewFormula.append(operandName);
    sNewFormula.append(" ");
    sNewFormula.append(oWflConditionForm.getOperator());
    sNewFormula.append(" ");
    if (operandType.equalsIgnoreCase("S")) {
      sNewFormula.append('"');
      sNewFormula.append(oWflConditionForm.getValues());
      sNewFormula.append('"');
    } else {
      sNewFormula.append(oWflConditionForm.getValues());
    } 
    sNewFormula.append(" ");
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("and")) {
      sNewFormula.append(" && ");
      sNewFormula.append(" ");
    } 
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("or")) {
      sNewFormula.append(" ) ");
      sNewFormula.append(" || ( ");
    } 
    if (oWflConditionForm.getHidAction().equalsIgnoreCase("show")) {
      sNewFormula.append(" ) ");
      sFormula.append(sNewFormula);
      oWflConditionForm.setDocAttr("");
      oWflConditionForm.setOperator("");
      oWflConditionForm.setValues("");
      oWflConditionForm.setFormula(sFormula.toString());
      oWflConditionForm.setDocAttr("");
      oWflConditionForm.setDisaboperator("true");
      oWflConditionForm.setDisabdocAttr("true");
      oWflConditionForm.setDisabvalues("true");
      oWflConditionForm.setDisabbutOr("true");
      oWflConditionForm.setDisabbutAnd("true");
      oWflConditionForm.setDisabbutShowFormula("true");
      oWflConditionForm.setDisabbutValues("true");
      oWflConditionForm.setDisabbutSelect("false");
    } else {
      sFormula.append(sNewFormula);
      oWflConditionForm.setDocAttr("");
      oWflConditionForm.setOperator("");
      oWflConditionForm.setValues("");
      oWflConditionForm.setFormula(sFormula.toString());
      oWflConditionForm.setDocAttr("");
    } 
  }
  
  private String unescape(String sSQL) {
    if (sSQL.indexOf("&gt;") != -1)
      sSQL.replaceAll("&gt;", ">"); 
    if (sSQL.indexOf("&lt;") != -1)
      sSQL.replaceAll("&lt;", "<"); 
    return sSQL;
  }
}
