OFAC/SDN.pm

filter: Statementstmt 38.7% 64 / 165 Branchbran 28.2% 13 / 46 Conditioncond 23.8% 5 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 60.0% 12 / 20 Podpod 0.0% 0 / 11 totaltotal 34.7% 96 / 276 CC 33 SCAR 34.2
CC
33
Cov
35%
buildDatabase
110.0
processTable
30.0
searchAddress
20.0
CRAP 30.5
↑ Summary OFAC.pm »
1 package Data::OFAC::SDN;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Archive::Extract;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::OFAC::SDN::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use constant { SDNURL => 'http://www.treasury.gov/ofac/downloads/sdall.zip' };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶sub new {
Subroutine new: called CC=12 SCAR=28.1
Pod undocumented
14 3 my $class = shift;
15  
16 3 my %args = @_;
17  
18 3 my $self = bless {}, $class;
19  
20 3 ▶ while ( my ( $key, $value ) = each %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 3 3 6
21 3 $self->{$key} = $value;
22 }
23  
24 # This logic is probably convoluted.
25  
26 3 my $databaseupdated;
27  
28 3 ▶ $databaseupdated = -1 unless ( -f $self->{p}->{database_file} );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"p"}{"database_file"} 2 1 3
29  
30 $self->{db}
31 = Data::OFAC::SDN::Schema->connect(
32 "dbi:SQLite:" . $self->{p}->{database_file} )
33 3 ▶ unless defined $self->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"db"} 3 0 3
34  
35 3 ▶ if ( defined $databaseupdated && $databaseupdated == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $databaseupdated and $databaseupdated == -1) 2 1 3
Condition66%2/3
defined $databaseupdated and $databaseupdated == -1
!ll&&!rl&&r
102
MC/DC50%1/2
defined $databaseupdated and $databaseupdated == -1
defined $databaseupdated $databaseupdated == -1
Truth table66%2/3
A and B
A= defined $databaseupdatedB= $databaseupdated == -1
ABresult
0X0
100
111
36  
37 # Force database update if no file is present.
38 2 $self->updateDatabase;
39 2 $databaseupdated = 1;
40  
41  
42 }
43  
44 3 $self->{_lastupdate} = (stat($self->{p}->{database_file}))[9];
45  
46 3 ▶ unless ( defined $databaseupdated || $self->{p}->{auto_update} eq '0' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $databaseupdated or $self->{"p"}{"auto_update"} eq 0) 1 2 3
Condition66%2/3
defined $databaseupdated or $self->{"p"}{"auto_update"} eq 0
l!l&&r!l&&!r
201
MC/DC50%1/2
defined $databaseupdated or $self->{"p"}{"auto_update"} eq 0
defined $databaseupdated $self->{"p"}{"auto_update"} eq 0
Truth table66%2/3
A or B
A= defined $databaseupdatedB= $self->{"p"}{"auto_update"} eq 0
ABresult
1X1
011
000
47  
48 1 my $lupdate = $self->{db}->resultset('Lastupdate')
49 ->search( {}, { order_by => 'LASTUPDATEDATETIME' } );
50 1 my $lastupdate = $lupdate->single;
51  
52 0 ▶ if ( defined $lastupdate
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastupdate and time - $lastupdate->lastupdatedatetime >= $self->{"p"}{"auto_update_frequency"} * 3600) 0 0 0
Condition0%0/3
defined $lastupdate and time - $lastupdate->lastupdatedatetime >= $self->{"p"}{"auto_update_frequency"} * 3600
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lastupdate and time - $lastupdate->lastupdatedatetime >= $self->{"p"}{"auto_update_frequency"} * 3600
defined $lastupdate time - $lastupdate->lastupdatedatetime >= $self->{"p"}{"auto_update_frequency"} * 3600
Truth table0%0/3
A and B
A= defined $lastupdateB= time - $lastupdate->lastupdatedatetime >= $self->{"p"}{"auto_update_frequency"} * 3600
ABresult
0X0
100
111
53 && ( time() - $lastupdate->lastupdatedatetime )
54 >= ( $self->{p}->{auto_update_frequency} * 3600 ) )
55 {
56 0 $self->updateDatabase();
57 }
58  
59 0 return $self;
60 }
61  
62 2 ▶ unless ( $self->{_status} && $self->{_status} eq 'dirty') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_status"} and $self->{"_status"} eq "dirty") 0 2 2
Condition33%1/3
$self->{"_status"} and $self->{"_status"} eq "dirty"
!ll&&!rl&&r
002
MC/DC0%0/2
$self->{"_status"} and $self->{"_status"} eq "dirty"
$self->{"_status"} $self->{"_status"} eq "dirty"
Truth table33%1/3
A and B
A= $self->{"_status"}B= $self->{"_status"} eq "dirty"
ABresult
0X0
100
111
63 0 $self->{_status} = 'clean';
64 }
65  
66 2 return $self;
67 }
68  
69 3 ▶sub updateDatabase {
Subroutine updateDatabase: called CC=2 SCAR=7.1
Pod undocumented
70 3 my $self = shift;
71  
72 3 my $lwp = LWP::UserAgent->new();
73 3 $lwp->timeout(10);
74 3 $lwp->env_proxy;
75  
76 3 my $rsp = $lwp->get(SDNURL);
77  
78 3 ▶ if ( $rsp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rsp->is_success) { } 0 3 3
79 0 $self->buildDatabase( $rsp->decoded_content );
80 }
81 else {
82 3 $self->{_status} = 'dirty';
83 }
84  
85 3 return 1;
86 }
87  
88 0 ▶sub buildDatabase {
Subroutine buildDatabase: not called CC=10 SCAR=47.0
Pod undocumented
89 0 my $self = shift;
90 0 my $zip = shift;
91  
92 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
93 0 $self->{tempdir} = $ENV{TEMP} . '/';
94 }
95 else {
96 # Seriously? WTF People?
97 0 ▶ $self->{tempdir} = ($ENV{TMPDIR} || '/tmp') . '/';
Condition0%0/2
$ENV{'TMPDIR'} || '/tmp'
l!l
00
MC/DC0%0/1
$ENV{'TMPDIR'} || '/tmp'
$ENV{'TMPDIR'}
Truth table0%0/2
A
A= $ENV{'TMPDIR'}
Aresult
11
00
98 }
99  
100 0 ▶ my $sdnzip = ( $self->{tempdir} || '/tmp/' ) . "sdn.zip";
Condition0%0/2
$self->{'tempdir'} || '/tmp/'
l!l
00
MC/DC0%0/1
$self->{'tempdir'} || '/tmp/'
$self->{'tempdir'}
Truth table0%0/2
A
A= $self->{'tempdir'}
Aresult
11
00
101  
102 0 ▶ open( my $fh, ">", $sdnzip )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $sdnzip or die 'Could not open file: ' . $! 0 0 0
103 or die "Could not open file: " . $!;
104  
105 0 binmode $fh;
106 0 print $fh $zip;
107 0 close $fh;
108  
109 0 ▶ my $sdnfiles = ( $self->{tempdir} || '/tmp/' );
Condition0%0/2
$self->{'tempdir'} || '/tmp/'
l!l
00
MC/DC0%0/1
$self->{'tempdir'} || '/tmp/'
$self->{'tempdir'}
Truth table0%0/2
A
A= $self->{'tempdir'}
Aresult
11
00
110  
111 0 my $ae = Archive::Extract->new( archive => $sdnzip );
112  
113 0 $ae->extract( to => $sdnfiles );
114  
115 # Files are not always cased properly, determine the actual files here
116  
117 0 map { $self->{_z}->{lc($_)} = $_ } @{$ae->files};
118  
119 0 map { $self->{_f}->{$_} = $self->{_z}->{$_} } qw{ sdn.csv sdn_comments.csv add.csv alt.csv};
120  
121 0 ▶ unless ( scalar keys %{$self->{_f}} == 4 ) { # Exactly four needed
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %{$$self{"_f"};} == 4) 0 0 0
122 0 $self->{_status} = 'dirty';
123 0 return; # Abort
124 }
125  
126 # Changed to set up a temporary database to make the swap out quick.
127  
128 $self->{dbh}
129 0 = DBI->connect( 'dbi:SQLite:' . ( $self->{p}->{database_file} ) . '_temp',
130 "", "", { RaiseError => 1 } );
131  
132 0 ▶ unless ( $self->{dbh} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}) 0 0 0
133 0 croak "Could not open the SQLite database: " . DBI->errstr();
134 }
135  
136 0 $self->SDNTable;
137 0 $self->ALTTable;
138 0 $self->ADDTable;
139 0 $self->LASTUPDATETable;
140 0 $self->SDNCOMMENTSTable;
141  
142 0 $self->{dbh}->disconnect();
143  
144 0 unlink($self->{p}->{database_file});
145 0 rename($self->{p}->{database_file}.'_temp', $self->{p}->{database_file});
146  
147 $self->{db}
148 = Data::OFAC::SDN::Schema->connect(
149 "dbi:SQLite:" . $self->{p}->{database_file} )
150 0 ▶ unless defined $self->{db};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"db"} 0 0 0
151  
152 0 $self->{csv} = Text::CSV->new();
153  
154  
155 # Process the SDN Table
156  
157 0 my @cols = qw{ent_num SDN_Name SDN_Type Program Title Call_Sign Vess_type
158 Tonnage GRT Vess_flag Vess_owner remarks};
159  
160 0 $self->processTable( $sdnfiles . $self->{_f}->{'sdn.csv'}, 'Sdn', @cols );
161  
162 0 @cols = qw{ent_num SDN_Name SDN_Type Program Title Call_Sign Vess_type
163 Tonnage GRT Vess_flag Vess_owner remarks};
164  
165 0 $self->processTable( $sdnfiles . $self->{_f}->{"sdn_comments.csv"},
166 'SdnComment', @cols );
167  
168 # Process the ADD Table
169  
170 @cols
171 0 = qw{ent_num add_num CityStateProvincePostalCode Country Add_remarks};
172  
173 0 $self->processTable( $sdnfiles . $self->{_f}->{"add.csv"}, 'Address', @cols );
174  
175 # Process the ALT Table
176  
177 0 @cols = qw{ent_num alt_num alt_type alt_name alt_remarks};
178  
179 0 $self->processTable( $sdnfiles . $self->{_f}->{"alt.csv"}, 'Alt', @cols );
180  
181 0 my $lu = $self->{db}->resultset('Lastupdate')->first;
182 0 ▶ $lu->delete if defined $lu;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lu 0 0 0
183  
184 0 $self->{_lastupdate} = time();
185  
186 $lu = $self->{db}->resultset('Lastupdate')
187 0 ->create( { lastupdatedatetime => $self->{_lastupdate} } );
188 0 $lu->update;
189  
190  
191  
192 0 return;
193  
194 }
195  
196 0 ▶sub processTable {
Subroutine processTable: not called CC=5 SCAR=34.0
Pod undocumented
197 0 my $self = shift;
198 0 my $file = shift;
199 0 my $table = shift;
200  
201 0 my @cols = @_;
202  
203 0 ▶ open( my $fh, "<", $file )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak('Could not open the SDN file: ' . $file . ' ( ' . $! . ' )') 0 0 0
204 or croak "Could not open the SDN file: " . $file . " ( " . $! . " )";
205  
206 0 ▶ while ( my $row = $self->{csv}->getline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{"csv"}->getline($fh)) 0 0 0
207  
208 my $r = $self->{db}->resultset($table)->create(
209 { map {
210 0 ▶ ( lc( $cols[$_] ) => (
Branch: 1 branch
BranchTrueFalseTotal
defined $row->[$_] && $row->[$_] eq '-0- ' ? : 0 0 0
Condition0%0/3
defined $row->[$_] and $row->[$_] eq "-0- "
!ll&&!rl&&r
000
MC/DC0%0/2
defined $row->[$_] and $row->[$_] eq "-0- "
defined $row->[$_] $row->[$_] eq "-0- "
Truth table0%0/3
A and B
A= defined $row->[$_]B= $row->[$_] eq "-0- "
ABresult
0X0
100
111
211 ( defined $row->[$_] && $row->[$_] eq '-0- ' )
212 ? undef
213 : $row->[$_]
214 )
215 )
216 } 0 .. ( scalar(@cols) - 1 )
217 }
218 );
219 0 $r->update;
220 }
221  
222 0 close $fh;
223  
224 0 return;
225 }
226  
227 0 ▶sub SDNTable {
Subroutine SDNTable: not called CC=1 SCAR=6.9
Pod undocumented
228 0 my $self = shift;
229  
230 0 $self->{dbh}->do("drop table if exists SDN");
231 0 $self->{dbh}->do( "
232 CREATE TABLE SDN (
233 ent_num NUMERIC NOT NULL,
234 SDN_Name TEXT,
235 SDN_Type TEXT,
236 Program TEXT,
237 Title TEXT,
238 Call_Sign TEXT,
239 Vess_type TEXT,
240 Tonnage TEXT,
241 GRT TEXT,
242 Vess_flag TEXT,
243 Vess_owner TEXT,
244 remarks TEXT,
245 sdn_name_phonetic_phonix TEXT,
246 title_phonetic_phonix TEXT
247 )" );
248 0 return;
249 }
250  
251 0 ▶sub SDNCOMMENTSTable {
Subroutine SDNCOMMENTSTable: not called CC=1 SCAR=6.9
Pod undocumented
252 0 my $self = shift;
253  
254 0 $self->{dbh}->do("drop table if exists SDN_COMMENTS");
255 0 $self->{dbh}->do( "
256 CREATE TABLE SDN_COMMENTS (
257 ent_num NUMERIC NOT NULL,
258 SDN_Name TEXT,
259 SDN_Type TEXT,
260 Program TEXT,
261 Title TEXT,
262 Call_Sign TEXT,
263 Vess_type TEXT,
264 Tonnage TEXT,
265 GRT TEXT,
266 Vess_flag TEXT,
267 Vess_owner TEXT,
268 remarks TEXT,
269 sdn_name_phonetic_phonix TEXT,
270 title_phonetic_phonix TEXT
271 )" );
272 0 return;
273 }
274  
275 0 ▶sub ADDTable {
Subroutine ADDTable: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my $self = shift;
277  
278 0 $self->{dbh}->do("drop table if exists \"ADDRESS\"");
279  
280 0 $self->{dbh}->do( "
281 CREATE TABLE \"ADDRESS\" (
282 ent_num NUMERIC NOT NULL,
283 add_num NUMERIC NOT NULL,
284 CityStateProvincePostalCode TEXT,
285 Country TEXT,
286 Add_remarks TEXT,
287 CityStateProvincePostalCode_phonetic_phonix TEXT,
288 Country_phonetic_phonix TEXT
289 )" );
290 0 return;
291 }
292  
293 0 ▶sub ALTTable {
Subroutine ALTTable: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my $self = shift;
295  
296 0 $self->{dbh}->do("drop table if exists ALT");
297 0 $self->{dbh}->do( "
298 CREATE TABLE ALT (
299 ent_num NUMERIC NOT NULL,
300 alt_num NUMERIC NOT NULL,
301 alt_type TEXT,
302 alt_name TEXT,
303 alt_remarks TEXT,
304 alt_name_phonetic_phonix TEXT
305 )" );
306 0 return;
307 }
308  
309 0 ▶sub LASTUPDATETable {
Subroutine LASTUPDATETable: not called CC=1 SCAR=6.9
Pod undocumented
310 0 my $self = shift;
311  
312 0 $self->{dbh}->do("drop table if exists LASTUPDATE");
313 0 $self->{dbh}->do( "
314 CREATE TABLE LASTUPDATE (
315 LASTUPDATEDATETIME TEXT
316 )" );
317 0 return;
318 }
319  
320 1 ▶sub search {
Subroutine search: called CC=5 SCAR=18.0
Pod undocumented
321  
322 1 my $self = shift;
323 1 my $table = shift;
324 1 my $string = shift;
325 1 my $resultset;
326  
327 1 ▶ if ( ((time() - $self->{_lastupdate})) >= ( $self->{p}->{auto_update_frequency} * 3600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $self->{"_lastupdate"} >= $self->{"p"}{"auto_update_frequency"} * 3600) 1 0 1
328 1 $self->updateDatabase();
329 }
330  
331 1 my $search = $self->{db}->resultset($table);
332  
333 1 for (@_) {
334  
335 1 my $result = $search->search_phonetic( { $_ => $string } );
336 1 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 1 0 1
337 1 ▶ while ( my $record = $result->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $result->next) 0 0 0
338 0 push @{$resultset},
339 {
340 entitynumber => $record->ent_num,
341 entityhit => $record->$_
342 };
343 }
344 }
345 }
346  
347 0 return $resultset;
348 }
349  
350 0 ▶sub searchAddress {
Subroutine searchAddress: not called CC=4 SCAR=30.0
Pod undocumented
351 0 my $self = shift;
352 0 my $csz = shift;
353 0 my $ctry = shift;
354  
355 0 ▶ if ( ((time() - $self->{_lastupdate})) >= ( $self->{p}->{auto_update_frequency} * 3600)) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $self->{"_lastupdate"} >= $self->{"p"}{"auto_update_frequency"} * 3600) 0 0 0
356 0 $self->updateDatabase();
357 }
358  
359 0 my $search = $self->{db}->resultset('Address');
360  
361 0 my $result = $search->search_phonetic(
362 { CityStateProvincePostalCode => $csz, Country => $ctry } );
363  
364 0 my $resultset;
365  
366 0 ▶ if ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result) 0 0 0
367 0 ▶ while ( my $record = $result->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $result->next) 0 0 0
368 0 push @{$resultset},
369 {
370 entitynumber => $record->ent_num,
371 0 entityhit => $self->{db}->resultset('Sdn')
372 ->search( ent_num => $record->ent_num )->first->SDN_Name
373 };
374 }
375  
376 }
377 0 return $resultset;
378  
379 }
380  
381 4 1;
↑ Summary OFAC.pm »