source: LATMOS-Accounts/lib/LATMOS/Accounts/Synchro.pm @ 41

Last change on this file since 41 was 41, checked in by nanardon, 15 years ago
  • start synchronisation work
  • Property svn:keywords set to Id Rev
File size: 3.2 KB
Line 
1package LATMOS::Accounts::Synchro;
2
3use 5.010000;
4use strict;
5use warnings;
6use base qw(Config::IniFiles);
7use LATMOS::Accounts::Bases;
8
9=head1 NAME
10
11LATMOS::Accounts::Synchro - Perl extension for blah blah blah
12
13=head1 SYNOPSIS
14
15  use LATMOS::Accounts;
16  blah blah blah
17
18=head1 DESCRIPTION
19
20Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the
21author of the extension was negligent enough to leave the stub
22unedited.
23
24Blah blah blah.
25
26=head1 FUNCTIONS
27
28=cut
29
30our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
31
32=head2 new($from, $to)
33
34Create a new synchronisation where $from and $to are LATMOS::Accounts::Base
35based objects. $to can be an array ref of objects.
36
37=cut
38
39sub new {
40    my ($class, $from, $to) = @_;
41
42    my $self = {};
43
44    $self->{from} = $from;
45
46    # allow ref and array ref of, eg
47    # to = $foo and $to = [ $foo, $bar ]
48    foreach (ref $to ? @{ $to || []} : ($to)) {
49        push(@{$self->{to}}, $_);
50    }
51    bless($self, $class)
52}
53
54sub from {
55    my ($self) = @_;
56    return $self->{from}
57}
58
59sub to {
60    my ($self) = @_;
61    return @{$self->{to} || []};
62}
63
64=head2 load_dest
65
66Try to loaded all base, return the count of filtrered base which cannot
67be loaded
68
69=cut
70
71sub load_dest {
72    my ($self) = @_;
73    my @loaded;
74    my $unloaded = 0;
75    foreach ($self->to) {
76        if($_->load) {
77            push(@loaded, $_);
78        } else {
79            $unloaded++;
80        }
81    }
82    $self->{to} = \@loaded;
83    return $unloaded;
84}
85
86sub process {
87    my ($self, %options) = @_;
88
89    $self->from->load or return;
90    # if any cannot be loaded, return,
91    # TODO we need a way to force if some still can be sync
92    $self->load_dest and return;
93   
94    # listing existing obj one time:
95    foreach my $otype ($self->from->list_supported_objects) {
96       
97        # If no dest support $otype, we skip
98        my @obj_dest_base = grep {
99            $_->is_supported_object($otype)
100        } $self->to or next;
101
102        # loading object list one time for all
103        # TODO optimize this, using rev of objects
104        my @lobjfrom = $self->from->list_objects($otype);
105
106        warn "Sync objects $otype\n";
107        foreach my $destbase (@obj_dest_base) {
108            warn "objects $otype => " . $destbase->type . "\n";
109            my %exists = map { $_ => 1 } $destbase->list_objects($otype);
110            foreach (@lobjfrom) {
111                if (!$exists{$_}) {
112                    warn "  $otype $_: created\n";
113                }
114            }
115        }
116    }
117
118    1;
119}
120
1211;
122
123__END__
124# Below is stub documentation for your module. You'd better edit it!
125
126=head1 SEE ALSO
127
128Mention other useful documentation such as the documentation of
129related modules or operating system documentation (such as man pages
130in UNIX), or any relevant external documentation such as RFCs or
131standards.
132
133If you have a mailing list set up for your module, mention it here.
134
135If you have a web site set up for your module, mention it here.
136
137=head1 AUTHOR
138
139Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt>
140
141=head1 COPYRIGHT AND LICENSE
142
143Copyright (C) 2009 by Thauvin Olivier
144
145This library is free software; you can redistribute it and/or modify
146it under the same terms as Perl itself, either Perl version 5.10.0 or,
147at your option, any later version of Perl 5 you may have available.
148
149
150=cut
Note: See TracBrowser for help on using the repository browser.