#!/usr/bin/perl
package Reflink;

#####################################################################
# Author : Shuai Weng
# Date   : July 2001
# 
# See documentation for the usage details. 
#    
# http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Reflink.html
#
#####################################################################

use strict;
use DBI;
use vars qw (@ISA %allowedConstructors);
use dictyBase_Table;
@ISA = qw (dictyBase_Table); # base class

# Class Globals

# put column names in the hash below, that are able to uniquely
# specify a row in the table

%allowedConstructors = (reflink_no=>undef,
       'reference_no:tab_name:primary_key:primary_key_col'=>undef);


####################################################################
sub GetRefLinkInfoBYrefNo {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || print "A database handle must passed to 'GetRefLinkInfoBYrefNo' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT tab_name, primary_key, primary_key_col, col_name
        FROM   $schema.reflink
        WHERE  reference_no = ?
    ");
    $sth->execute($args{'reference_no'});
    my $arrayRef = $sth->fetchall_arrayref();
    $sth->finish;
    return $arrayRef;
}

####################################################################
sub GetRefNoBYlocusNo {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || print "A database handle must passed to 'GetRefNoBYlocusNo' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT unique reference_no
        FROM   $schema.reflink
        WHERE  tab_name = 'LOCUS'
        AND    primary_key_col = 'LOCUS_NO'
        AND    primary_key = ?
    ");
    $sth->execute($args{'locus_no'});
    my $refNoList;
    while(my($refNo) = $sth->fetchrow()) {
	$refNoList .= ":".$refNo;
    }
    $refNoList =~ s/^\://;
    $sth->finish;
    return $refNoList;
}

####################################################################
sub GetRefNoListBYtabNmPrikeyPrikeycol {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'} || print "A database handle must passed to 'GetRefNoListBYtabNmPrikeyPrikeycol' method.";
    my $schema = $self->schema;
    my $sth = $dbh->prepare("
        SELECT reference_no
        FROM   $schema.reflink
        WHERE  tab_name = ?
        AND    primary_key = ?
        AND    primary_key_col = ?
    ");
    $sth->execute($args{'tab_name'}, $args{'primary_key'}, 
		  $args{'primary_key_col'});
    my $refNoList;
    while(my($refNo) = $sth->fetchrow()) {
	$refNoList .= ":".$refNo;
    }
    $refNoList =~ s/^\://;
    $sth->finish;
    return $refNoList;
}

####################################################################
sub DESTROY{
####################################################################
# nothing needs to be done 

}


####################################################################
1; #################################################################
####################################################################

=pod

=head1 Name

Reflink.pm

=head1 Description

This perl object (Reflink.pm) acts as container for Reflink info in oracle database. Once an object has been instantiated, several methods are available to retrieve the attributes of the object. 


=head1 Instantiating a New Reflink Object

To instantiate a new Reflink object, you may use one of the following syntaxes: 

my $Obj = Reflink->new(dbh=>$dbh,
		       reflink_no=>$reflinkNo);


OR


my $Obj = Reflink->new(dbh=>$dbh, 
		       reference_no=>$refNo,
		       tab_name=>$tabNm,
		       primary_key=>$prikey,
		       primary_key_col=>=>$prikeyCol);


where $dbh is a valid database handle to either dictyBase or SDEV. All passed in values must be valid values for the columns that were provided, otherwise the script will die, with an appropriate error message.
 

=head1 Accessor Methods


All accessor methods take the form of : 

my $column_value = $Obj->column_name, eg: 

my $dateCreated = $Obj->date_created; 


etc. You can use an accessor for any valid column in the Reflink table. 


See valid columns in reflink table:

http:///usr/local/dicty/www_dictybase/db/lib/cgi-bin/dictyBase/tableSpecifications?table=REFLINK

=head2 getRow

Usage :

my $row = $Obj->getRow;

This method returns a tab-delimited row from reflink table.

=head2 GetRefLinkInfoBYrefNo

Usage :

my $arrayRef = Reflink->GetRefLinkInfoBYrefNo(dbh=>$dbh,
					      reference_no=>$refNo);


foreach my $rowRef (@$arrayRef) {

    my ($tabNm, $prikey, $prikeycol, $colNm) = @$rowRef;

    ....

}

This method returns info in reflink table for a given reference_no.


=head2 GetRefNoBYlocusNo 

Usage :

my $refNoList = Reflink->GetRefNoBYlocusNo(dbh=>$dbh,
					   locus_no=>$locusNo);


This method returns reference_no list (colon delimited) for a given locus_no.


=head2 GetRefNoListBYtabNmPrikeyPrikeycol

Usage :

my $refNoList = Reflink->GetRefNoListBYtabNmPrikeyPrikeycol(dbh=>$dbh,
					 tab_name=>$tabNm,
					 primary_key=>$prikey,
					 primary_key_col=>$prikeyCol);



This method returns reference_no list (colon delimited) for a given tab_name/primary_key/primary_key_col.


=head1 Insert and delete Methods

You can also use 'Insert' class method and 'delete' 
instance method for inserting new row into database or 
deleting info for a specified row. 

See dictyBase_Table documentation for usage details :

Insert : http:///usr/local/dicty/www_dictybase/db/lib/staff/dictyBase/programmer/dictyBase_Table.html#Insert_Method

Delete : http:///usr/local/dicty/www_dictybase/db/lib/staff/dictyBase/programmer/dictyBase_Table.html#Delete_Method

=head1 Author

Shuai Weng

shuai@genome.stanford.edu

=cut











