package LATMOS::Accounts::SynchAccess::base;

use strict;
use warnings;
use vars qw($AUTOLOAD);
use Carp;
use LATMOS::Accounts::Log;

our $VERSION = (q$Rev: 1405 $ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Accounts::SynchAccess::base - Fake base object for sync access

=head1 DESCRIPTION

This module fake a base object to send same actions tu multiple base. This is
used for actions basic synchronisation process cannot handle.

This module contain common functions between Base and Object managment.

=head1 FUNCTIONS

=cut

sub AUTOLOAD {
    my ($self, @args) = @_;
    my ($constname, $sub) = $AUTOLOAD =~ m/(.*)::([^:]+)/;
    if ($sub =~ /^(wexported)/) {
        $self->_no_trap($sub, @args);
    } elsif ($sub =~ /^(check_password)$/) {
        $self->_run_first($sub, @args);
    } else {
        $self->_trap_false($sub, @args);
    }
}

=head2 new( \@bases, %config )

Create a new SynchAccess instance over given bases

C<%config>:

=over 4

=item cb

Callback, see L<SetCB>

=back

=cut

sub new {
    my ($class, $bases, %config) = @_;
    bless {
        bases => [ @{ $bases } ],
        %config
    }, $class;
}

=head2 bases

Return lists of bases in ths instance.

=cut

sub bases {
    my ($self) = @_;
    @{ $self->{bases} || []}
}

=head2 SetCB ( $callback )

Set a callback to receive the object being call, return the previous one.

=cut

sub SetCB {
    my ( $self, $cb ) = @_;

    my $oldCB = $self->{cb};
    $self->{cb} = $cb;
    return $oldCB;
}

sub _return_all {
    my ($self, $sub, @args) = @_;
    my @allres;
    foreach my $ba ($self->bases) {
        my @res = $_->$sub(@args);
        $self->{cb}->($ba, @res) if($self->{cb});
        push(@allres, @res);
    }
    @allres;
}

sub _run_first {
    my ($self, $sub, @args) = @_;
    my ($fbase) = $self->bases;
    $fbase->$sub(@args);
}

sub _no_trap {
    my ($self, $sub, @args) = @_;
    foreach ($self->bases) {
        $_->$sub(@args);
    }
}

sub _trap_false {
    my ($self, $sub, @args) = @_;
    my $failure = 0;
    foreach ($self->bases) {
        my $res = $_->$sub(@args);
        $self->{cb}->($_, $res) if($self->{cb});
        $res or do {
		    la_log(LA_ERR, "SyncA: Erreur call $sub() for %s", $_);
	        $failure = 1;
        };
    }
    $failure ? 0 : 1;
}

sub _trap_true {
    my ($self, $sub, @args) = @_;
    my $failure = 0;
    foreach ($self->bases) {
        my $res = $_->$sub(@args) and $failure = 1;
        $self->{cb}->($_, $res) if($self->{cb});
    }
    $failure ? 0 : 1;
}

=head2 rename_object

=cut

sub rename_object {
    my ($self, $otype, $uid, $newuid) = @_;
    foreach ($self->bases) {
        if ($_->is_supported_object($otype)) {
            my $res = $_->rename_object($otype, $uid, $newuid);
            $self->{cb}->($_, $res) if($self->{cb});
            $res or do {
                la_log(LA_ERR,
                    "Cannot get object $otype/$uid in %s/%s",
                    $_->type,
                    $_->label
                );
                return;
            };
        }
    }
    return 1;
}

sub DESTROY {}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts>, L<LATMOS::Accounts::Bases>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
