package Safelib::AL3::Loader ; use strict ; use warnings ; use Carp ; use Data::Dumper ; use Safelib::AL3::DB ; use Safelib::AL3::Parser ; my $parent_child = load_al3_parent_child_from_db() ; sub new { my ( $class ) = @_ ; my $self = bless {}, $class ; return $self ; } sub parse { my( $self, $al3_file ) = @_ ; $self->{al3_file} = $al3_file ; my $al3 = Safelib::AL3::Parser->new( al3_file => $al3_file ) ; $self->{al3_tree} = $al3->parse() ; #print Dumper $self->{al3_tree} ; $self->{file_date} = get_file_date( $al3_file ) ; } sub store_transactions { my( $self ) = @_ ; # my $file_date = $self->{file_date} ; my $al3_tree = $self->{al3_tree} ; foreach my $al3_msg ( @{$al3_tree->{'1MHG'}} ) { my $broker_num = $al3_msg->{contract_number} ; foreach my $al3_2trg ( @{$al3_msg->{CHILD}{'2TRG'}} ) { #print Dumper $al3_trans ; my $common_data = $self->get_common_data( $al3_2trg ) ; $common_data->{broker_num} = $broker_num ; my $trans_id = insert_al3_transaction( $common_data ) ; $common_data->{trans_id} = $trans_id ; $self->{common_data} = $common_data ; $self->store_al3_records( '2TRG', '', # top level type is blank $trans_id, # top level parent is trans $al3_2trg, ) ; } } } my %skip_rec_types = ( '2TCG' => 1, '3MTG' => 1, '5PAY' => 1, '5PPH' => 1, ) ; sub store_al3_records { my( $self, $rec_type, $parent_type, $parent_id, @records ) = @_ ; #print "TYPE [$rec_type]\n" ; return if $skip_rec_types{ $rec_type } ; # make an array ref into an array of records @records = @{$records[0]} if ref $records[0] eq 'ARRAY' ; #print Dumper \@records ; my $common_data = $self->{common_data} ; # loop over all the records and add in common and parent data foreach my $record ( @records ) { # store the common data into this record @{$record}{keys %{$common_data}} = values %{$common_data} ; # store the parent info into this record $record->{parent_type} = $parent_type ; $record->{parent_id} = $parent_id ; } my $has_children = $parent_child->{ $rec_type } ; unless( $has_children ) { insert_al3_records( $rec_type, @records ) ; return ; } foreach my $record ( @records ) { my $children = delete( $record->{CHILD} ) ; my $child_id = (insert_al3_records( $rec_type, $record ))->[0]; next unless $children ; #print Dumper $children ; foreach my $child_type ( sort keys %{$children} ) { my $child_recs = $children->{$child_type} ; $self->store_al3_records( $child_type, $rec_type, # parent type $child_id, # parent id, $child_recs ) ; } } } sub get_common_data { my( $self, $al3_2trg ) = @_ ; my $bpi = $al3_2trg->{CHILD}{'5BIS'}{CHILD}{'5BPI'} ; my $eff_year = '20' . substr( $bpi->{policy_effective_date}, 0, 2 ) ; return { policy_num => $bpi->{policy_number}, eff_year => $eff_year, file_date => $self->{file_date}, } ; } sub get_file_date { my( $file_name ) = @_ ; my( $file_date ) = $file_name =~ /(\d{8})/ or croak "bad csc file - no date: $file_name" ; $file_date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/ ; return $file_date ; } 1 ;