ASNMTAP

 view release on metacpan or  search on metacpan

lib/ASNMTAP/Asnmtap/Applications.pm  view on Meta::CPAN

      $rvOpen = open($EMAILREPORT, "$emailReport");

      if ($rvOpen) {
        while (<$EMAILREPORT>) { $emailMessage .= $_; }
        close($EMAILREPORT);

        if (defined $emailMessage) {
          use Sys::Hostname;
          my $subject = $prgtext .' / Daily status from '. hostname() .': '. get_csvfiledate();
          $returnCode = sending_mail ( $SERVERLISTSMTP, $SENDEMAILTO, $SENDMAILFROM, $subject, $emailMessage, $debug );
          print "Problem sending email to the '$APPLICATION' server administrators\n" unless ( $returnCode );
        }
      } else {
        print "Cannot open $emailReport to send email report information\n";
      }
    } else {
      print "$emailReport to send email report information doesn't exist\n";
    }
  }

  return ($returnCode);
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub sending_mail {
  my ( $serverListSMTP, $mailTo, $mailFrom, $mailSubject, $mailBody, $debug ) = @_;

  # look at Mail.pm !!!
  use Mail::Sendmail qw(sendmail %mailcfg);
  $mailcfg{port}     = 25;
  $mailcfg{retries}  = 3;
  $mailcfg{delay}    = 1;
  $mailcfg{mime}     = 0;
  $mailcfg{debug}    = ($debug eq 'T') ? 1 : 0;
  $mailcfg{smtp}     = $serverListSMTP;

  use Sys::Hostname;
  my %mail = ( To => $mailTo, From => $mailFrom, Subject => $mailSubject .' from '. hostname(), Message => $mailBody );
  my $returnCode = ( sendmail %mail ) ? 1 : 0;
  print "\$Mail::Sendmail::log says:\n", $Mail::Sendmail::log, "\n" if ($debug eq 'T');
  return ( $returnCode );
}

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

sub CSV_prepare_table {
  my ($path, $tableFilename, $extention, $tableName, $columnSequence, $tableDefinition, $logger, $debug) = @_;

  my $rv = 1;
  my $dbh = DBI->connect ("DBI:CSV:", "", "", {f_schema => undef, f_dir => $path, f_ext => $extention} ) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot connect to the database", $logger, $debug);

  if ( $rv ) {
    $dbh->{csv_tables}{$tableName}  = { file => $tableFilename };

    $dbh->{csv_null}                = 1;
    $dbh->{csv_allow_whitespace}    = 0;
    $dbh->{csv_allow_loose_quotes}  = 0;
    $dbh->{csv_allow_loose_escapes} = 0;

    $dbh->{csv_eol}                 = $\;
    $dbh->{csv_sep_char}            = ',';
    $dbh->{csv_quote_char}          = '"';
    $dbh->{csv_escape_char}         = '"';

    if ( -e "$path$tableFilename$extention" ) {
      @{$columnSequence} = ();

      use Text::CSV;
      my $csv = Text::CSV->new( { binary => 1 } );

      if ( open my $rvOpen, "<", "$path$tableFilename$extention" ) {
        if ( my $fields = $csv->getline ($rvOpen) ) {
          @{$columnSequence} = @$fields;
        } else {
          CSV_error_message (*EMAILREPORT, 'Failed to parse line: '. $csv->error_input, $debug);
        }

        close $rvOpen;
      } else {
        CSV_error_message (*EMAILREPORT, "Cannot open $path$tableFilename$extention to print debug information", $debug);
      }
    } else {
      my $create;

      foreach my $columnName ( @{$columnSequence} ) {
        $create .= "  $columnName " .$tableDefinition->{$columnName}. ",\n";
      }

      chomp $create; chop $create;
      my $sql = "CREATE TABLE $tableName (\n$create\n)";
      print "$sql\n\n" if ($debug);

      $dbh->do ($sql) or $rv = DBI_error_trap(*EMAILREPORT, "Cannot dbh->do: $sql", $logger, $debug);
    }

    if ( $debug ) {
      foreach my $columnName ( @{$columnSequence} ) { print "$columnName\n"; };
      print "\n";
    }

    return $dbh;
  } else {
    return undef;
  }
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sub CSV_insert_into_table {
  my ($rv, $dbh, $tableName, $columnSequence, $tableValues, $columnNameAutoincrement, $logger, $debug) = @_;

  if ( defined $dbh and $rv ) {
	my ($column, $placeholders, @values);

    foreach my $columnName ( @{$columnSequence} ) { 
	    $column .= $columnName .',';
      $placeholders .= '?,';
	    push ( @values, ( ( $columnName eq $columnNameAutoincrement ) ? '' : $tableValues->{$columnName} ) );
    }



( run in 1.560 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )