<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="java.io.Writer"
%><%@page import="java.util.List"
%><%@page import="java.util.Map"
%><%@page import="java.util.HashMap"
%><%@page import="java.util.ArrayList"
%><%@page import="java.util.Iterator"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.DateFormat"
%><%@page import="java.util.StringTokenizer"
%><%@page import="com.fujitsu.ibpm.dtm.*"
%><%@page import="com.fujitsu.ibpm.dtm.engine.datamatch.NumericDataMatch"
%>
<%-- calls the debug() function --%>
<%@ include file="Debug.jsp" %>

<%  //checks the session is valid or not
    request.setCharacterEncoding("UTF-8");
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
   }
%>
<%@ include file="dtmCommon.jsp"%>
<%-- sets all Scenarios and Mappings of the Specified DecisionTable --%>
<%@ include file="setDecisionTable.jsp" %>
<%
    String displayWarning="";
%>
<html>
<head>
<title>Interstage Business Process Manager | Approval DecisionTable Detail</title>
<link href="styles/dtm.css" rel="stylesheet" type="text/css">

</head>
<body class="bodyClass">
<center>
  <jsp:include page="DTMHeader.jsp" flush="true"/><br/>


<!-- calls methods for checking validity of user inputs -->
<script type="text/javascript" language="javascript" src="jscript/check.js"> </script>

  <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
    <td valign="top">
      <table width="90%" border="0" cellpadding="2" cellspacing="2" align="center" class="tableclass">
        <tr>
          <td colspan="2">
            <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
              <tr align="center" class="headrow">
                <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" align="right">
                  <div align="left"><font color="#000000" size="2"> Decision Table:
                      <%QuickForm.writeHtmlWithNBSPs(out, decisionTable.getName()); %>
                    <div align="right"> </div>
                    </font></div>                </td>
              </tr>
              <tr class="oddrow">
                <td>
                  <!-- Encloses 2nd level tables -->
                  <br/>
                  <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                    <tr align="center" class="headrow">
                      <td bgcolor="#ffffff"  align="right">
                        <p><font color="#000000" size="2"></font></p>
                        <table border="0" cellpadding=0 cellspacing=0>
                          <tr> <% if(!isActive){  %>
                            <td align="right">
                              <table border="0" cellspacing="0" cellpadding="0">
                                <tr background="images/buttons/com_button_bg.gif">

                            <%     // enables or disables the AddDecision button

                                int primConditionKeyId = -1;
                                int secConditionKeyId = -1;
                                boolean isAddDecision = false;
                                boolean isPrimaryExist = false;
                                boolean isCompositeExist = false;
                                boolean isCompositExists= false;
                                Iterator primaryCheckItr = conditionKeys.iterator();
                                while(primaryCheckItr.hasNext()){
                            ConditionKey conditionKey = (ConditionKey) primaryCheckItr.next();

                            if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                                isCompositeExist=true;

                                //checks for the primary key if it is deleted
                                primConditionKeyId=conditionKey.getPrimaryConditionKeyId();
                                ConditionKey primaryCkey = decisionTable.getConditionKey(primConditionKeyId);

                                if(primaryCkey==null){
                                    isCompositExists=true;
                                }

                                //checks for the secondary key if it is deleted
                                secConditionKeyId=conditionKey.getSecondryConditionKeyId();
                                ConditionKey secondaryCKey = decisionTable.getConditionKey(secConditionKeyId);

                                if(secondaryCKey==null){
                                    isCompositExists=true;
                                }

                                List PrimaryValuesList = conditionKey.getPrimaryKeyValues();
                                if(PrimaryValuesList.size()>0) {
                                    isPrimaryExist = true;
                                }else isPrimaryExist = false;
                            }
                        }
                            if(conditionKeys.size()>0 || possibleSelections.size()>0)
                                isAddDecision=true;
                            if(isCompositExists)isAddDecision=false;
                            if(isCompositeExist&&!isPrimaryExist)isAddDecision=false;
                        %>
                      <% if(isAddDecision)
                        {
                        %>
                                  <td width="15"><img src="images/buttons/but_table_view.gif" width="32" height="24" ></td>
                                  <td background="images/buttons/com_button_bg.gif" nowrap>
                                    <a  href="javascript:newScenario(<%=decisionTableId%>)" >
                                    <div align="center" class="buttonlink"> Add
                                      Decision</div>
                                    </a> </td>
                                  <td width="9">
                                    <div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div>                                  </td><%}%>
                                </tr>
                              </table>                            </td>
                            <td>&nbsp;</td>
                            <td>
                              <table border="0" cellspacing="0" cellpadding="0">
                                <tr background="images/buttons/com_button_bg.gif">
                                  <td width="15"><img src="images/buttons/but_kpi_settings.gif" width="29" height="24"></td>
                                  <td background="images/buttons/com_button_bg.gif" nowrap>
                                    <a href="DecisionTableConfig.jsp?decisionTableId=<%=decisionTableId%>">
                                    <div align="center" class="buttonlink"> Manage                                    </div>
                                    </a> </td>
                                  <td width="9">
                                    <div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div>                                  </td>
                                </tr>
                              </table>                            </td>
                            <td>&nbsp;</td> <%}%>
                            <td>
                              <table border="0" cellspacing="0" cellpadding="0">

                       <% if(decisionTable.getScenarios().size()>0)
                        {%>
                                <tr background="images/buttons/com_button_bg.gif">
                                  <td width="15"><img src="images/buttons/but_dashboard_view.gif" width="30" height="24"></td>
                                  <td background="images/buttons/com_button_bg.gif" nowrap>
                                  <a href="DecisionTableTest.jsp?decisionTableId=<%=decisionTableId%>&isActive=<%=isActive%>">
                                    <div align="center" class="buttonlink"> Test
                                      DecisionTable</div>
                                    </a> </td>
                                  <td width="9">
                                    <div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div>                                  </td>
                                </tr><%}%>
                              </table>                            </td>
                          </tr>
                        </table>
                        <table bgcolor="#CCCCCC" width="100%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
                          <tr class="oddrow">
                            <td align="left" valign="top" width="200"><b>Process
                              Definitions:</b></td>
                            <td align="left" valign="top">
                              <%
            //displays process definitions
                    List processDefinitions = decisionTable.getProcessDefinitions();
                    Iterator processDefinitionsItr = processDefinitions.iterator();
                    int count=0; // counts number of Process Definitions
                    String processDefinition="";
                    while(processDefinitionsItr.hasNext()){
                         processDefinition += (String)processDefinitionsItr.next();
                                 // checks for more than one Process Definitions
                           if(count++>=0 && count!=(processDefinitions.size()))
                                 processDefinition += ",";

                                }%>
               <%QuickForm.writeHtml(out, processDefinition);%>                              </td>
                          </tr>
                        </table>
                        <br>                      </td>
                    </tr>
                   <tr align="center" class="headrow">
                      <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" align="right">
                        <div align="left"><font color="#000000" size="2"> Decisions
                            <div align="right"> </div>
                          </font></div>                      </td>
                    </tr>
                </table>
                   <table width="100%"  border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC" class="tableClass">
                     <tr class="headrow" >
                       <!-- displays the condition and selection row header-->
                       <td align="center">&nbsp;</td>
                       <td align="center" width="30"># </td>
                       <%

                       //makes the primary and secondary condition back when the corresponding composite condition is deleted
                        boolean isComposit=false;
                              Iterator compositConditionSearchItr = conditionKeys.iterator();
                              while(compositConditionSearchItr.hasNext()){
                                 ConditionKey conditionKey = (ConditionKey) compositConditionSearchItr.next();
                                        if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                                            if(conditionKey.getPrimaryKeyValues().size()>0)
                                                isComposit=true;
                                        }
                            }

                            int primaryCKeyId=-1;
                            int secondaryCKeyId=-1;
                            boolean isPriAndSecInComposit=false;
                            boolean isconditionExist=false;
                              Iterator compositSearchItr = conditionKeys.iterator();
                              while(compositSearchItr.hasNext()){
                                ConditionKey conditionKey = (ConditionKey) compositSearchItr.next();
                                 isconditionExist=decisionTable.isVisbleConditionKey(conditionKey);
                                if(!isComposit){
                                     if(isconditionExist)conditionKey.setPassive(false);
                                }else{
                                    isconditionExist=decisionTable.isPassiveConditionKey(conditionKey);
                                    if(isconditionExist)conditionKey.setPassive(false);
                                }
                              }

                       //calculates the length of the Condition cell's length in case of normal and composite conditions
                           int totalIsPassiveConditions = 0;
                           int totalConditions = 0;
                        String printCondition="Conditions";
                        int totalCompConditions=0;
                        int totalPrimaryKeyValues=0;
                        int totalCompositeKeyWithValues=0;
                        boolean cmpExists= false;
                        List PrimaryValuesList = new ArrayList();
                        conditionKeysItr = conditionKeys.iterator();
                        while(conditionKeysItr.hasNext()){
                        ConditionKey conditionKey = (ConditionKey) conditionKeysItr.next();
                            if(conditionKey.isPassive())totalIsPassiveConditions++;
                            if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                                    totalCompConditions++;
                                PrimaryValuesList = conditionKey.getPrimaryKeyValues();
                                if(PrimaryValuesList.size()>0) {
                                    totalCompositeKeyWithValues++;
                                    totalPrimaryKeyValues+=PrimaryValuesList.size();
                                }
                                cmpExists=true;
                            }
                    }
                        conditionKeysItr = conditionKeys.iterator();
                        if(cmpExists && totalPrimaryKeyValues>0 && totalCompConditions>=2)
                            totalConditions=conditionKeys.size()+totalPrimaryKeyValues-totalIsPassiveConditions-totalCompositeKeyWithValues;
                        else if(totalCompositeKeyWithValues<2)
                            totalConditions=conditionKeys.size()+totalPrimaryKeyValues-totalCompositeKeyWithValues*3;

                        //displays if there is no conditions
                        if (conditionKeys.size()<1){printCondition="Conditions"; %>
                       <td colspan=<%=conditionKeys.size()%> align="center" class="conditioncell" ><%=printCondition%></td>
                       <%}

                        if (conditionKeys.size()>0){printCondition="Conditions"; %>
                       <td colspan=<%=totalConditions%> align="center" class="conditioncell"><%=printCondition%></td>
                       <%}

                    String printSelection="Results";
                    //displays if there is no results
                    if (possibleSelections.size()<1){printSelection="Results"; %>
                       <td  colspan=<%=possibleSelections.size()%> align="center" class="selectioncell"><%=printSelection%></td>
                       <%}

                        if (possibleSelections.size()>0){printSelection="Results"; %>
                       <td  colspan=<%=possibleSelections.size()%> align="center" class="selectioncell"><%=printSelection%></td>
                       <% }if(!isActive){  %>
                       <td align="center">Action </td>
                       <%}%>
                     </tr>
                     <%
                        if( conditionKeys.size()>0 || possibleSelections.size()>0)
                        {%>
                     <tr align="center" class="headrow" bgcolor="#0066CC" valign="top">
                       <th align="center" >&nbsp; </th>
                       <th align="center" width="30">&nbsp;</th>
                       <%}%>
                       <%
                     //displays if there is no conditions
                     if(!(possibleSelections.size()<1 && conditionKeys.size()<1)){
                        if (conditionKeys.size()<1){%>
                       <td colspan=<%=conditionKeys.size()%> align="center" class="conditioncell" >&nbsp;</td>
                       <%}}
            // reading all Condition Keys
                         StringBuffer tempStringForRowHeader = new StringBuffer();
                         tempStringForRowHeader.append("<tr align=\"center\" class=\"conditioncell\"  valign=\"top\"> <th align=\"center\"> </th><th align=\"center\"> </th>");
                        List tempPrimaryValues= new ArrayList();
                        Iterator tempConditionKeysItr=conditionKeys.iterator();
                        while(tempConditionKeysItr.hasNext()){
                        ConditionKey conditionKey = (ConditionKey) tempConditionKeysItr.next();

                        int compositeKeyId = -1;
                        if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                            compositeKeyId = conditionKey.getId();


                        if(compositeKeyId!=-1){
                                 tempPrimaryValues = conditionKey.getPrimaryKeyValues();
                        }
                    }
                }

                        boolean isPrimaryCondValueExists = false;
                        boolean isPrimaryValues=false;
                        boolean isComposite=false;
                        while(conditionKeysItr.hasNext()){
                        ConditionKey conditionKey = (ConditionKey) conditionKeysItr.next();
                        String selection = "";
                        int compositeKeyId = -1;

                        if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                            compositeKeyId = conditionKey.getId();
                            isComposite=true;
                        }else isComposite=false;
                        String colspan="1";
                        if(compositeKeyId!=-1){
                                 List primaryValues = conditionKey.getPrimaryKeyValues();
                                 if(primaryValues.size()>0) {
                                     colspan = ""+primaryValues.size();
                                }else{
                                    tempStringForRowHeader.append("<th >  </th>");
                                }
                        }

                        if(!conditionKey.isPassive()){
                                 %>
                       <th colspan=<%=colspan%> <%if(selection.length()>0) out.println("bgcolor=\"#009933\"");%>align="center" valign="top"> <%
            // getting all Primary and Secondary values
                          String primaryKeyName=null;
                          String secondaryKeyName =null;
                          if(compositeKeyId!=-1){
                                 List primaryValues = conditionKey.getPrimaryKeyValues();
                                 if(primaryValues.size()>0)
                                     isPrimaryCondValueExists=true;
                                 Iterator primaryValuesItr = primaryValues.iterator();
                                 int primaryConditionKeyId = conditionKey.getPrimaryConditionKeyId();
                                    if(primaryConditionKeyId!=-1){
                                ConditionKey primaryKey = decisionTable.getConditionKey(primaryConditionKeyId);            if(primaryKey!=null)

                                primaryKeyName=primaryKey.getName();
                                        int secondaryConditionKeyId = conditionKey.getSecondryConditionKeyId();

                                if(secondaryConditionKeyId!=-1){
                                    ConditionKey secondaryKey = decisionTable.getConditionKey(secondaryConditionKeyId);            if(secondaryKey!=null)
                                    secondaryKeyName=secondaryKey.getName();
                                }


                            }

                          String resultString=conditionKey.getName();
                                if(secondaryKeyName!=null){
                                    resultString=secondaryKeyName;
                                }
                                if(primaryKeyName!=null){
                                    resultString+=" ("+primaryKeyName+")";
                                }

                                while(primaryValuesItr.hasNext()){
                                tempStringForRowHeader.append("<th align=\"center\">");
                                String primaryValue = primaryValuesItr.next().toString();

    %>
                           <%
                            tempStringForRowHeader.append(primaryValue+"</th>");
                            }
                            %>
                           <%=resultString%>
                           <%
                           if(!isActive){  %>
                           <div align="right"><a href="CompositeKey.jsp?decisionTableId=<%=decisionTable.getId()%>&compositeKeyId=<%=compositeKeyId%>"><img src="images/dtm/preferences.png" width="16" height="16" border="0" class="borderTable"></a>
                               <%}
                          }else{
                          if(selection.length()>0){
                              tempStringForRowHeader.append("<th bgcolor=\"#009933\"></th>");
                          }else{
                              tempStringForRowHeader.append("<th></th>");
                            }
                          %>
                               <%QuickForm.writeHtml(out,conditionKey.getName());%>
                               <%
                         }%>
                         </div></th>
                       <%
                        }
                        }

                        //displays if there is no result
                        if(!(possibleSelections.size()<1 && conditionKeys.size()<1)){
                        if (possibleSelections.size()<1){%>
                       <td colspan=<%=possibleSelections.size()%> align="center" bgcolor="#009933">&nbsp;</td>
                       <%}}

                //display selections
                        while(possibleSelectionsItr.hasNext()){
                        String tempSelection = possibleSelectionsItr.next().toString();
                          tempStringForRowHeader.append("<th  bgcolor=\"#009933\"></th>");
%>
                       <th bgcolor="#009933"align="center"><%QuickForm.writeHtml(out,tempSelection);%></th>
                       <%
                        }
                        //displays if there is no results but composit conditions
                          if (possibleSelections.size()<1){
                        tempStringForRowHeader.append("<th bgcolor=\"#009933\">&nbsp;</th>");
                        }
                        if (conditionKeys.size()>0 || possibleSelections.size()>0)
                        {
                        %>
                       <% if(!isActive){  %>
                       <th align="center" width="100">&nbsp;</th>
                       <%}}%>
                     </tr>
                     <%if(isComposit && !isActive){tempStringForRowHeader.append("<th>&nbsp;</th>");}
                     if(isPrimaryCondValueExists){
                     %>
                     <%=tempStringForRowHeader.toString()%>
                     <%}

    // display input mappings table
      List scenarios = decisionTable.getScenarios();
    Iterator itr = scenarios.iterator();
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
        while (itr.hasNext())
        {
            scenario = (Scenario) itr.next();
            debug(trace, out, "\n scenario="+scenario.toString());
             rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;

            List selections = scenario.getSelections();
               Iterator selectionsItr = selections.iterator();

            List conditions = scenario.getConditions();
               Iterator conditionsItr = conditions.iterator();

            if(selections.size()>0 || conditions.size()>0){
%>
                     <tr align="center" class="<%=rowColor%>" valign="top">
                       <td align="center" width=20><% if(!isActive){  %>
                           <table border="0" cellspacing="0" cellpadding="0" align="center" width="20">
                             <tr>
                               <%if (scenario.getId()>1){%>
                               <td align="left" class="smalllinks" valign="top" height="12" ><a href="javascript:ScenariomoveUp('<%=decisionTable.getId()%>','<%=scenario.getId()%>')"><img src="images/dtm/up.gif" border="0"></a></td>
                               <%}%>
                             </tr>
                             <tr>
                               <%if (scenario.getId()<scenarios.size()){%>
                               <td class="smalllinks" align="left" valign="top"><a href="javascript:ScenariomoveDown('<%=decisionTable.getId()%>','<%=scenario.getId()%>')"><img src="images/dtm/down.gif" border="0"></a></td>
                               <%}%>
                             </tr>
                           </table>
                         <% } %>                       </td>
                       <td align="center"><%=scenario.getId()%></td>
                       <%
                      if (conditionKeys.size()<1){%>
                       <td colspan=<%=conditionKeys.size()%> align="center" >&nbsp;</td>
                       <%}

            //displays primary and secondary values
                            isPrimaryValues=false;
                            isComposite=false;
                            while (conditionsItr.hasNext())
                            {
                            Object obj = conditionsItr.next();
                            Condition condition = (Condition) obj;
                            String conditionValue = condition.getConditionValue();
                            if(conditionValue==null) conditionValue="";
                            ConditionKey conditionKey = decisionTable.getConditionKey(condition.getConditionKeyId());

                            int compositeKeyId = -1;
                            if(conditionKey.getType().equals(ConditionKey.NUMBER_VALUE)) {
                                String operator = NumericDataMatch.getNumericOperator(conditionValue);
                                if (operator == null && !conditionValue.equals("")) {
                                    conditionValue = NumericDataMatch.DEFAULT_OPERATOR + conditionValue;
                                }
                            }
                            if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                            compositeKeyId = conditionKey.getId();
                            isComposite=true;
                            }


                    if(!conditionKey.isPassive()){
                        if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                            compositeKeyId = conditionKey.getId();
                            isComposite=true;
                        }
                            %>
                       <%
                          if(compositeKeyId!=-1){
                                 List primaryValues = conditionKey.getPrimaryKeyValues();
                                 if(primaryValues.size()==0){%>
                       <td align="center"></td>
                       <%}


                                 Iterator primaryValuesItr = primaryValues.iterator();

                                 int primaryConditionKeyId = conditionKey.getPrimaryConditionKeyId();
                                String primaryKeyName = null;
                                if(primaryConditionKeyId!=-1){
                                    ConditionKey primaryKey = decisionTable.getConditionKey(primaryConditionKeyId);                if(primaryKey!=null)
                                    primaryKeyName=primaryKey.getName();
                                }

                                Map compositeValues = condition.getCompositeValueList();

                                while(primaryValuesItr.hasNext()){
                                String key = primaryKeyName+"="+primaryValuesItr.next();
                                    String resultString = "";
                                    Object valueObj = compositeValues.get(key);
                                    if(valueObj!=null){
                                        resultString = valueObj.toString();
                                    }
                                %>
                       <td align="center"><%QuickForm.writeHtml(out, resultString);%></td>
                       <%}%>
                       <%}else{
                          %>
                      <td align="center"><%QuickForm.writeHtmlWithNBSPs(out, conditionValue);%></td>
                       <%
                          }}%>
                       <%
                            }

                            //displays if there is no results
                            if (possibleSelections.size()<1){%>
                       <td colspan=<%=possibleSelections.size()%> align="center">&nbsp;</td>
                       <%}

                //displays Selection values
                            while (selectionsItr.hasNext())
                            {
                            Object obj = selectionsItr.next();
                            Selection selection = (Selection) obj;
                            String selectionValue=selection.getSelectionValue();
                            if(selectionValue==null)selectionValue="";
                            %>
                       <td align="center"><%QuickForm.writeHtmlWithNBSPs(out, selectionValue);%>                       </td>
                       <%
                            }
                            %>
                            <%     // enables or disables the AddDecision button

                                int primConditionKeyId2 = -1;
                                int secConditionKeyId2 = -1;
                                boolean isAddDecision2 = false;
                                boolean isPrimaryExist2 = false;
                                boolean isCompositeExist2 = false;
                                boolean isCompositExists2= false;
                                Iterator primaryCheckItr2 = conditionKeys.iterator();
                                while(primaryCheckItr2.hasNext()){
                            ConditionKey conditionKey = (ConditionKey) primaryCheckItr2.next();

                            if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
                                isCompositeExist2=true;

                                //checks for the primary key if it is deleted
                                primConditionKeyId2=conditionKey.getPrimaryConditionKeyId();
                                ConditionKey primaryCkey = decisionTable.getConditionKey(primConditionKeyId2);

                                if(primaryCkey==null){
                                    isCompositExists2=true;
                                }
                                //checks for the secondary key if it is deleted
                                secConditionKeyId2=conditionKey.getSecondryConditionKeyId();
                                ConditionKey secondaryCKey = decisionTable.getConditionKey(secConditionKeyId2);

                                if(secondaryCKey==null){
                                    isCompositExists2=true;
                                }

                                List PrimaryValuesList2 = conditionKey.getPrimaryKeyValues();
                                if(PrimaryValuesList2.size()>0) {
                                    isPrimaryExist2 = true;
                                }else isPrimaryExist2 = false;
                            }
                        }
                            if( conditionKeys.size()>0 || possibleSelections.size()>0)
                                isAddDecision2=true;
                                if(isCompositExists2)isAddDecision2=false;
                            if(isCompositeExist2&&!isPrimaryExist2)isAddDecision2=false;
                        %>
                       <% if(!isActive){

                        %>

                       <td align="center" width="60"><table border="0" cellspacing="0" cellpadding="0" align="center">
                           <tr><%if(isAddDecision2){%>
                             <td class="smalllinks" align="center" width="10" valign="bottom"><a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>&scenarioId=<%=scenario.getId()%>"><img src="images/buttons/edit_small_icon.gif" width="17" height="17" border="0">EDIT</a></td>
                             <td  width="18">&nbsp;</td>
                             <td class="smalllinks" align="center" width="42" valign="bottom"><a href="javascript:onClick=confirmScenarioDelete('<%=decisionTable.getId()%>','<%=scenario.getId()%>')"><img src="images/dtm/delete.gif" width="12" height="12" border="0">DELETE</a></td><%}%>
                           </tr>
                       </table></td>
                       <%}%>
                     </tr>
                     <%
    }}
%>
                   </table>
                   <%=decisionTableNote%>
                    <br>
                  <br>
                  <table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                    <tr align="center" class="headrow">
                      <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" align="right">
                        <div align="left"><font color="#000000" size="2"> Conditions
                          Mapping </font> </div>                      </td>
                    </tr>
                  </table>
                  <table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                    <tr align="center" class="headrow" bgcolor="#0066CC">
                      <th  width="5%" align="center"> Id</th>
                      <th width="25%"> Name</th>
                      <th width="30%"> Description</th>
                      <th width="15%"> User Defined Attributes</th>
                      <th width="15%" align="center"> Value Substitute</th>
                       <% if(!isActive){  %>
                        <th align="center" width="10%"> Action</th>
                        <%}%>
                    </tr>
                    <%
    // display input mappings table
      List mappings = decisionTable.getMappings();

    itr = mappings.iterator();
    nextColor = 0;
    int mapId=0;
    rowColor = bcolor[nextColor];
        while (itr.hasNext())
        {
            Mapping mapping = (Mapping) itr.next();
            debug(trace, out, "\n mapping="+mapping.toString());
            ConditionKey  cKey = decisionTable.getConditionKey(mapping.getName());


            if(!cKey.isComposite()){

            rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;


%>
                    <tr align="center" class="<%=rowColor%>" valign="top">
                      <td align="center" width="5%"><%=++mapId%> </td>
                      <td width="25%"><%QuickForm.writeHtml(out, mapping.getName());%></td>
                      <td width="30%"><%QuickForm.writeHtml(out, mapping.getDescription());%></td>
                      <td width="15%">
                        <%
                //displaying dataField References
                        boolean isUdaContainsparenthesis=false;
                        StringBuffer displayString=new StringBuffer();
                        Iterator dataReferencesItr = mapping.getDataFieldReferences().iterator();
                        int index=0;
                        while(dataReferencesItr.hasNext()){
                            if(index++!=0) displayString.append(",");
                            displayString.append((String)dataReferencesItr.next());
                        }

                        if(displayString.length()<1)
                            displayWarning="Please set User Defined Attribute";
                        %>
                        <%QuickForm.writeHtml(out, displayString.toString());%>                        </td>
                      <td width="15%" align="center">
                        <%
                //displaying valueMappings
                        displayString=new StringBuffer();
                        Iterator valueMappingsItr = mapping.getValueMappings().keySet().iterator();
                        index=0;
                        while(valueMappingsItr.hasNext()){
                            if(index++!=0) displayString.append(",");
                            String key = (String)valueMappingsItr.next();
                            displayString.append(key);
                            displayString.append("=");
                            displayString.append(mapping.getValueMappings().get(key).toString());
                        }
                        %>
                <option selected><%QuickForm.writeHtml(out, displayString.toString());%>
                        <font color="#000000" size="2">                        </font></td> <% if(!isActive){  %>
                      <td align="center" width="10%">
                        <table border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td align="center"><a href="Mapping.jsp?decisionTableId=<%=decisionTableId%>&mappingId=<%=mapping.getId()%>"><img src="images/buttons/edit_small_icon.gif" width="17" height="17" border="0"></a></td>
                            <td rowspan="2">&nbsp;</td>
                          </tr>
                          <tr>
                            <td class="smalllinks" align="center"><a href="Mapping.jsp?decisionTableId=<%=decisionTableId%>&mappingId=<%=mapping.getId()%>">EDIT</a></td>
                          </tr>
                        </table>                      </td><%}%>
                    </tr>
                    <%}}%>
                  </table>
                  <p><br>
</p>
                  <table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                    <tr align="center" class="headrow">
                      <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" align="right"><div align="left"><font color="#000000" size="2"> Results
                        Mapping </font> </div></td>
                    </tr>
                  </table>
                  <table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                    <tr align="left" class="resultHeader">
                      <th align="center"  width="5%" class="resultHeader">Id</th>
                      <th width="25%" class="resultHeader"> Name</th>
                      <th width="45%" class="resultHeader"> Description</th>
                   <th width="15%" class="resultHeader"> User Defined Attributes</th>
                   <% if(!isActive){%>
                   <th width="10%" align="center" class="resultHeader"> Action</th>
                   <%}%>
                    </tr>
                    <%
    // display result mapping table
     List resultMappings = decisionTable.getResultMappings();
     Iterator resultMappingsItr= resultMappings.iterator();
     nextColor = 0;
      rowColor = bcolor[nextColor];
        while (resultMappingsItr.hasNext())
        {
            Mapping mapping  = (Mapping)resultMappingsItr.next();

            rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;


%>
                    <tr align="center" class="<%=rowColor%>" valign="top">

                      <td align="center" width="5%"><%= mapping.getResultId()%></td>
                  <td width="25%"><%QuickForm.writeHtml(out, mapping.getResultName());%></td>
                 <td width="45%"><%QuickForm.writeHtml(out,mapping.getResultDescription());%></td>
                     <td width="15%"><%
            //displays data field references
                        StringBuffer displayString=new StringBuffer();
                        Iterator dataReferencesItr = mapping.getResultDataFieldReferences().iterator();
                        int index=0;
                        while(dataReferencesItr.hasNext()){
                            if(index++!=0) displayString.append(",");
                            displayString.append((String)dataReferencesItr.next());
                        }

                        if(displayString.length()<1)
                            displayWarning="Please set User Defined Attribute";
                        %>
                       <%QuickForm.writeHtml(out, displayString.toString());%></td>
                    <% if(!isActive){  %>
                    <td align="center" width="10%">
                        <table border="0" align="center" cellpadding="0" cellspacing="0">
                          <tr>
                            <td><a href="ResultMapping.jsp?decisionTableId=<%=decisionTableId%>&resultMappingId=<%=mapping.getResultId()%>"><img src="images/buttons/edit_small_icon.gif" width="17" height="17" border="0"></a></td>
                            <td align="center"><a href="ResultMapping.jsp?decisionTableId=<%=decisionTableId%>&resultMappingId=<%=mapping.getResultId()%>"></a></td>
                            </tr>
                          <tr>
                            <td class="smalllinks" align="center"><a href="ResultMapping.jsp?decisionTableId=<%=decisionTableId%>&resultMappingId=<%=mapping.getResultId()%>">EDIT</a></td>
                          </tr>
                        </table>                        </td><%}%>
                    </tr>
                    <%}%>
                  </table></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
    </tr>
  </table>
</center>
</body>
</html>

<%!
  public
    boolean
    isInteger(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            try
            {
                Double i = Double.valueOf(text);
                isMatch = true;
            }
            catch(NumberFormatException e)
            {
                isMatch = false;
            }
        }
        return isMatch;
    }

    public
    boolean
    isBoolean(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
             if (text.trim().equalsIgnoreCase("true") ||
                text.trim().equalsIgnoreCase("false"))
            {
                isMatch = true;
            }
        }
        return isMatch;
    }
    public
     boolean
    isOperator(String conditionValue)
    {
    String subConditionValue=conditionValue.substring(0,2);

        switch(subConditionValue.charAt(0))
        {
            case '<':if(subConditionValue.length()>0)
                        {
                            String temp=String.valueOf(subConditionValue.charAt(1));
                            if(subConditionValue.charAt(1)=='=')return true;
                            else if(isInteger(temp))return true;
                            else return false;
                        }
                        return true;
            case '>':if(subConditionValue.length()>0)
                        {
                            String temp=String.valueOf(subConditionValue.charAt(1));
                            if(subConditionValue.charAt(1)=='=')return true;
                            else if(isInteger(temp))return true;
                            else return false;
                        }
                        return true;
            case '=':return true;
            case '!':if(subConditionValue.length()>0)
                        {
                            String temp=String.valueOf(subConditionValue.charAt(1));
                            if(subConditionValue.charAt(1)=='=')return true;
                            else if(isInteger(temp))return true;
                            else return false;
                        }
                        return true;
            default:if(isInteger(subConditionValue))return true;
                    else    return false;
        }
    }
%>