File Coverage

blib/lib/Data/Reconciliation.pm
Criterion Covered Total %
statement 102 108 94.4
branch 12 20 60.0
condition 1 3 33.3
subroutine 14 14 100.0
pod 8 8 100.0
total 137 153 89.5


line stmt bran cond sub pod time code
1             ##======================================================================
2             ## Authors:
3             ## Martial.Chateauvieux@sfs.siemens.de
4             ## O.Capdevielle@cadextan.fr
5             ##======================================================================
6             ## Copyright (c) 2001, Siemens Financial Services. All rights reserved.
7             ## This library is free software; you can redistribute it and/or modify
8             ## it under the same terms as Perl itself.
9             ##======================================================================
10             ## $Log:$
11             ##======================================================================
12              
13             package Data::Reconciliation;
14              
15             require 5.005_62;
16 1     1   72922 use strict;
  1         3  
  1         47  
17 1     1   6 use warnings;
  1         2  
  1         37  
18              
19 1     1   5 use Data::Table;
  1         7  
  1         19  
20 1     1   6 use Carp;
  1         3  
  1         84  
21              
22 1     1   1276 use Data::Reconciliation::Rule;
  1         4  
  1         1446  
23              
24             require Exporter;
25              
26             our $VERSION = '0.07';
27              
28             sub new {
29 1     1 1 11 my $class = shift;
30 1         13 my $source1 = shift;
31 1         2 my $source2 = shift;
32 1         3 my %prms = @_;
33              
34 1         8 croak 'Sources must be of type Data::Table'
35 1         6 if (! eval {$source1->isa('Data::Table')}) ||
36 1 50 33     3 (! eval {$source2->isa('Data::Table')});
37            
38 1         2 my @rules;
39 1 50       6 if (exists $prms{-rules}) {
40 1         2 @rules = @{$prms{-rules}};
  1         4  
41              
42 1         27 croak 'Invalid Data::Reconciliation::Rule'
43 1 50       3 if grep(! eval {$_->isa('Data::Reconciliation::Rule')},
44             @rules);
45            
46             } else {
47 0         0 my $r = new Data::Reconciliation::Rule;
48 0         0 $r->identification([0], undef, [0], undef);
49              
50 0         0 my $col_nb = 0;
51 0         0 foreach ($source1->header) {
52 0         0 $r->add_comparison([++$col_nb], undef, [$col_nb], undef, undef);
53             }
54 0         0 push @rules, $r;
55             }
56              
57 1         9 return bless {'srcs' => [$source1, $source2],
58             'rules' => \@rules,
59             #'result-store' => $result_store
60             }, $class;
61             }
62              
63             sub build_signatures {
64 1     1 1 495 my $this = shift;
65 1         2 my $rule_nb = shift;
66              
67 1 50       4 croak 'usage: $r->build_signatures();'
68             if ! defined $rule_nb;
69              
70 1         8 croak 'invalid rule nb'
71 1 50       2 if $rule_nb >= @{$this->{'rules'}};
72              
73 1         3 for my $src_nb (0 .. 1) {
74 2         3 my %signature;
75 2         6 my $rule = $this->{'rules'}->[$rule_nb];
76 2         3 my $src = $this->{'srcs'}->[$src_nb];
77 2         9 for(my $i = 0 ; $i < $src->nofRow ; $i++) {
78 12         92 push @{$signature{$rule->signature($src_nb, $src->rowRef($i))}}, $i;
  12         34  
79             }
80 2         21 $this->{'signatures'}->[$src_nb] = \%signature;
81             }
82             }
83              
84             sub signatures {
85 1     1 1 186 my $this = shift;
86 1 50       6 return ([], []) if ! defined $this->{'signatures'};
87 1         3 return @{$this->{'signatures'}};
  1         4  
88             }
89              
90             sub duplicate_signatures {
91 3     3 1 197 my $this = shift;
92 3         4 my @results;
93 3         7 foreach my $src_nb (0..1) {
94 6         10 my $signature = $this->{'signatures'}->[$src_nb];
95 2         12 $results[$src_nb] = {
96             map {
97 32         67 ($_ => $signature->{$_})
98 6         20 } grep(1 < @{$signature->{$_}}, keys %$signature)
99             }
100             }
101 3         9 return @results;
102             }
103              
104             sub _delete {
105 2     2   3 my $this = shift;
106 2         5 my @signs = @_;
107 2         4 my @results = ({},{});
108 2         12 foreach my $src_nb (0..1) {
109              
110 4         5 my $signs = $signs[$src_nb];
111 4 100       19 next if @$signs == 0;
112              
113 2         6 my $src = $this->{'srcs'}->[$src_nb];
114 2         3 my $signature = $this->{'signatures'}->[$src_nb];
115              
116 2         4 %{$results[$src_nb]} = map { ($_ => delete $signature->{$_}) } @$signs;
  2         9  
  3         9  
117             }
118 2         7 return @results;
119             }
120              
121             sub delete_dup_signatures {
122 1     1 1 213 my $this = shift;
123 1         4 return $this->_delete(map {[keys %$_]} $this->duplicate_signatures);
  2         9  
124             }
125              
126             sub widow_signatures {
127 3     3 1 362 my $this = shift;
128 3         9 my @results = ({},{});
129 3         8 foreach my $src_nb (0..1) {
130            
131 6         10 my $sign = $this->{'signatures'}->[$src_nb];
132 6 100       14 my $sign_other_src = $this->{'signatures'}->[ $src_nb == 0 ? 1 : 0 ];
133 6         7 my $result = $results[$src_nb];
134              
135 6         40 %$result = map {($_ =>$sign->{$_})}
  4         31  
136             grep(! exists $sign_other_src->{$_}, keys %$sign);
137              
138             # foreach my $sign (keys %$sign) {
139             # push @$result, ($sign => $sign->{$sign})
140             # if ! exists $sign_other_src->{$sign};
141             # }
142             }
143 3         9 return @results;
144             }
145              
146             sub delete_wid_signatures {
147 1     1 1 201 my $this = shift;
148 1         4 return $this->_delete(map {[keys %$_]} $this->widow_signatures);
  2         10  
149             }
150              
151             sub reconciliate {
152 1     1 1 182 my $this = shift;
153 1         2 my $rule_nb = shift;
154              
155 1 50       4 croak 'usage: $r->reconciliate();'
156             if ! defined $rule_nb;
157              
158 1         5 croak 'invalid rule nb'
159 1 50       1 if $rule_nb >= @{$this->{'rules'}};
160              
161 1         2 my $rule = $this->{'rules'}->[$rule_nb];
162 1         9 my @results;
163              
164 1         3 my $sign_1 = $this->{'signatures'}->[0];
165 1         2 my $src_1 = $this->{'srcs'}->[0];
166 1         3 my $sign_2 = $this->{'signatures'}->[1];
167 1         2 my $src_2 = $this->{'srcs'}->[1];
168              
169 1         4 foreach my $signature (keys %$sign_1) {
170              
171 4         6 (my $idx1) = @{$sign_1->{$signature}};
  4         9  
172 4         6 (my $idx2) = @{$sign_2->{$signature}};
  4         9  
173              
174 4         13 my $rec1 = [ $src_1->row($idx1) ];
175 4         87 my $rec2 = [ $src_2->row($idx2) ];
176              
177 1         7 push @results, map {
178             # if ($mode eq 'all') {
179 4         81 [$signature, [$idx1, $idx2], $rule, $_];
180             # } elsif ($mode eq 'ok') {
181             # $_ ? () : [$key, $rule, $_];
182             # } else {
183             # $_ ? [$key, $rule, $_] : ();
184             # }
185             } $rule->compare($rec1, $rec2);
186            
187             }
188              
189 1         5 return @results;
190             }
191              
192             1;
193             __END__