package com.fitbank.uci.webservice.client;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.axis.Message;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;
import org.w3c.dom.NodeList;

public class ArmyWS {

    private String urlService = null;
    private String nameSpace = null;
    private String soapAction = null;

    public ArmyWS() {}

    public int actualizaIngreso(String pPartida, String pFondo, String pAnio, String pReparto, Date pFechaLeg, String pConcepto, BigDecimal pValor, String pType, String pIng, String pUsuario, String pMaquina)
        throws Exception {
        Call call = new Call(this.urlService);

        call.setSOAPActionURI(this.nameSpace + this.soapAction);
        call.setUseSOAPAction(true);

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        String soapString = "<?xml version='1.0' encoding='utf-8'?>" +
                        "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><SOAP-ENV:Body>" +
                        "  <ActualizaIngreso xmlns='http://tempuri.org/'>" + "    <partida>" + pPartida + "</partida>" +
                        "    <fondo>" + pFondo + "</fondo>" + "    <anio>" + pAnio + "</anio>" + "    <reparto>" +
                        pReparto + "</reparto>" + "    <fecha_leg>" + sdf.format(pFechaLeg) + "</fecha_leg>" +
                        "    <concepto>" + pConcepto + "</concepto>" + "    <valor>" + pValor + "</valor>" +
                        "    <tipo>" + pType + "</tipo>" + "    <ind>" + pIng + "</ind>" + "    <usuario>" + pUsuario +
                        "</usuario>" + "    <maquina>" + pMaquina + "</maquina>" + "  </ActualizaIngreso>" +
                        "</soap:Body>" + "</soap:Envelope>";
        InputStream soapStream = new ByteArrayInputStream(soapString.getBytes());
        SOAPEnvelope soapEnvelope = new SOAPEnvelope(soapStream);

        call.invoke(soapEnvelope);

        Message msg = call.getResponseMessage();
        NodeList nodeList = msg.getSOAPPart().getElementsByTagName("ActualizaIngresoResult");

        return Integer.parseInt(nodeList.item(0).getChildNodes().item(0).toString());
    }

    public void setUrlService(String urlService) {
        this.urlService = urlService;
    }

    public void setNameSpace(String nameSpace) {
        this.nameSpace = nameSpace;
    }

    public void setSoapAction(String soapAction) {
        this.soapAction = soapAction;
    }

    public static void main(String[] args) {
        try {
            ArmyWS soapClient = new ArmyWS();

            soapClient.setUrlService("http://201.218.51.167/sifweb/WebServiceIng.asmx");
            soapClient.setNameSpace("http://tempuri.org/");
            soapClient.setSoapAction("http://tempuri.org/ActualizaIngreso");

            int result = soapClient.actualizaIngreso("20108068281", "10309673", "1903", "AA", new Date(), "aa",
                            new BigDecimal(100), "a", "aaaa", "PRUEBAS INOCAR", "PRUEBAS INOCAR");

            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
