#!/usr/bin/perl -w
#
# $Id: Interface.pm,v 1.2 2002/03/15 16:11:33 fpmip Exp $
#
# IPFC Backend interface definition
#
# 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::Backend::Interface;

#use Data::Dumper;

use IPFC::Log;
use IPFC::Event;
use IPFC::EventGroup;
use IPFC::Logfile;
use IPFC::LogUnit;

use strict;
use warnings;

sub new() {
# constructor for the backend connection object.
# May take parameters
	die "Do not call IPFC::Backend::Interface methods directly !";
}
	
sub get_event($) {
# takes an event_id
# returns an IPFC::Event object

  my $self = shift;
  my $event_id = shift;

  my $event_ids = [ $event_id ];
  
  my $eventgroup = $self->get_events($event_ids); # IPFC::EventGroup
  
  my $event = $eventgroup->get_event(0); # get the first event
  
  return $event;
}

sub get_events($) {
# takes a reference to an array of event_ids to get
# returns a IPFC::EventGroup

	die "Do not call IPFC::Backend::Interface methods directly !";
}

sub put_event($) {
# takes an IPFC::Event object
# and puts it in the database
	die "Do not call IPFC::Backend::Interface methods directly !";
}

sub put_eventgroup($) {
# takes an EventGroup object and puts all events in the EventGroup in the database
	my $self = shift;
	my $eventgroup = shift;
	
	if ($eventgroup->isa("IPFC::EventGroup")) {
		my $i = 0;
		my $number_of_events = $eventgroup->count_events;
		my $ok = 1;
		while ($ok && ($i < $number_of_events)) {
			my $event = $eventgroup->get_event($i);
			$ok = $self->put_event($event);
			$i++;
		}
		return $ok;
	} else {
		warn "The object given to IPFC::Backend::Interface::put_eventgroup is not an IPFC::EventGroup";
		return 0;
	}
}

sub put_logfile($) {
# puts a logfile object in the Backend
	die "Do not call IPFC::Backend::Interface methods directly !";
}

sub put_logunit($) {
# puts a logunit object in the Backend
	my $self = shift;
	my $logunit = shift;
	
	if ($logunit->isa("IPFC::LogUnit")) {
		my $logfile = $logunit->get_logfile;
		my $eventgroup = $logunit->get_eventgroup;
		
		$self->put_logfile($logfile) or return 0;
		$self->put_eventgroup($eventgroup) or return 0;
		
		return 1;
	} else {
		warn "The object given to IPFC::Backend::Interface::put_logunit is not an IPFC::LogUnit";
		return 0;
	}
}

sub pre_commit() {
# flush all queries out to the database so that we can actually perform a commit when we want to
	die "Do not call IPFC::Backend::Interface methods directly !";
}
	
sub commit($) {
# used to commit to the database
	die "Do not call IPFC::Backend::Interface methods directly !";
}

sub rollback($) {
# used to rollback the changes
	die "Do not call IPFC::Backend::Interface methods directly !";
}


return 1;
