// $Id: geonames.java,v 1.4 2002/04/26 05:20:27 mdean Exp $


/**
 * generate DAML on demand from the geonames information stored in an RDBMS
 */
class geonames
{
    static String baseURI = "http://www.daml.org/cgi-bin/geonames?";

    static void usage()
    {
	System.err.println("Usage:  f<id>|n<id>|c<code>");
	System.exit(1);
    }

    public static void main(String args[])
	throws Exception
    {
	if ((args.length != 1))
	    usage();
	String arg = args[0];
	if (arg.length() < 1)
	    usage();

	// connect to database
	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/geonames");
	java.sql.Statement statement = connection.createStatement();
	java.sql.ResultSet results;

	com.hp.hpl.mesa.rdf.jena.model.Model model = new com.hp.hpl.mesa.rdf.jena.mem.ModelMem();

	switch (arg.charAt(0))
	    {
	    case 'f':
		// return information about this feature
		{
		    String id = arg.substring(1);
		    results = statement.executeQuery("select latitude, longitude, dimension, primary_country, designation from feature where id = " + id);
		    if (! results.next())
			{
			    System.err.println("unknown feature " + id);
			    System.exit(1);
			}
		    com.hp.hpl.mesa.rdf.jena.model.Resource feature = model.createResource("");
		    model.add(feature,
			      com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			      model.createResource(gendaml.featureTypesBase + results.getString("designation")));
		    model.add(feature,
			      geonames_ont.uniqueIdentifier,
			      id);
		    model.add(feature,
			      geonames_ont.latitude,
			      results.getString("latitude"));
		    model.add(feature,
			      geonames_ont.longitude,
			      results.getString("longitude"));
		    String dimension = results.getString("dimension");
		    if (dimension.length() > 0) 
			model.add(feature,
				  geonames_ont.dimension,
				  results.getString("dimension"));
		    model.add(feature,
			      geonames_ont.primaryCountry,
			      model.createResource(gendaml.fipsBase + results.getString("primary_country")));
		    /*
		    model.add(feature,
			      geonames_ont.modifyDate,
			      results.getString("modified")); */

		    // get names
		    results = statement.executeQuery("select distinct name from map where feature = " + id);
		    while (results.next())
			{
			    model.add(feature,
				      geonames_ont.name,
				      model.createResource(baseURI + "n" + results.getString("name")));
			}
		}
		break;
	    case 'n':
		// return information about this name
		{
		    String id = arg.substring(1);
		    com.hp.hpl.mesa.rdf.jena.model.Resource name = model.createResource("");
		    model.add(name,
			      com.hp.hpl.mesa.rdf.jena.vocabulary.RDF.type,
			      geonames_ont.Name);
		    results = statement.executeQuery("select distinct full_name, short_name from name where id = " + id);
		    if (! results.next())
			{
			    System.err.println("unknown name " + id);
			    System.exit(1);
			}
		    model.add(name,
			      geonames_ont.fullName,
			      results.getString("full_name"));
		    model.add(name,
			      geonames_ont.shortName,
			      results.getString("short_name"));
		}
		break;
	    case 'c':
		// return list of features for specified country
		{
		    String countryCode = arg.substring(1).toUpperCase();
		    results = statement.executeQuery("select distinct feature from map where country = '" + countryCode + "'");
		    com.hp.hpl.mesa.rdf.jena.model.Resource country = model.createResource("http://www.daml.org/2001/09/countries/fips#" + countryCode);
		    while (results.next())
			{
			    model.add(country,
				      geonames_ont.feature,
				      model.createResource(baseURI + "f" + results.getString("feature")));
			}
		}
		break;
	    default:
		usage();
	    }

	// serialize model
	java.io.PrintWriter writer = new java.io.PrintWriter(System.out);
	writer.println("<?xml version='1.0' encoding='ISO-8859-1'?>");
	model.write(writer);
	writer.close();
    }
}
