<%@page contentType="text/xml;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.common.DOMUtils"
%><%@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.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMBase"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.util.WriterUTF8OutputStream"
%><%@page import="java.awt.Point"
%><%@page import="java.util.*"
%><%@page import="java.io.*"
%><%@page import="org.w3c.dom.Document"
%><%@page import="org.w3c.dom.Element"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1request.getParameterNames(); //Need dummy call for WL9.1
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setDateHeader("Expires", 0);           //prevents caching
    String userName = null;
    String password = null;
    String planIdStr = request.getParameter("planId");
    String planName = "Unknown"+planIdStr;
    boolean generateXPDLForCV = true;

    /*******************************************
     * 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;
    }
    
    // presence of this param will decide whether to generate the XPDL for analytics or Client.
    String requestFromConsole = request.getParameter("fromConsole");
    if (requestFromConsole != null && requestFromConsole.equalsIgnoreCase("true") ) {
        generateXPDLForCV = false;
    }

    // attempt to use an existing session if possible
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == 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;
        }

        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());
        }

        bps = BPMBase.getSession(userName, password, prop);
    }

    out.clearBuffer();
    // catch the exception, so that it will not redirect to Error page.
    try {

        Plan plan = bps.getPlan(Long.parseLong(planIdStr));
        planName = plan.getName();
        Document doc = plan.convertToXPDLDom();
        // remove the "xmlns" attribute if generating for Analytics..
        if (generateXPDLForCV) {
            Element root = doc.getDocumentElement();
            root.removeAttribute("xmlns");
        }
        String xmlString = DOMUtils.convertDomToString(doc);
        out.print(xmlString);
        out.flush();

    } 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;
    }
%>
