package Go;

##########################################################
#                                                        #
# dictyBase Extension of Go                                                           #
#                                                        #
##########################################################

use Go_base;

BEGIN { %Go:: = %Go_base:: }


########################################################################
sub getAmiGoLinkUrl {
########################################################################
    my ($self) = @_;

    my $amigoUrl = "javascript:NewWindow('http://www.godatabase.org/cgi-bin/go.cgi?query=".$ self->goid . "', 'Details', '850', '600', 'center', 'front');";

    return $amigoUrl;
}

#
# we are only using external Id for features,
# on top of that, I changed the primary_key column to a numerical type
#  so that we could have it foreign keyed to feature.  so this query
#  breaks when you try to query, so just have it return empty array
#  since we won't have it linked anyway
#
###################################################################
sub GetExternalIdSourceInfo {
###################################################################
#    my ($self, %args) = @_;
#    my $dbh = $args{'dbh'} ||
#        die "A database handle must be passed to GetExternalIdSourceInfo method.";
#    my $tabNm = $args{'tab_name'};
#    my $prikey = $args{'primary_key'};
#    my $schema = $self->schema;
#    my $sth = $dbh->prepare("
#        SELECT external_id, source
#        FROM   $schema.external_id
#        WHERE  tab_name = ?
#        AND    primary_key = ?
#        ORDER BY source
#    ");
#    $sth->execute($tabNm, $prikey);
#    my $arrayRef = $sth->fetchall_arrayref();
#    $sth->finish;
#    return $arrayRef;
my @array;
return \@array;
}

####################################################################
sub DeleteAnnotationNew {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || die "A database handle must be passed to DeleteAnnotation method.";
    my $schema = $self->schema;
    my $externalIdNo = $args{'external_id_no'};
    my $refNo = $args{'reference_no'};
    my $goid = $args{'goid'};
    my $No = $args{'No'};
    my $eviCodeNo = $args{'evidence_code_no'};
    my $type = uc($args{'type'});
    my $NoNm = $type."_NO";
    my $tabNm = "GO_".$type."_GOEV";
    ### change GO_FEATURE_GOEV to GO_FEAT_GOEV
    $tabNm =~ s/FEATURE/FEAT/;
    my $priKeyCol = "GOID::".$NoNm."::GO_EVIDENCE_NO::IS_NOT";
    my $priKey = "${goid}::${No}::${eviCodeNo}::%";

    # Inserting the locus no and goid pair into the Go_Locus_Exclude table for evidence code 10
    if($eviCodeNo == 10)
    {
        my $exist_go_locus_exclude = $dbh->prepare("select * from $schema.go_locus_exclude where goid = ? and locus_no = ?");
        $exist_go_locus_exclude->execute($goid,$No);
        my @arr = $exist_go_locus_exclude->fetchrow_array();
        if(!@arr)
        {
           my $go_locus_exclude_sth = $dbh->prepare("insert into $schema.GO_LOCUS_EXCLUDE values(?,?,SYSDATE,'Curator')");
           $go_locus_exclude_sth->execute($goid,$No);
           $go_locus_exclude_sth->finish();
        }
    }
    ##########################################################################################


    ###### delete the specified reflink entry if there is no more
    ###### external_id entry associated with ...
    my $sth = $dbh->prepare("
            DELETE from $schema.reflink
            WHERE  tab_name = ?
            AND    reference_no = ?
            AND    primary_key_col = ?
            AND    primary_key like ?
    ");
    $sth->execute($tabNm, $refNo, $priKeyCol, $priKey);
    $dbh->commit;

    $sth = $dbh->prepare("
            SELECT reference_no
            FROM   $schema.reflink
            WHERE  tab_name = ?
            AND    primary_key_col = ?
            AND    primary_key like ?
    ");
    $sth->execute($tabNm, $priKeyCol, $priKey);
    my $arrayRef = $sth->fetchall_arrayref();
    if (!@$arrayRef) {
        $sth = $dbh->prepare("
            DELETE from $schema.$tabNm
            WHERE  goid = ?
            AND    $NoNm = ?
            AND    go_evidence_no = ?
        ");
        $sth->execute($goid, $No, $eviCodeNo);
        $sth->finish;
        $dbh->commit;
    }
    else {

        foreach my $rowRef (@$arrayRef) {

            my ($refNo) = @$rowRef;

            print "The reference_no = $refNo is still associated with $priKey entry in $tabNm table. The entry for $priKey can not be deleted from $tabNm table.<p>";

        }

    }
    ###### delete the specified external_id entry
    if ($externalIdNo) {

         my $sth = $dbh->prepare("
            select goid from $schema.go_locus_goev
            WHERE  with_from = ?
        ");
        $sth->execute($externalIdNo);
        my @temparray = $sth->fetchrow();
        $sth->finish();
        if(!@temparray)
        {
         my $with_from_sth = $dbh->prepare("
            DELETE from $schema.go_annotation_with_from
            WHERE  id = ?
        ");
        $with_from_sth->execute($externalIdNo);
        $with_from_sth->finish();
        $dbh->commit;

        }

    }
}

########################################################################
sub getLocusArrayRef {
########################################################################
    my ($self) = @_;
    my $schema = $self->schema;
    my $sth = $self->dbh->prepare("
       SELECT   DISTINCT G.FEATURE_ID, G.NAME
        FROM    $schema.GO_LOCUS_GOEV GLG, $ENV{'CHADO_USER'}.V_GENE_FEATURES G
        WHERE   GLG.GOID = ?
        AND     GLG.LOCUS_NO = G.FEATURE_ID
        ORDER BY UPPER(G.NAME)
    ");
    $sth->execute($self->goid);
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}


#
# we dropped the external id table, code accessing this method was generating an error
#
########################################################################
sub _getExternalIdInfo {
########################################################################
    my ($self, $dbh, $reflinkNo) = @_;


    return [];

}


1;