1 | package LATMOS::Accounts::Synchro; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use base qw(Config::IniFiles); |
---|
7 | use LATMOS::Accounts::Bases; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::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 | |
---|
20 | Stub documentation for LATMOS::Accounts, created by h2xs. It looks like the |
---|
21 | author of the extension was negligent enough to leave the stub |
---|
22 | unedited. |
---|
23 | |
---|
24 | Blah blah blah. |
---|
25 | |
---|
26 | =head1 FUNCTIONS |
---|
27 | |
---|
28 | =cut |
---|
29 | |
---|
30 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
31 | |
---|
32 | =head2 new($from, $to) |
---|
33 | |
---|
34 | Create a new synchronisation where $from and $to are LATMOS::Accounts::Base |
---|
35 | based objects. $to can be an array ref of objects. |
---|
36 | |
---|
37 | =cut |
---|
38 | |
---|
39 | sub 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 | |
---|
54 | sub from { |
---|
55 | my ($self) = @_; |
---|
56 | return $self->{from} |
---|
57 | } |
---|
58 | |
---|
59 | sub to { |
---|
60 | my ($self) = @_; |
---|
61 | return @{$self->{to} || []}; |
---|
62 | } |
---|
63 | |
---|
64 | =head2 load_dest |
---|
65 | |
---|
66 | Try to loaded all base, return the count of filtrered base which cannot |
---|
67 | be loaded |
---|
68 | |
---|
69 | =cut |
---|
70 | |
---|
71 | sub 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 | |
---|
86 | sub 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 | |
---|
121 | 1; |
---|
122 | |
---|
123 | __END__ |
---|
124 | # Below is stub documentation for your module. You'd better edit it! |
---|
125 | |
---|
126 | =head1 SEE ALSO |
---|
127 | |
---|
128 | Mention other useful documentation such as the documentation of |
---|
129 | related modules or operating system documentation (such as man pages |
---|
130 | in UNIX), or any relevant external documentation such as RFCs or |
---|
131 | standards. |
---|
132 | |
---|
133 | If you have a mailing list set up for your module, mention it here. |
---|
134 | |
---|
135 | If you have a web site set up for your module, mention it here. |
---|
136 | |
---|
137 | =head1 AUTHOR |
---|
138 | |
---|
139 | Thauvin Olivier, E<lt>olivier.thauvin@latmosipsl.frE<gt> |
---|
140 | |
---|
141 | =head1 COPYRIGHT AND LICENSE |
---|
142 | |
---|
143 | Copyright (C) 2009 by Thauvin Olivier |
---|
144 | |
---|
145 | This library is free software; you can redistribute it and/or modify |
---|
146 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
147 | at your option, any later version of Perl 5 you may have available. |
---|
148 | |
---|
149 | |
---|
150 | =cut |
---|