#!/usr/local/bin/perl
#
# dnaprep
#
# Class to access the dnaprep table in cas
#
# Author: Jennifer Liddle (js10)
#
# $Id: dnaprep.pm,v 1.11 2007/03/08 17:02:53 js10 Exp $
#
# $Log: dnaprep.pm,v $
# Revision 1.11  2007/03/08 17:02:53  js10
# Added Statusoperator to dnaprep, and priority to clone
#
# Revision 1.10  2006/09/13 16:34:49  js10
# another typo
#
# Revision 1.9  2006/09/13 16:11:23  js10
# correct typo
#
# Revision 1.8  2006/09/13 15:58:16  js10
# Added a 'remarks' column to dnaprep_status
#
# Revision 1.7  2006/06/27 16:39:02  js10
# Optimised by moving virtualSqlLib()s into Tapir.sql_lib
#
# Revision 1.6  2006/06/26 09:27:32  js10
# moved common code into Table.pm
#
# Revision 1.5  2006/06/08 15:39:24  js10
# Added rdrequest and replace 'die' with 'confess'
#
# Revision 1.4  2006/05/30 10:04:38  js10
# Do not override dnaprep.operator when reading dnaprep_status
#
# Revision 1.3  2006/05/30 09:06:55  js10
# Store email instead of role ID in status table
#
# Revision 1.2  2006/05/24 14:28:03  js10
# Replace full pathname in status tables with base filename
#
# Revision 1.1.1.1  2006/05/12 08:47:19  js10
# Initial import
#
#

package Tapir::dnaprep;

use strict;
use warnings;
use Carp;

=head1 NAME

Tapir::dnaprep

=head1 SYNOPSIS

for documentation see http://intweb.sanger.ac.uk/Teams/Team71/Prod_sw_group/PSGDocs/Tapir

=cut

use Tapir::Table;
our @ISA = ("Tapir::Table");

sub new
{
	my $class = shift;
	my $self = $class->SUPER::new(@_);
	bless($self, $class);
	$self->_init(@_);
	return $self;
}

sub _init
{
	my ($self, undef, $id) = @_;
	if ($id) {
		$self->loadRecord($id);
	}
	$self->{statusDict} = $self->loadDict('Tapir/Tapir::getDnaprepstatusdict');
	if ($self->{Record}->{status}) {
		$self->{statusDesc} = $self->{statusDict}->{$self->{Record}->{status}};
	}
	
}

sub loadRecord
{
	my ($self, $id) = @_;
	$self->SUPER::loadRecord($id, 'Tapir/Tapir::getDnaprep');
	$self->SUPER::loadRecord($id, 'Tapir/Tapir::getDnaprep_status');
}

#
# Change the status by marking the current record non-current and adding a new status record
#
sub changeStatus
{
	my ($self, $newStatus, $remark) = @_;

	confess "No record read" if !$self->{Record}->{id_prep};
	my $status = $self->normaliseStatus($newStatus);
	confess "No such status '$newStatus'" if !$status;

	$remark = '' if !defined $remark;

	$self->{se}->do('Tapir/Tapir::deleteDnaprep_status', [$self->{Record}->{id_prep}], $self->{dbh});

	# this bit is primarily for backwards compatibility, before we added statusoperator
	my $so;
	if ($self->{Record}->{statusoperator}) {
		$so = $self->{Record}->{statusoperator};
	} else {
		$so = $self->roleId2email($self->{Record}->{operator});
	}

	$self->{se}->do('Tapir/Tapir::addDnaprep_status', [$self->{Record}->{id_prep}, 
	                                                   $status, 
	                                                   $$, 
	                                                   $self->{Program},
	                                                   $so, 
	                                                   $remark],
	                                                  $self->{dbh});
}

#
# get the largest prep number for a given clone
#
sub getPrepNumber
{
	my ($self, $cloneName) = @_;
	confess "You must specify a clone name" unless $cloneName;

	my $prepNumber = $self->{se}->getRow('Tapir/Tapir::getPrepNumber', [$cloneName], $self->{dbh});
	return $prepNumber || 0;
}

#
# Insert a new record
#
# NB that the new id_prep will be stored in the record
#
sub insert
{
	my $self = shift;

    $self->{Record}->{id_prep} = $self->{se}->getRow('Tapir/Tapir::seqDnaprep', [], $self->{dbh});

	$self->{se}->do('Tapir/Tapir::addDnaprep', [$self->{Record}->{id_prep},
	                                            $self->{Record}->{id_culture},
	                                            $self->{Record}->{prep_number},
	                                            $self->{Record}->{prep_type},
	                                            $self->{Record}->{operator},
	                                            $self->{Record}->{bandquality},
	                                            $self->{Record}->{is_priority},
	                                            $self->{Record}->{clonename},
	                                            $self->{Record}->{comments},
	                                            $self->{Record}->{fish_comments}],
	                                            $self->{dbh});

	$self->changeStatus('Actioned');	# don't forget to add a status record
}

1;
