<%@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.DataItemRef"
%><%@page import="java.io.Writer"
%><%@page import="java.util.List"
%><%@page import="java.util.Map"
%><%@page import="java.util.ArrayList"
%><%@page import="java.util.Iterator"
%><%@page import="java.util.Enumeration"
%><%@page import="java.util.StringTokenizer"
%><%@page import="java.text.DateFormat"
%><%@page import="com.fujitsu.ibpm.dtm.*"
%>


<%    //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" %>
<%-- calling the getSortedTemplates() function --%>
<%@ include file="sortProcessDefinition.jsp" %>
<html>
<head>
<title>Interstage Business Process Manager | Approval Data Mapping </title>
<link href="styles/dtm.css" rel="stylesheet" type="text/css">
<script>
function doSubmit() {

    var mappingDescription = document.main.mappingDescription;
    if(mappingDescription.value.length>256)
    {
        alert("Result mapping description cannot be more than 256 characters.");
        return;
    }
      document.main.datas.value = "";
    for(var dataListIndex = 0; dataListIndex < document.main.dataList.options.length; dataListIndex++)         {
        if (document.main.datas.value == "" || document.main.datas.value == null) {
            document.main.datas.value = document.main.dataList.options[dataListIndex].text;
        }
        else {
            document.main.datas.value += "," + document.main.dataList.options[dataListIndex].text;
        }
    }
    document.main.pageaction.value = "saveResultMapping";
 document.main.submit();
}
</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/>

<%-- calls all the global variables --%>
<%@ include file="commonDtmIterators.jsp" %>
<%
    String mappingIdStr = request.getParameter("resultMappingId");
    int mappingId = (new Integer(mappingIdStr)).intValue();
    Mapping mapping = dtm.getResultMapping(decisionTableId,mappingId);

    session.setAttribute("Mapping",mapping);
    Plan[] plans = getSortedTemplates(wfSession); // returns all Default sorted Process Definitions
    List dataReferences = mapping.getResultDataFieldReferences(); // returns all the data field references
    Iterator dataReferencesItr = dataReferences.iterator();
%>

     <form name="main" method="post" action="DecisionTableDetail.jsp?saveResultMapping&decisionTableId=<%=decisionTableId%>">
  <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()">
                  <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>
    <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>
  </table>

    <br>
    <table bgcolor="#CCCCCC" width="90%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
      <tr class="headrow">
        <td colspan="5" class="resultHeader"> <b>Result Mapping</b></td>
      </tr>
    </table>

    <table bgcolor="#CCCCCC" width="90%" border="0" cellpadding="2" cellspacing="1" align="center" class="tableclass">
      <tr class="evenrow">
        <td width="200"><b>Name</b>:</td>
        <td>
        <%QuickForm.writeHtml(out, mapping.getResultName());%>
        </td>
      </tr>
      <tr class="oddrow">
        <td valign="top" width="200"><b>Description</b>:</td>
        <td valign="top">
          <textarea name="mappingDescription" cols="30"><%QuickForm.writeHtml(out,mapping.getResultDescription());%></textarea>
        </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" class="resultHeader"> <b>User Defined Attributes</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="dataList" size="2">
                    <%
        while(dataReferencesItr.hasNext()){
             String displayString = (String)dataReferencesItr.next();
                    %>
                              <option selected>
                              <%QuickForm.writeHtml(out, displayString);%>
                    </option>
                    <%}%>
                  </select>
                  <input type="hidden" name="datas">
                </div></td>
                      </table>
        </td>
        <td align="center" valign="top" width="50%">
          <table width="100%" border="0" class="tableclass">
            <tr>
<%

    int isUdaExist=0;
    //loops untill all Default Process Definitions
     //matches a single User Selected Process Definition with all Default Process Definitions
    while (processDefinitionsItr.hasNext())
    {
     String pplan=(String)processDefinitionsItr.next();
      for (int plansIndex = 0; plansIndex < plans.length; plansIndex++)
      {
          //getting process definition with version number
        String planName = plans[plansIndex].getName()+" ("+plans[plansIndex].getVersion()+")";
        if(planName.equals(pplan))
         {
                int last;
        DataItemRef[] dirz = plans[plansIndex].getDataItemRefs();
                last = dirz.length;
                //geting all User Defined Attributes(UDA)
                for ( int udaNamesIndex=0; udaNamesIndex<last; udaNamesIndex++)
                {
                    DataItemRef  dir = dirz[udaNamesIndex];
                    udaDisplayNames.add(dir.getName()+" ("+dir.getType()+")"+" ("+planName+")");
                    udaNames.add(dir.getName());
                }
            break;
          }
        }
    }%><td width="35%"><div align="left"><b>User Defined Attribute(UDA)</b>:</div></td><%
    if(udaNames.size()>0)
    {
    isUdaExist=1;
    //prints all UDA's in a Select List
    Iterator udaNamesItr= udaNames.iterator();
    Iterator udaDisplayNamesItr = udaDisplayNames.iterator();
%>
    <td>
            <select name="dataName"  onChange="setUdaChoice()">
            <option selected>- SELECT UDA -</option>
            <%
            while(udaNamesItr.hasNext())
            {
                String UDA=(String)udaNamesItr.next();
                String UDADisplay = (String)udaDisplayNamesItr.next();
                %>
                   <option value="<%QuickForm.writeHtml(out, UDA);%>"><%QuickForm.writeHtml(out, UDADisplay);%></option>
            <%}%>
                   <Option value="Others">- Others -</Option>
             </select></td></tr>
            <!-- displays the UDA Text Field  when Others selected -->
            <tr><td width="35%"><div align="left"><strong id="OtherUDALabel" style="visibility:hidden">Others :</strong></div></td>
            <td><strong id="OtherUDA" style="visibility:hidden">
            <input name="OthersUDA"  type="text" size="20"  /></strong></td></tr>

            <tr>
            <td></td>
             <td>&nbsp;</td>
            </tr>
    <%} // end  IF
    else // user have to enter the desired UDA
    {    isUdaExist=0;
    %>
        <td><input name="txtDataName" type="text"  />              </td>
                  </tr>
                <tr>
                <td></td>

         <%}%>
          </table>
        </td>
         <tr class="evenrow">
            <td colspan="1"><div align="center">
              <input value=" Remove UDA " type="button" class="core-button" onClick="javascript:removeDataField()" name="RemoveDataField"/>
            </div></td>
            <% if(isUdaExist==1){%>
        <td colspan="1"><div align="center">
          <input value=" Add UDA " type="button" class="core-button" onClick="javascript:addResultDataField()" name="DefaultUda"/>
        </div></td><%}else{%>
        <td><div align="center">
                <input value=" Add UDA " type="button" class="core-button" onClick="javascript:addResultUserDataField()" name="UserDefUda"/></div></td><%}%>
            </tr>
      </tr>
      </tr>
    </table>
   <br>
    <p>&nbsp;</p>
  </form>
</center>
</body>
</html>

