<%@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="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() function --%>
<%@ include file="Debug.jsp" %>
<%-- calls the getSortedTemplates() function --%>
<%@ include file="sortProcessDefinition.jsp" %>

<%  //check 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" %>

<html>
<head>
<title>Interstage Business Process Manager | Approval DecisionTable Detail</title>
<link href="styles/dtm.css" rel="stylesheet" type="text/css">
<script>
function doSubmit(compostExists) {
	
    document.main.conditionKeys.value = "";
	var conditionLists = "";
    for(var conditionListIndex = 0; conditionListIndex < document.main.conditionList.options.length; conditionListIndex++) {
        if (document.main.conditionKeys.value == "" || document.main.conditionKeys.value == null) {
            document.main.conditionKeys.value = document.main.conditionList.options[conditionListIndex].text;
        }
        else {
            document.main.conditionKeys.value += "," + document.main.conditionList.options[conditionListIndex].text;
        }
		conditionLists+=","+document.main.conditionList.options[conditionListIndex].text;
    }
	if(document.main.conditionName.value!=""){
		alert("Please add the Condition: "+document.main.conditionName.value);
		return;
	}
	if(document.main.roleName.value!=""){
		alert("Please add the Result: "+document.main.roleName.value);
		return;
	}
	
	if(document.main.OtherProDef.value!=""){
		alert("Please add Other Process Definition: "+document.main.OtherProDef.value);
		return;
	}
	
	
if(compostExists=="false"){
	var conditionListWithComma = conditionLists.split(",");
	var conditionCount = 0;
	var compositKeyExists = "false";
	var conditionListWithCommaLength = document.main.conditionList.options.length;
	for(var conditionListsIndex = 0; conditionListsIndex < conditionListWithCommaLength; conditionListsIndex++) {
				var conditionText = document.main.conditionList.options[conditionListsIndex].text;
				 var conditionType=conditionText.substring(conditionText.lastIndexOf("(")+1,conditionText.length-1);
				 
				 if(conditionType!="COMPOSITE")conditionCount++;
				 else compositKeyExists= "true";
		}
		
		if(conditionCount<2 && compositKeyExists == "true")
		{
			alert("Atleast two Conditions needed to create a Composite Condition. Please enter two Conditions first and then create a Composite Condition.");
			return;
		}
}	
	   document.main.roles.value = "";
    for(var roleListIndex = 0; roleListIndex < document.main.roleList.options.length; roleListIndex++) {
        if (document.main.roles.value == "" || document.main.roles.value == null) {
            document.main.roles.value = document.main.roleList.options[roleListIndex].text;
        }
        else {
            document.main.roles.value += "," + document.main.roleList.options[roleListIndex].text;
        }
	}
		document.main.processDefinitions.value = "";
		for(var processDefinitionListIndex = 0; processDefinitionListIndex < document.main.processDefinitionList.options.length; processDefinitionListIndex++) {
        if (document.main.processDefinitions.value == "" || document.main.processDefinitions.value == null) {
            document.main.processDefinitions.value = document.main.processDefinitionList.options[processDefinitionListIndex].text;
        }
        else {
            document.main.processDefinitions.value += "," + document.main.processDefinitionList.options[processDefinitionListIndex].text;
        }
    }
	document.main.pageaction.value = "saveDecisionTable";
   document.main.submit();
}
</script>
<!-- calls methods for checking validity of user inputs -->
<script type="text/javascript" language="javascript" src="jscript/check.js"> </script>

<style type="text/css">
<!--
.style2 {font-family: sans-serif}
-->
</style>
</head>

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

	<%-- calls all the global variables --%>
	<%@ include file="commonDtmIterators.jsp" %>

	<%
	Plan[] plans = getSortedTemplates(wfSession); // returns all templates by sorting
	debug(trace, out, "\n decisionTableId="+decisionTableId);
	session.setAttribute("conditionKeysBeforeRemove",decisionTable.getConditionKeys());
	
	//checks if there are composit conditions exists or not
	boolean compostExists = false;
	int pConditionKeyId = -1;
	int sConditionKeyId = -1;
	Iterator serchCondKeysItr = conditionKeys.iterator();
	while(serchCondKeysItr.hasNext()){
	ConditionKey conditionKey = (ConditionKey) serchCondKeysItr.next();
		if(conditionKey.getType().equals(ConditionKey.COMPOSITE_VALUE)) {
		
			pConditionKeyId=conditionKey.getPrimaryConditionKeyId();
			if(pConditionKeyId!=-1){
							ConditionKey primaryCkey = decisionTable.getConditionKey(pConditionKeyId);
							if(primaryCkey!=null){
								compostExists = true;
							}
			}
			
			sConditionKeyId=conditionKey.getSecondryConditionKeyId();
			if(sConditionKeyId!=-1){
							ConditionKey secondaryCKey = decisionTable.getConditionKey(sConditionKeyId);
							if(secondaryCKey!=null){
								compostExists = true;
							}
			}
		}
	}
	
	
	%>
             
	<form name="main" method="post" action="DecisionTableDetail.jsp?decisionTableId=<%=decisionTableId%>">
                    <table width="90%" align="center" border="0">
                      <tr>
                        <td align="right"><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('<%=compostExists%>')">
                                    <input name="pageaction" type="hidden" id="pageaction">
                                    <input type="hidden" name="decisionTableId" value="<%=decisionTableIdStr%>">
                                    <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>
                    <table width="90%"  bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" 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><br><table bgcolor="#CCCCCC" width="100%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
                          <tr class="headrow">
                            <td colspan="2"><b>Conditions </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="conditionList" size="4">
                                        <% 
	// displays all Condition Keys 
        while(conditionKeysItr.hasNext()){
			ConditionKey conditionKey = (ConditionKey) conditionKeysItr.next();
			 String displayString = conditionKey.getName() + " (" + conditionKey.getType() ;
			 if((conditionKey.getConditionManipulator())!=null){
			 	displayString+="["+conditionKey.getConditionManipulator()+"]";
			 }
			 displayString+= ") " ;
%>
                                        <option selected>
                                        <%QuickForm.writeHtml(out, displayString);%>
                                        </option>
                                        <%
            }
%>
                                      </select>
                                      <input type="hidden" name="conditionKeys">
                                    </div>                              </tr>
                            </table></td>
                            <td align="center" valign="top" width="50%"><table width="100%" border="0" class="tableclass">
                                <tr>
                                  <td width="32%"><b>Name</b>:</td>
                                  <td width="48%"><input type="text" name="conditionName" size="15"/>                                  </td>
                                </tr>
                                <tr>
                                  <td width="32%"><b>Type</b>:</td>
                                  <td width="48%"><select name="conditionType" onChange="javascript:typeChanged()">
                                      <option selected>STRING</option>
                                      <option>NUMBER</option>
                                      <option>BOOLEAN</option>
                                      <option>COMPOSITE</option>
                                    </select>                                  </td>
                                  <td width="20%">&nbsp;</td>
                                </tr>
                                <tr>
                                  <td width="32%"><b>Number Multiplier:<br>
                                  </b>(Default is 1)</td>
                                  <td width="48%"><input name="conditionManipulator" type="text" disabled value="1" size="15">
                                      <br>
                                    e.g. &quot;1000000&quot; for Million</td>
                                  <td width="20%">&nbsp;</td>
                                </tr>
                            </table></td>
                          </tr>
                          <tr class="evenrow">
                            <td colspan="1"><div align="center">
                                <input value="Remove Condition" type="button" class="core-button" onClick="javascript:removeCondition()" name="RemoveKey"/>
                            </div></td>
                            <td colspan="1"><div align="center">
                                <input value="Add Condition" type="button" class="core-button" onClick="javascript:addCondition()" name="AddKey"/>
                            </div></td>
                          </tr>
                        </table></td>
                      </tr>
                           <tr class="oddrow">
                        <td><br>
                          <table bgcolor="#CCCCCC" width="100%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
                            <tr class="headrow">
                              <td colspan="2"><b>Results </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="roleList" size="5">
                                          <% 
	// display all Selections
        while(possibleSelectionsItr.hasNext()){
		     String displayString = (String)possibleSelectionsItr.next();
%>
                                          <option selected>
                                          <%QuickForm.writeHtml(out, displayString);%>
                                          </option>
                                          <%
            }
%>
                                        </select>
                                        <input type="hidden" name="roles">
                                    </div></td>
                                  </tr>
                              </table></td>
                              <td align="center" valign="top" width="50%"><table width="100%" border="0" class="tableclass">
                                  <tr>
                                    <td width="33%"><div align="left"><b>Name</b>:</div></td>
                                    <td width="65%"><input type="text" name="roleName"  size="18"/>                                    </td>
                                    <td width="2%">&nbsp;</td>
                                  </tr>
                                  <tr><br>
                                      <br>
                                    <td></td>
                                  </tr>
                              </table></td>
                            </tr>
                            <tr class="evenrow">
                              <td colspan="1"><div align="center">
                                  <input value="Remove Result" type="button" class="core-button" onClick="javascript:removeRole()" name="RemoveSelection"/>
                              </div></td>
                              <td colspan="1"><div align="center">
                                  <input value="Add Result" type="button" class="core-button" onClick="javascript:addResult()" name="AddSelection"/>
                              </div></td>
                            </tr>
                          </table></td>
                      </tr>
                           <tr class="oddrow">
                        <td><br>
                          <table bgcolor="#CCCCCC" width="100%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
                            <tr class="headrow">
                              <td colspan="2"><b>Process Definitions</b></td>
                            </tr>
                            <tr class="oddrow">
                              <td align="center" valign="top" width="50%"><table width="100%" height="174" border="0" class="tableclass">
                                  <tr>
                                    <td valign="middle"><div align="center">
                                        <select name="processDefinitionList" size="7">
                                          <%
					// display all selected Process Definitions
					while(processDefinitionsItr.hasNext()){
					String displayString = (String)processDefinitionsItr.next();
				%>
                                          <option selected>
                                          <%QuickForm.writeHtml(out,displayString);%>
                                          </option>
                                          <%}%>
                                        </select>
                                        <input type="hidden" name="processDefinitions">
                                    </div></td>
                                  </tr>
                              </table></td>
                              <td align="center" valign="middle" width="50%"><table width="321" height="144" border="0" align="left" class="tableclass">
                                  <tr>
                                    <td width="73"></td>
                                  </tr>
                                  <tr>
                                    <td width="73"><div align="left"><b>Name:</b></div></td>
                                    <td width="238"><!-- displays all Process Definitions -->
                                        <select name="processDefinitionName" onChange="setOtherProcessDef()" >
                                          <option selected>- SELECT PROCESS DEFINITION -</option>
                                          <%
			for (int plansIndex = 0; plansIndex < plans.length; plansIndex++)
			{
				String planName = plans[plansIndex].getName()+" ("+plans[plansIndex].getVersion()+")";%>
                                          <option value="<%=planName%>"><%=planName%></option>
                                          <%}%>
                                          <option value="Others">- Others -</option>
                                        </select>                                    </td>
                                  </tr>
                                <td>&nbsp;</td>
                                <tr>
                                  <td width="73"><div id="otherProcDefDivLabel" style="visibility:hidden"><b>Others :</b></div></td>
                                  <td><div id="otherProcDefDiv" style="visibility:hidden">
                                    <input name="OtherProDef" type="text" size="20%" />
                                  </div></td>
                                </tr>
                                <tr></tr>
                                <tr></tr>
                                <tr>
                                  <td>&nbsp;</td>
                                </tr>
                                <tr><br>
                                </tr>
                                <tr>
                                  <td colspan="3"><div align="center"> </div></td>
                                </tr>
                                </table>
                                 </td>
                            </tr>
                            <tr class="evenrow">
                              <td ><div align="center">
                                  <input value="Remove Process Definition" type="button" class="core-button" onClick="javascript:removeProcessDefinition()" name="RemoveProcessDefinition"/>
                              </div></td>
                              <td ><div align="center">
                                  <input value="Add Process Definition" type="button" class="core-button" onClick="javascript:addProcessDefinition()" name="addProcessDef"/>
                              </div></td>
                            </tr>
                          </table></td>
                      </tr>
                    </table>
                    <p></p>
                    <br>
                    <div align="right"><br>
                    </div>
                    <p>&nbsp;</p>
  </form>     
</center>
</body>
</html>
