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

    out.clearBuffer();
    
    Plan plan = null;
    String isArchived = request.getParameter("isArchived");
    if(isArchived != null && isArchived.equals("true"))
    {
        BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
        BPMAdmin bpas = bps.getAdmin();
        plan = bpas.getArchivedPlan(Long.parseLong(planIdStr));
    }
    else {
        plan = WFObjectFactory.getPlan(Long.parseLong(planIdStr), wfSession);
    }

	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];
        SVGRender.nodeDefinition(out, aNode);
    }
%>
<%!
	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>