#!/usr/bin/perl
package Code;

#####################################################################
# Author : Shuai Weng
# Date   : July 2001
#####################################################################
#   Usage: 
#     
#   use Code;
#
#   ### You may use one of the following syntax 
#   ### instantiate a new Code object.
#
#   my $codeObj = Code->new(dbh=>$dbh,
#                           code_no=>$codeNo);
#       
#
#   ### You can use an accessor for any valid column 
#   ### in the Code table. For example: 
#   my $tabNm = $codeObj->tab_name;
#   my $codeVal = $codeObj->code_value;
#
#   See documentation for detail.
#   
#   http:///usr/local/dicty/www_dictybase/db/lib/html/dictyBase/programmer/Code.html
#
#####################################################################
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 = (code_no=>undef);


####################################################################
sub GetCodeDetail {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'};
    my $colHeader = $args{'colHeader'};
    my $filterBy = $args{'filterBy'};
    my $table = $self->schema.".code";
    my $sth;
    if ($filterBy) {
	$filterBy =~ s/\*/\%/g;
	$filterBy = "\U$filterBy";
	$sth = $dbh->prepare("
             SELECT code_no, tab_name, col_name, code_value, 
                    description, date_created, created_by 
	     FROM   $table
	     WHERE  upper($colHeader) like  ?
	     ORDER BY $colHeader
        ");
	$sth->execute($filterBy);
    }
    else {
	$sth = $dbh->prepare("
             SELECT code_no, tab_name, col_name, code_value, 
                    description, date_created, created_by 
	     FROM   $table
	     ORDER BY $colHeader
        ");
	$sth->execute;
    }
    my $arrayRef = $sth->fetchall_arrayref();
    return $arrayRef;
}

####################################################################
sub IsCodeInUse {
####################################################################
    my ($self, %args) = @_;
    my $dbh = $args{'dbh'};
    my $colNm = $args{'col_name'};
    my $codeVal = $args{'code_value'};
    my $table = $self->schema.".".$args{'tab_name'};
    my $statement = "SELECT * 
                     FROM   $table
                     WHERE  $colNm = '$codeVal'";
    my $sth = $dbh->prepare($statement);
    $sth->execute;
    my $found;
    while(my(@row) = $sth->fetchrow()) {
	$found++;
	last;
    }
    $sth->finish;
    return $found;
}

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

}


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


=pod

=head1 Name

Code.pm

=head1 Description

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


=head1 Instantiating a New Code Object

To instantiate a new Code object, you may use following syntax: 

my $codeObj = Code->new(dbh=>$dbh, $colNm=>$value); 

where $colNm is code_no and $dbh is a valid database handle to either dictyBase or SDEV. $value must be a valid value for the column that was provided, otherwise the script will die, with an appropriate error message.
 

Syntax example :

my $codeObj = Code->new(dbh=>$dbh, code_no=>$codeNo); 


=head1 Accessor Methods


All accessor methods take the form of : 


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


my $tab_name = $codeObj->tab_name; 


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


See valid columns in abstact table:

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


In addition, the following accessor methods are provided to access no code columns in database.


=head2 getRow method

Usage:

my $row = $obj->getRow;

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


=head2 GetCodeDetail

Usage:

my $arrayRef = Code->GetCodeDetail(dbh=>$dbh,
				   colHeader=>$colHeader,
				   filterBy=>$filterBy);
foreach my $rowRef (@$arrayRef) {

    my ($codeNo, $tabNm, $colNm, $codeVal, $desc, $dateCreated, $createdBy) 
	= @$rowRef;

    ....

}


=head2 IsCodeInUse

Usage:

my $found = Code->IsCodeInUse(dbh=>$dbh,
			      tabNm=>$tabNm,
			      $colNm=>$colNm,
			      $codeVal=>$codeVal);

This class method returns true if the code is in use; Otherwise
returns false. 


=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











