<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.common.UdaNameSanitizer"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.QuickFormOperation"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.Arrow"
%><%@page import="com.fujitsu.iflow.model.workflow.DataItem"
%><%@page import="com.fujitsu.iflow.model.workflow.FormRef"
%><%@page import="com.fujitsu.iflow.model.workflow.Node"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.awt.Point"
%><%@page import="java.io.File"
%><%@page import="java.io.PrintWriter"
%><%@page import="java.io.Writer"
%><%@page import="java.lang.Long"

%><%@page import="java.net.URL"
%><%@page import="java.text.ParseException"
%><%@page import="java.text.DateFormat"
%><%@page import="java.util.Date"
%><%@page import="java.util.Hashtable"
%><%@page import="java.util.Locale"
%><%
    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;
    }
    // change this variable if you save this file to a new name.
    String nameOfThisPage = "EditPD.jsp";

    String pdIdStr = request.getParameter("pdId");
    if (pdIdStr == null) {
        throw new Exception(nameOfThisPage + " needs a pdId parameter to tell it which process definition to work on.");
    }

    long piId = 0;
    try {
        piId = Long.parseLong(pdIdStr);
    }
    catch (Exception e) {
        throw new Exception(nameOfThisPage + " pdId parameter must be an integer value instead of ("+pdIdStr+")");
    }

    Plan plan = bps.getPlan(piId);

    String mode = request.getParameter("mode");
    String aux = request.getParameter("aux");
    String nodeStr = request.getParameter("node");
    String arrowStr = request.getParameter("arrow");

    int auxInt = 0;
    if (aux != null) {
        auxInt = Integer.parseInt(aux);
    }

    String prompt = "UNKNOWN PURPOSE";
    String oldValue = null;

    try {
        if (mode == null) {
            throw new Exception("This page requires a 'mode' parameter");
        }

        try {
            if (mode.equals("name")) {
                prompt = "Edit the Process Definition Name";
                oldValue = plan.getName();
            }
            else if (mode.equals("desc")) {
                prompt = "Edit the Process Definition Description";
                oldValue = plan.getDesc();
            }
            else if (mode.equals("title")) {
                prompt = "Edit the Process Definition Title";
                oldValue = plan.getTitle();
            }
            else if (mode.equals("iden")) {
                prompt = "Edit the Process Definition Identifier";
                oldValue = plan.getPlanIdentifier();
            }
            else if (mode.equals("oscript")) {
                prompt = "Edit the Process Definition Owners Role Script";
                oldValue = plan.getProcOwnerRoleScript();
            }
            else if (mode.equals("init")) {
                prompt = "Edit the Process Definition Init Script";
                oldValue = plan.getInitScript();
            }
            else if (mode.equals("commit")) {
                prompt = "Edit the Process Definition Commit Script";
                oldValue = plan.getCommitScript();
            }
            else if (mode.equals("nname")) {
                prompt = "Edit the Name of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getName();
            }
            else if (mode.equals("ndesc")) {
                prompt = "Edit the Description of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getDesc();
            }
            else if (mode.equals("role")) {
                prompt = "Edit the Role of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getRole();
            }
            else if (mode.equals("npos")) {
                prompt = "Edit the Position of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName()+" (x & y coordinates from upper left corner of canvas)";
                oldValue = getNodePosition(getNode(plan,nodeStr));
            }
            else if (mode.equals("prol")) {
                prompt = "Edit the Prologue Script of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getPrologueScript();
            }
            else if (mode.equals("rscript")) {
                prompt = "Edit the Role Script of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getRoleScript();
            }
            else if (mode.equals("epil")) {
                prompt = "Edit the Epilogue Script of the Node "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNode(plan,nodeStr).getEpilogueScript();
            }
            else if (mode.equals("aname")) {
                prompt = "Edit the Name of the Arrow "+arrowStr+":"+getArrow(plan,arrowStr).getName();
                oldValue = getArrow(plan,arrowStr).getName();
            }
            else if (mode.equals("points")) {
                prompt = "Edit the Position of the Arrow "+arrowStr+":"+getArrow(plan,arrowStr).getName();
                oldValue = getArrowPosition(getArrow(plan,arrowStr));
            }
            else if (mode.equals("fname")) {
                prompt = "Edit the Path of the form "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNodeForm(getNode(plan,nodeStr), aux).getName();
            }
            else if (mode.equals("fpath")) {
                prompt = "Edit the Path of the form "+nodeStr+":"+getNode(plan,nodeStr).getName();
                oldValue = getNodeForm(getNode(plan,nodeStr), aux).getPath();
            }
            else {
                throw new Exception("The edit command '"+mode+"' is not supported at this time.");
            }
        }
        catch (Exception e) {
            throw new Exception("Unable to handle mode '"+mode+"' request. {"+e.toString()+"}");
        }

%>

<html>
<head>
<title>Interstage Business Process Manager | Editing Process Definition</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
</HEAD>
<body topmargin="0" leftmargin="0">
<table><tr><td><img src="images/clear.gif" width="10" height="17"/></td></tr></table>
<form name="EditValueForm" method="post" action="EditPDAction.jsp" target="_parent">
<table width="95%" class="tableClass" align="center" cellpadding="2" cellspacing="5">
<tr class="headrow">
<td><b><%= prompt %></b></td>
</tr>
<tr class="oddrow">
<td><br>
<textarea name="newValue" rows="16" cols="80"><% QuickForm.writeHtml(out, oldValue); %></textarea>
<input type="hidden" name="pdId" value="<% QuickForm.writeHtml(out, pdIdStr); %>">
<input type="hidden" name="mode" value="<% QuickForm.writeHtml(out, mode); %>">
<% if (nodeStr!=null) { %><input type="hidden" name="node" value="<% QuickForm.writeHtml(out, nodeStr); %>"> <% } %>
<% if (arrowStr!=null) { %><input type="hidden" name="arrow" value="<% QuickForm.writeHtml(out, arrowStr); %>"> <% } %>
<% if (aux!=null) { %><input type="hidden" name="aux" value="<% QuickForm.writeHtml(out, aux); %>"> <% } %>
<input type="hidden" name="oldvalue" value="<% QuickForm.writeHtml(out, oldValue); %>"> <br>
<input type="submit" value="Save Changes" class="core-button"><br><br>
<b>Existing Value:</b> <% QuickForm.writeHtml(out, oldValue); %>
</form>
</td></tr>
<tr class="evenrow"><td>
<b>Process Definition Information:</b><br>
</td></tr>
<tr class="oddrow"><td>
<a href="ProcessDefinition.jsp?planID=<%= plan.getId() %>" target="_parent"><%= plan.getId() %></a>:
<%= plan.getName() %> <br>
</body>
</html>

<%
    // ============= put your custom form page code above this line =================
    }
    catch (Exception e) {
        throw new Exception ("Unable to render form '"+nameOfThisPage+
               "' for work item (id="+pdIdStr+") because: "+e.getMessage());
    }

%>

<%!

public Node getNode(Plan plan,String nodeStr) throws Exception
{
    if (nodeStr == null) {
        throw new Exception("Needs a 'node' parameter.");
    }
    long nodeId = safeParseInt(nodeStr);
    Node node = plan.getNode(nodeId);
    if (node == null) {
        throw new Exception("Can't find a node with id = '"+nodeStr+"'.");
    }
    return node;
}

public Arrow getArrow(Plan plan,String arrowStr) throws Exception
{
    if (arrowStr == null) {
        throw new Exception("Needs a 'arrow' parameter.");
    }
    long arrowId = safeParseInt(arrowStr);
    Arrow arrow = plan.getArrow(arrowId);
    if (arrow == null) {
        throw new Exception("Can't find an arrow with id = '"+arrowStr+"'.");
    }
    return arrow;
}


public FormRef getNodeForm(Node node, String aux) throws Exception
{
    if (aux == null)  {
        throw new Exception("needs an 'aux' parameter.");
    }
    int auxInt = safeParseInt(aux);
    FormRef[] forms = node.getForms();
    if (forms == null) {
        throw new Exception("Got a null back from getForms.  Process Instance has probably been changed recently.");
    }
    if (auxInt < 0 || auxInt >= forms.length) {
        throw new Exception("'aux' parameter value must be the index to the form, between zero and "+forms.length);
    }
    return forms[auxInt];
}


public int safeParseInt(String val) throws Exception
{
    try {
        int intVal = Integer.parseInt(val);
        return intVal;
    }
    catch (Exception e) {
        throw new Exception("Not able to parse an integer value from '"+val+"'");
    }
}

public
String
getNodePosition(Node node)
{
    Point aPoint = node.getPosition();
    return "(" + aPoint.x+ ", " + aPoint.y + ")";
}

public
String
getArrowPosition(Arrow arrow)
    throws Exception
{
    StringBuffer val = new StringBuffer();
    Point aPoint = arrow.getStartPoint();
    val.append('(');
    val.append(Integer.toString(aPoint.x));
    val.append(", ");
    val.append(Integer.toString(aPoint.y));
    val.append("); ");
    Point[] midPoints = arrow.getPoints();
    if (midPoints != null) {
        for (int i=0; i<midPoints.length; i++) {
            aPoint = midPoints[i];
            if (aPoint == null) {
                throw new Exception("Point number '"+i+"' was unexpectedly null.");
            }
            val.append('(');
            val.append(Integer.toString(aPoint.x));
            val.append(", ");
            val.append(Integer.toString(aPoint.y));
            val.append("); ");
        }
    }
    aPoint = arrow.getEndPoint();
    val.append('(');
    val.append(Integer.toString(aPoint.x));
    val.append(", ");
    val.append(Integer.toString(aPoint.y));
    val.append(")");
    return val.toString();
}

%>

