//
// COPYRIGHT:   (c) Awasu Pty. Ltd. 2002-11 (all rights reserved).
//              Unauthorized use of this code is prohibited.
//
// LICENSE:     This software is provided 'as-is', without any express 
//              or implied warranty.
//
//              In no event will the author be held liable for any damages 
//              arising from the use of this software.
//  
//              Permission is granted to anyone to use this software 
//              for any non-commercial purpose and to alter it and
//              redistribute it freely, subject to the following restrictions: 
//
//              - The origin of this software must not be misrepresented; 
//                you must not claim that you wrote the original software. 
//                If you use this software, an acknowledgment is requested
//                but not required. 
//
//              - Altered source versions must be plainly marked as such, 
//                and must not be misrepresented as being the original software. 
//                Altered source is encouraged to be submitted back to 
//                the original author so it can be shared with the community. 
//                Please share your changes. 
//
//              - This notice may not be removed or altered from any 
//                source distribution. 
//

import java.sql.* ;

public class SampleJavaPlugin
{

    // --- MAIN -------------------------------------------------------

    public static void
    main( String[] args )
    {
        try
        {
            doMain( args ) ; 
        }
        catch( Exception xcptn )
        {
            System.err.println( APP_PLUGIN_ID + " error:" ) ;
            System.err.println( "  " + xcptn.toString() ) ;
            System.exit( 1 ) ; 
        }
    }
    
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    private static void 
    doMain( String[] args ) throws Exception
    {
        // get the name of the INI file 
        if ( args.length != 1 )
            throw new Exception( "Missing plugin INI file." ) ;
        iniFiles configFile = new iniFiles( args[0] ) ;
        configFile.loadIni() ;
    
        // process the requested command 
        String cmd = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "Command" ) ;
        if ( cmd.compareToIgnoreCase("GenerateMainPage") == 0 )
            generateMainPage( configFile ) ; 
        else if ( cmd.compareToIgnoreCase("ProcessRequest") == 0 ) 
        {
            String pluginRequest = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "PluginRequest" ) ;
            String paramString = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "ParamString" ) ;
            processRequest( configFile , pluginRequest , paramString ) ;
        }
        else
            throw new Exception( "Unknown script command: " + cmd ) ;
    }

    // ----------------------------------------------------------------

    private static void
    generateMainPage( iniFiles configFile ) throws Exception
    {
        // get the app settings 
        String appServerUrl = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "AppServerUrl" ) ;
        if ( appServerUrl == "" )
            throw new Exception( "No app server URL was specified." ) ;
        String appPluginServerUrl = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "AppPluginServerUrl" ) ;
        if ( appPluginServerUrl == "" )
            throw new Exception( "No app plugin server URL was specified." ) ;
        String apiToken = configFile.getValue( SYSTEM_PARAMETERS_SECTION_NAME , "ApiToken" ) ;
    
        // generate the main page
        System.out.println( "<HTML>" ) ;
        System.out.println( "<BODY>" ) ;
        System.out.println( "<H2>Sample Java Plugin</H2>" ) ;
        System.out.println( "<P>This is the main page for the sample Java plugin." ) ;
        System.out.println( "<P>Click on one of the following links to subscribe to a channel that will monitor the specified database table." ) ;
        System.out.println( "<UL>" ) ;
        System.out.println( "  <LI>" + generateChannelLink(appServerUrl,appPluginServerUrl,apiToken,"tblSimpleRssItems") ) ;
        System.out.println( "  <LI>" + generateChannelLink(appServerUrl,appPluginServerUrl,apiToken,"tblCustomerSales") ) ;
        System.out.println( "</UL>" ) ;
        System.out.println( "</BODY>" ) ;
        System.out.println( "</HTML>" ) ;
    }            

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    private static String
    generateChannelLink( String appServerUrl , String appPluginServerUrl , String apiToken , String tableName )
    {
        // generate a channel URL 
        StringBuffer buf = new StringBuffer() ;
        buf.append( "<a href='" ) ;
        buf.append( appServerUrl + "/channels/subscribe" ) ;
        buf.append( "?token=" + apiToken ) ;
        buf.append( "&url=" + appPluginServerUrl + "/" + APP_PLUGIN_ID + "/" + tableName ) ;
        buf.append( "'>" ) ;
        buf.append( tableName ) ;
        buf.append( "</a>" ) ;
        return buf.toString() ;
    }
        
    // ----------------------------------------------------------------

    private static void
    processRequest( iniFiles configFile , String pluginRequest , String paramString ) throws Exception
    {
        // NOTE:  We get here when Awasu tries to access the URL's we generated 
        // previously in generateMainPage() to get the latest channel feed.
        // These URL's were constructed in such a way that the pluginRequest is
        // the name of the table we are to monitor.
        String tableName = pluginRequest ;

        // extract our request parameters 
        String databasePath = configFile.getValue( PLUGIN_PARAMETERS_SECTION_NAME , "DatabasePath" ) ;
        int maxRows = configFile.getIntValue( CHANNEL_PARAMETERS_SECTION_NAME , "MaxRows" ) ;
                
        // initialize the driver
        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;

        // connect to the database
        String connString = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" + databasePath + ";" ;
        Connection conn = DriverManager.getConnection( connString ) ;

        // generate the RSS feed 
        if ( tableName.compareToIgnoreCase("tblSimpleRssItems") == 0 )
            querySimpleRssItems( conn , maxRows ) ; 
        else if ( tableName.compareToIgnoreCase("tblCustomerSales") ==  0)
            queryCustomerSales( conn , maxRows ) ; 
        else 
            throw new Exception( "Unknown table name: " + tableName ) ;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    private static void 
    querySimpleRssItems( Connection conn , int maxRows ) throws Exception
    {
        // prepare the SQL query 
        String sql ;
        if ( maxRows < 0 )
            sql = "SELECT * FROM tblSimpleRssItems" ; 
        else 
            sql = "SELECT TOP " + Integer.toString(maxRows) + " * FROM tblSimpleRssItems" ; 
        Statement stmt = conn.createStatement() ;
        ResultSet rs = stmt.executeQuery( sql ) ;
        
        // put together a channel description 
        StringBuffer descriptionBuf = new StringBuffer() ;
        descriptionBuf.append( "<P>This RSS feed was generated by the sample Java plugin." ) ; 
        descriptionBuf.append( "<P>The SQL used to query the database was:" ) ; 
        descriptionBuf.append( "<pre>" ) ; 
        descriptionBuf.append( "&nbsp;&nbsp;&nbsp;&nbsp;" + sql + "\n" ) ; 
        descriptionBuf.append( "</pre>" ) ; 

        // generate the RSS feed 
        StringBuffer rssBuf = new StringBuffer() ; 
        rssBuf.append( "<rss>\n" ) ;
        rssBuf.append( "<channel>\n" ) ;
        rssBuf.append( "<title>Sample Java Plugin</title>\n" ) ;
        rssBuf.append( "<description><![CDATA[" + descriptionBuf.toString() + "]]></description>\n" ) ;
        while( rs.next() ) 
        {
            rssBuf.append( "<item>\n" ) ;
            rssBuf.append( "<link>" + rs.getString("LINK") + "</link>\n" ) ; 
            rssBuf.append( "<title>" + rs.getString("TITLE") + "</title>\n" ) ;
            rssBuf.append( "<description>" + rs.getString("DESCRIPTION") + "</description>\n" ) ; 
            rssBuf.append( "</item>\n" ) ;
        }
        rssBuf.append( "</channel>\n" ) ;
        rssBuf.append( "</rss>\n" ) ;
        System.out.println( rssBuf.toString() ) ;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    private static void 
    queryCustomerSales( Connection conn , int maxRows ) throws Exception
    {
        // prepare the SQL query 
        String sql ;
        if ( maxRows < 0 )
            sql = "SELECT * FROM tblCustomerSales" ; 
        else 
            sql = "SELECT TOP " + Integer.toString(maxRows) + " * FROM tblCustomerSales" ; 
        Statement stmt = conn.createStatement() ;
        ResultSet rs = stmt.executeQuery( sql ) ;
        
        // generate the RSS feed 
        StringBuffer rssBuf = new StringBuffer() ; 
        rssBuf.append( "<rss>\n" ) ;
        rssBuf.append( "<channel>\n" ) ;
        rssBuf.append( "<link>http://intranet/sales</link>\n" ) ;
        rssBuf.append( "<title>Latest Customer Sales</title>\n" ) ;
        while( rs.next() ) 
        {
            rssBuf.append( "<item>\n" ) ;
            // NOTE: This is a dummy URL that demonstrates how one might be generated for a given sales item.
            String url = "http://intranet/sales/" + rs.getString("ID") ; 
            rssBuf.append( "<link>" + url + "</link>\n" ) ; 
            String title = rs.getString("CustomerName") + " ($" + rs.getInt("Amount") + ")" ;
            rssBuf.append( "<title>" + title + "</title>\n" ) ;
            StringBuffer buf = new StringBuffer() ;
            buf.append( rs.getDate("DateOfSale").toString() + ": " ) ;
            buf.append( rs.getString("ItemCount") + " x " + rs.getString("ItemName") ) ;
            String comments = rs.getString("Comments") ; 
            if ( comments != null )
                buf.append( "<br><i>" + comments + "</i>" ) ; 
            rssBuf.append( "<description><![CDATA[" + buf.toString() + "]]></description>\n" ) ; 
            rssBuf.append( "</item>\n" ) ;
        }
        rssBuf.append( "</channel>\n" ) ;
        rssBuf.append( "</rss>\n" ) ;
        System.out.println( rssBuf.toString() ) ;
    }
    
    // --- DATA MEMBERS ---

    private final static String APP_PLUGIN_ID = "SampleJavaPlugin" ;
    private final static String SYSTEM_PARAMETERS_SECTION_NAME = "System" ; 
    private final static String PLUGIN_PARAMETERS_SECTION_NAME = "PluginParameters" ;
    private final static String CHANNEL_PARAMETERS_SECTION_NAME = "ChannelParameters" ;
        
}

