<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.common.AsapUtils"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.util.ModelException"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.util.Vector"
%><%@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 planIdToUpload = (String)session.getAttribute("PlanIdToUpload");

    String url = request.getParameter("Url");
    if (url == null || url.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 did not specify any URL. Please provide a URL to upload to." +
                    "</blockquote/></td></tr></table>";
        request.setAttribute("message", message);
        request.setAttribute("nextPage", "");
        request.setAttribute("nextLink", "");
%>
            <jsp:forward page="Error.jsp"/>
<%
    }
    else {
        // Check which radio button is active. Then check that the respective field is filled.
        String sendType = request.getParameter("sendType");
        Plan plan = bps.getPlan(Long.parseLong(planIdToUpload));
        Document templateDoc = plan.convertToXPDLDom();
        if (sendType.equals("New")) {
            // Send NewDefinition request.
            AsapUtils.sendWfxmlNewDefinition(templateDoc, url, bps);
        }
        else if (sendType.equals("Update")) {
            // Send SetDefinition request.
            AsapUtils.sendWfxmlSetDefinition(templateDoc, url, bps);
        }
        response.sendRedirect("TemplateList.jsp");
    }
%>
