#!@@_perl_root_@@/bin/perl.exe 
package GeneRegistry;

##########################################################
#                                                        #
# dictyBase Extension of GeneRegistry                    #
#                                                        #
##########################################################

use GeneRegistry_base;

BEGIN { %GeneRegistry:: = %GeneRegistry_base:: }

#
# use function 'Makedictybasid' on database to make dictybaseid
#
#######################################################################
sub assigndictyBaseid4newLocus {
########################################################################
    my ($self, $locusNo) = @_;
    if (param('assigndictyBaseidCB') !~ /on/i) {  return; }
    my $sth = $dbh->prepare("
         SELECT Makedictybaseid(CGM_DDB.dictyBaseidno_seq.nextval)
         FROM   dual
    ");
    $sth->execute;
    my $dictyBaseid = $sth->fetchrow;
    eval {
	dictyBaseid->Insert(dbh=>$dbh,
		      literals=>{dictyBaseid_no=>'CGM_DDB.dictyBaseidno_seq.currval'},
		      binds=>{dictyBaseid=>$dictyBaseid,
			      dictyBaseid_type=>'Primary',
			      tab_name=>'LOCUS',
			      primary_key=>$locusNo});
    };
    if ($@) {
	print "An error occurred when inserting new dictyBaseid entry for locus_no = $locusNo into database:$@", p;
	$dbh->rollback;
    }
    else {
	print "The new dictyBaseid entry for locus_no = $locusNo has been inserted into database.", p;
	$dbh->commit;
    }
}
########################################################################
sub insertJournal {
########################################################################
    my ($self, $journal) = @_;
    if ($journal =~ /^[0-9]+$/) {
	my $journalObj = Journal->new(dbh=>$dbh,
				      journal_no=>$journal);
	if ($journalObj) {
	    return ($journalObj->journal_no, $journalObj->abbreviation);
	}
	else {
	    print "The journal_no = $journal is not found in database.",br;
	    return;
	}
    }
    my $journalObj = Journal->new(dbh=>$dbh,
  				  abbreviation=>$journal);
    if ($journalObj) {
 	return ($journalObj->journal_no, $journalObj->abbreviation);
    }
    #my ($abbrev, $issn, $Jtitle) = &findFullJournalInfo($journal);
    eval {
 	Journal->Insert(dbh=>$dbh,
 			literals=>{journal_no=>'CGM_DDB.journalno_seq.nextval'},
 			binds=>{abbreviation=>$journal,
			        issn=>$issn,
			        full_name=>$journal});
    };
    if ($@) {
 	print "An error occurred when inserting journal '$journal' into database:$@", br;
 	$dbh->rollback;
 	return ;
    }
    else {
 	print "The journal '$journal' has been inserted into journal table.", br;
 	$dbh->commit;
 	my $journalObj = Journal->new(dbh=>$dbh,
 				     abbreviation=>$journal);
 	return ($journalObj->journal_no, $journalObj->abbreviation);
    }
}
#
# had to add 
#
#######################################################################
sub commitInfo {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);
    my @author;
    $self->processData(\@author);
    
    if (!$self->{'_gene'}) {
	print "The gene name is required for completing this gene registry. Please go back, enter the gene name and try again.";
        exit;
    }
    $self->{'_email'} =~ s/^(.+)[;, ]/$1/;
    
    my $colleague_no = Colleague->GetColleagueNoBYnameAndEmail(dbh=>$dbh,
					  last_name=>$self->{'_lname'},
					  first_name=>$self->{'_fname'},
					  email=>$self->{'_email'});

    if (!$colleague_no) {
	print p, b("Colleague ".font({-color=>'red'}, $self->{'_lname'}.", ".$self->{'_fname'})." with email address ".font({-color=>'red'}, $self->{'_email'})." can't be found in the database. Please go back and make sure you have commited this colleague info into database."),p;
	exit;
    }

    #### get feature object
    my $featObj = $self->getFeatureObject;

    #### update locus related tables
    my $chr;
    if ($featObj) { $chr = $featObj->chromosome; }
	
    my $locusObj = $self->updateLocusRelatedTables($colleague_no, $chr);
    
    #### update feature table
    if ($featObj) {
	$self->updateFeatureTable($featObj, $locusObj->locus_no, 
				  $self->{'_briefId'});
    }

    #### update alias table
    $self->updateAliasTable($locusObj, $self->{'_othergene'});
    
    #### update reference related tables
    my $refNo = $self->updateReferenceRelatedTables($locusObj, \@author);
    
    #### update gene_product
    $self->updateGeneProduct($locusObj, $self->{'_geneproduct'},
			     $refNo);
    

    #### update sacchdb_phenotype table
    $self->updateSacchdbPheno($locusObj, $self->{'_phenotype'},
			      $refNo);
    
    ####### update curator_note
    $self->updateCuratorNoteTable($locusObj, param('note'), 
				  param('idList'), param('isPublic'));


    #################
    $dbh->disconnect;
    #################
    my $id = param('id');

   #
   # changed command to use our path to mv and to explicitly state target file 
   #  ( not just mv $dir/$id.MajorUpdate $dir/archive/ )
   #
    my $mv = $configPath->unixUtilDir."mv";

    system("$mv $dir/$id.GeneRegistry4ace $dir/archive/$id.GeneRegistry4ace");
    &returnToCuratorCentral($self->user, $configUrl->dictyBaseCGIRoot."$dblink/curatorLogin");
    &printEndPage;
}

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



















