/*
 * NIST
 * Hl7GazelleClient.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.validation.messagevalidation;

import gov.nist.hl7.ws.repository.RepositoryRepositorySOAP12PortStub;
import gov.nist.hl7.ws.validation.messagevalidation.Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub;
import gov.nist.hl7.ws.validation.messagevalidation.Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub.GetDetailedResults;
import gov.nist.hl7.ws.validation.messagevalidation.Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub.GetSummaryResults;
import gov.nist.hl7.ws.validation.messagevalidation.Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub.ValidateMessage;

import java.io.File;


import net.ihe.hl7.ws.xmldetailedresults.ReferencedStandardDocument;
import net.ihe.hl7.ws.xmldetailedresults.ReferencedStandardType;
import net.ihe.hl7.ws.xmlmessagedata.MessageMetaDataDocument;
import net.ihe.hl7.ws.xmlmessagedata.MessageMetaDataType;
import net.ihe.hl7.ws.xmlvalidationcontext.NISTHL7V2ValidationContextDefinitionDocument;
import net.ihe.hl7.ws.xmlvalidationcontext.ValidationContextDocument;
import net.ihe.hl7.ws.xmlvalidationcontext.ValidationContextType;

/**
 * @author Roch Bertucat (NIST)
 *
 */
public final class NISTHL7GazelleClient {

    //private static final String URL = "http://localhost:8080/axis2/services/";
    private static final String URL = "http://xreg2.nist.gov:8080/HL7WS/services/";

    private NISTHL7GazelleClient() {

    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(final String[] args) throws Exception {

        // Create the client instance
        Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub mvStub =
            new Hl7GazelleMessageValidationHl7GazelleMessageValidationSOAP12PortStub(URL + "Hl7GazelleMessageValidation");
        RepositoryRepositorySOAP12PortStub rStub =
            new RepositoryRepositorySOAP12PortStub(URL + "Repository");
        // Set the timeout for 60 minutes (for large files)
        org.apache.axis2.client.Options options = mvStub._getServiceClient().getOptions();
        options.setTimeOutInMilliSeconds(600000);
        options = rStub._getServiceClient().getOptions();
        options.setTimeOutInMilliSeconds(600000);

        // ---------------------------------------------
        // 1. Load the profile into the repository
        // ---------------------------------------------
//      LoadProfile lp = new LoadProfile();
//      lp.setParam0(Utils.getContents(new File("files/NIST_DemoProfile_ADTA04.xml")));
//      String OID1 = rStub.loadProfile(lp).get_return();

        // ---------------------------------------------
        // 2. Load the profile into the repository
        // ---------------------------------------------
//      LoadResource lr = new LoadResource();
//      lr.setParam0(Utils.getContents(new File("files/localTables.xml")));
//      String OID2 = rStub.loadResource(lr).get_return();

        // ---------------------------------------------
        // 3. Bind the profile and the resource together
        // ---------------------------------------------
        //Bind bind = new Bind();
        //bind.setParam0(OID1);
        //bind.setParam1(OID2);
        String oid3 = "1.2.81499.3.2";
        //rStub.bind(bind).get_return();

        // ---------------------------------------------
        // 4. Build the Gazelle validation context
        // ---------------------------------------------
        // Load NIST HL7 message context
        NISTHL7V2ValidationContextDefinitionDocument mvc =
            NISTHL7V2ValidationContextDefinitionDocument.Factory.parse(
                    new File("files/MVCCheckComponent.xml"));

        ValidationContextDocument validContextDoc =
            ValidationContextDocument.Factory.newInstance();
        ValidationContextType validContext = validContextDoc.addNewValidationContext();
        validContext.setGenerateSummaryResults("True");
        validContext.setGenerateDetailedResults("True");
        validContext.setTestSpecification(oid3);
        validContext.addNewValidationSpecificContext().
        setNISTHL7V2ValidationContextDefinition(mvc.getNISTHL7V2ValidationContextDefinition());

        String xmlValidationContext = validContextDoc.toString();

        // ---------------------------------------------
        // 5. Build the Gazelle XML Reference Standard file
        // ---------------------------------------------
        ReferencedStandardDocument refStandardDoc =
            ReferencedStandardDocument.Factory.newInstance();
        ReferencedStandardType refStandard = refStandardDoc.addNewReferencedStandard();
        // mandatory
        refStandard.setStandardName("HL7");
        refStandard.setStandardVersion("2.5");
        // not used
        refStandard.setStandardExtension("USA");
        String xmlReferenceStandard = refStandardDoc.toString();

        // ---------------------------------------------
        // 6. Build the Gazelle Message MetaData file
        // ---------------------------------------------
        MessageMetaDataDocument msgMetaDataDoc =
            MessageMetaDataDocument.Factory.newInstance();
        MessageMetaDataType msgMetaData = msgMetaDataDoc.addNewMessageMetaData();
        // English or null
        msgMetaData.setEncodedCharacter("English");
        //msgMetaData.setMessageProfileID("xxx"); // ???
        String xmlMetaData = msgMetaDataDoc.toString();

        // ---------------------------------------------
        // 7. Set the message to be validated
        // ---------------------------------------------
        String message = Utils.getContents(
                new File("files/NIST_DemoMessage_ADTA04.er7"));

        // ---------------------------------------------
        // 8. Check the status of the service
        // ---------------------------------------------
        String xmlValidationServiceStatus =
            mvStub.getValidationServiceStatus().get_return();
        System.out.println(xmlValidationServiceStatus);

        if (!xmlValidationServiceStatus.equals("OK")) {
            throw new Exception("Service Status.");
        }

        // ---------------------------------------------
        // 9. Set the Gazelle objectGUID
        // ---------------------------------------------
        String objectGUID1 = "12334-8942";

        // ---------------------------------------------
        // 10. Call the Gazelle EVS Validate Method
        // ---------------------------------------------
        ValidateMessage vm = new ValidateMessage();
        vm.setParam0(objectGUID1);
        vm.setParam1(xmlReferenceStandard);
        vm.setParam2(xmlValidationContext);
        vm.setParam3(xmlMetaData);
        vm.setParam4(message);
        String xmlResultsOverview = mvStub.validateMessage(vm).get_return();

        // ---------------------------------------------
        // 11. Display xmlResultsOverview
        // ---------------------------------------------
        System.out.println(xmlResultsOverview);

        // ---------------------------------------------
        // 12. Summary Results
        // ---------------------------------------------
        String xmlSummaryResults = mvStub.getSummaryResults(new GetSummaryResults()).get_return();
        System.out.println(xmlSummaryResults);

        // ---------------------------------------------
        // 13. Detailed Results
        // ---------------------------------------------
        String xmlDetailedResults = mvStub.getDetailedResults(new GetDetailedResults()).get_return();
        System.out.println(xmlDetailedResults);
    }
}
