<%-- called from DecisionTableTest.jsp file and it generates the AUDIT REPORT ACCORDING TO input value mapping and show the matched DecisionTable name and evaluation date and time with input data and Selections --%>

<%@page import="com.fujitsu.ibpm.dtm.*"
%><%@page import="java.util.*"
%><%@page import="com.fujitsu.ibpm.dtm.engine.*"
%>
<%
     DecisionTableManager dtManger = (DecisionTableManager) session.getAttribute("DecisionTableManager");
    String decisionTableIdStr = request.getParameter("decisionTableId");
    int decisionTableId = (new Integer(decisionTableIdStr)).intValue();
    String actionStrg = request.getParameter("pageaction");
    DecisionTable decisionTable = dtManger.getDecisionTable(decisionTableId);
        String inputs = "";
        String testResult="";
        String auditRes="";
        String displayWarning="";
        List result=null;
        boolean isResult = false;
        DecisionTableEngine ce =null;
        String auditData="";

        boolean isActive = true;
        String isActiveStr = request.getParameter("isActive");
        if(isActiveStr!=null){
         isActive = new Boolean(isActiveStr).booleanValue();
        }

        // Display Test DecisionTable result
     if (actionStrg != null && actionStrg.length() > 0) {
        if (actionStrg.equals("runDecisionTableTest")) {
            ce = new DecisionTableEngine(dtManger.getDecisionTables());
            ce.setAudit(true);
              String decisionTableName = decisionTable.getName();

            List mappings = decisionTable.getMappings();
            Iterator itr = mappings.iterator();

    // getting  input values of corresponding data field references
            while (itr.hasNext())
            {
                Mapping mapping = (Mapping) itr.next();
                ConditionKey  cKey = decisionTable.getConditionKey(mapping.getName());

                if(!cKey.isComposite()){
                List dataReferences = mapping.getDataFieldReferences();
                if(dataReferences.size()==0) continue;
                    String name = dataReferences.get(0).toString();

                    String value = request.getParameter(name);
                    if(value!=null && value.trim().length()!=0){
                        //checks validation if user input is invalid
                        if(cKey.getType().equals(ConditionKey.NUMBER_VALUE)) {
                                    
                                     if (!isInt(value))
                                            throw new Exception("The value \""+value+"\" of the condition "+cKey.getName()+" does not match its type ("+cKey.getType()+").");
                                }
                                if(cKey.getType().equals(ConditionKey.BOOLEAN_VALUE)) {
                                    
                                     if (!isBool(value) || value.length()==0)
                                            throw new Exception("The value \""+value+"\" of the condition "+cKey.getName()+" does not match its type ("+cKey.getType()+").");
                                }


                        if(value==null) value="";
                        inputs+=name+"="+value+",";

                        }
                }
            }
            if(inputs.endsWith(",")) inputs = inputs.substring(0,inputs.length()-1);

    // return test result
              testResult =  ce.processDecisionTable(decisionTableName,inputs, false);

    //separating the test result values from test result

        if(!testResult.equals("")){
        result = new ArrayList();
        String[] resultArray = testResult.split(",");
        for (int resultIndex=0; resultIndex<resultArray.length; resultIndex++){
            String partTestResult=resultArray[resultIndex];
            String partTestResultData= partTestResult.substring(partTestResult.indexOf("=")+1,partTestResult.length());
                    result.add(partTestResultData);
            }
        }

    // Returns the AUDIT Report
            auditData=ce.getAuditDataHTML();
            isResult = true;




        }
       }

       if(testResult == null) testResult="";
           List conditionKeys = decisionTable.getConditionKeys();
        Iterator conditionKeysItr = conditionKeys.iterator();
        List possibleSelections = decisionTable.getPossibleSelections();
        Iterator possibleSelectionsItr = possibleSelections.iterator();

     %>
<%!

public
    boolean
    isInt(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.length()>0)
        {
            try
            {
                if (text.startsWith(" ") || text.endsWith(" ")) {
                    isMatch = false;
                } else {
                    Double i = Double.valueOf(text);
                    isMatch = true;
                }
            }
            catch(NumberFormatException e)
            {
                isMatch = false;
            }
        }
        return isMatch;
    }

    public
    boolean
    isBool(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.length()>0)
        {
             if (text.equalsIgnoreCase("true") ||
                text.equalsIgnoreCase("false"))
            {
                isMatch = true;
            }
        }
        return isMatch;
    }
  %>