<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Error.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.DataItemRef"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="java.util.*"%>


<%

    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;
    }

    String planName = request.getParameter("planName");
    String test1 = "Hello World";
    String test4 = request.getParameter("test4");
    if (test4 == null) {
        test4 = "";
    }
    StringBuffer t2 = new StringBuffer("Th");
    t2.append((char) 246 );
    t2.append( 's' );
    t2.append((char) 232 );
    t2.append( " f" );
    t2.append((char) 251);
    t2.append((char) 241);
    t2.append((char) 241);
    t2.append((char) 255);
    t2.append(" ");
    t2.append((char) 231);
    t2.append('h');
    t2.append((char) 227);
    t2.append((char) 174);
    t2.append('a');
    t2.append((char) 169);
    t2.append('t');
    t2.append((char) 232);
    t2.append('r');
    t2.append((char) 167);

    String test2 = t2.toString();
    StringBuffer tbuilder = new StringBuffer();
    tbuilder.append( (char)1057);
    tbuilder.append( (char)1090);
    tbuilder.append( (char)1091);
    tbuilder.append( (char)1076);
    tbuilder.append( (char)1077);
    tbuilder.append( (char)1085);
    tbuilder.append( (char)1095);
    tbuilder.append( (char)1077);
    tbuilder.append( (char)1089);
    tbuilder.append( (char)1082);
    tbuilder.append( (char)1072);
    tbuilder.append( (char)1103);
    tbuilder.append( (char)32);
    tbuilder.append( (char)3586);
    tbuilder.append( (char)3629);
    tbuilder.append( (char)3591);
    tbuilder.append( (char)3617);
    tbuilder.append( (char)3627);
    tbuilder.append( (char)3634);
    tbuilder.append( (char)3623);
    tbuilder.append( (char)3636);
    tbuilder.append( (char)3607);
    tbuilder.append( (char)3618);
    tbuilder.append( (char)3634);
    tbuilder.append( (char)3621);
    tbuilder.append( (char)3633);
    tbuilder.append( (char)3618);
    tbuilder.append( (char)3617);
    tbuilder.append( (char)3627);
    tbuilder.append( (char)3636);
    tbuilder.append( (char)3604);
    tbuilder.append( (char)3621);
    tbuilder.append( (char)32);
    tbuilder.append( (char)20140);
    tbuilder.append( (char)22478);
    //tbuilder.append( (char)39318);
    //tbuilder.append( (char)33719);
    //tbuilder.append( (char)26691);
    //tbuilder.append( (char)33457);
    //tbuilder.append( (char)27700);
    //tbuilder.append( (char)27597);
    //tbuilder.append( (char)24188);
    //tbuilder.append( (char)20307);

    String test3 = tbuilder.toString();

    String udaName = request.getParameter("uda");
    String op = request.getParameter("op");

    if (planName == null) {
        throw new Exception("Process definition Name parameter must be specified for this test page to work");
    }


    ProcessInstance pi = null;
    Plan aPlan = null;
    long procId = -1;

    try {
        // given the plan name get the first process id.
        WFObjectList wfol = WFObjectFactory.getWFObjectList(((BPMSessionImpl)bps)._wfs);
        wfol.addFilter(WFObjectList.LISTFIELD_PLAN_NAME , "=", "'"+ planName +"'");
        wfol.openBatchedList(Filter.MyProcesses);
        Object[] processList = wfol.getNextBatch(0,1);

        if (processList != null && processList.length > 0)
            pi = (ProcessInstance) processList[0];

        procId = pi.getId();
        aPlan = pi.getPlan();
    }
    catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Unable to find the process definition with name  '" + planName + "' " +e.toString());
    }

    // if no uda specified, then just pick the first string uda
    if (udaName == null) {
        DataItemRef[] refs = aPlan.getDataItemRefs();
        int last = refs.length;
        for (int i=0; i<last; i++) {
            DataItemRef dir = refs[i];
            if ("STRING".equals(dir.getType())) {
                udaName = dir.getName();
                break;
            }
        }
    }

    if (udaName == null) {
        throw new Exception("This process instance '"+procId+"' does not have any string UDA values, so it can not be used for testing.");
    }

    String oldValue = pi.getDataItemValue(udaName);
    String newValue = null;
    String finalValue = null;

    if (op != null) {
        if ("Hello World".equals(op)) {
            newValue = test1;
        }
        else if ("Euro Characters".equals(op)) {
            newValue = test2;
        }
        else if ("Unicode Test".equals(op)) {
            newValue = test3;
        }
        else if ("User".equals(op)) {
            newValue = test4;
        }
        if (newValue != null) {
            Properties nvlist = new Properties();
            nvlist.setProperty(udaName, newValue);
            pi.startEdit();
            pi.setDataItemValues(nvlist);
            pi.commitEdit();
        }
    }

    finalValue = pi.getDataItemValue(udaName);
%>
<HTML>
<HEAD>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<TITLE>Process Instance Manager</TITLE>
</HEAD>

<body class="bodyClass">
<jsp:include page="ProcessHeader.jsp" flush="true"/>

<h3>Test process instance '<%= procId %>' attribute '<%=udaName%>'</h3>

<form name="testForm" method="get" action="UnicodeTest.jsp">
<input type="hidden" name="planName" value="<%=planName%>">
<input type="hidden" name="uda" value="<%=udaName%>">
<TABLE border="1" cellpadding=2 cellspacing=1 class="tableClass">
<tr><td>previous value</td><td><%=oldValue%></td></tr>
<tr><td></td><td>
<%
    if (oldValue != null) {
        int last1 = oldValue.length();
        for (int j=0; j<last1; j++) {
            out.write(Integer.toString(oldValue.charAt(j)));
            out.write(" ");
        }
    }
%></td></tr>
<tr><td>set value</td><td><%=newValue%></td></tr>
<tr><td></td><td>
<%
    if (newValue != null) {
        int last1 = newValue.length();
        for (int j=0; j<last1; j++) {
            out.write(Integer.toString(newValue.charAt(j)));
            out.write(" ");
        }
    }
%></td></tr>
<tr><td>final value</td><td><%=finalValue%></td></tr>
<tr><td></td><td>
<%
    if (finalValue != null) {
        int last1 = finalValue.length();
        for (int j=0; j<last1; j++) {
            out.write(Integer.toString(finalValue.charAt(j)));
            out.write(" ");
        }
    }
%></td></tr>
<tr><td><input type="submit" name="op" value="Refresh"></td>
    <td>Don't set a value, just refresh the page from the API.</td></tr>
<tr><td><input type="submit" name="op" value="Hello World"></td>
    <td>This button sets the uda to the string '<%=test1%>' as an
        example that uses only ASCII characters</td></tr>
<tr><td><input type="submit" name="op" value="Euro Characters"></td>
    <td>This button sets the uda to a string with 'upper ascii characters'
        from the western European character set: '<%=test2%>'</td></tr>
<tr><td><input type="submit" name="op" value="Unicode Test"></td>
    <td>This tests character values greater than 255, often referred to
        as Unicode characters (but of course, these are all Unicode characters).
        The specific value used is: '<%=test3%>'</td></tr>
<tr><td><input type="submit" name="op" value="User"></td>
    <td><input type="text" name="test4" value="<%=test4%>"></td></tr>

</TABLE>

</form>
</center>
</BODY>
</HTML>