<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.common.AsapUtils"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="org.w3c.dom.Document"
%><%
    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;
    }

    String servRegUri = request.getParameter("ServRegUriField");
    session.setAttribute("ServiceRegUri", servRegUri);
    String listFactories = request.getParameter("ListFactories");
    String defUri = request.getParameter("DefnUriField");

    if (listFactories != null) {
        if (servRegUri == null || servRegUri.trim().length() == 0) {
            String message = "<br/><table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableClass\" width=\"450\" align=\"center\" bgcolor=\"#CCCCCC\" align=\"center\"><tr><td class=\"headrow\"><b>Warning</b></td></tr><tr><td bgcolor=\"#FFFFFF\"><font class=\"arialfonts\"><br/><blockquote/>You need to provide a Service Registry URI to list the factories registered there." +
                        "</blockquote/></td></tr></table>";
            request.setAttribute("message", message);
            request.setAttribute("nextPage", "");
            request.setAttribute("nextLink", "");
%>
            <jsp:forward page="Error.jsp"/>
<%
        }
        else {
            session.setAttribute("listRequester", "Download.jsp");
            response.sendRedirect("FactoryList.jsp");
        }
    }
    else {
        if (defUri == null || defUri.trim().length() == 0) {
            String message = "<br/><table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableClass\" width=\"450\" align=\"center\" bgcolor=\"#CCCCCC\" align=\"center\"><tr><td class=\"headrow\"><b>Warning</b></td></tr><tr><td bgcolor=\"#FFFFFF\"><font class=\"arialfonts\"><br/><blockquote/>You need to provide a Factory URI to download from." +
                        "</blockquote/></td></tr></table>";
            request.setAttribute("message", message);
            request.setAttribute("nextPage", "");
            request.setAttribute("nextLink", "");
%>
            <jsp:forward page="Error.jsp"/>
<%
        }
        else {
            //Construct and send GetDefinition message.
            Document templateDoc = AsapUtils.sendWfxmlGetDefinition(defUri, bps);

            //Create template.
            Plan plan = bps.newPlan();
            plan.startEdit();
            plan.convertFromXPDLDom(templateDoc);
            plan = plan.createProcessDef();

            session.setAttribute("ServiceRegUri", "");
            response.sendRedirect("TemplateList.jsp");
        }
    }
%>
