<%@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.ArrayList"
%><%@page import="java.util.Iterator"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.DateFormat"
%><%@page import="com.fujitsu.ibpm.dtm.*"
%>
<%-- calls the debug() method --%>
<%@ 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" %>
<%
    
    String decisionTableIdStr = request.getParameter("decisionTableId");
    int decisionTableId = (new Integer(decisionTableIdStr)).intValue();
    debug(trace, out, "\n decisionTableId="+decisionTableId);
    String compositeKeyIdStr = request.getParameter("compositeKeyId");
    int compositeKeyId = (new Integer(compositeKeyIdStr)).intValue();
    debug(trace, out, "\n compositeKeyId="+compositeKeyId);
    DecisionTable decisionTable = dtm.getDecisionTable(decisionTableId);
    //get selection header
    ConditionKey compositeKey = decisionTable.getConditionKey(compositeKeyId);
    List primaryKeyValues = compositeKey.getPrimaryKeyValues();
    Iterator primaryKeyValuesItr = primaryKeyValues.iterator();
    session.setAttribute("compositeKeyId",compositeKeyIdStr);

    //getting the primary key names and types
    String selectedPrimaryKey="";
    String keyTypes="";
        List checkConditionKeys = decisionTable.getConditionKeys();
        Iterator checkConditionKeysItr = checkConditionKeys.iterator();
        while(checkConditionKeysItr.hasNext()){
            ConditionKey conditionKey = (ConditionKey) checkConditionKeysItr.next();
        
            if(!(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)))
            {
                String displayString = conditionKey.getName();
                
                selectedPrimaryKey+=","+displayString;
                keyTypes+=","+conditionKey.getType();
            }
        }

%>
<html>
<head>
<title>Interstage Business Process Manager | Approval DecisionTable Detail</title>
<link href="styles/dtm.css" rel="stylesheet" type="text/css">
<script>

function doSubmit() {
    var primaryCondition = document.main.primaryCondition;
    var secondaryCondition = document.main.secondaryCondition;
    var primaryConditionText = primaryCondition.options[primaryCondition.selectedIndex].text;
    var secondaryConditionText = secondaryCondition.options[secondaryCondition.selectedIndex].text;
    if(primaryConditionText==secondaryConditionText)
    {
        alert("Primary and Secondary Condition cannot be same.");
        return;
    }
    if (primaryConditionText==null || primaryConditionText=="- Select Primary Condition -")
    {
        alert("Please select Primary Condition");
        return;
    }
   if (secondaryConditionText==null || secondaryConditionText=="- Select Secondary Condition -")
    {
        alert("Please select Secondary Condition");
        return;
    }
    
    var primaryKeyValueListLength = document.main.primaryKeyValueList.options.length;
    if(primaryKeyValueListLength<1)
        {
            alert("Please enter Primary Condition value");
            return;
        }
    
    //checks the elements of primary condition value list when the primary condition will change next time to other data type
    var primaryCondition = document.main.primaryCondition;
    var primaryConditionText = primaryCondition.options[primaryCondition.selectedIndex].text;
    
    var primKeyTypes='<%=keyTypes%>';
    var primKeyNames='<%=selectedPrimaryKey%>';
    var primaryKeyTypes=primKeyTypes.split(",");
    var primaryKeyNames=primKeyNames.split(",");
    
    var primaryKeyValueList = document.main.primaryKeyValueList;
    var primaryKeyValueListLength = primaryKeyValueList.options.length;
    
     for(var primaryKeyValueListIndex = 0; primaryKeyValueListIndex < primaryKeyValueListLength; primaryKeyValueListIndex++) {
        
        for(var primaryKeyNamesIndex = 0; primaryKeyNamesIndex < primaryKeyNames.length; primaryKeyNamesIndex++) {
        
        //check for numeric value
        if(primaryConditionText==primaryKeyNames[primaryKeyNamesIndex]){
             if (primaryKeyTypes[primaryKeyNamesIndex]=="NUMBER"){
                var value = document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text;
                var operator = "";
                if (value.length > 2) {
                     operator = value.substring(0,2);
                     if (operator=="<=" || operator==">=" || operator=="!=") {
                       value = value.substring(2,value.length);
                     } else {
                       operator = value.substring(0,1);
                       if (operator=="=" || operator==">" || operator=="<") {
                         value = value.substring(1,value.length);
                       }
                     }
                } else if (value.length > 1) {
                     operator = value.substring(0,1);
                     if (operator=="=" || operator==">" || operator=="<") {
                       value = value.substring(1,value.length);
                     }
                }
                
                if (isNaN(value)){
                    alert(document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text+" is not a numeric value. Please enter a numeric value.");
                    return;
                }
            }
        }
        
        // check for boolean value
        if(primaryConditionText==primaryKeyNames[primaryKeyNamesIndex]){
             if (primaryKeyTypes[primaryKeyNamesIndex]=="BOOLEAN"){
                var primKeyValue=document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text;
                if (primKeyValue=="true" || (primKeyValue=="false")){
                }else{
                        alert(document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text+" is not a boolean value. Please enter a boolean value.");
                        return;
                }
            }
          }
       }
    }
    
    //End check primary condition value list values
    
    
    document.main.primaryConditionValue.value = primaryConditionText;
    document.main.secondaryConditionValue.value = secondaryConditionText;


       document.main.primaryKeyValues.value = "";
    for(var primaryKeyValueListIndex = 0; primaryKeyValueListIndex < document.main.primaryKeyValueList.options.length; primaryKeyValueListIndex++) {
        if (document.main.primaryKeyValues.value == "" || document.main.primaryKeyValues.value == null) {
            document.main.primaryKeyValues.value = document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text;
        }
        else {
            document.main.primaryKeyValues.value += "&sep;" + document.main.primaryKeyValueList.options[primaryKeyValueListIndex].text;
        }
    }
    document.main.submit();
}

function handleEnter(field, event) 
{
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if(keyCode == 13) 
    {
       addPrimaryKeyValue('<%=keyTypes%>','<%=selectedPrimaryKey%>')
       return false;
    } 
    else
       return true;
}    

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

</head>

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

     <form name="main" method="post" action="DecisionTableDetail.jsp?pageaction=saveCompositeKey&decisionTableId=<%=decisionTableId%>&compositeKeyId=<%=compositeKeyId%>">               
  <TABLE  border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
    <tr> 
      <td align="right" height="35" valign="top"> 
        
        
        <table border="0" cellpadding=0 cellspacing=0>
          <tr> 
            <td align="right"> 
              <table border="0" cellspacing="0" cellpadding="0">
                <tr background="images/buttons/com_button_bg.gif"> 
                  <td width="15"><img src="images/buttons/but_save.gif"></td>
                  <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:doSubmit()"> 
                    <div align="center" class="buttonlink"> OK</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/cancel.gif"></td>
                  <td background="images/buttons/com_button_bg.gif" nowrap><a href="DecisionTableDetail.jsp?decisionTableId=<%=decisionTableId%>"> 
                    <div align="center" class="buttonlink"> Cancel</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>
      </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"> DecisionTable: <%QuickForm.writeHtmlWithNBSPs(out, decisionTable.getName()); %> 
            <div align="right"> </div>
            </font></div>
      </td>
    </tr>
  </table>
                
    <table bgcolor="#CCCCCC" width="90%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
      <tr class="headrow"> 
        <td colspan="4"> <b>Composite Condition </b></td>
      </tr>
      <tr class="oddrow"> 
        <td align="center" valign="top" width="40%"> 
          <table width="100%" border="0" class="tableclass">
            <tr class="evenrow" > 
              <td width="24%" height="25"><b>Primary Condition </b>:</td>
              <td width="76%" height="25"> 
                <select name="primaryCondition" >
                      <option selected> - Select Primary Condition - </option>
        <% 
        // display all condition Keys for primary condition
        List conditionKeys = decisionTable.getConditionKeys();
        Iterator conditionKeysItr = conditionKeys.iterator();
        while(conditionKeysItr.hasNext()){
            ConditionKey conditionKey = (ConditionKey) conditionKeysItr.next();
        
            if(!(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)))
            {
                String displayString = conditionKey.getName();
                
                int id = compositeKey.getPrimaryConditionKeyId();
                ConditionKey primaryCkey = decisionTable.getConditionKey(id);
                
                String selected = "";
                if(primaryCkey!=null && primaryCkey.getName().equals(displayString)) selected = "selected";     
                    %>
                 <option <%=selected%>><%QuickForm.writeHtml(out, displayString);%></option>
                  <%
            }
            }
%>
                </select>
                <input type="hidden" name="primaryConditionValue">
              </td>
            </tr>
            <tr class="oddrow"> 
              <td width="24%" height="25"><b>Secondary Condition </b>:</td>
              <td height="25"> 
                <select name="secondaryCondition" >
                  <option selected> - Select Secondary Condition - </option>
                 <% 
        // display all condition Keys for secondary condition
        
        conditionKeysItr = conditionKeys.iterator();
        while(conditionKeysItr.hasNext()){
            ConditionKey conditionKey = (ConditionKey) conditionKeysItr.next();
            
            if(!(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)))
            {
                String displayString = conditionKey.getName();
                
                int id = compositeKey.getSecondryConditionKeyId();
                ConditionKey secondarycKey = decisionTable.getConditionKey(id);
                String selected = "";
                if(secondarycKey!=null && secondarycKey.getName().equals(displayString)) selected = "selected"; 
%>
                  <option <%=selected%>><%QuickForm.writeHtml(out, displayString);%></option>
                  <%
            }
            }
%>
                </select>
                <input type="hidden" name="secondaryConditionValue">
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    <br>
    <table bgcolor="#CCCCCC" width="90%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
      <tr class="headrow"> 
        <td colspan="5"> <b>Primary Condition Values</b></td>
      </tr>
      <tr class="oddrow"> 
        <td align="center" valign="top" width="50%"> 
          <table width="100%" border="0" class="tableclass">
            <tr> 
              <td valign="top"> 
                <div align="center">
                  <select name="primaryKeyValueList" size="6">
                    <%
    // displays all primary key values        
        while(primaryKeyValuesItr.hasNext()){
                    String displayString = (String)primaryKeyValuesItr.next();
%>
                    <option selected><%QuickForm.writeHtml(out, displayString);%></option>
                    <%
            }
%>
                  </select>
                  <input type="hidden" name="primaryKeyValues">              
                </div></td>
            </tr>
          </table>
        </td>
        <td align="center" valign="top" width="50%"><p>&nbsp;</p>
          <table width="100%" border="0" class="tableclass">
            <tr>
              <td width="35%"><b>Value</b>:</td>
              <td width="65%"><input type="text" name="primaryKeyValueName" size="15" onkeypress="return handleEnter(this, event)"/>              </td>
            </tr>
          </table>          
        <p>&nbsp;</p></td>
      </tr>
      <tr class="evenrow">
          <td colspan="1"><div align="center">
            <input value="Remove Value" type="button" class="core-button" onClick="removePrimaryKeyValue()" name="RemoteValue"/>
          </div></td>
          <td colspan="1"><div align="center">
            <input value="Add Value" type="button" class="core-button" onClick="addPrimaryKeyValue('<%=keyTypes%>','<%=selectedPrimaryKey%>')" name="AddValue"/>
          </div></td>
        </tr>
    </table>
    <br>
    <table  border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
      <tr> 
        <td align="right" height="35" valign="top"> 
          <table border="0" cellpadding=0 cellspacing=0>
            <tr> 
              <td align="right"> 
                <table border="0" cellspacing="0" cellpadding="0">
                  <tr background="images/buttons/com_button_bg.gif"> 
                    <td width="15"><img src="images/buttons/but_save.gif"></td>
                    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:doSubmit()"> 
                      <div align="center" class="buttonlink"> OK</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/cancel.gif"></td>
                    <td background="images/buttons/com_button_bg.gif" nowrap><a href="DecisionTableDetail.jsp?decisionTableId=<%=decisionTableId%>"> 
                      <div align="center" class="buttonlink"> Cancel</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>
        </td>
      </tr>
    </table>
    <p>&nbsp;</p>
  </form>     
</center>
</body>
</html>
