#
# $Id: Example.pm,v 1.1 2002/03/05 23:44:51 fpmip Exp $
#

# Author : Tycho Fruru <tycho@fruru.com>

# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

package IPFC::Parse::Event::Example;

use strict;
use warnings;
use IPFC::Log;

BEGIN {
	use Exporter   ();
        our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);

        # set the version for version checking
        $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker

        @ISA         = qw(Exporter);
        @EXPORT      = qw( );
        %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
        @EXPORT_OK   = qw( );
}
our @EXPORT_OK;

# use anything that you might need
use Time::Local;

# name of the sub must be "parse", the name of the package is found 
# in the ipfc database in table_names for the corresponding agent type and event
# type and category

# the procedure takes 3 parameters :
# $dbh, a database handle (DBI) for an open connection to the ipfc database
# $entry, a reference to an IPFC XML structure on the ->{'entry'}->[index] level
#	(see below for how to use this)
# $r_sql_hash, a reference to a hash containing already filled-in fields that
#	should be set in the event table.  SQL Column names are the keys.
# 	It is possible to redefine already existing data (eg. for time calculation)

sub parse($$$) {
	shift; # current class
	my $dbh = shift;
	my $entry = shift;
	my $r_sql_hash = shift;
	
	# make local copies so that we don't pollute the global variables
	my %sql_hash = %{$r_sql_hash};
	
# <entry>
#   <content>
#     <foo>bar</foo>
#     <foo>baz</foo>
#     <foo format="uri">quux</foo>
#   </content>
# </entry>

	my $foo_line = $entry->{'content'}->[0]->{'foo'};

	if (!defined($foo_line)) { 
		warn "There is no foo entry !";
		return 0;
	}
	
	my $rc = 1;
	
	if (ref($foo_line) eq "ARRAY") {
		my $index = 0;
		
		while (defined($foo_line->[$index]) && $rc==1) {

		 	my %foo_sql_hash = %sql_hash;
			
			# to support uri encoding, base64, ...
			my $foo_decoded_line=decode_entry($foo_line->[$index]);
			
			$foo_sql_hash{'message'} = $foo_decoded_line;
			
			# do the database insert 
			# (commit will be done later, depending on 
			#	our return value !)
			
			$rc = IPFC::DB::Event::put_event($dbh,\%foo_sql_hash);
			$index++;
		}
	} else { 
		warn "There are no foo-lines in this file !"; 
		$rc = 0; 
	}
			
	IPFC::Log::logger(5,"End of example...\n");
	
	# return 1 -> everything went OK, this part can be committed
	# return 0 -> something went wrong, this part must be rolled back		
	return $rc;
}

# to make "require" happy
return 1;
