//$Id$  

package com.adventnet.zoho.client.report.jdbc.v2;

import java.sql.*;
import java.util.Properties;

public class MetaDataSample
{
    public static void main(String args[])
    {
        Connection con = null;
        ResultSet workspaces = null;
        ResultSet tables = null;
        try
        {
            Class.forName("com.zoho.analytics.jdbc.ZohoAnalyticsDriver");
            Properties conProps = new Properties();

            // ZohoAnalytics OAuth authentication parameters
            conProps.put("CLIENT_ID","1000.XXXXXXXXXX");
            conProps.put("CLIENT_SECRET","XXXXXXXXXXXXXXXXXXXX");
            conProps.put("REFRESH_TOKEN","1000.XXXXXXXXXX.XXXXXXXXXX");

            // Uncomment this in case you need proxy settings
            /*
            // Proxy host name to connect the internet
            conProps.put("PROXYSERVER","proxy_hostname");
            // Proxy port to connect the internet
            conProps.put("PROXYPORT","proxy_port");
            // Proxy username to connect the internet
            conProps.put("PROXYUSERNAME","proxy_username");
            // Proxy password to connect the internet
            conProps.put("PROXYPASSWORD","proxy_password");
            */

            con = DriverManager.getConnection("jdbc:zohoanalytics://<Domain_Name>/<Org_ID>/<Workspace_Name>",conProps);
            //con = DriverManager.getConnection("jdbc:zohoanalytics://analyticsapi.zoho.com/15068809/SalesWorkspace",conProps);
            DatabaseMetaData dbMeta = con.getMetaData();
            workspaces = dbMeta.getCatalogs();
            while (workspaces.next()) {
                String catalog = workspaces.getString("TABLE_CAT");
                System.out.println("Workspace Name: " + catalog);
                tables = dbMeta.getTables(catalog, null, "%", null);
                System.out.println("Table List: ");
                while (tables.next()) {
                    String tableName = tables.getString("TABLE_NAME");
                    System.out.println("\t"+tableName);
                }
            }
        }
        catch(SQLException se)
        {
            System.out.println("SQLException: " + se.getMessage());
            System.out.println("Zoho Analytics Error code: " + se.getErrorCode());
        }
        catch(Exception e)
        {
            System.out.println("Exception: " + e.getMessage());
        }
        finally
        {
            if(workspaces != null)
            {
                try
                {
                    workspaces.close();
                }
                catch(SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
            if(tables != null)
            {
                try
                {
                    tables.close();
                }
                catch(SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
            if(con != null)
            {
                try
                {
                    con.close();
                }
                catch (SQLException sqlEx)
                {
                    System.out.println(sqlEx.getMessage());
                }
            }
        }
    }
}