########################################################################
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
#
# Module name   : test_cqconnection.pl
# Module type   : Makefile system
# Compiler(s)   : Perl
# Environment(s): jats
#
# Description   : Test the connectivity to the ClearQuest Database
#
# Usage         : See POD
#
#......................................................................#

require 5.008_002;
use strict;
use warnings;

use JatsError;

use Pod::Usage;                             # required for help support
use Getopt::Long;
use JatsRmApi;

use DBI;
use Cwd;

#
#   Global data
#
my $VERSION      = "1.0.0";                 # Update this
my $opt_debug    = $ENV{'GBE_DEBUG'};        # Allow global debug
my $opt_verbose  = $ENV{'GBE_VERBOSE'};      # Allow global verbose
my $opt_help = 0;

#
#   Release Manager Connection Information
#   Deployment manager Connection information
#
my $CQ_URL = $ENV{GBE_CQ_URL};
my $CQ_DB;

#-------------------------------------------------------------------------------
# Function        : Mainline Entry Point
#
# Description     :
#
# Inputs          :
#
my $result = GetOptions (
                "help+"         => \$opt_help,              # flag, multiple use allowed
                "manual:3"      => \$opt_help,
                "verbose:+"     => \$opt_verbose,           # flag, multiple use allowed
                );

                #
                #   UPDATE THE DOCUMENTATION AT THE END OF THIS FILE !!!
                #

#
#   Process help and manual options
#
pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);
pod2usage(-verbose => 1) if ($opt_help == 2 );
pod2usage(-verbose => 2) if ($opt_help > 2);

#
#   Configure the error reporting process now that we have the user options
#
ErrorConfig( 'name'    =>'CQTEST',
             'verbose' => $opt_verbose,
            );


#
#   Open the connection and display some data
#
getClearQuestData();
Message "Test Complete\n";
exit;


#-------------------------------------------------------------------------------
# Function        : getClearQuestData
#
# Description     : Extract Info from the DEVI database
#
# Inputs          :
#
# Returns         :
#
sub getClearQuestData
{
    my $count = 0;
    my (@row);

    Message("Test ClearQuest Interface");

    #
    #   Connect to database
    #
    connectCQ(\$CQ_DB, $opt_verbose);

    Message("Extract data");
    my $m_sqlstr = "SELECT si.dbid AS iss_id, ".
                    "si.new_num AS iss_num,".
                    "si.headline AS summary, ".
                    "sd.NAME AS state , ".
                    "si.package_ref, ".
                    "si.project, ".
                    "si.product, ".
                    "sd.id, ".
                    "si.submit_date".
         " FROM CQ_DEVI.admin.software_issue si,".
         " CQ_DEVI.admin.STATEDEF sd".
         " WHERE si.STATE = sd.ID";
    my $sth = $CQ_DB->prepare($m_sqlstr);
    if ( defined($sth) )
    {
        if ( $sth->execute( ) )
        {
            if ( $sth->rows )
            {
                while ( @row = $sth->fetchrow_array )
                {
                    $count++;
                    if ( $opt_verbose > 1 )
                    {
                        printf join(',',@row) . "\n";
                    }
                }
            }
            $sth->finish();
        }
        else
        {
            Error("Execute failure" );
        }
    }
    else
    {
        Error("Prepare failure" );
    }

    #
    #   Report the data extracted
    #
    Error("No data extracted from the database") unless ( $count );
    Message "Extracted $count records\n";

    #
    #   Close down the connection
    #
    disconnectCQ(\$CQ_DB);
}


#-------------------------------------------------------------------------------
#   Documentation
#

=pod

=head1 NAME

test_cqconnection - Test the connection to the ClearQuest Database

=head1 SYNOPSIS

jats etool test_cqconnection [options]

 Options:
    -help              - Brief help message
    -help -help        - Detailed help message
    -man               - Full documentation
    -verbose           - Display additional information

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-help -help>

Print a detailed help message with an explanation for each option.

=item B<-man>

Prints the manual page and exits.

=item B<-verbose>

Prints additional information on the progress of the program.

=back

=head1 DESCRIPTION

This program is provided to test the connection between the users computer and
the ClearQuest Database. It does this by querying the database in the
same manner as many of the Release Manager Web Server scripts.

=head1 EXAMPLE

jats etool test_cqconnection

=cut

