package Ernad::Indat::Sborn; use strict; use warnings; use base 'Ernad::Indat'; use Carp qw(confess); use Clone 'clone'; use Data::Dumper; use XML::LibXML; #use Ernad::Indat; #use Ernad::Dates; use Ernad::Files; #use Ernad::FileInfo; #use Ernad::Store; use Ernad::Pages; use Krichel::Shoti; our $e=$main::e; ## loads josen sub load { my $i=shift; my $what=shift // confess "I need to load what?"; ## save to use with output of document $i->{'what'}=$what; my $e=$i->{'e'}; my $in_json=$i->{'dir'}->{'indat'}."/$what.json"; my $out_xml=$i->{'dir'}->{'indat'}."/$what.xml"; if(not $in_json) { confess "I don't know $what to get for you."; } my $data={}; if(-f $in_json) { $data=&Ernad::Store::load($in_json); if(not $data->{'d'}) { $e->echo(__LINE__,"The data in $in_json has no ->{'d'}"); $i->{'i'}->{'d'}=$data; } } else { $e->echo(__LINE__,"I don't see the file $in_json."); } $i->{'i'}=$data; } sub to_xml { my $i=shift; my $what=shift // ''; my $e=$i->{'e'}; my $d=$i->{'i'}->{'d'}; if(not $d) { ## we still have to produce the file for the XSLT transformation $e->echo(__LINE__,"I have no data for $what."); } my $doc=XML::LibXML::Document->new('1.0','utf-8'); my $sborn_ele=$doc->createElement('sborn'); ## no update information needs setting $sborn_ele->appendText("\n "); foreach my $issuedate (reverse sort keys %$d) { $e->echo(__LINE__,"I work for $what on $issuedate.",10); if(not $issuedate=~m|^\d{4}-\d{2}-\d{2}$|) { confess "I can't deal with your issuedate '$issuedate'."; } if($what eq 'complete') { my $json_file=$i->{'dir'}->{'issues'}."/$issuedate.json"; if(-f $json_file) { $e->echo(__LINE__,"I see an in_labor $json_file, I skip work on $what."); next; } } elsif($what eq 'in_labor') { my $json_file=$i->{'dir'}->{'issues'}."/complete/$issuedate.json"; if(-f $json_file) { $e->echo(__LINE__,"I see a complete $json_file, I skip work on $what."); next; } } else { confess "I don't understand your what '$what'"; } my $di=$d->{$issuedate}; my $issue_ele=$doc->createElement('issue'); $issue_ele->setAttribute('date',$issuedate); $issue_ele->setAttribute('date_pretty',&Ernad::Dates::pretty_date($issuedate)); #$issue_ele->appendText("\n"); my $repcodes=$i->get_sort_by_snore($di); foreach my $repcode (@$repcodes) { my @sents=sort keys %{$di->{'report'}->{$repcode}->{'sent'}}; my $first_shoti=$sents[0]; $issue_ele->appendText("\n "); my $dir=$di->{$repcode}->{'sent'}->{$first_shoti}->{'d'}; my $vypot_ele=$doc->createElement('vypot'); $vypot_ele->setAttribute('repcode',$repcode); my $total=$dir->{'total'}; $vypot_ele->setAttribute('total',$total); my $sent_shoti=$dir->{'sent'} // $first_shoti; my $sent_pretty=&Krichel::Shoti::pretty($sent_shoti); $vypot_ele->setAttribute('sent',$sent_pretty); $issue_ele->appendChild($vypot_ele); ## was in code but unclear as to why delete $di->{'report'}->{$repcode}->{'sent'}->{$first_shoti}; } $issue_ele->appendText("\n "); my $missing=''; foreach my $repcode (sort keys %{$di->{'about'}->{'snorlabo'}}) { my $vypot_ele=$doc->createElement('snorlabo'); $vypot_ele->setAttribute('repcode',$repcode); $issue_ele->appendChild($vypot_ele); $issue_ele->appendText("\n "); $missing.="$repcode "; } chop $missing; $issue_ele->setAttribute('missing',$missing); $sborn_ele->appendChild($issue_ele); $sborn_ele->appendText("\n "); } $doc->setDocumentElement($sborn_ele); ## save if we know what if($what) { my $out_file=$i->{'file'}->{'xml'}->{$what}; if(not $out_file) { confess "I don't know about your xml out file for '$what'"; } ## FixMe: this should be changed to a function that uses text::diff &Ernad::Files::save_if_diff($out_file,$doc); $e->echo(__LINE__,"I saved $out_file",1); } else { $e->echo(__LINE__,"I did not save $what xml",1); } return $doc; } sub get_sort_by_snore { my $i=shift; my $di=shift; my $type=shift // 'array'; my $e=$i->{'e'}; my $l; my @codes=keys %{$di->{'report'}} // confess "I need thes here."; ## FixMe: there should be a standard way to do that my $repcodes; my $count=0; my $snore=$di->{'about'}->{'snore'}; if(not $snore) { confess "I have no snore data."; } foreach my $code (keys %{$di->{'report'}}) { if(not $snore->{$code}) { $e->echo(__LINE__,"I don't have a snore for $code."); next; } $repcodes->[$count++]=$code; } my $out; $count=0; foreach my $repcode (sort {$snore->{$a} <=> $snore->{$b}} @$repcodes) { if($type eq 'array') { $out->[$count++]=$repcode; } elsif($type eq 'hash') { $out->$repcode=1; } else { confess 'My type argument has to be "array" (default) or "hash".'; } } return $out; } sub show_all { my $i=shift; foreach my $what (@{$i->{'whats'}}) { $i->show($what); } } ## sub show { my $i=shift; my $what=shift // confess "Tell me what to show, in_labor or complete."; my $xml_file=$i->{'file'}->{'xml'}->{$what}; my $html_file=$i->{'file'}->{'html'}->{$what}; if(-f $xml_file and -f $html_file and -M $html_file <= -M $xml_file) { $e->echo(__LINE__,"$html_file is younger than $xml_file, I skip"); return 0; } my $e=$i->{'e'}; ## doc to show my $in_doc=shift // ''; ## out doc my $doc; ## the xml to transform my $xml; my $update=&Ernad::Dates::pretty_time(time); ## better use now as to be able to invoque show before the save of the XML ## file #if(-f $xml_file) { # my $mtime=&Ernad::Dates::mtime($xml_file); # $update=&Ernad::Dates::pretty_time($mtime); #} my $params={'update' => "'$update'",'type'=>"'$what'",'mod'=>"'changed'"}; if($in_doc) { $doc=$e->{'t'}->transform($doc,'sborn',$params); } else { $i->load($what); $e->echo(__LINE__,"I am runnig ->to_xml()"); $i->to_xml($what); if(not -f $xml_file) { confess "The file $xml_file must now be here."; } my $update=&Ernad::Dates::mtime($xml_file); $doc=$e->{'t'}->transform_file($xml_file,'sborn',$params); } ## FixMe: This is awkward. It must be read from an xslt parameter my $file_name; if($what eq 'in_labor') { $file_name='current'; } elsif($what eq 'complete') { $file_name='past'; } else { confess "I don't get a sense of your \$what '$what'"; } my $out_file_html=$e->{'dir'}->{'blatt'}."/issues/$file_name.html"; &Ernad::Pages::install($doc,$out_file_html,$e->{'testing'}); } 1;