import OdbToolsApplic.*;           // The package containing our stubs.
import org.omg.CosNaming.*;  // HelloClient will use the naming service.
import org.omg.CORBA.*;      // All CORBA applications need these classes.
import java.io.*;
import java.util.StringTokenizer;
import java.util.Map;

import java.net.InetAddress;

// Momis tools package
import  tools.*;

public class  ClientSimple {


   /**  
    *   main routine.
    *   <br>
    * 
    *   Simplest ODB-Tools client.
    *   This client is designed to read the momis configuration file
    *   for retrieving the naming server host and port.
    *
    */
   public static void main(String args[])   {
      ORB orb;
      org.omg.CORBA.Object objRef;    // Get the root naming context
      NamingContext ncRef;
      NameComponent nc;
      String serverName = "odbt";
      String orbPort;
      String orbServerName;
      String connArray[];             // array used to initialize the ORB
      //
      OdbToolsFactory factoryObj;     // reference all'oggetto factory
      OdbTools myRef;                 // reference all'oggetto servant
      // -----
      if (args.length < 1) {
	 System.out.println("Use: ");
	 System.out.println("  java ClientSimple " +
			    "configurationFile");
	 System.out.println(" example: ");
	 System.out.println(
	    "  java ClientSimple $momis_HOME/momis.conf");
	 System.exit(1);
      }
      //
      // Read the configuration file
      //
      if ( !Tools.isReadable(args[0]) ) {
	 System.out.println(
	    " I cannot read the configuration file ["+args[0]+"]");
	 System.exit(1);
      }
      Map configuration = Tools.readConfFile(args[0]);
      orbServerName = ((String)configuration.get("orbServer")).trim();
      orbPort       = ((String)configuration.get("orbPort")).trim();
      serverName    = ((String)configuration.get("odbt_namingName")).trim();
      //
      // -- crating the array for ORB initialization 
      connArray = new String[4];
      connArray[0] = "-ORBInitialHost";
      connArray[1] = orbServerName;
      connArray[2] = "-ORBInitialPort";
      connArray[3] = orbPort;
      try{
	 //
	 // Create and initialize the ORB
	 orb = ORB.init(connArray, null);
	 //
	 // Get the root naming context
	 System.out.println(" - resolving naming...");
	 objRef = orb.resolve_initial_references("NameService");
	 System.out.println(" - narrowing naming object...");
	 ncRef = NamingContextHelper.narrow(objRef);
	 //
	 // Resolve the object reference in naming
	 System.out.println(" - search for ODB-Tools CORBA object...");
	 nc = new NameComponent(serverName, "");
	 System.out.println(" - creating path...");
	 NameComponent path[] = {nc};
	 //
	 System.out.println(" - narrowing ODB-Tools object...");
	 factoryObj = OdbToolsFactoryHelper.narrow(ncRef.resolve(path));
	 System.out.println(" - CORBA connection ok.");
	 //
	 // Get a new servant object
	 myRef = factoryObj.newServant("DemoClient");
	 //
	 // Input schema
	 // 
	 String sin = "";
	 sin = sin + "interface Material ()\n";
	 sin = sin + "	{\n";
	 sin = sin + "	attribute string name;\n";
	 sin = sin + "	attribute int risk;\n";
	 sin = sin + "	attribute set<string> feature;\n";
	 sin = sin + "	};\n";
	 System.out.println("Schema:\n" + sin);
	 System.out.println("\n");
	 //
	 // Input query
	 // 
	 String qin = "select * from Material";
	 System.out.println("Query:\n" + sin);
	 System.out.println("\n");
	 //
	 // Query Optimizer   OQL  --> OQL
	 // 
	 String fileOdl = "";
	 String fileOql = "";
	 String fileOutputName = "";
	 StringHolder stdOut = new StringHolder();
	 String s ;
	 // 
	 System.out.println("Optimizing query...\n");
	 s = myRef.optimize_Oql(sin, qin, stdOut);
	 s = stdOut.value + 
	    "\n\n -------- optimized OQL ------- \n\n " + s;
	 System.out.println("Output:\n" + s + "\n");
	 //
	 // Kill Servant Object
	 // 
	 System.out.println("Sending Kill request...\n");
	 myRef.killObject();
      } catch(Exception e) {
	 System.out.println("ERROR : " + e);
	 e.printStackTrace(System.out);
      }  
   }
}