/*
 * NIST
 * Hl7Client.java Feb 19, 2008
 *
 * This code was produced by the National Institute of Standards and
 * Technology (NIST). See the "nist.disclaimer" file given in the distribution
 * for information on the use and redistribution of this software.
 */

package gov.nist.hl7.ws.client.messagevalidation;

import gov.nist.hl7.ws.repository.RepositoryRepositorySOAP12Port_httpStub;
import gov.nist.hl7.ws.messagevalidation.Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub;
import gov.nist.hl7.ws.messagevalidation.Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub.LoadMessage;
import gov.nist.hl7.ws.messagevalidation.Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub.LoadProfile;
import gov.nist.hl7.ws.messagevalidation.Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub.SetValidationContext;
import gov.nist.hl7.ws.messagevalidation.Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub.UseHandle;

import java.io.File;

import org.apache.axis2.client.Options;

/**
 * @author Roch Bertucat (NIST)
 *
 */
public final class Hl7Client {

    private static final String URL = "http://localhost:8080/NISTHl7WS/services/";
    //private static final String URL = "http://xreg2.nist.gov:8080/HL7WS/services/";

    private Hl7Client() {

    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(final String[] args) throws Exception {

        // Create the client instance
        Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub mvStub =
            new Hl7MessageValidationHl7MessageValidationSOAP12Port_httpStub(URL + "Hl7MessageValidation");
        RepositoryRepositorySOAP12Port_httpStub rStub =
            new RepositoryRepositorySOAP12Port_httpStub(URL + "Repository");
        // Set the timeout for 60 minutes (for large files)
        Options options = mvStub._getServiceClient().getOptions();
        options.setTimeOutInMilliSeconds(600000);
        options = rStub._getServiceClient().getOptions();
        options.setTimeOutInMilliSeconds(600000);

        // ---------------------------------------------
        // Case 1: Load the profile, Load the message,
        // Load the MVC, Call the Validate Method
        // ---------------------------------------------
        // 1. Load the profile
        LoadProfile lp = new LoadProfile();
        lp.setXmlProfile(Utils.getContents(new File("files/ValidProfileForMessageValidation.xml")));
        if (!mvStub.loadProfile(lp).get_return()) {
            throw new Exception("Error Loading the Profile.");
        }
        
        // 2. Load the message
        LoadMessage lm = new LoadMessage();
        lm.setMessageStr(Utils.getContents(new File("files/IncorrectXUsage.er7")));
        if (!mvStub.loadMessage(lm).get_return()) {
            throw new Exception("Error Loading the Message.");
        }

        // 3. Load the MVC
        SetValidationContext svc = new SetValidationContext();
        svc.setXmlValidationContext(Utils.getContents(new File("files/MVCCheckComponent.xml")));
        if (!mvStub.setValidationContext(svc).get_return()) {
            throw new Exception("Error Loading the MVC.");
        }

        // 4. Call the Validate Method
        if (mvStub.validate().get_return()) {
            System.out.println("Message Valid.");
        } else {
            System.out.println("Message Not Valid.");
        }

        System.out.println(mvStub.getValidationReport().get_return());

        // ---------------------------------------------
        // Case 2: Load a profile in the repository,
        // Load a resource, bind, Load a message,
        // useHandle, Call the Validate Method
        // ---------------------------------------------

        // 1. Load the profile into the repository
        RepositoryRepositorySOAP12Port_httpStub.LoadProfile rlp = new RepositoryRepositorySOAP12Port_httpStub.LoadProfile();
        rlp.setXmlProfile(Utils.getContents(new File("files/ValidProfileForMessageValidation.xml")));
        String oid1 = rStub.loadProfile(rlp).get_return();

        // 2. Load the resource into the repository
        RepositoryRepositorySOAP12Port_httpStub.LoadResource lr = new RepositoryRepositorySOAP12Port_httpStub.LoadResource();
        lr.setXmlResource(Utils.getContents(new File("files/Tables.2.5.xml")));
        String oid2 = rStub.loadResource(lr).get_return();

        // 3. Bind the profile and the resource together
        RepositoryRepositorySOAP12Port_httpStub.Bind bind = new RepositoryRepositorySOAP12Port_httpStub.Bind();
        bind.setProfileOID(oid1);
        bind.setResourceOID(oid2);
        String oid3 = rStub.bind(bind).get_return();

        // 4. Load the message
        if (!mvStub.loadMessage(lm).get_return()) {
            throw new Exception("Error Loading the Message.");
        }

        // 5. Use the handle
        UseHandle uh = new UseHandle();
        uh.setOid(oid3);
        if (!mvStub.useHandle(uh).get_return()) {
            throw new Exception("Error Using the Handle.");
        }

        // 6. Call the Validate Method
        if (mvStub.validate().get_return()) {
            System.out.println("Message Valid.");
        } else {
            System.out.println("Message Not Valid.");
        }

        System.out.println(mvStub.getValidationReport().get_return());
    }
}
