##########################################################################
#
# routine: db_osp_processing_steps.pl
#
# Purpose: Look up a dataset name in the otfr_special_processing relation.
#          Return the value of the osp_processing_steps field.
#
#          If the dataset name is an ASN, then look up the full 9-char
#          ipppssoot, but if it is NOT an ASN, then chop the last character
#          and only look up 8 (to avoid last char mismatches).
#
# Input:   $DSET - name of exposure to look up
#          ENV.ARCH_SERVER - name of DB server
#          ENV.ARCH_DB - name of database
#
# Return:  osp_processing_steps, if an entry exists for this dataset
#          "" if no entry found
#
# Errors:  Will fail if unable to connect to database
#          Will fail if database query fails to execute
#
# Calls:   PrintMsg
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 04/03/00 42246  MSwam     first version
# 10/11/04 48944  MSwam     chop last character for non-ASN names
# 06/30/10 64432  MSwam     Use single quotes for SQLServer
# 08/09/10 44712  MSwam     use DBI for database access
# 
#########################################################################
sub DB_osp_processing_steps{
    my ($DSET) = @_;                            # ARGUMENTS from @_
    my ($db, $query, $count);  # LOCAL VARS
    my ($osp_processing_steps);

    require 'do_dbi_pkg.pl';

    # set exit value as if failed
    $osp_processing_steps = "";

    $EXIT_FAILURE = 0;
    $db = DoDBIopen( $ENV{"ARCH_SERVER"}, lc($ENV{"ARCH_DB"}), $EXIT_FAILURE);

    # if this is NOT an ASN (determined by last char) and is a 9-char
    # name, then chop down to 8-chars for the lookup
    if ($DSET =~ /.*[0-9a-i]$/) { # asn/product name (ends in 0-9 or a-i)
      # use name unchanged
    }
    elsif (length($DSET) >= 9) {
      # trim last character so that transmission char doesn't affect DB query
      chop($DSET);
      PrintMsg("D","chopped last char, looking up $DSET");
    }
    else {
      # non-ASN name is already trimmed, use as is
    }

    $query = <<"EOQ";
SELECT osp_processing_steps FROM otfr_special_processing
WHERE osp_data_set_name = '$DSET' AND osp_status = 'ACTIVE'
EOQ

    my $sth = DoDBIexecute( $db, $query);
    $count = 0;
    while ( my ($tmp) = DoDBIfetch($db, $query, $sth)) {
        $osp_processing_steps = $tmp;
        PrintMsg("D","found $DSET,$osp_processing_steps in otfr_special_processing");
        $count++;
    }
    PrintMsg("I","$DSET: osp_processing_steps count = $count");

    $sth->finish;
    DoDBIclose($db);
    $osp_processing_steps;   # return value
}
1;
