package Ernad::Namf; use strict; use warnings; use Carp qw(cluck longmess shortmess croak confess); use Data::Dumper; use Ernad::Common; use Ernad::Dates; use Ernad::Rix; use Krichel::File; ## we set this as global states my $e; #$e=$main::e or confess "I have no erimp."; my $l; my $o_k_n; my $issuedate; ## this makes $e visible sub setup { $e=$main::e or confess "I have no erimp."; } #sub namf_precedents { # $e=$main::e or confess "I have no erimp."; # my $namf_dir=$e->{'dir'}->{'namf'} // confess "I have no namf_dir"; # my $namf_dates=&Ernad::Common::find_dates_in_dir($namf_dir); # my $count=1; # $e->{'namf_prec'}={}; # while(defined $namf_dates->[$count]) { # my $past=$namf_dates->[$count-1]; # my $current=$namf_dates->[$count]; # $e->{'namf_prec'}->{$current}=$past; # $count++; # } # return $e->{'namf_prec'}; #} sub create_issue { $e=$main::e or confess "I have no erimp."; my $in=shift // confess "I need an in_file here"; my $type=shift // confess "I need a type 'seeded' or 'presorted'"; my $in_file=''; if(not ref($in) eq 'XML::LibXML::Document') { $in_file=$in; if(not -f $in_file) { confess "I can't see $in_file."; } $in=&Krichel::File::load($in_file); } my $repcode=shift // ''; if(not $repcode) { $repcode=$e->{'repcode'}; } if(not $repcode) { confess "I don't see the report."; } my $issuedate=shift // ''; if(not $issuedate) { $issuedate=$e->{'issuedate'}; } if(not $issuedate) { confess "I don't see the issuedate."; } $e->echo(__LINE__,"I have the report $repcode and the issuedate $issuedate."); ## creates $rerc->{'amf_doc'} &create_issue_amf($in); my $rerc=$e->{'report'}->{$repcode}; my $amf_doc=$rerc->{'amf_doc'}; ## Save report to unsorted my $source_dir = $rerc->{'dir'}->{'source_dir'}; my $out_dir= $rerc->{'dir'}->{$type}; # // confess "I need an source directory here."; if(not -d $out_dir) { die "I don't seed the directory $out_dir."; } my $rif=$out_dir.'/'.$e->{'f'}->by_date($issuedate); &Ernad::Files::save_xml_with_temp_and_time($rif,$rerc->{'amf_doc'}); $e->echo(__LINE__,"I did write $rif."); undef($rerc->{'amf_doc'}); } sub create_issue_amf { my $in=shift // confess "I need an input here"; my $in_ref=ref($in); if(not $in_ref eq 'XML::LibXML::Document') { if(not -f $in) { confess "I can not open the file $in"; } $in=&Krichel::File::load($in); } $e=$main::e or confess "I have no erimp."; my $repcode=$e->{'repcode'} // confess "I have no repcode."; my $xpc=$e->{'xpc'} // confess "I need an xpc here."; ## Create a report issue from allport issue my $rerc=$e->{'report'}->{$repcode} // confess 'I need a rerc.'; ## if the amf_doc is already defined, return it if(defined($rerc->{'amf_doc'})) { return $rerc->{'amf_doc'}; } my $repdoc=$rerc->{'repdoc'} // "I need a repdoc for $repcode."; if(not ref($repdoc)) { ## should not have to do this again here $repdoc=$e->{'r'}->load($repcode); if(not ref($repdoc)) { confess "I need a repdoc to continue"; } $rerc->{'repdoc'}=$repdoc; } my $report_id=$rerc->{'id'} // 'I need a report id here.'; my $issuedate=$e->{'issuedate'} // confess "I need an issuedate here."; my $report_issue_handle = $report_id.'/'.$issuedate; my $amf_element=$repdoc->documentElement(); my $collection_element; ## post-Osborne if(not $e->{'repdoc'}->{$repcode}) { ## temporary if if($e->{'r'}) { $e->{'r'}->load($repcode); } } if($e->{'repdoc'}->{$repcode}) { $collection_element=$e->{'repdoc'}->{$repcode}->documentElement->cloneNode(1); ## this should be the case post-Osborne; if($collection_element->nodeName eq 'amf') { $collection_element=$xpc->find('/amf:amf/amf:collection[1]',$collection_element)->[0]; } } else { #$collection_element=&Ernad::Common::get_collection_element($repdoc); $collection_element=$e->{'x'}->collection_element($repdoc); $collection_element->setAttribute('id', $report_issue_handle); } ## add the issue date to the data my $ernad_ns=$e->{'const'}->{'ernad_ns'}; my $issuedate_element=$repdoc->createElementNS($ernad_ns,'issuedate'); $issuedate_element->appendText($issuedate); $collection_element->appendChild($issuedate_element); ## find the ids that are already there my $already_there_ids={}; ## this seem silly but it was written as a duplicatin work about about 1 April 2016 #foreach my $text_element ($xpc->findnodes("//amf:haspart/amf:text",$in)->get_nodelist()) { # my $id=$text_element->getAttribute('ref') or next; # $already_there_ids->{$id}=1; #} foreach my $haspart_element ($xpc->findnodes("//amf:haspart[amf:text]",$in)->get_nodelist()) { my $id; foreach my $text_element ($xpc->findnodes("./amf:text",$haspart_element)) { $id=$text_element->getAttribute('ref') or die; } if(defined($already_there_ids->{$id})) { $e->echo(__LINE__,"$id is there",10); next; } else { $e->echo(__LINE__,"$id is NOT there",10); } $collection_element->appendChild($haspart_element); } #my $amf_doc=$e->get_amf_doc(); my $amf_doc=$e->{'x'}->amf_doc(); $amf_element=$amf_doc->documentElement(); $amf_element->appendChild($collection_element); $rerc->{'amf_doc'}=$amf_doc; return $amf_doc; } ## from Ernad::Seed sub sort_by_score { my $repcode=shift; my $issuedate=shift; my $max_papers=shift // ''; #my $doc=Krichel::File::load($amf_file) my $e=$main::e // confess 'I need a erimp here'; #my $repcode=$e->{'repcode'} // confess "I need a repcode here"; my $rerc=$e->{'report'}->{$repcode} // confess "I need a rerc here"; my $tsoin_file=$rerc->{'file'}->{'tsoin_amf'}; ## check that we have the right data my $tsoin_bana=basename($tsoin_file); my $date_on_file=substr($tsoin_bana,length('tsoin_'),10); if(not $date_on_file eq $issuedate) { confess "I don't know the issuedate $issuedate or $date_on_file?"; } my $amf_doc=Krichel::File::load($tsoin_file) or confess "I could not load the tsoin_file $tsoin_file to sort"; my $seed_time=$rerc->{'seed_time'} // confess "I need a seed time here."; my $criterion='seed_'.$rerc->{'seed_time'}; my $score=$rerc->{'score'} // confess "I need a score here."; my $seed_date=$rerc->{'seed_date'} // confess "I need a seed_date here."; my $out_amf_file=$rerc->{'file'}->{'out_amf'} // confess "I need an output amf file defined here."; if(-f $out_amf_file) { $e->echo(__LINE__,"I found $out_amf_file. I won't renew it."); return $out_amf_file; } $amf_doc=&Ernad::Sort::inject_sort($amf_doc,$score,$criterion); ## 2018-11-14 does not truncate $amf_doc=&Ernad::Sort::sort_by_criterion($amf_doc,$criterion,'d'); ## truncate if($max_papers) { my $t=Ernad::Rix->new(); $amf_doc=$t->truncate_by_delete($amf_doc,$max_papers); $amf_doc->toFile($out_amf_file,2); } else { $amf_doc->toFile($out_amf_file,2); } return $out_amf_file; } ## from Learn.pm sub create_unsorted_learn_pm { my $l=shift; my $o_k_n=shift; if(not defined($o_k_n)) { confess "The o_k_n parameter is missing on create_unsorted"; } if(($o_k_n ne 'overwrite') and ($o_k_n ne 'keep') and ($o_k_n ne 'no_write')) { confess 'o_k_n has to be overwrite or keep or no_write'; } if(not ($o_k_n eq 'no_write')) { if($l->{'e'}->{'s'}->is_futile($l->{'repcode'},$l->{'issuedate'})) { return 0; } } if($o_k_n eq 'keep') { my $rerc=$l->{'rerc'} or die "no rerc"; my $us_dir= $rerc->{'dir'}->{'unsorted'} or die; my $us_rif_glob="$us_dir/*$issuedate*"; my @files=glob($us_rif_glob); if(scalar @files) { my $us_rif=$files[$#files]; $l->{'e'}->echo(__LINE__,"There is an unsorted file $us_rif, I won't make a new one.",1); $l->{'us_rif'}=$us_rif; $l->{'unsorted_doc'}=&Krichel::File::load($us_rif); return 1; } } $l->check_dapor(); my $allport_dom=$l->{'allport_dom'} or die "I need the allport_dom here."; my @papers=@{$l->{'papers'}}; if(not scalar(@papers)) { die "The issue of $issuedate seems to have no papers"; } my $e=$l->{'e'}; my $xpc=$l->{'xpc'}; ## Createe a report issue from allport issue ## first, set report id my $rerc=$l->{'rerc'} or die "no rerc"; my $repdoc=$rerc->{'repdoc'} or die; #my $to_string=$repdoc->toString(); #$l->{'e'}->echo(__LINE__,"here is repdoc\n$to_string"); my $report_id=$rerc->{'id'} or die; my $report_issue_handle = $report_id.":".$issuedate; my $amf_element=$repdoc->documentElement(); #my $collection_element=&Ernad::Common::get_collection_element($repdoc); my $collection_element=$e->{'x'}->collection_element($repdoc); $collection_element->setAttribute( 'id', $report_issue_handle ); ## add the issue date to the data my $issuedate_element=$allport_dom->createElementNS($l->{'ernad_ns'},'issuedate'); $issuedate_element->appendText($issuedate); $collection_element->appendChild($issuedate_element); foreach my $paper (@{$l->{'papers'}}) { my $haspart_element=$allport_dom->createElementNS($l->{'amf_ns'},'haspart'); $haspart_element->appendChild($paper->cloneNode(1)); $collection_element->appendChild($haspart_element); } ## remove existing ones if set to overwrite #my $amf_doc=$e->get_amf_doc(); my $amf_doc=$e->{'x'}->amf_doc(); $amf_element=$amf_doc->documentElement(); $amf_element->appendChild($collection_element); $l->{'unsorted_doc'}=$amf_doc; #$amf_doc->toFile('/tmp/o.xml'); if($o_k_n eq 'no_write') { $l->{'e'}->echo(__LINE__,"I give up on creating an unsorted issue because I have no_write set"); return; } ## Save report to unsorted my $source_dir = $rerc->{'dir'}->{'source_dir'}; my $us_dir= $rerc->{'dir'}->{'unsorted'} or die; my $us_rif=$us_dir.'/'.$e->{'f'}->by_date($issuedate); $l->{'e'}->echo(__LINE__,"Copying allport issue to $us_rif.",1); $l->{'us_rif'}=$us_rif; ## transit: change the unsorted report issue document, mainly to ## address futli report pointers my $transit_xslt_file=$e->{'dir'}->{'style'}.'/transit'.$e->{'const'}->{'xsl_ext'}; if(-f $transit_xslt_file) { $amf_doc=$e->{'t'}->transform($amf_doc,'transit'); $l->{'unsorted_doc'}=$amf_doc; } $amf_doc->toFile($us_rif,1); $l->{'e'}->echo(__LINE__,"I write $us_rif."); if($o_k_n eq 'overwrite') { $l->delete_already_there('unsorted',$us_rif); } ## not yet implemented &Ernad::Adverts::inject_adverts_into_rif($us_rif,$l->{'e'}); $l->{'e'}->echo(__LINE__,"I placed adverts into $us_rif."); } ## from Learn.pm sub delete_already_there_learn_pm { my $l=shift; my $us_ps=shift; ## except the file, to delete all others after creation my $except=shift // ''; if(($us_ps ne 'unsorted') and ($us_ps ne 'presorted')) { die 'us_ps has to be unsorted or presorted.'; } $l->check_dapor(); my $repcode; ### bad here $l->{'e'}->echo(__LINE__,"I delete $repcode $issuedate $us_ps except $except",1); my $rerc=$l->{'rerc'} or die "I need the allport_dom here."; my $dir=$rerc->{'dir'}->{$us_ps} or die; my @already_there_files=glob("$dir/$issuedate*"); foreach my $file (@already_there_files) { if($except and $file eq $except) { $l->{'e'}->echo(__LINE__,"I keep except $file",3); next; } $l->{'e'}->echo(__LINE__,"I remove $file",3); unlink $file; } } ## from Learn.pm sub is_already_there_learn_pm { my $l=shift; my $us_ps=shift; if(($us_ps ne 'unsorted') and ($us_ps ne 'presorted')) { die 'us_ps has to be unsorted or presorted.'; } ## do we have report and issuedate set $l->check_dapor(); my $rerc=$l->{'rerc'} or die "I need the allport_dom here."; my $dir=$rerc->{'dir'}->{$us_ps} or die; my @already_there_files=glob("$dir/$issuedate*"); foreach my $file (@already_there_files) { my $fufi="$file"; $l->{'e'}->echo(__LINE__,"I found $fufi"); } if(scalar(@already_there_files)) { return 1; } return 0; } 1;