<%@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.QuickForm"
%><%@page import="java.io.InputStream"
%><%@page import="java.io.OutputStream"
%><%@page import="java.net.HttpURLConnection"
%><%@page import="java.net.URL"
%><%@page import="javax.xml.parsers.DocumentBuilder"
%><%@page import="javax.xml.parsers.DocumentBuilderFactory"
%><%@page import="javax.xml.transform.dom.DOMSource"
%><%@page import="javax.xml.transform.stream.StreamResult"
%><%@page import="javax.xml.transform.TransformerFactory"
%><%@page import="javax.xml.transform.Transformer"
%><%@page import="org.w3c.dom.Document"
%><%@page import="org.w3c.dom.Element"
%><%@page import="sun.misc.BASE64Encoder"
%><%
    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 = (String)session.getAttribute("ServiceRegUri");
    
    String select = "";
    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    session.setAttribute("TabIndex","2");

    String listRequester = (String)session.getAttribute("listRequester");
%>


<HTML>
<HEAD>
<title>Interstage Business Process Manager | Process Definition List</TITLE>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>

<script>
function setSelectedPlanUri(key, pForm)
{
    pForm.selectedPlanUri.value=key
    pForm.submit()
}

</script>
</HEAD>
<body class="bodyClass" onLoad="goforit();">
    <center>
    <jsp:include page="Header.jsp" flush="true"/>
    <br>
    <form name="planForm" method="post" action="<%= response.encodeURL(listRequester) %>">
        <input type="hidden" name="selectedPlanUri" value="-1">
 
        <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">
            <tr align="center" class="headrow">
                <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">List of Factories in remote Service Registry</font></td>
            </tr>
        </TABLE>
        
        <TABLE border="0" cellpadding=4 cellspacing=1 class="tableClass" width="90%" bgcolor="#CCCCCC"  align="center">
        <COL WIDTH="10%"><COL WIDTH="20%"><COL width="50%"><COL WIDTH="10%"><COL WIDTH="10%">
        <tr class="headrow">
        <th></th>
        <th>Factory Name</th>
        <th>Description</th>
        <th>Version</th>
        <th>Status</th>
        </tr>

<%
        String soapUri = AsapUtils.soapUri;
        String soapPrefix = AsapUtils.soapPrefix;
        String soapNSattr = AsapUtils.soapNSattr;
        String asapUri = AsapUtils.asapUri;
        String asapPrefix = AsapUtils.asapPrefix;
        String asapNSattr = AsapUtils.asapNSattr;
        String schemaDefUri = AsapUtils.schemaDefUri;
        String schemaDefPrefix = AsapUtils.schemaDefPrefix;
        String schemaDefNSattr = AsapUtils.schemaDefNSattr;
        String iflowNSPrefix = AsapUtils.iflowNSPrefix;
        String iflowNSattr = AsapUtils.iflowNSattr;
        String wfxmlUri = AsapUtils.wfxmlUri;
        String wfxmlPrefix = AsapUtils.wfxmlPrefix;
        String wfxmlNSattr = AsapUtils.wfxmlNSattr;

        URL url = new URL(servRegUri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setRequestProperty("Accept", "text/xml");
        // Authorization header of the form:
        // Authorization: Basic <base64 encoding of username:password>
        BASE64Encoder encoder = new BASE64Encoder();
        String userPass = bps.getServerOption("SwapLinkageUserName") + ":" +
        		          bps.getServerOption("SwapLinkagePassword");
        String base64UserPass = encoder.encode(userPass.getBytes("UTF-8"));
        conn.setRequestProperty("Authorization", "Basic " + base64UserPass);
        conn.setRequestProperty("SOAPAction", "");
        conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Create & write request xml
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        Document doc = builder.newDocument();

        Element envelopeTag = doc.createElementNS(soapUri, soapPrefix + "Envelope");
        envelopeTag.setAttribute(asapNSattr, asapUri);
        envelopeTag.setAttribute(soapNSattr, soapUri);
        envelopeTag.setAttribute(wfxmlNSattr, wfxmlUri);
        doc.appendChild(envelopeTag);

        Element headerTag = doc.createElement(soapPrefix + "Header");
        envelopeTag.appendChild(headerTag);

        Element requestTag = doc.createElementNS(asapUri, asapPrefix + "Request");
        headerTag.appendChild(requestTag);

        Element senderKeyTag = doc.createElement(asapPrefix + "SenderKey");
        //senderKeyTag.appendChild(doc.createTextNode(_session.getServerOption("TWFRegistryUrl")));
        requestTag.appendChild(senderKeyTag);

        Element receiverKeyTag = doc.createElement(asapPrefix + "ReceiverKey");
        receiverKeyTag.appendChild(doc.createTextNode(servRegUri));
        requestTag.appendChild(receiverKeyTag);

        Element responseRequiredTag = doc.createElement(asapPrefix + "ResponseRequired");
        responseRequiredTag.appendChild(doc.createTextNode("Yes"));
        requestTag.appendChild(responseRequiredTag);

        Element bodyTag = doc.createElement(soapPrefix + "Body");
        envelopeTag.appendChild(bodyTag);

        Element newDefinitionRqTag = doc.createElement(wfxmlPrefix + "ListDefinitionsRq");
        bodyTag.appendChild(newDefinitionRqTag);

        DOMSource docSource = new DOMSource(doc);

        OutputStream outputStrm = conn.getOutputStream();
        StreamResult outResult = new StreamResult(outputStrm);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(docSource, outResult);

        // Read the response
        DocumentBuilderFactory docFactory2 = DocumentBuilderFactory.newInstance();
        docFactory2.setNamespaceAware(true);
        DocumentBuilder builder2 = docFactory2.newDocumentBuilder();
        InputStream is = conn.getInputStream();
        Document responseDoc = builder2.parse(is);


        Element responseRoot = responseDoc.getDocumentElement();
        Element bodyElement = AsapUtils.getNamedChildElement(responseRoot, "Body");
        Element methodElement = AsapUtils.getNamedChildElement(bodyElement, "ListDefinitionsRs");

        Element[] definitionInfos = AsapUtils.getAllChildElements(methodElement);
        for (int j = 0; j < definitionInfos.length; j++) {
            Element definitionInfoElement = definitionInfos[j];
            String s = definitionInfoElement.getLocalName();
            
            if (definitionInfoElement.getLocalName().equals("DefinitionInfo")) {
                String defnKey    = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "DefinitionKey"));   
                String nameTag    = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "Name"));   
                String descTag    = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "Description"));   
                String versionTag = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "Version"));   
                String statusTag  = AsapUtils.getTagValue(AsapUtils.getNamedChildElement(definitionInfoElement, "Status"));   
                        
                // Render the definition.
                rowColor = bcolor[nextColor];
                nextColor = (nextColor + 1)%2;
                
            
%>            
                <tr class="<%=rowColor%>">
                <td>
                    <input type="button" onClick="setSelectedPlanUri('<%= response.encodeURL(defnKey) %>', this.form)"
                        value="Select" name=" <%= nameTag %> " class="core-button">
                </td>
                <td>
                        <% QuickForm.writeHtml(out, nameTag); %>
                </td>
                <td>
                        <%= descTag %>
                </td>
                <td>
                        <%= versionTag %>
                </td>
                <td>
                        <%= statusTag %>
                </td>    
                </tr> 
<%                
            }
        }

%>
</table>
</form>
    </center>
</BODY>
</HTML>

