<%@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.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.TrackedUDARecord"
%><%@page import="com.fujitsu.iflow.model.util.IflowEnumeration"
%><%@page import="java.util.Hashtable"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }

    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
    
    String udaValue = "";
    String udaName = request.getParameter("udaName");
    String udaValueType = request.getParameter("udaValueType");
    String pocessId = request.getParameter("procId");
    String hId = request.getParameter("historyId");
    String isArchived = request.getParameter("isArchived");
    if (pocessId==null) {
        throw new Exception("Can not display UDA Track Value: Page requires that the process instance be specified with an 'id' parameter");
    }
    
    long piidlong = Long.parseLong(pocessId);
    ProcessInstance process = null;
    if(isArchived != null && isArchived.equals("true")){
        BPMAdmin bpas = bps.getAdmin();
        process = bpas.getArchivedProcess(piidlong);
    }
    else
        process = WFObjectFactory.getProcessInstance(piidlong, wfSession);
    if (process==null) {
        throw new Exception("Can not display UDA Track Value: Can not find the process Instance for id="+pocessId);
    }
    
    long hidlong = Long.parseLong(hId);
    
    int _processHistoryFields[] = {ProcessInstance.HISTORY_ID,
                                    ProcessInstance.HISTORY_MODIFIED_TRACKEDUDAS};
    IflowEnumeration sElements = process.getHistory(_processHistoryFields);

    Integer udaHistoryKey = new Integer(ProcessInstance.HISTORY_MODIFIED_TRACKEDUDAS);
    Integer historyIdKey = new Integer(ProcessInstance.HISTORY_ID);
    boolean bSuccess = false;
    while(sElements.hasMoreElements()) 
    {
        Hashtable historyData = (Hashtable) sElements.nextElement();
        long historyId = ((Long)historyData.get(historyIdKey)).longValue();
        if(historyId == hidlong)
        {
            TrackedUDARecord[] tUdas = (TrackedUDARecord[] )historyData.get(udaHistoryKey);
            if (tUdas != null) 
            {
                for(int i=0; i< tUdas.length; i++)
                {
                    if(tUdas[i].getName().equals(udaName)) 
                    {
                        if(udaValueType.equalsIgnoreCase("New"))
                            udaValue = tUdas[i].getNewValue();
                        else
                            udaValue = tUdas[i].getOldValue();
                        bSuccess = true;                        
                        break;
                    }
                }
            }
        }
        if(bSuccess)
            break;
    }
   
%>
<html>
<head>
<title>Interstage Business Process Manager | UDA History for <%= udaName%></title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
</head>
<body>
<TABLE style=WORD-BREAK:BREAK-ALL 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')" colspan="7"><font color="#000000" size="2">History UDA Name: <%= udaName %></font></td>
    </tr>
<tr bgcolor="white">
    <td><%= udaValue %></td>
</tr>
</table>
</body>
</html>