package gov.nist.hl7.ws.client.messagevalidation;

import gov.nist.hl7.ws.repository.LoadProfileFaultException;
import gov.nist.hl7.ws.repository.QueryRepositoryForProfilesFaultException;
import gov.nist.hl7.ws.repository.RepositoryRepositorySOAP12Port_httpStub;

import java.io.File;
import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;


/**
 * 
 * @version 1.0
 * @author Abdallah MILADI / INRIA Rennes IHE development Project
 *                      	<pre>http://ihe.irisa.fr</pre>   
 *										<pre>amiladi@irisa.fr</pre>
 */
public class LoadProfiles
{
   private static final String URL = "http://localhost:8080/NISTHl7WS/services/";
   
   public static void main(String[] args) 
   {
      RepositoryRepositorySOAP12Port_httpStub rStub=null;
      try
      {
         rStub = new RepositoryRepositorySOAP12Port_httpStub(URL + "Repository");
      }
      catch (AxisFault e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
      RepositoryRepositorySOAP12Port_httpStub.LoadProfile rlp = new RepositoryRepositorySOAP12Port_httpStub.LoadProfile();
      rlp.setXmlProfile(Utils.getContents(new File("/home/amiladi/workspace/Data/HL7MessageProfiles/OP/LAB-1/OML_O21/profile/OML_O21.xml")));
      String oid1=null;
      try
      {
         oid1 = rStub.loadProfile(rlp).get_return();
      }
      catch (RemoteException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      catch (LoadProfileFaultException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      System.out.println("oid1=="+oid1);
      // 2. Load the resource into the repository
      RepositoryRepositorySOAP12Port_httpStub.LoadResource lr = new RepositoryRepositorySOAP12Port_httpStub.LoadResource();
      lr.setXmlResource(Utils.getContents(new File("tables/PIX_Tables.2.5.xml")));
      String oid2=null;
      try
      {
         oid2 = rStub.loadResource(lr).get_return();
      }
      catch (RemoteException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      catch (QueryRepositoryForProfilesFaultException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      System.out.println("oid2=="+oid2);

      // 3. Bind the profile and the resource together
      RepositoryRepositorySOAP12Port_httpStub.Bind bind = new RepositoryRepositorySOAP12Port_httpStub.Bind();
      bind.setProfileOID(oid1);
      bind.setResourceOID(oid2);
      try
      {
         String oid3 = rStub.bind(bind).get_return();
      }
      catch (RemoteException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      catch (QueryRepositoryForProfilesFaultException e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
}
