package Ernad::Presort::Class; use strict; use warnings; use base ('Ernad::Presorter'); use Carp qw(confess); use Data::Dumper; use File::Path; use Ernad::Presort::Model; ## use Ernad::Presort::Vemli::Class; use Ernad::Presort::Vemli; use Ernad::Presort::State; binmode(STDOUT,':utf8'); ## returns the repcode sub setup { my $c=shift; #my $r=Ernad::Presort::Ranfi->new({'e'=>$c->{'e'}}); #$c->{'r'}=$r; $c->get_repcode(); my $v=Ernad::Presort::Vemli->new({'e'=>$c->{'e'}}); $c->{'v'}=$v; $c->{'v'}->motto('class'); my $m=Ernad::Presort::Model->new({'e'=>$c->{'e'}}); $c->{'m'}=$m; $c->{'t'}=Ernad::Presort::State->new({'e' => $c->{'e'}}); } sub setup_for_report { my $c=shift; my $repcode=$c->get_repcode() // confess "I can't get the repcode."; ## list the vemli files. my $e=$c->{'e'} // confess "I need an erimp here."; my $learn_dir=$e->{'dir'}->{'learn'}; ## set_dir can't be used here, as we use the report my $class_dir=$learn_dir.'/class/'.$repcode; if(not -d $class_dir) { $c->echo(__LINE__,"I make $class_dir."); mkpath($class_dir); } $c->{'v'}->setup_for_report(); $c->{'dir'}->{'class'}=$class_dir; $c->{'dir'}->{'vemli'}=$c->{'v'}->{'dir'}->{'vemli'} // confess "I need this."; $c->{'ranid'}=$c->{'m'}->last_ranid(); if(not $c->{'ranid'}) { return 0; } $c->{'file'}->{'model'}=$c->{'m'}->most_recent() // confess "I need a model here."; return $repcode; } ## main function for testing sub update_for_report { my $c=shift; my $report=shift // confess "I need a report here."; $c->setup(); ## has to be run for every report $c->setup_for_report($report); ## we have to reset the sources my $ranid=$c->{'m'}->last_ranid(); ## if there is no model, just exit if(not $ranid) { $c->echo(__LINE__,"I have no model."); return 0; } $c->{'ranid'}=$ranid; $c->set_sources(); my $vemli_files=$c->{'sources'}->{$report} // confess "I need sources here."; foreach my $vemli_file (sort keys %$vemli_files) { $c->build_file($vemli_file); } } sub set_sources { my $c=shift; if(not $c->{'v'}) { $c->setup(); } ## clear vemli files to avoid them building up $c->{'v'}->clear_files(); #my $repcode=$c->setup_for_report(); my $ranid=$c->{'ranid'} // confess "I need this set up."; my $restrict; $restrict->{'source'}="_$ranid"."_class"; $restrict->{'destin'}="_$ranid".".test"; my $sources=$c->get_sources($c->{'dir'}->{'vemli'},$c->{'dir'}->{'class'},$restrict); $c->{'sources'}->{$c->{'repcode'}}=$sources; return $sources; } ## sub build_file { my $c=shift; my $vemli_file=shift // confess "I need a file argument here."; if(not -f $vemli_file) { confess "I don't see your file $vemli_file"; } ## optional restriction to a single papid my $given_papid=shift // ''; ## check for setup if(not $c->{'v'}) { $c->setup(); } $c->setup_for_report() or return 0; my $issuedate=$c->deduct_issuedate_from_file($vemli_file); if(not $issuedate) { $c->echo(__LINE__,"I can't seen the issuedate on $vemli_file."); return 0; } ## FixMe: this should check whether the test is more recent than ## last file in the sent directory. Here we run the test whatever ## which is a waste of resources. my $glob; my $e=$c->{'e'} // confess "Where is my erimp?"; my $date=$e->{'f'}->issuedate($vemli_file); my $state=$c->{'t'}->find($date); if(($state eq 'done') or ($state eq 'prenatal')) { $c->echo(__LINE__,"I skip $vemli_file, because it is in state '$state'."); return; } my $ranid=$c->{'ranid'}; my $test_file=$c->{'dir'}->{'class'}."/$issuedate"."_$ranid.test"; $c->{'file'}->{'test'}=$test_file; $c->{'fh'}->{'test'}=IO::File->new("> $test_file"); ## We also assume that the learning vemlis are up-to-date. my $count_papers=0; ## set the vemli_file as the only to read $c->{'v'}->{'files'}={$vemli_file=>''}; my $p=$c->{'v'}->next_paper(); while(defined($p)) { ## end of a file reached if(not ref $p) { $p=$c->{'v'}->next_paper(); next; } if(not defined($p)) { last; } if(not $p) { next; } if($given_papid) { my $papid=$p->{'u'} // confess "I need a papid here."; if($papid ne $given_papid) { next; } } ## code to deal with paper $c->deal_with_paper($p); ## $p=$c->{'v'}->next_paper(); } $c->{'fh'}->{'test'}->close(); return $test_file; } ## same as in Model.pm, with just one line changed. sub deal_with_paper { my $c=shift; my $p=shift // confess "I need a paper here."; if(not $p->{'i'}) { confess "My papers needs a date."; } my $status='0'; my $fh=$c->{'fh'}->{'test'} // confess "Looks like I have no file."; #print Dumper $p; print $fh "$status ". $p->{'d'}.' # '.$p->{'u'}."\n"; } sub clear_files { my $c=shift; my $repcode=shift // confess "I need a repcode here."; my $do_for_real = shift // ''; ## check for setup #if(not $c->{'v'}) { # $c->setup(); #} my $e=$c->{'e'} // $main::e // confess "Where is my erimp?"; #$c->setup_for_report($repcode) or return 0; ## set_dir can't be used here, as we use the report my $class_dir=$e->{'dir'}->{'learn'}.'/class/'.$repcode; my $glob="$class_dir/*"; foreach my $fufi (glob($glob)) { my $issuedate = $c->deduct_issuedate_from_file($fufi) or next; if($e->{'s'}->stage_done($repcode,$issuedate,'sent')) { $c->echo(__LINE__,"I need to delete $fufi"); if($do_for_real) { unlink($fufi); } } else { $c->echo(__LINE__,"$repcode is not done for $issuedate"); } } } 1;