#!/usr/local/bin/perl

# dn1 #
# 
# Description; A module to allow additions and (some) changes to gull's
#              EMBL accession related tables

use strict;
use Carp;
use gull_general;
use WrapDBI;

package gull_acc;

require Exporter;
@gull_acc::ISA = qw(Exporter);
@gull_acc::EXPORT = qw(gull_acc);

BEGIN {
die "James Gilbert says this file is no longer required (2004-04-19)";
}

sub gull_acc {

    # What arguments did we get ?
    my ($project, $type, $acc, $id, $length, $suffix) = @_;
    my $db = 'gull';
    my $embl_update_flag = 0;

    unless ($project ne '' && $type ne '' && $acc ne '' && $id ne '' && $length ne ''){
      Carp::croak "gull_acc got an empty value for 1 or many of project, type, acc, id length !\n";
    }

    # Suffix
    if ($suffix ne '' and $type ne 'Finished'){
      Carp::croak "Only Finished submission entries support a suffix !\n";
    }

    if ($type eq 'Finished'){
	unless ( ($suffix =~ /^\w{1,1}$/) || ($suffix eq '') ){
	  Carp::croak "Suffix entry must be a 1 character alphanumeric if provided !\n";
	}
    }

    # Finished or Unfinished
    if ($type ne 'Finished' && $type ne 'Unfinished'){
      Carp::croak "Submission must be either Finished or Unfinished !\n";
    }

    # Length must be a number or 'NULL'
    unless ( ($length =~ /^\d+$/) || ($length eq 'NULL') ){
      Carp::croak "Length must be a number or NULL !\n";
    }

    # Database connection
    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});

    # Do we have a real project to play with ?
    my $p_check = check->projectname($dbh, $project);
    if ($$p_check eq 'Not in database'){
	$dbh->disconnect();
      Carp::croak "Project $project is not in the database !\n";
    }

    ##############################################
    # Lock all tables in question for the interim
    ##############################################
    #lock
    eval {
	lock->embl_submission($dbh, $acc);
    };
    if ($@ ne ''){
	$dbh->disconnect();
      Carp::croak "Problem locking entry in embl_submission table for accession $acc !\n$@\n";
    }
    #lock
    eval {
	lock->finished_submission($dbh, $project, $acc);
    };
    if ($@ ne ''){
	$dbh->disconnect();
      Carp::croak "Problem locking entry in finished_submission table for project $project accession $acc !\n$@\n";
    }
    #lock
    eval {
	lock->unfinished_submission($dbh, $project, $acc);
    };
    if ($@ ne ''){
	$dbh->disconnect();
      Carp::croak "Problem locking entry in unfinished_submission table for project $project accession $acc !\n$@\n";
    }


    # Is there already an entry in embl_submission for this accession ?
    my $embl_check = check->accession($dbh, $acc);
    if ($$embl_check eq 'Not in database'){
	$embl_update_flag = 1;
    } elsif ($$embl_check eq 'In database'){
	# We may need to make some changes to the entry later
	 $embl_update_flag = 2;
    }
    
    # What about the finished_submission or unfinished_submission table ?
    if ($type eq 'Finished'){
	&finished_submission($dbh, $project, $acc, $length, $id, $suffix, $embl_update_flag);
    } else {
	&unfinished_submission($dbh, $project, $acc, $length, $id, $embl_update_flag);
    }
    
    
    if ($embl_update_flag == 2){
	# NAME or LENGTH_BP or BOTH could be different
	my ($length_check, $id_check) = check->what_has_changed_in_embl_submission($dbh, $acc, $length, $id);


	if ($$length_check eq 'changed'){

	    ###################################################################################
	    eval {
		embl_submission->change_length($dbh, $acc, $length);
	    };
            ###################################################################################

	    if ($@ ne ''){
		$dbh->disconnect();
	      Carp::croak "Problem updating length field in embl_submission table !\n$@\n";
	    } else {
	      Carp::carp "Updated length in embl_submission table for accession $acc to $length\n";
	    }  
	}

	if ($$id_check eq 'changed'){

	    ###################################################################################
	    eval {
		embl_submission->change_name($dbh, $acc, $id);
	    };
	    ###################################################################################

	    if ($@ ne ''){
		$dbh->disconnect();
	      Carp::croak "Problem updating name field in embl_submission table !\n$@\n";
	    } else {
	      Carp::carp "Updated name in embl_submission table for accession $acc to $id\n";
	    }  
	}

    }

    $dbh->disconnect();
    
}

sub finished_submission {
    my ($dbh, $project, $acc, $length, $id, $suffix, $embl_update_flag) = @_;
    # Is there already a finished_submission entry with this $project, $acc combo ?
    my $fin_check = check->finished_submission($dbh, $project, $acc);
    if ($$fin_check eq 'Not in database'){

	# We may need an embl_submission entry first ?
	if ($embl_update_flag == 1){


	    ###################################################################################
	    eval {
		embl_submission->insert_new($dbh, $acc, $id, $length);
	    };
		
	    if ($@ ne ''){
		$dbh->disconnect();
	      Carp::croak "Problem inserting into embl_submission table !\n$@\n";
	    } else {
	      Carp::carp "Inserted new entry in embl_submission table for accession $acc\n";
	    } 
	    ###################################################################################
	}

	###################################################################################
	eval {
	    embl_submission->insert_finished($dbh, $project, $acc, $suffix);
	};
	###################################################################################
	    
	if ($@ ne ''){
	    $dbh->disconnect();
	  Carp::croak "Problem inserting into finished_submission table !\n$@\n";
	} else {
	    if ($suffix eq ''){
	      Carp::carp "Inserted new entry in finished_submission table for project $project accession $acc\n";
	    } else {
	      Carp::carp "Inserted new entry in finished_submission table for project $project accession $acc suffix $suffix\n";
	    }
	}
	    
    } else {
      Carp::carp "There is already an entry in the finished_submission table for project $project accession $acc !\n";
	return();
    }

    
}

sub unfinished_submission {
    my ($dbh, $project, $acc, $length, $id, $embl_update_flag) = @_;
    # Is there already an unfinished_submission entry for this $project ?
    my $unfin_check = check->unfinished_submission($dbh, $project);
    if ($$unfin_check eq 'Not in database'){

	# We may need an embl_submission entry first ?
	if ($embl_update_flag == 1){
	    ###################################################################################
	    eval {
		embl_submission->insert_new($dbh, $acc, $id, $length);
	    };
	    ###################################################################################

	    if ($@ ne ''){
		$dbh->disconnect();
	      Carp::croak "Problem inserting into embl_submission table !\n$@\n";
	    } else {
	      Carp::carp "Inserted new entry in embl_submission table for accession $acc\n";
	    } 
	}

	###################################################################################
	eval {
	    embl_submission->insert_unfinished($dbh, $project, $acc);
	};
        ###################################################################################

	if ($@ ne ''){
	    $dbh->disconnect();
	  Carp::croak "Problem inserting into unfinished_submission table !\n$@\n";
	} else {
	  Carp::carp "Inserted new entry in unfinished_submission table for project $project accession $acc\n";
	}

    } else {
      Carp::carp "There is already an entry in the unfinished_submission table for project $project !\n";
	return();
    }
}

1;

=head2 Some documentation for gull_acc.pm


A module to allow insertions and updates of 
embl submission data in the Oracle database.

=head2 Usage

use gull_acc;

gull_acc('PROJECT1', 'Finished', 'acc2', 'finid2', '1000', ['a']);

ie. supply;
projectname, Type(Finished or Unfinished), Accession number, Accession id, finished length(bp), optional Suffix
(for Finished submissions only).


=head2 Notes

Debugging info in this version is activated.

You must supply all values accept Suffix. 

Length should be given a value of 'NULL' if it is empty.

The projectname must already exist in Oracle.

You can Enter Finished and Unfinished submissions.

You can change the Accession id and finished length(bp) values for existing records
but you cannot change the Suffix value for an existing finished submission.

You can view what's been changed via gull...
BUT  If there is an unfinished submission AND a finished submission,
with the same EMBL accession then gull will only show the finished one.

=cut



