/*
 * NIST
 * RepositoryClient.java March 03, 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.repository.RepositoryRepositorySOAP12Port_httpStub.BindAnotherResource;
import gov.nist.hl7.ws.repository.RepositoryRepositorySOAP12Port_httpStub.GetBindings;
import gov.nist.hl7.ws.repository.RepositoryRepositorySOAP12Port_httpStub.QueryRepositoryForProfilesResponse;

import java.io.File;
import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.List;

import org.apache.axis2.client.Options;

/**
 * @author Roch Bertucat (NIST)
 *
 */
public final class RepositoryClient {

    private static final String URL = "http://localhost:8080/NISTHl7WS/services/";
    //private static final String URL = "http://xreg2.nist.gov:8080/HL7WS/services/";

    private RepositoryClient() {

    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) {
        RepositoryRepositorySOAP12Port_httpStub rStub = null;
        try {
            // Create the client instance
            rStub = new RepositoryRepositorySOAP12Port_httpStub(URL + "Repository");
            // Set the timeout for 60 minutes (for large files)
            Options options = rStub._getServiceClient().getOptions();
            options.setTimeOutInMilliSeconds(600000);

            // ---------------------------------------------
            // Case 1: QueryRepositoryForProfiles, Load 2 resources,
            // Bind, Bind another resource, queryRepositoryForHandles
            // ---------------------------------------------
            // 1. Load the profile
            QueryRepositoryForProfilesResponse profiles = rStub.queryRepositoryForProfiles();
            String oid1 = profiles.get_return()[0];

            // 2. Load 2 resources
            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();
            String oid3 = rStub.loadResource(lr).get_return();

            // 3. Bind
            String oid4 = "";
            boolean useBindAnotherResource = false;
            String [] handles = rStub.queryRepositoryForHandles().get_return();
            GetBindings gb = new GetBindings();
            for (int i = 0; i < handles.length; i++) {
                gb.setHandleOID(handles[i]);
                // the profile
                String profileOID = rStub.getBindings(gb).get_return()[0];
                if (oid1.equals(profileOID)) {
                    useBindAnotherResource = true;
                    oid4 = handles[i];
                }
            }

            BindAnotherResource bar = new BindAnotherResource();
            if (!useBindAnotherResource) {
                RepositoryRepositorySOAP12Port_httpStub.Bind bind = new RepositoryRepositorySOAP12Port_httpStub.Bind();
                bind.setProfileOID(oid1);
                bind.setResourceOID(oid2);
                oid4 = rStub.bind(bind).get_return();
            } else {
                bar.setResourceOID(oid2);
                bar.setHandleOID(oid4);
                if (!rStub.bindAnotherResource(bar).get_return()) {
                    throw new Exception("Error while binding another resource.");
                }
            }

            bar.setResourceOID(oid3);
            bar.setHandleOID(oid4);
            if (!rStub.bindAnotherResource(bar).get_return()) {
                throw new Exception("Error while binding another resource.");
            }

            // 4. queryRepositoryForHandles
            handles = rStub.queryRepositoryForHandles().get_return();
            List<String> handlesList = Arrays.asList(handles);
            if (!handlesList.contains(oid4)) {
                throw new Exception("Error: No bindings created.");
            }

            gb.setHandleOID(oid4);
            System.out.println(Arrays.asList(rStub.getBindings(gb).get_return()));

        } catch (Exception e) {
            if (rStub != null) {
                String exception;
                try {
                    exception = rStub.getLastExceptionMessage().get_return();
                    System.out.println(exception);
                    System.out.println(e);
                } catch (RemoteException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
