#!/usr/local/bin/perl5.6.1 -w
# $Id: BC2DBaseData.pm,v 1.1 2003/10/21 15:42:03 jx1 Exp $

package BC2DBaseData;

use strict;
use warnings;
use Carp qw(croak cluck carp);
use Class::Struct;
use sigtrap qw(die untrapped normal-signals
               stack-trace any error-signals);

use User;
use sql_engine;
use BarcodeRevised_V2_0;
use Checkbarcode;
use BC2DBaseLook;

my $db = '2dbarcode';
my $sqllib = 'BC2D.sql_lib';
my $user_role = 'DNA_collection';

####    tester for object    ####
unless( caller )
{
    use BC2DBaseData;

    my $dataH = BC2DBaseData->new()->init();
    my $vres = $dataH->verifyUserId('ID8384I');
    print "Verification result: $vres\n";
}


####    object definition    ####
struct BC2DBaseData =>
{
    User => '$',
    QueryHandlr => '$',
    View => '$', 
};


sub init
{
    my $self = shift;

#    $self->UserId('');
#    $self->UserEmail('');

    $self->User( User->new() );
    my $qhdlr = statements->new("$db");
    $self->QueryHandlr( $qhdlr );
#    $qhdlr->default_sql_lib_path('./');
    $qhdlr->load_sql_lib(["BC2D.sql_lib::all"]);
#    print $self->QueryHandlr(), "\n";

    $self->View( shift );

    return $self;
}


sub verifyUserId
{
    my $self = shift;
    my $userid = shift;
    my $dbqueryh = $self->QueryHandlr();
    my $view = $self->View();

    my $ret_val;

#    $self->UserId($userid);
 
    # validate format
    my $result = Verifynumber($userid); #, 'ID');

#    print keys %{$result}, "\n"; #->{Number}, $result->{Process}, "\n";
#    print "Whole: ", $result->{Whole}, "\n";
#    print "Type: ", $result->{Type}, "\n";
#    print "Number: ", $result->{Number}, "\n";
#    print "Check: ", $result->{Check}, "\n";

    if( $result->{Process} eq 'Good' )
    {
	# validate user
	my $res_query = ($dbqueryh->get('BC2D.sql_lib::is_person_for_role', 
					[$result->{Number}, $user_role])
			 );#->[0];
			   
	#print "query result: ", scalar(@{$res_query}), "\n";

	if ( scalar(@{$res_query}) )
	{
	    my ($email, $team, $roleid) = @{$res_query->[0]};
	    print "User: ", $email, " for team: ", $team, "\n";
	    
	    if( defined $view )
	    {
		$view->updateMessage("User $email authorised for Team $team.");
	    }

	    $ret_val = $email;	     

	    my $user = $self->User();
	    $user->PersonId( $result->{Number} );
	    $user->Email( $email );
	    $user->Team( $team );
	    $user->RoleId( $roleid );
	}
	else
	{
	    # person unauthorized for running this app
	    if( defined $view )
	    {
		$view->updateWarning( "$userid is not registered to use this software.", 
				      "CONTACT BUG." );
	    }
	    $ret_val = 0;
	}
    }
    else
    {
	if( defined $view )
	{
	    $view->updateWarning( "'$userid' is not in valid format. Please try again.");
	}
	$ret_val = 0;
    }
    
    print "The login user is: ",
           $self->User()->PersonId(), " - ", 
           $self->User()->Team(), " - ", 
           $self->User()->RoleId(), "\n";

    return $ret_val;
}


sub resetUser
{
    my $self = shift;
    my $user = $self->User();

    $user->PersonId( undef() );
    $user->Team(  undef() );
    $user->RoleId( undef() );

    return;
}


sub DESTROY
{
    my $self = shift;

    $self->View( undef()) ;

    my $qh = $self->QueryHandlr();
    if( $qh )
    {
	$qh->{DBH}->rollback();
	print "call sqlengin end method now.\n";
	$qh->end();
    }

    print "BC2DBaseData destroyed.\n";
}
1; # keep perl happy
