##########################################################################
#
# routine: db_get_member_count.pl
#
# Purpose: Given an asn name, return a count of the members.
#          This includes non-products that are also non-orphans.
#
# Input:   $ASN - name of association to count members for
#          ENV.OPUS_SERVER - name of DB server
#          ENV.OPUS_DB - name of database containing asn_members
#
# Return:  count of members in asn
#          or -1 if db error
#
# Calls:   PrintMsg
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/30/03 49496  MSwam     from dc_collect.pl
# 03/25/10 64274  MSwam     Replace ST_DBlib with DBI
# 06/30/10 64432  MSwam     Use single quotes for SQLServer
# 09/18/12 72255  Sherbert  get rid of DSQUERY
# 
#########################################################################
sub DB_get_member_count{
    my ($ASN) = @_;                           # ARGUMENTS from @_
    my ($db, $query, $s, $count, $k, $v, $u_asn, %info);  # LOCAL VARS

    require 'do_dbi_pkg.pl';

    $server = $ENV{"OPUS_SERVER"};
    $database = lc($ENV{"OPUS_DB"});

    my $db = STScI::DBI->connect( "dbi:Sybase:server=$server");

    if (!defined($db)) {
        PrintMsg("E","Cannot connect to server: $DBI::errstr");
        return;
    }
    DoDBI($db,"use $database"); # open database

    $u_asn = uc($ASN);

    $query = <<"EOQ";
SELECT * FROM asn_members
WHERE association_id = '$u_asn'
AND member_status != 'O'
AND member_status != 'P'
EOQ

    if ($STDB_REPORT_LEVEL) {
      print "\n* * * in DB_get_member_count subroutine... \n" ;
      print "\n",$query,"\n" ;
    }

    PrintMsg("D","search for members of $u_asn");
    $count = 0;

    $sth = DoDBIexecute( $db, $query);
    while ( ( %info ) = DoDBIfetch( $db, $query, $sth) ) {
        PrintMsg("D","found $asn");
        while ( ($k, $v) = each %info) {
          PrintMsg("D","$v");
        }
        $count++;
    }

    PrintMsg("D","member count = $count");
    DoDBIclose($db);
    $count;   # return value
}
1;
