source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/SynchAccess/base.pm @ 2283

Last change on this file since 2283 was 2283, checked in by nanardon, 5 years ago

Allow to callback to SynchAccess? to follow process

  • Property svn:keywords set to Id Rev
File size: 3.4 KB
Line 
1package LATMOS::Accounts::SynchAccess::base;
2
3use strict;
4use warnings;
5use vars qw($AUTOLOAD);
6use Carp;
7use LATMOS::Accounts::Log;
8
9our $VERSION = (q$Rev: 1405 $ =~ /^Rev: (\d+) /)[0];
10
11=head1 NAME
12
13LATMOS::Accounts::SynchAccess::base - Fake base object for sync access
14
15=head1 DESCRIPTION
16
17This module fake a base object to send same actions tu multiple base. This is
18used for actions basic synchronisation process cannot handle.
19
20This module contain common functions between Base and Object managment.
21
22=head1 FUNCTIONS
23
24=cut
25
26sub AUTOLOAD {
27    my ($self, @args) = @_;
28    my ($constname, $sub) = $AUTOLOAD =~ m/(.*)::([^:]+)/;
29    if ($sub =~ /^(wexported)/) {
30        $self->_no_trap($sub, @args);
31    } elsif ($sub =~ /^(check_password)$/) {
32        $self->_run_first($sub, @args);
33    } else {
34        $self->_trap_false($sub, @args);
35    }
36}
37
38=head2 new( \@bases, %config )
39
40Create a new SynchAccess instance over given bases
41
42C<%config>:
43
44=over 4
45
46=item cb
47
48Callback, see L<SetCB>
49
50=back
51
52=cut
53
54sub new {
55    my ($class, $bases, %config) = @_;
56    bless {
57        bases => [ @{ $bases } ],
58        %config
59    }, $class;
60}
61
62=head2 bases
63
64Return lists of bases in ths instance.
65
66=cut
67
68sub bases {
69    my ($self) = @_;
70    @{ $self->{bases} || []}
71}
72
73=head2 SetCB ( $callback )
74
75Set a callback to receive the object being call, return the previous one.
76
77=cut
78
79sub SetCB {
80    my ( $self, $cb ) = @_;
81
82    my $oldCB = $self->{cb};
83    $self->{cb} = $cb;
84    return $oldCB;
85}
86
87sub _return_all {
88    my ($self, $sub, @args) = @_;
89    my @allres;
90    foreach my $ba ($self->bases) {
91        my @res = $_->$sub(@args);
92        $self->{cb}->($ba, @res) if($self->{cb});
93        push(@allres, @res);
94    }
95    @allres;
96}
97
98sub _run_first {
99    my ($self, $sub, @args) = @_;
100    my ($fbase) = $self->bases;
101    $fbase->$sub(@args);
102}
103
104sub _no_trap {
105    my ($self, $sub, @args) = @_;
106    foreach ($self->bases) {
107        $_->$sub(@args);
108    }
109}
110
111sub _trap_false {
112    my ($self, $sub, @args) = @_;
113    my $failure = 0;
114    foreach ($self->bases) {
115        my $res = $_->$sub(@args);
116        $self->{cb}->($_, $res) if($self->{cb});
117        $res or do {
118                    la_log(LA_ERR, "SyncA: Erreur call $sub() for %s", $_);
119                $failure = 1;
120        };
121    }
122    $failure ? 0 : 1;
123}
124
125sub _trap_true {
126    my ($self, $sub, @args) = @_;
127    my $failure = 0;
128    foreach ($self->bases) {
129        my $res = $_->$sub(@args) and $failure = 1;
130        $self->{cb}->($_, $res) if($self->{cb});
131    }
132    $failure ? 0 : 1;
133}
134
135=head2 rename_object
136
137=cut
138
139sub rename_object {
140    my ($self, $otype, $uid, $newuid) = @_;
141    foreach ($self->bases) {
142        if ($_->is_supported_object($otype)) {
143            my $res = $_->rename_object($otype, $uid, $newuid);
144            $self->{cb}->($_, $res) if($self->{cb});
145            $res or do {
146                la_log(LA_ERR,
147                    "Cannot get object $otype/$uid in %s/%s",
148                    $_->type,
149                    $_->label
150                );
151                return;
152            };
153        }
154    }
155    return 1;
156}
157
158sub DESTROY {}
159
1601;
161
162__END__
163
164=head1 SEE ALSO
165
166L<LATMOS::Accounts>, L<LATMOS::Accounts::Bases>
167
168=head1 AUTHOR
169
170Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
171
172=head1 COPYRIGHT AND LICENSE
173
174Copyright (C) 2012 CNRS SA/CETP/LATMOS
175
176This library is free software; you can redistribute it and/or modify
177it under the same terms as Perl itself, either Perl version 5.10.0 or,
178at your option, any later version of Perl 5 you may have available.
179
180=cut
Note: See TracBrowser for help on using the repository browser.