#!/usr/bin/perl
package External_id;

#####################################################################
# Author : Shuai Weng
# Date   : July 2001
# 
#     
# 
#
#####################################################################

use strict;
use DBI;
use Carp;
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 = (external_id_no=>undef,
			'external_id:source:tab_name:primary_key'=>undef);


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

}

####################################################################
sub GetUniqueSource {
####################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || die "You have to pass the database handle to External_id object.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT code_value
        FROM   $schema.code
        WHERE  tab_name = 'EXTERNAL_ID'
        AND    col_name = 'SOURCE'
        ORDER BY code_value
    ");

    $sth->execute;

    my $arrayRef = $sth->fetchall_arrayref();

    $sth->finish;

    return $arrayRef; 

}

####################################################################
sub GetTemplateUrlForExternalIdNm {
####################################################################
    my ($self, %args) = @_;

    my $dbh = $args{'dbh'} || die "You have to pass the database handle to External_id object.";

    my $schema = $self->schema;

    my $sth = $dbh->prepare("
        SELECT unique E.external_id, T.template_url
        FROM   $schema.external_id E, $schema.ei_tu ET, 
               $schema.template_url T
        WHERE  E.external_id_name = ?
        AND    E.external_id_no = ET.external_id_no
        AND    ET.template_url_no = T.template_url_no
    ");

    $sth->execute($args{'external_id_name'}); 

    my $url;

    while(my ($externalId, $templateUrl) = $sth->fetchrow()) {

	$templateUrl =~ s/_SUBSTITUTE_THIS_/$externalId/;
	
	$url = $templateUrl;

	last;
	
    }
    
    $sth->finish;

    return $url; 

}


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

=pod

=head1 Name

External_id.pm

=head1 Description

This perl object (External_id.pm) acts as container for external id 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 External_id Object

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

my $obj = External_id->new(dbh=>$dbh,
			   external_id_no=>$externalIdNo);


or 


my $obj = External_id->new(dbh=>$dbh,
			   external_id=>$externalId,
			   source=>$source,
			   tab_name=>$tabNm,
			   primary_key=>$prikey);


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 $source = $obj->source; 


my $external_id = $obj->external_id;


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

See valid columns in external_id table:

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

=head1 getRow method

Usage:

my $row = $obj->getRow;

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

=head1 Insert, update and delete Methods

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

See dictyBase_Table documentation for usage details :

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

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











