package Ernad::Indat::Add; use strict; use warnings; use base 'Ernad::Indat'; use Carp qw(confess); use Clone 'clone'; use Data::Dumper; use Ernad::Files; use Ernad::FileInfo; use Ernad::Store; use Ernad::Common; use Krichel::Shoti; use Krichel::File; our $e=$main::e; ## returns 1 if save needed ## this is to be used by external callers sub file { my $i=shift; my $amf_file=shift // confess "I need a file here"; my $do_keep_load=shift; my $e=$i->{'e'} // confess "I see no erimp here."; my $ref_e = ref $e; if(not $ref_e eq 'Ernad::Erimp') { confess "I need you erimp to be an Ernad::Erimp, not '$ref_e'"; } my $issuedate=&Ernad::FileInfo::issuedate($amf_file,'do_strict'); $i->{'issuedate'}=$issuedate; my $stage=&Ernad::FileInfo::stage($amf_file); $e->echo(__LINE__,"My stage is $stage."); if($stage eq 'namf') { return $i->namf($amf_file); } return $i->vypot($amf_file,$do_keep_load); } ## not meant for use by external callers sub namf { my $i=shift; my $amf_file=shift; my $e=$i->{'e'}; my $shoti_on_file; my $issuedate=$i->{'issuedate'}; my $json_file=$i->{'dir'}->{'indat'}."/namf/$issuedate.json"; if(not -f $json_file) { &Krichel::File::prepare($json_file); } ## this works for namf as there is only one amf_file to fill the json elsif(-M $json_file < -M $amf_file) { $e->echo(__LINE__,"I don't renew $json_file over $amf_file for namf"); return 0; } $i->{'file'}->{'add'}->{'json'}=$json_file; ## no need to load since we replace it. my $x=$i->{'i'}->{'d'}->{$i->{'issuedate'}}->{'namf'}; $x->{'d'}=clone($i->papids($amf_file)); #print Dumper $x->{'d'}; $x->{'v'}=&Ernad::Dates::mshoti($amf_file); my $now= &Krichel::Shoti::now; $i->{'i'}->{'v'}=$now; $x->{'v'}=$now; ## otherwise the caller has to save it. $e->echo(__LINE__,"I'm done with $amf_file."); $i->{'i'}->{'d'}->{$i->{'issuedate'}}->{'namf'}=clone($x); #print Dumper $i->{'i'}->{'d'}; #$i->save(); ## return 1 saves return 1; } ## not meant for use by external callers sub vypot { my $i=shift; my $amf_file=shift; my $do_keep_load=shift // ''; my $issuedate=$i->{'issuedate'} // confess "I should have this set by ->file"; my $stage=&Ernad::FileInfo::stage($amf_file,'do_strict'); my $repcode=&Ernad::FileInfo::repcode($amf_file,'do_strict'); my $shoti_on_file=&Ernad::FileInfo::shoti($amf_file,'do_strict'); ## no distinction between complete and incomplete here. my $json_file=$i->{'dir'}->{'vypot'}."/$repcode/$issuedate.json"; my $e=$i->{'e'}; $e->echo(__LINE__,"In Add.pm I see $stage."); if($do_keep_load) { $e->echo(__LINE__,"I am told to keep load ..."); } ## for the ->save $i->{'file'}->{'add'}->{'json'}=$json_file; $e->echo(__LINE__,"I set the json_file to $json_file"); my $json_version=0; if(-f $json_file) { if(not $i->{'i'} or not $do_keep_load) { $e->echo(__LINE__,"I load $json_file."); $i->{'i'}=&Ernad::Store::load($json_file); $json_version=$i->{'i'}->{'v'} // confess "I must have a version here."; $e->echo(__LINE__,"it is on version $json_version"); } } else { $e->echo(__LINE__,"I don't have $json_file, start empty."); ## shoti if(not $i->{'i'}->{'v'}) { $i->{'i'}->{'v'}=Krichel::Shoti::now(); &Krichel::File::prepare($json_file); } } my $stage_for_version=$stage; if($stage=~m|source|) { $e->echo(__LINE__,"I change stage from source to class."); $stage_for_version='class'; } ## start point in the structure my $x; $e->echo(__LINE__,"I have $repcode $issuedate $stage $shoti_on_file"); $x=$i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{$stage_for_version}->{$shoti_on_file}; my $v_in_json=$x->{'v'}; if(-f $json_file) { $v_in_json=$i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{$stage_for_version}->{$shoti_on_file}->{'v'}; if($v_in_json) { $e->echo(__LINE__,"The version of $amf_file in the json is $v_in_json"); } my $mshoti=&Ernad::Dates::mshoti($amf_file); if($v_in_json) { if(&Krichel::Shoti::compare($mshoti,$v_in_json)) { $e->echo(__LINE__,"I don't renew $json_file for $amf_file of $mshoti."); return 0; } } } ## force reload $x->{'v'}=&Krichel::Shoti::now; # delete $x->{'d'}; $e->echo(__LINE__,"I load $amf_file."); ## if it is a source file, collect the classing if($stage=~m|source|) { $e->echo(__LINE__,"I collect classing from source."); my $doc=&Krichel::File::load_xml($amf_file); #my $class=&Ernad::Rif::crit_and_class($amf_file); my $class=$e->{'x'}->crit_and_class($doc); ## presorting data? if($class->{'class'}) { ## shoti on the data, not sure where this is used. $x->{'d'}->{'shoti'}=$shoti_on_file; ## other data returned by the crit_and_class $x->{'d'}->{'papid'}=$class->{'class'}->{'papid'}; $x->{'d'}->{'crit'}=$class->{'crit'}; ## for uniformity $x->{'d'}->{'total'}=$class->{'total'}; $i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{'class'}->{$shoti_on_file} = clone($x); } else { ## may not be anythere there for source/us $e->echo(__LINE__,"I am returning without learning records form source."); return 0; } } else { $x->{'d'}=clone($i->papids($amf_file)); $x->{'d'}->{'shoti'}=$shoti_on_file; $i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{$stage}->{$shoti_on_file}=clone($x); } ## otherwise the caller has to save it. $e->echo(__LINE__,"I'm done with $amf_file."); return 1; } ## called by ->namf and ->vypot to get data sub papids { my $i=shift; my $amf_file=shift // confess "I need a file here"; my $e=$i->{'e'}; my $papids=$e->{'x'}->papids($amf_file,'array'); my $count=0; my $o; foreach my $papid (@$papids) { $count++; $o->{'papid'}->{$papid}=$count; } my $total=scalar @$papids; $o->{'total'}=$total; return $o; } sub save { my $i=shift; $i->{'i'}->{'v'}=&Krichel::Shoti::now; my $e=$i->{'e'}; my $file=$i->{'file'}->{'add'}->{'json'}; $e->echo(__LINE__,"I save $file"); &Ernad::Store::save($i->{'i'},$file); } sub clear { my $i=shift; delete $i->{'i'}; } 1;