/*
 * NIST
 * MessageGeneration.java Jun 20, 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.testengine.generation;

import org.apache.axis2.AxisFault;

import gov.nist.hl7.ws.messagegeneration.HL7MessageGenerationHL7MessageGenerationSOAP12Port_httpStub;
import gov.nist.hl7.ws.messagegeneration.HL7MessageGenerationHL7MessageGenerationSOAP12Port_httpStub.SetGenerationContext;
import gov.nist.hl7.ws.messagegeneration.HL7MessageGenerationHL7MessageGenerationSOAP12Port_httpStub.UseProfile;

public class MessageGeneration {

    protected HL7MessageGenerationHL7MessageGenerationSOAP12Port_httpStub mgStub;

    public MessageGeneration(String url) throws AxisFault {
        mgStub = new HL7MessageGenerationHL7MessageGenerationSOAP12Port_httpStub(url + "HL7MessageGeneration");

    }

    public String generate(String pid, String msgCriteria) throws Exception {
        String message = "";
        if (!"".equals(msgCriteria)) {

            SetGenerationContext sgc = new SetGenerationContext();
            sgc.setSimpleGenerationContext(msgCriteria);
            if (!mgStub.setGenerationContext(sgc).get_return()) {
                throw new Exception("Error loading the message generation context.");
            }

            // Profile
            UseProfile up = new UseProfile();
            up.setOID(pid);
            if (!mgStub.useProfile(up).get_return()) {
                throw new Exception("Error loading the profile");
            }

            if (!mgStub.generate().get_return()) {
                throw new Exception("Error generating the message.");
            }
            message = mgStub.getMessage().get_return();
        }

        return message;
    }
}
