/* **********************************************************************
 * :vim ts=4 sts=4 sw=4 et ai:
 * Copyright 2000 Intevation GmbH
 * This is free software under the GNU LGPL
 *
 * useful with OpenMap by BBN Technologies, tested with version 3.6.2. 
 * Add this file to openmap-3.6.2/com/bbn/openmap/layer/vpf/
 *   and add a line to the Makefile
 *
 * 20.12.2000 Bernhard Reiter <bernhard@intevation.de>
 * version=1.0
 *
 * $Id$
 * **********************************************************************
 */

package com.bbn.openmap.layer.vpf;

import java.io.File;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import com.bbn.openmap.util.FormatException;

/**
 * This class will print out some basic information about a VPF database,
 * suitable for inclusion into the openmap.properties file.
 * <pre>
 * Usage:
 * java com.bbn.openmap.layer.vpf.GenerateVPFproperties /path/to/vpf/database
 * </pre>
 * It will then print out VPFlayer descriptions which you can use
 * to view the VPF layers with the openmap application, to the standard output.
 * There is no GUI.
 * 
 * If you add the output to the openmap.properties files, pay attention
 * to the Summary: lines. They make it easy to add all the layernames 
 * to the recognised ones.
 * 
 */

public class GenerateVPFproperties extends DescribeDB {
     static File rootpath;
     static LibrarySelectionTable lst;

    /**
     * The main program.  Takes path arguments, and prints the DB it finds
     * @param args the paths to print
     */
  public static void main(String[] args) throws FormatException {
        for (int argsi = 0; argsi < args.length; argsi++) {
             rootpath = new File(args[argsi]);
             lst = new LibrarySelectionTable(rootpath);
             //println("Path to database: " + rootpath);
             //println("Database Name: " + lst.getDatabaseName());
             println("### Generated openmap.propterties for");
             println("# VPF Data at: " + rootpath);
             println("# Description: " + lst.getDatabaseDescription());
             String[] libraries = lst.getLibraryNames();
             //print("Database Libraries: ");
             //for (int i = 0; i < libraries.length; i++) {
             //    print(libraries[i], " ");
             //}
             //println("");
             //println("");
        for (int i = 0; i < libraries.length; i++) {
            String prefix = lst.getDatabaseName()+"_" + libraries[i];
            println("#Library: " + prefix );
            printLibrary(prefix, lst.getCAT(libraries[i]));
            println("");
        }
    }
  }
  
    /**
     *  Prints a VPF Library
     * @param prefix lines get printed with this prefix
     * @param cat the CoverageAttributeTable (Library) to print
     */
  public static void printLibrary(String prefix, CoverageAttributeTable cat){
    StringBuffer printedlayers = new StringBuffer();
    String printedlayername = null;

        if (cat == null) {
        System.err.println(prefix + "Library doesn't exist");
        return;
    }
    String[] coverages = cat.getCoverageNames();
    //println(prefix, "uses " + (cat.isTiledData() ? "tiled" : "untiled") + " data");

    for (int i = 0; i < coverages.length; i++) {
      printedlayername=printCoverageProperties(prefix, cat, coverages[i]);
      if(printedlayername!=null) {
        printedlayers.append(" "+printedlayername);
      } 
    }
    println("#Summary:"+printedlayers);
  }
  
  /**
   *  Prints a VPF Coverage
   * @param prefix this will be the prefix of the generated layer name
   * @param covname the name of the coverage to print
   * @param cat the CoverageAttributeTable to get the Coverage from
   */
  public static String printCoverageProperties(String prefix, CoverageAttributeTable cat, String covname){
        String layername=prefix + "_" + covname;
        Vector text_features=new Vector();
        Vector edge_features=new Vector();
        Vector area_features=new Vector();
        Vector point_features=new Vector();
    
    
        //add topology level
        CoverageTable ct = cat.getCoverageTable(covname);
    
        File path = ct.getDataPath();
        File fca = new File(path, "fca");
        if (!fca.exists()) {
            fca = new File(path, "fca.");
        }
        if (!fca.canRead()) {
            println("");
            return null;
        }
        try {
            DcwRecordFile fcadesc = new DcwRecordFile(fca);
            int fclass = fcadesc.whatColumn("fclass");
            int type = fcadesc.whatColumn("type");
            int descr = fcadesc.whatColumn("descr");
            Vector v;
            while ((v = fcadesc.parseRow()) != null) {
                String name = (String)v.elementAt(fclass);
                String t = (String)v.elementAt(type);
                String desc = (String)v.elementAt(descr);
                //String tstring = "[unknown] ";
                if (t.equals("T")) {
                    text_features.addElement(name);
                } else if (t.equals("L")) {
                    edge_features.addElement(name);
                } else if (t.equals("A")) {
                    area_features.addElement(name);
                } else if (t.equals("P")) {
                    point_features.addElement(name);
                }
            }
        } catch (FormatException fe) {
            //nevermind, skip it
    }

    // only print something, if we really found features
    if(!(   text_features.isEmpty()&& edge_features.isEmpty()&&
            area_features.isEmpty()&& point_features.isEmpty())) {

        println("###VPF "+ cat.getCoverageDescription(covname)+" Layer" );
        println(layername +".class=com.bbn.openmap.layer.vpf.VPFLayer");
        println(layername + ".prettyName=" 
            + "VPF "+ cat.getCoverageDescription(covname)+" "+prefix);
        println(layername + ".vpfPath=" + rootpath);
        println(layername + ".coverageType=" + covname);
        println(layername + ".featureTypes=" + "area edge text point");
    
        printFeatures("text",text_features,layername);
        printFeatures("edge",edge_features,layername);
        printFeatures("area",area_features,layername);
        printFeatures("point",point_features,layername);
        println("");

    } else {
        return null;
    }

    return layername;

  }
    /**
     * Print some featureclass names 
     * @param fcis an array of FeatureClassInfo objects whose names get
     * printed
     */
  public static void printFeatures(
        String fname, Vector features, String layername) {
    if(!features.isEmpty()) {
        print(layername+"."+fname+"=");
        for (int i = 0; i < features.size(); i++) {
            print(features.elementAt(i)+ " ");
        }
        println("");
    }
  }
}
