<%@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.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.FormRef"
%><%@page import="com.fujitsu.iflow.model.workflow.Node"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="com.fujitsu.iflow.server.dataobjects.WorkItemStatesEnum"
%><%@page import="java.io.Writer"
%><%@page import="java.text.DateFormat"
%><%@page import="java.util.Date"
%>

<script language="JavaScript">
    window.onbeforeunload = confirmExit;

    function confirmExit()
    {
        var curWorkTime = document.wiForm.workTime.value;
        if (curWorkTime != "00:00:00" && document.wiForm.displayWrnMsg.value == "yes") {
            return "You have attempted to leave this page. " +
                   "If you have made any changes to the fields without clicking the Save button, "+
                   "your changes will be lost. Are you sure you want to exit this page?";
        }
    }
</script>

<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        throw new Exception("Somehow got to the ProcessPanel.jsp without a valid session.  Any page including the ProcessPanel.jsp must first assure there is a valid session");
    }

    String formName = request.getParameter("Forms");
    WorkItem task = (WorkItem)request.getAttribute("task");
    if(task == null) {
        throw new Exception("Invalid or missing parameter to ProcessPanel: task");
    }
    long taskId = task.getId();
    String includeSet = (String)request.getAttribute("includeSet");
    if (includeSet == null || includeSet.length()==0) {
        includeSet = "links,activity,from,to,date,process,due,desc,form,choices,commands";
    }

    ProcessInstance pi = task.getProcessInstance();
    Node activity = task.getNodeInstance();

    String twfSARoption = getServerOption(bps, "TWFServerAcceptRequired");
    boolean isAcceptRequired = twfSARoption.equalsIgnoreCase("true");
    String[] forms =  null;
    String state =    "Completed";
    String from =     task.getInitiator();
    String to =       task.getAssignee();
    String date = ""+task.getCreationTime();

    Date dd = task.getDueDate();

    String forTask =  "";
    String process =  pi.getName();
    String description=activity.getDesc();
    String[] choices = task.getChoices();
    boolean formsExist = false;
    String acceptButton = "";
    String declButton = "";
    String saveButton = "";
    String reassignButton = "";
    String choiceButton = "";
    String frselected = "";
    String workTimerButton= "";
    boolean showEditButton = false;  //set to true for edit button behavior
    int piSeqId = pi.getSequenceId();
    int stateValue = task.getState();

    if(isAcceptRequired) {
        workTimerButton = "disabled";
        acceptButton = "";
        choiceButton = "disabled";
        saveButton = "disabled";

    }
    if(stateValue == task.STATE_DECLINED){
        choiceButton = "disabled";
        declButton = "disabled";
        acceptButton="";
        workTimerButton = "disabled";
        saveButton = "disabled";
        reassignButton = "disabled";
    }
    else if(stateValue == task.STATE_ACCEPTED){
        acceptButton = "disabled";
        choiceButton = "";
        workTimerButton = "";
        saveButton = "";
    }

    if(stateValue == task.STATE_COMPLETED){
        choiceButton = "disabled";
        saveButton = "disabled";
        acceptButton = "disabled";
        declButton = "disabled";
        reassignButton = "disabled";
        workTimerButton = "disabled";
    }
    else {
        state = WorkItemStatesEnum.from_int(stateValue).toString();
    }

    forms = getFormNames(task);
    if(forms != null && forms.length > 0)
    {
        formsExist = true;
    }

    forTask = task.getName() + "  (state: " + state +")";

    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    long piIdLong = task.getProcessInstanceId();
    String graphicalViewURL = "PIGraphic.jsp?piId="+piIdLong;
    String graphicalViewBPMNURL = "PIGraphicBPMN.jsp?piId="+piIdLong;
    String processViewURL = "ProcessInstance.jsp?piId="+piIdLong;
    String historyURL = "ShowHistory.jsp?id="+piIdLong;
%>
  <script language="javascript" src="styles/timerScripts.js"></script>
  <script language="javascript" src="styles/clockscripts.js"></script>
  <input type="hidden" name="procSeq" value="<%= piSeqId %>">
  <input type="hidden" name="taskId" value="<%= taskId %>">
  <input type="hidden" name="displayWrnMsg" value="yes">

<!-- Main Table starts here -->
<% if (includeSet.indexOf("links")>=0) { %>
<table width="650" class="tableClass" align="center">
  <tr>
    <td width="300" > <b> &nbsp;</b> </td>
    <td width="10%">
      <%makeButton(out, historyURL, "History", "images/buttons/but_history.gif");%>
    </td>
    <td width="10%">
      <%makeButton(out, processViewURL, "Process Instance", "images/buttons/but_table_view.gif");%>
    </td>
    <td width="10%">
      <%makeButton(out, graphicalViewURL, "Snapshot", "images/buttons/but_graphical_view.gif");%>
    </td>
    <td width="10%">
      <%makeButton(out, graphicalViewBPMNURL, "BPMN", "images/buttons/but_graphical_view.gif");%>
    </td>
  </tr>
</table>
<% } %>
<table width="650" class="tableClass" align="center">
<col width="20%"><col width="70%"><col width="10%">
<%
    rowColor = bcolor[nextColor];
    nextColor = (nextColor + 1)%2;
    if (includeSet.indexOf("activity")>=0) {
        %>
        <tr class="headrow">
        <td width="22"><img src="images/icons/book_details.gif" border="0"></td>

        <td width="628" colspan="2"> <b>
          <% QuickForm.writeHtml(out, forTask); %>
          </b></td>
        </tr>
        <%
    }
    if (includeSet.indexOf("from")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">From</td>
        <td><% QuickForm.writeHtml(out, from); %></td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("to")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">To</td>
        <td ><% QuickForm.writeHtml(out, to); %></td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("date")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">Date</td>
        <td ><script> showDate('<%=date%>'); </script></td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("process")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">Process Instance</td>
        <td >

        <% QuickForm.writeHtml(out, process); %>
        (<a href="ProcessInstance.jsp?piId=<%=pi.getId()%>"><%=pi.getId()%></a>)
        <% QuickForm.writeHtml(out, pi.getTitle()); %>
        <% writeWithLinks(out, pi.getDescription()); %>
        </td></tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("due")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">Due Date</td>
        <td >
        <% if (dd != null) { %>
            <script> showDate('<%=dd.getTime()%>'); </script>
        <% } %>
        </td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("desc")>=0) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">Description</td>
        <td><% writeWithLinks(out, description); %></td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    if (includeSet.indexOf("form")>=0 && formsExist) {
        %>
        <tr class="<%=rowColor%>">
        <td class="prompt">Form</td>
        <td nowrap>
        <%
        String formSelectedOption = "";
        for ( int i = 0; i < forms.length; i++ )
        {
            String thisFormName = forms[i];
            if (formName != null && thisFormName.equals(formName)) {
                %><b><% QuickForm.writeHtml(out, thisFormName); %>
                </b>       <input type="hidden" name="Forms" value="<%= formName %>">
                <%
            }
            else {
                %>&nbsp;<a href="WorkItem.jsp?taskId=<%=taskId%>&Forms=<%=URLEncoder.encode(thisFormName, "UTF-8")%>">
                <% QuickForm.writeHtml(out, thisFormName); %></a>&nbsp;
                <%
            }
        }
        %>
        </td>
        </tr>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
    %></table><%
    if (includeSet.indexOf("choices")>=0) {
        %>
        <br>
        <table width="650" class="tableClass" align="center">
        <tr class="headrow">
            <td width="22"><img src="images/icons/check_mark.gif" border="0"></td>
            <td width="628">Choices</td>
        </tr>
        <tr  class="<%=rowColor%>">
            <td nowrap colspan="2">
            <%
            for (int i = 0; i < choices.length; i++)
            {
                %>
                <input class="submitClass" type="submit" name="choice"
                 value="<%  QuickForm.writeHtml(out, choices[i]); %>" <%= choiceButton %>>
                <%
            }
            %>
            </td>
        </tr>
        </table>
        <br>
        <%
    }
    if (includeSet.indexOf("commands")>=0) {
        %>
        <table width="650" class="tableClass" align="center">
        <tr class="headrow">
            <td width="22"><img src="images/icons/settings.gif" border="0"></td>
            <td width="628">Commands</td>
        </tr>
        <tr class="<%=rowColor%>">
            <td nowrap colspan="2">
            <table border="0" cellpadding="2" cellspacing="2">
            <tr>
                <td width="70"><input class="submitClass" type="button" name="workTimerControl" value=" Start  "
                        onclick="toggleWorkTimer()" <%= workTimerButton %>></td>
                <td><input class="TxtField" type="text" name="workTime" value="00:00:00" size="5"></td>
                <td><input class="submitClass" onClick="document.wiForm.displayWrnMsg.value='no';" type="submit" name="cmd" value="Save" <%= saveButton %>></td>
                <td><input class="submitClass" type="submit" name="cmd" value="Accept" <%= acceptButton %>></td>
                <td><input class="submitClass" type="submit" name="cmd" value="Decline" <%= declButton %>></td>
                <td><input class="submitClass" type="submit" name="cmd" value="Reassign" <%= reassignButton %>></td>
            </tr></table>
            </td>
        </tr>
        </table>
        <br>
        <%
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;
    }
%>

<%!
    // Utility method to read some parameters from iFlow.properties:
    public
    String
    getServerOption(
        BPMSession bps,
        String parameterName)
        throws Exception
    {
        String value = null;
        if (parameterName != null && parameterName.trim().length() > 0)
            value = bps.getServerOption(parameterName);

        return value;
    }


    public
    String[]
    getFormNames(
        WorkItem wi)
        throws Exception
    {
        String[] formList = null;
        FormRef[] fref = wi.getNodeInstance().getForms();
        if (fref != null) {
           formList = new String[fref.length];
           for (int i=0; i< fref.length ; i++) {
              formList[i] = fref[i].getName();
           }
        }
        return formList;
    }

    private
    void
    makeButton(Writer out, String url, String name, String icon)
        throws Exception
    {
        out.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n");
        out.write("  <tr background=\"images/buttons/com_button_bg.gif\">\n");
        out.write("  <td width=\"15\"><img src=\"");
        out.write(icon);
        out.write("\" border=\"0\"></td>\n");
        out.write("  <td background=\"images/buttons/com_button_bg.gif\" nowrap><a href=\"");
        out.write(url);
        out.write("\">\n");
        out.write("  <div align=\"center\" class=\"buttonlink\">");
        QuickForm.writeHtml(out, name);
        out.write("</div></a></td>\n");
        out.write("  <td width=\"9\"><div align=\"right\">\n");
        out.write("  <img src=\"images/buttons/com_button_right.gif\" width=\"9\" height=\"24\"></div></td>\n");
        out.write("  </tr>\n</table>\n");
    }

%>

<%!

public void writeWithLinks(Writer out, String value)
    throws Exception
{
    int pos = value.indexOf("http:");
    int startPos = 0;
    while (pos > startPos) {
        QuickForm.writeHtmlWithLineBreaks(out, value.substring(startPos, pos));
        startPos = pos;

        //find the next space or return character
        pos = value.indexOf(" ", startPos);
        int retPos = value.indexOf("\n", startPos);
        if (retPos > startPos && retPos < pos) {
            pos = retPos;
        }
        if (pos < startPos) {
            pos = value.length();
        }
        String addr = value.substring(startPos, pos);
        out.write("<a href=\"");
        QuickForm.writeHtml(out, addr);
        out.write("\">");
        QuickForm.writeHtml(out, addr);
        out.write("</a>");
        if (pos == value.length()) {
            return;   //this is case where link when to end
        }
        startPos = pos;
        pos = value.indexOf("http:", startPos);
    }
    QuickForm.writeHtmlWithLineBreaks(out, value.substring(startPos));
}

%>

