<%@page contentType="image/svg+xml;charset=UTF-8"
	pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Error.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.*"
%><%@page import="com.fujitsu.iflow.model.util.ModelException"
%><%@page import="org.w3c.dom.Document"
%><%@page import="java.awt.Point"
%><%@page import="com.fujitsu.iflow.svg.SVGRender"
%><%request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
%><%
	WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
	if (wfSession == null) {
	    response.sendRedirect(goToLogin(request, "Continue to Process Instance Graphic"));
	    return;
	}
		String piId = request.getParameter("piId");

		out.clearBuffer();
		
		long piIdLong;
		try {
		     piIdLong = Long.parseLong(piId);
		}
		catch (Exception e4) {
		    throw new Exception("The piID variable had a problem being parsed as a long value: "+e4.toString());
		}
		
		ProcessInstance pi = null;
		String isArchived = request.getParameter("isArchived");
		if(isArchived != null && isArchived.equals("true")){
		    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
		    BPMAdmin bpas = bps.getAdmin();
		    pi = bpas.getArchivedProcess(piIdLong);
		}
		else {
		    pi = WFObjectFactory.getProcessInstance(piIdLong, wfSession);
		}
		Plan plan = pi.getPlan();
	
		Point p = SVGRender.calculateExtents(plan);
	%><?xml version="1.0" encoding="UTF-8"?>
<svg width="<%=p.x+100%>px" height="<%=p.y+100%>px" overflow="scroll"
	xmlns="http://www.w3.org/2000/svg">
	<%
          if(application.getServerInfo().indexOf("WebSphere") >= 0)
          {
        %>
            <%@ include file="SVGDefs_WS.xml"%>
        <%
          }
          else
          {
        %>
            <%@ include file="SVGDefs.xml"%>
        <%
          }
        %>         

	<%
		Node [] allNodes	= plan.getNodes();
		int		last		= allNodes.length;
		Document att = null;

		try
		{
		    att = plan.getExtendedAttributes();
		}
		catch (ModelException e)
		{
			// in some cases there are badly formed xml errors thrown
			// prevent them being raised
		}
		
		SVGRender.extendedAttributes(out, att);
	
	    for (int i=0; i<last; i++) {
	        Node aNode = allNodes[i];
	        SVGRender.arrowDefinitions(out, aNode);       
	    }
	
	    for (int i=0; i<last; i++) {
	        Node aNode = allNodes[i];
	        NodeInstance ni = pi.getNodeInstance(aNode);
	
	        SVGRender.nodeDefinition(out, aNode, ni.getState());
	    }
%>
<%!
	public
	String
	goToLogin(HttpServletRequest request, String pageName)
		throws Exception
	{
		StringBuffer s1 = request.getRequestURL();
		s1.append("?");
		s1.append(request.getQueryString());
		
		String loginUrl = "Login.jsp?nextPage="
		              +URLEncoder.encode(s1.toString(), "UTF-8")
		              +"&nextLink="
		              +URLEncoder.encode(pageName, "UTF-8");
		
		return loginUrl;
	}
%></svg>