import java.rmi.*;
import java.rmi.server.*;
import java.math.*;
import java.net.MalformedURLException;

import gov.nih.nlm.kss.api.*;
import gov.nih.nlm.kss.models.meta.concept.*;
import gov.nih.nlm.kss.models.meta.assocExp.*;
import gov.nih.nlm.kss.models.meta.relation.*;
import gov.nih.nlm.kss.models.meta.context.*;
import gov.nih.nlm.kss.models.meta.cooccurrence.*;
import gov.nih.nlm.kss.models.meta.attribute.*;
import gov.nih.nlm.kss.util.DatabaseException;

import java.util.*;
import java.io.*;

public class UMLS 
{
  // --- default values
  static String dbYear = "2007AA";
  static String host   = "umlsks.nlm.nih.gov";
  static String name   = "//" + host + "/KSSRetriever";

  static KSSRetrieverV2_1 retriever = null;


  // ---------------------------------------------------------------------------------
  public UMLS() throws Exception 
  {
    retriever = (KSSRetrieverV2_1) Naming.lookup(name);
  }
  // ---------------------------------------------------------------------------------


  // ---------------------------------------------------------------------------------
  public UMLS(String host, String year) throws Exception 
  {
    dbYear = year;

    String name   = "//" + host + "/KSSRetriever";
    retriever = (KSSRetrieverV2_1) Naming.lookup(name);
  }
  // ---------------------------------------------------------------------------------


  // ---------------------------------------------------------------------------------
  // --- execute the requested query and print out the resulting XML returned
  // ---------------------------------------------------------------------------------
  public static String executeQuery(String xmlQuery)
  {
    try
    {
      char[] result = retriever.query(dbYear, xmlQuery);

      if (result == null) System.out.println("UMLS query: no results returned!");
      else                return new String(result);
    }
    catch (DatabaseException ex) {System.err.println("query: DatabaseException -> " + ex.getMessage());}
    catch (RemoteException ex)   {System.err.println("query: RemoteException -> "   + ex.getMessage());}

    return null;
  }
  // ---------------------------------------------------------------------------------

}

