package Ernad::Issues; use strict; use warnings; use Carp qw(cluck longmess shortmess croak confess); use Data::Dumper qw(Dumper); use File::Copy; use Ernad::Constant; use Krichel::File; binmode(STDOUT,':utf8'); sub create { my $first_arg=shift; my $allport_dom; if(ref $first_arg eq 'XML::LibXML::Document') { $allport_dom=$first_arg; } ## assume it is a file elsif(-f $first_arg) { $allport_dom=&Krichel::File::load($first_arg) or confess "I could not parse the file $first_arg."; } else { confess "I don't like your argument $first_arg"; } my $e=shift // ''; if(not $e) { $e=$main::e // confess "I need an erimp"; } if(not $e) { $e=$main::e // confess "I need an erimp here"; } my $repcode=$e->{'repcode'} // confess "I need \$e->{'repcode'} here."; my $issuedate=$e->{'issuedate'} // confess "I need \$e->{'issuedate'} here."; my $papers_xp=$e->{'const'}->{'papers_xp'}; my @papers=$e->{'xpc'}->findnodes($papers_xp,$allport_dom); ## Createe a report issue from allport issue ## first, set report id my $rerc=$e->{'report'}->{$repcode} // confess "I don't know about the report $repcode"; 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 $ernad_ns=$e->{'const'}->{'ernad_ns'} // confess "I need a namespace here."; my $amf_ns=$e->{'const'}->{'amf_ns'} // confess "I need a namespace here."; my $issuedate_element=$allport_dom->createElementNS($ernad_ns,'issuedate'); $issuedate_element->appendText($issuedate); $collection_element->appendChild($issuedate_element); foreach my $paper (@papers) { my $haspart_element=$allport_dom->createElementNS($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); return $amf_doc; } sub install { my $first_arg=shift; my $doc=''; my $file=''; if(ref $first_arg eq 'XML::LibXML::Document') { $doc=$first_arg; } elsif(-f $first_arg) { $file=$first_arg; } else { confess "I don't like your argument '$first_arg'."; } my $destination=shift // confess "I need a destintation"; if($destination eq 'seeded') { if(not $file) { confess "I should have been called with a file for seeding."; } &Ernad::Namf::create_issue($file,'seeded'); } my $e=shift // ''; if(not $e) { $e=$main::e // confess "I need an erimp."; } my $repcode=$e->{'repcode'} // confess "I need a repcode set."; my $issuedate=$e->{'issuedate'} // confess "I need a issuedate set."; my $rerc=$e->{'report'}->{$repcode} // confess "I don't know report $repcode."; my $destination_dir=$rerc->{'dir'}->{$destination} // confess "I don't know about your destination $destination."; if($destination ne 'seeded') { # my $there=&Ernad::Common::find_file_for_issue($destination_dir,$issuedate); my $there=$e->{'d'}->find_file_for_issue($destination_dir,$issuedate); my $count_issues=scalar @{$there}; if($count_issues > 0) { my $first_file=$there->[0]; $e->echo(__LINE__,"I have a file $first_file, I will not create a new one."); return 0; } } ## set file name my $out_bana=$e->{'f'}->by_date($issuedate); my $out_fufi="$destination_dir/$out_bana"; if($file) { copy($file,$out_fufi); $e->echo(__LINE__,"I wrote $out_fufi.",2); } elsif($doc) { $doc->toFile($out_fufi,2); $e->echo(__LINE__,"I wrote $out_fufi.",2); } else { confess "Something is wrong here ;-(."; } } 1;