package Ernad::Main; use strict; use warnings; use Carp qw(confess); use Data::Dumper; use File::Slurper; use IO::File; use Ernad::Login; use Ernad::ReportState; use Ernad::IssueSelect; use Ernad::Papers; use Ernad::Sorting; use Ernad::Final; sub handle_query { my $wepas = shift(); my $e=shift // ''; if(not $e) { $e=$Erimp::e // $main::e; } if(not $e) { confess "Where is my erimp?"; } my $mode = $wepas->{'Mode'} // ''; if($e->{'testing'}) { my $log_file=$e->{'dir'}->{'test'}.'/handle_query.log'; #open(L,"> $log_file") or confess "I can't open the log $log_file."; $e->{'testlog'} = IO::File->new(); $e->{'testlog'}->open("> $log_file"); $e->{'testlog'}->binmode('utf8'); } if(not $mode) { if($e->{'testing'}) { my $dump_wepas=Dumper $wepas; print($e->{'testlog'}, "I have no Mode wepa,\n $dump_wepas I show Login\n"); # close L; } return &Ernad::Login::show(); } my $repcode = $wepas->{'Report'}; ## 2020-07-13 remove space $repcode=~s|\s+||g; ## 2020-07-30 feed change back to wepas $wepas->{'Report'} = $repcode; if(not $repcode) { #my $print_wepas = Dumper $wepas; #confess "I see no Report parameter in wepas $print_wepas."; my $login_failure="I have lost track of your report."; return &Ernad::Login::show($login_failure); } if($mode eq "Login") { ## If report is not set then reshow login if(not $repcode ) { return &Ernad::Login::show(); } ## incorrect login case my $password=$wepas->{'Password'}; ## returns 0 on success my $login_failure=&Ernad::Login::process( $repcode, $password ); if($login_failure) { return &Ernad::Login::show($login_failure); } } ## returns state and string if($e->{'testing'}) { print($e->{'testlog'}, "I process the parameters\n"); my $dump_wepas=Dumper $wepas; print($e->{'testlog'}, $dump_wepas); } my $process_done=process($wepas, $e) // ''; my $state=$process_done->{'state'}; my $string=$process_done->{'string'}; if($e->{'testing'}) { print($e->{'testlog'}, "process_done return\n"); print($e->{'testlog'}, "state is $state\n"); if($string) { print($e->{'testlog'}, "sting is $string\n"); } } ## show current state if($state) { $string=show( $wepas, $state ); if($e->{'testing'}) { print($e->{'testlog'}, "process_done now shows the state $state\n"); #close L; } } elsif($e->{'testing'}) { print($e->{'testlog'}, "process_done does not show anything as there is no state.\n"); #close L; } return $string; } sub show { ## Retrieve reference to the POST parameters hash my $wepas = shift(); my $rs = shift(); ## State name my $stage = $rs->stage(); my $e=$Erimp::e // $main::e; my $string=''; my $repcode=$rs->{'repcode'} // ''; my $rerc=$e->{'report'}->{$repcode}; my $trace=$rerc->{'trace'}; my $trace_count=$rerc->{'trace_count'}; if(not defined($trace_count)) { $rerc->{'trace_count'}=0; } else { $rerc->{'trace_count'}++; } $trace->{'trace_count'}->{'stage'}=$stage; if($e->{'testing'}) { open(L,'> '.$e->{'dir'}->{'test'}.'/show.log'); print($e->{'testlog'}, "in show, stage is $stage\n"); } if ( $stage eq $e->{'const'}->{'source_dir'} or $stage eq $e->{'const'}->{'sent_dir'}) { ## No state forces issue selecting stage # print $e->{'testlog'} "showing issues\n"; close L; ## desperation: try to delete the selected_doc #if( not exists( $$wepas{"Back"} ) ) { # my $e=$rs->{'e'} or die 'erimp not defined'; # my $repcode=$rs->{'repcode'}; # my $rerc=$e->{'report'}->{$repcode}; # if(defined($rerc->{'selected_doc'})) { # delete $rerc->{'selected_doc'}; # $rerc->{'selected_doc'}=undef; # } #} return &Ernad::IssueSelect::Show( $wepas, $rs ); } elsif ( $stage eq $e->{'const'}->{'created_dir'} or $stage eq $e->{'const'}->{'selected_dir'} ) { if($e->{'testing'}) { print($e->{'testlog'}, "stage is $stage\n"); print($e->{'testlog'}, "showing created/selected\n"); #close L; } return &Ernad::Papers::Show( $wepas, $rs ); } elsif ( $stage eq $e->{'const'}->{'filtered_dir'} ) { if($e->{'testing'}) { print($e->{'testlog'}, "showing sorting\n"); #close L; } return &Ernad::Sorting::Show( $wepas, $rs ); } else { if($e->{'testing'}) { print($e->{'testlog'}, "showing final\n"); #close L; } return &Ernad::Final::Show( $wepas, $rs ); } if($e->{'testing'}) { print($e->{'testlog'}, "at the end of the show\n"); #close L; } } # Processes input data and returns stage info sub process { # Sets ReportState object analyzing $wepas my $wepas = shift(); my $e=shift; my $mode = $wepas->{"Mode"} or die "no Mode wepa"; my $repcode = $wepas->{"Report"} or die "no Report wepa"; ## Alpesh Thakker's problem 2020-05-04 $repcode=~s|^\s+||; $repcode=~s|\s+$||; my $password = $wepas->{"Password"}; ## write the webpas to a file in testing if($e->{'testing'}) { my $out_file=$e->{'dir'}->{'test'}.'/wepas'; my $wepas_dump=Dumper $wepas; &File::Slurper::write_text($out_file,$wepas_dump); } if($e->{'testing'}) { open(L,'> '.$e->{'dir'}->{'test'}.'/process.log'); ## Get session stage print($e->{'testlog'}, "finding a report stage\n"); print($e->{'testlog'}, "report is $repcode\n e is $e\n"); } my $rs = new Ernad::ReportState( $repcode, $e ); my $d_rs=Dumper $rs; # open(D,"> /tmp/drs"); # print D $d_rs; # close D; $rs->load(); ## fix stage misaligment if($wepas->{'Stage'} and ($rs->stage ne $wepas->{'Stage'})) { $rs->set_stage($wepas->{'Stage'}); ## $stage=$rs->{'stage'} // confess "I need a stage defined here."; } my $stage = $rs->stage(); my $file = $rs->file(); if($e->{'testing'}) { print($e->{'testlog'}, "line is ", __LINE__ , "\n"); print($e->{'testlog'}, "Current file: $file\n"); print($e->{'testlog'}, "State mode: $mode\n"); print($e->{'testlog'}, "Path: ".$rs->{'path'},"\n"); print($e->{'testlog'}, "File: ",$rs->file(),"\n"); } my $string; ## fixme: introduced to avoid perl gripe in the apache log if(not defined($rs->{'state'})) { $rs->{'state'}=''; } if($e->{'testing'}) { print($e->{'testlog'}, "mode is $mode\n"); print($e->{'testlog'}, "stage is $stage\n"); } ## Update the stage if ( $stage eq $e->{'const'}->{'source_dir'} && $mode ne "Issue" ) { if($e->{'testing'}) { print($e->{'testlog'}, "do nothing\n"); } } elsif ( $mode eq "Issue" ) { #nep-all issue selecting if($e->{'testing'}) { print($e->{'testlog'}, "do Issue\n"); } #die "mode $mode stage $stage"; $string = Ernad::IssueSelect::Process( \%$wepas, $rs ); ## process again if the report state is sent, which means an ## issue was deleted if($rs->{'state'} eq 'sent') { $string = Ernad::IssueSelect::Process( \%$wepas, $rs ); } } elsif ( $mode eq "Papers" ) { if($e->{'testing'}) { print($e->{'testlog'}, "do Papers\n"); } $string = Ernad::Papers::Process( \%$wepas, $rs ); if($e->{'testing'}) { my $stage=$rs->{'stage'} // die "I need a stage here"; my $out_file=$e->{'dir'}->{'test'}.'/stage_at_end_of_process'; &File::Slurper::write_text($out_file,"$stage\n"); } if($string == -2) { $string = Ernad::IssueSelect::Show( \%$wepas, $rs ); } if($string == '-1') { my $stage=$rs->{'stage'}; #die $stage; } } elsif ( $mode eq "Sorting" ) { if($e->{'testing'}) { print($e->{'testlog'}, "do Sorting\n"); } $string = Ernad::Sorting::Process( \%$wepas, $rs ); } elsif ( $mode eq "Final" ) { if($e->{'testing'}) { print($e->{'testlog'}, "do Final\n"); } $string = Ernad::Final::Process( \%$wepas, $rs ); } else { # print L "do NOTHING\n"; } if($e->{'testing'}) { my $dump_state=Dumper $rs; print($e->{'testlog'}, "after doing rs is\n"); print($e->{'testlog'}, 'stage is ', $rs->{'stage'}, "\n"); print($e->{'testlog'}, 'file is ', $rs->{'file'}, "\n"); print($e->{'testlog'}, 'repcode is ' => $rs->{'repcode'},"\n"); print($e->{'testlog'}, 'path is ', $rs->{'path'},"\n"); my $rix=$rs->{'rix'}; if(ref $rix) { my $rix_string=$rs->{'rix'}->toString(); $rix_string=~s|{'testlog'}->close(); my $out_file=$e->{'dir'}->{'test'}.'/dump_state'; &File::Slurper::write_text($out_file,$dump_state); } my $out; $out->{'string'}=$string; $out->{'state'}=$rs; return $out; } 1;