<%@page contentType="text/xml;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFAdminSessionImpl"
%><%@page import="com.fujitsu.iflow.common.UdaNameSanitizer"
%><%@page import="com.fujitsu.iflow.model.util.AEServers"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.*"
%><%@page import="com.fujitsu.iflow.model.wfcore.*"
%><%@page import="com.fujitsu.iflow.model.util.WriterUTF8OutputStream"
%><%@page import="java.awt.Point"
%><%@page import="java.util.*"
%><%@page import="java.io.*"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMAdminImpl"
%><%
    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;
    }
    
    BPMAdmin bpas = bps.getAdmin();
    WFAdminSessionImpl wfSession = ((BPMAdminImpl)bpas)._wfas;

    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setDateHeader("Expires", 0);           //prevents caching

    String serverName = null;
    String userName = null;
    String password = null;
    String planIdStr = request.getParameter("planId");
    String planName = "Unknown"+planIdStr;

    /*******************************************
     * This page is used to test to see if the servlet engine is up
     * by passing a parameter testing=true and you get a simple
     * response back.  This tells only that the JSP servlet
     * engine is running, and not that the iFlow server is running.
     * This should be changed to produce valid XML...
     */
    String testing = request.getParameter("testing");
    if (testing != null && testing.equalsIgnoreCase("true")) {
        %>ok<%
        return;
    }

    // attempt to use an existing session if possible
   /* 
    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {

        userName = request.getParameter("userId");
        password = request.getParameter("pwd");

        // if not from analytics.
        if (userName == null || password == null) {
            %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
            return;
        }

        serverName = request.getParameter("serverName");
        String fileName = application.getRealPath("/").trim();

        if (fileName.endsWith(File.separator))
            fileName = fileName + "Taskmanager.conf";
        else
            fileName = fileName + File.separator + "Taskmanager.conf";

        Properties prop = new Properties();
        BufferedReader in = new BufferedReader(new FileReader(fileName));
        String line = null;
        for(line = in.readLine(); line != null; line = in.readLine())
        {
            int pos = line.indexOf("=");
            if (pos == -1)
                continue;
            String name = line.substring(0,pos);
            String value = line.substring(pos+1);
            prop.setProperty(name.trim(),value.trim());
        }

        wfSession = WFObjectFactory.getWFSession();
        wfSession.initForApplication(null, prop);
        wfSession.logIn(serverName, userName, password);
    }
    else {
        String[] slist = wfSession.getServerList();
        if (slist != null && slist.length >= 1) {
            serverName = slist[0];
        }
        else {
            serverName = "{unknown}";
        }
    }
*/
    out.clearBuffer();
    // catch the exception, so that it will not redirect to Error page.
    try {
        Plan plan = WFObjectFactory.getPlan(Long.parseLong(planIdStr), wfSession);
        planName = plan.getName();

        // create an OutputStream object that does the right conversions
        // to write to the Writer -- still converts twice, but there is
        // no unnecessary buffering.
        WriterUTF8OutputStream wuos = new WriterUTF8OutputStream(out);
        plan.convertToXPDL(wuos);
    } catch (Exception e) {
        e.printStackTrace();
%><?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://www.wfmc.org/2002/XPDL1.0"
         xmlns:xpdl="http://www.wfmc.org/2002/XPDL1.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="xpdl.xsd"
         Id="<%=planIdStr%>"
         Name="<% QuickForm.writeHtml(out, planName); %>">
    <PackageHeader>
        <XPDLVersion>1.0</XPDLVersion>
        <Vendor>Fujitsu Software Corporation, iFlow, Version 6.0.</Vendor>
        <Created><%= new Date().toString()%>  </Created>
    </PackageHeader>
    <RedefinableHeader PublicationStatus="UNDER_TEST"/>
    <WorkflowProcesses>
        <WorkflowProcess Id="<%=planIdStr%>" AccessLevel="PRIVATE">
        <RedefinableHeader PublicationStatus="UNDER_TEST"/>
        </WorkflowProcess>
    </WorkflowProcesses>
</Package>
<%
        return;
    }
%>
