#!/usr/bin/perl
package sdevNewColleague;
#######################################################################
##### Author :	Shuai Weng
##### Date   :  August 2000
##### Description : This package contains all necessary methods for dictyBase
#####               curators to insert new colleague info  
#####               into oracle database. 
#####              
#######################################################################
##################           RCS INFORMATION          ################
# $Author: emj466 $
# $Date: 2010/08/31 18:58:21 $
# $Header: /projects/dicty/build/source_files/db/lib/site_name/curation/sdevNewColleague.pm,v 1.5 2010/08/31 18:58:21 emj466 Exp $
# $Log: sdevNewColleague.pm,v $
# Revision 1.5  2010/08/31 18:58:21  emj466
# merged HEAD with release-2-18
# Committed on the Free edition of March Hare Software CVSNT Server.
# Upgrade to CVS Suite for more features and support:
# http://march-hare.com/cvsnt/
#
# Revision 1.4.20.1  2010/08/31 16:54:22  emj466
# merged with no-prepare-cache branch
# Committed on the Free edition of March Hare Software CVSNT Server.
# Upgrade to CVS Suite for more features and support:
# http://march-hare.com/cvsnt/
#
# Revision 1.4.24.1  2010/08/30 21:04:17  emj466
# changed prepare_cached to prepare
# Committed on the Free edition of March Hare Software CVSNT Server.
# Upgrade to CVS Suite for more features and support:
# http://march-hare.com/cvsnt/
#
# Revision 1.4  2007/06/19 15:11:13  emj466
# substitute @@_dbuser_ @@ for cgm_ddb
#
# Revision 1.3  2006/10/26 20:38:23  emj466
# change temporary file directory to not include site_name
# ie. dicty/data/submission/colleague
# instead of
# dicty/data/submission/colleague/DICTYBASE
#
# Revision 1.2  2006/10/26 15:46:51  emj466
# replaced is_dictyBase_contact with 'is_subscribed'
#
# Revision 1.1  2006/10/25 18:29:11  emj466
# new build directory
#
# Revision 1.1.1.2  2003/08/18 22:50:41  emj466
# no message
#
# Revision 1.1.1.1  2003/08/15 20:18:40  emj466
# initial load of dicty/build
#
# Revision 1.17  2003/07/25 16:51:45  shuai
# *** empty log message ***
#
# Revision 1.16  2003/03/20 00:49:41  qdong
# *** empty log message ***
#
# Revision 1.15  2003/01/11 17:30:53  kara
# made changes to handle is_subscribed
#
# $Revision: 1.5 $
# $Source: /projects/dicty/build/source_files/db/lib/site_name/curation/sdevNewColleague.pm,v $
# $State: Exp $
# $Locker:  $
#####################################################################

use strict;
use DBI;
use CGI qw/:all :html3 :noDebug/;
use lib "/usr/local/dicty/www_dictybase/db/lib/common";
use Login qw (ConnectToDatabase);
use TextUtil qw(DeleteUnwantedChar);
use DBUtils qw(GetColumnSizes);
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase";
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/curation";
use dictyBaseCentralMod qw(:formatPage :getInfo);
use CuratorNote qw (:updateInfo);
use sdevColleagueCurationMod qw (:formSub :insertUpdateInfo :deleteInfo);
use sdevColleague;
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use Locus;
use ConfigURLdictyBase;

#######################################################################
#################### global variables #################################
#######################################################################

my $dbh;
my $dblink; 
my $dbname;
my $dir;
my $configUrl;

my $maxNumURLs = 3;

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

	$self = {};
	bless $self;

      	$self->{'_database'} = $args{'database'};
	$self->{'_user'}     = $args{'user'};
	$self->{'_id'} = param('id');

        $configUrl = ConfigURLdictyBase->new;
      	return $self;
}

sub database { $_[0]->{_database} }
sub user { $_[0]->{_user} }
sub title { $_[0]->{_title} }
sub help { $_[0]->{_help} }
sub id { $_[0]->{_id} }

######################################################################
sub DESTROY {   ############ destructor ##############################
######################################################################
    	if (defined $dbh) {
		$dbh->disconnect;
    	}
}

######################################################################
sub start{
######################################################################
	my ($self) = @_;

	if ($self->database eq "dictyBase") {
	    $dblink = "dictyBase";
	}
	else {
	    $dblink = "dictyBaseDEV";
	}
	$dbname = $self->database;
	$dbname = "\U$dbname";
	$self->{'_title'} = "$dbname Curator Page for Colleague Information";
	if (!$self->user) {
	    print "location: " . $configUrl->dictyBaseCGIRoot. "$dblink/curatorLogin\n";
	    print "Content-type: text/html\n\n";
	    exit;
	}
	my $user = $self->user;
	$user = "\U$user";
	my ($dbuser, $dbpasswd) = &getUsernamePassword($user,
						       $self->database);
	if (!$dbuser || !$dbpasswd) {
	    print "location: " . $configUrl->dictyBaseCGIRoot . "$dblink/curatorLogin\n";
	    print "Content-type: text/html\n\n";
	    exit;
	}
	$dir = "/usr/local/dicty/www_dictybase/db/data/submission/colleague";

	if (param('fname') && param('lname')) {
	    if (param('delayCB') =~ /on/i || param('deleteCB') =~ /on/i 
		|| param('convertCB') =~ /on/i) {
		$self->processFile;
	    }
	    else {
		$dbh = &ConnectToDatabase($self->database, $dbuser, 
					  $dbpasswd);
		$self->commitInfo;
		$dbh->disconnect;
	    }
	}
	else {
	    $self->displayInfo;
	}

}

#######################################################################
sub displayInfo {
#######################################################################
    # KD: add stuff for dictyBase contact
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);
      
    print b("Please check the information below and make any changes necessary."), p;
    my (@phone, @addr, @pi, @assocsByPi, @assocs, @keyword, @url);
    $self->populateInfo(\@phone, \@addr, \@pi, \@assocsByPi, \@assocs, 
			\@keyword, \@url);
    my $dbh = &ConnectToDatabase($self->database);
    my @collNo = &getColleagueNoBYname($dbh, $self->{'_lname'}, 
				       $self->{'_fname'});   
    my $matchIDs;
    foreach my $collNo (@collNo) {
	$matchIDs .= ":".$collNo;
	print font({-color=>'red'}, b("The first name and last name for this colleague matched a colleague that already exists in dictyBase (see ".a({-href=>$configUrl->dictyBaseCGIRoot . "$dblink/colleague/colleagueSearch?id=$collNo&win=1", -target=>"infowin", -onClick=>"open_win()"}, "colleague_no $collNo")."). A separate entry has been created below for the new information. If you want to merge these two entries, check the 'Convert this entry to update' checkbox and choose a colleague_no in the list below before press 'submit' button.")), p;
    }
    $matchIDs =~ s/^://;
    print startform,
          hidden(-name=>'id',
		 -value=>$self->id),
          hidden(-name=>'user',
		 -value=>$self->user),
          hidden(-name=>'webtitle',
		 -value=>$self->{'_webtitle'});
    print &subtitle("Your Address and Contact Information");
    print table({-border=>'0',
		 -cellpadding=>'2',
		 -cellspacing=>'3',
		 -width=>'650'},
		&newEntryHeader.
                ##KD: adding dictyBase contact stuff ***
		&dictyBasecontact($self->{'_dictyBasecontact'}).
	        &lastName($self->{'_lname'}).
		&firstName($self->{'_fname'}).
		&suffix($self->{'_suffix'}).
		&otherName($self->{'_oname'}).
		&email($self->{'_email'}).
		&jobTitle($self->{'_jobtitle'}).
		&profession($self->{'_profession'}).
		&institution($self->{'_institution'}).
		&address(\@addr).
	        &city($self->{'_city'}).
		&state($dbh, $self->{'_country'}, $self->{'_state'}).
		&region($self->{'_region'}).
		&country($self->{'_country'}).
		&zipCode($self->{'_zip_code'}).
		&phone(\@phone).
		&webPage(\@url)
    ), p;
    print &subtitle("Your Associates/Collaborators");
    
    print &associate4pi(\@assocsByPi, undef, undef, undef, $self->{'_newPIOnly'});
    print &pi(\@pi);
    print &associate(\@assocs);

    print &subtitle("Your Research Interests");
    print &interest($self->{'_interests'});

    print &smalltitle("Research Topics");
    print &researchTopics($self->{'_researchTopics'}, undef, undef, $dbh);

    print &smalltitle("Keywords");
    print &keyword(\@keyword);
    
    print &subtitle("Your Comments");
    print &comment($self->{'_Comments'});

    print &subtitle("Curator Section");

    print &smalltitle("Associated Genes");
    print &associatedLoci($self->{'_newLoci'}, undef,
			  $self->{'_oldLoci'}, $dbh);

    print &smalltitle("Curator Note:");
    print &newCuratorNote;
    print &delayDelete($matchIDs);   

    print p, font({-color=>'red'}, "Note: please check the 'Associates/Collaborators' section to make sure the format for each name is correct."),p;

    print &submitReset;
    print end_form;
    $dbh->disconnect;
    &printEndPage;
}


#######################################################################
sub commitInfo {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);

    print b("Please wait"), ", committing data into the database...", p;

    my $commitInfoErrorCount = 0;

    $self->processData;
    my $sth = $dbh->prepare("
        INSERT INTO CGM_DDB.colleague (colleague_no, last_name, 
                          first_name, is_subscribed, suffix, other_name, profession, 
                          job_title, institution,  address1, address2, 
                          address3, address4, address5, city, state, 
                          region, country, postal_code, source, 
                          date_modified, date_created, created_by)
            VALUES (CGM_DDB.collno_seq.nextval, 
                          ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
                          'Direct Submission', sysdate, sysdate,?)
    ");
    eval {

	# check column lengths
	{
	    my %colLengths;

	    &GetColumnSizes($dbh, \%colLengths, "colleague");

	    my @values = ('_lname', '_fname', '_dictyBasecontact', 
			  'suffix', '_oname', 
			  '_profession', '_jobtitle', 
			  '_institution',
			  '_address1', '_address2', 
			  '_address3', '_address4',
			  '_address5', '_city', 
			  '_state', '_region', 
			  '_country', '_zip_code');

	    my @columns = ("last_name", "first_name","is_subscribed", 
			   "suffix", "other_name", 
			   "profession", "job_title", 
			   "institution",  
			   "address1", "address2", 
			   "address3", "address4", 
			   "address5", "city", 
			   "state", "region", 
			   "country", "postal_code");

	    my $errorMsg = "";

	    for (my $index = 0; $index < scalar @columns; $index++) {
		my $value = $self->{$values[$index]};
		my $maxColLength = $colLengths{uc($columns[$index])};

		if (length($value) > $maxColLength) {
		    $errorMsg 
			 .= ("The value \"" . font({-color=>"blue"},
						 i("$value")) . 
			     "\" specified is too long " .
			     "for the column " . b(uc($columns[$index])) 
			     . "." .
			     br . 
			     " The maximum allowable length for this column is " .
			     b($maxColLength)
			     . " characters.  The value above " . 
			     "contains " . font({-color=>"red"},
						b(length($value)))
			     . " characters. " . p
			    );
		}
	    }

	    if (length($errorMsg) > 0) {
		$errorMsg .= 
		     (p . "To resolve this problem, please " .
		      "use the BACK button on your browser to reduce " .
		      " length of these lines and re-submit the colleague " .
		      " entry again. " . br);
		die $errorMsg;
	    }


	} # end check column lengths

	$sth->execute($self->{'_lname'}, $self->{'_fname'}, 
                      $self->{'_dictyBasecontact'}, 
		      $self->{'suffix'}, $self->{'_oname'}, 
		      $self->{'_profession'}, $self->{'_jobtitle'}, 
		      $self->{'_institution'},
		      $self->{'_address1'}, $self->{'_address2'}, 
		      $self->{'_address3'}, $self->{'_address4'},
		      $self->{'_address5'}, $self->{'_city'}, 
		      $self->{'_state'}, $self->{'_region'}, 
		      $self->{'_country'},  $self->{'_zip_code'},
		      $self->user); };
    if ($@) {

	my $errorMsg = $@;

	print "Error occurred when inserting data into colleague table. See following error message:", p, code($errorMsg), br;
	$dbh->rollback();
	&returnForm($self->user, $dblink);
	&printEndPage;
	exit;
    }
    else {
	print "The colleague info has been inserted into ".font({-color=>'red'}, b("colleague"))." table", br;
    }

    $sth = $dbh->prepare( "
        SELECT CGM_DDB.collno_seq.currval
        FROM   dual
    ");
    $sth->execute;
    my $colleague_no = $sth->fetchrow;
    if ($colleague_no) {
	print "Current colleague_no = ".a({-href=>$configUrl->dictyBaseCGIRoot . "$dblink/colleague/colleagueSearch?id=$colleague_no&win=1", -target=>"infowin", -onClick=>"open_win()"}, $colleague_no), br;
    }
    ###### insert into coll_phone and phone tables
    my $update = 0;
    for (my $i = 1; $i <= 5; $i++) {
	my $phone_num = param("phone$i");
	&DeleteUnwantedChar(\$phone_num);
	if (!$phone_num) {next;}
	$update++;
	my (${phone_location}, ${phone_type}) 
                = split(/ /, param("phone_type$i"));
	if (!$phone_type) {
	     $phone_type = $phone_location;
	     $phone_location = "Unspecified";
	}
        my $sth = $dbh->prepare( "
             SELECT  phone_no
             FROM    CGM_DDB.phone
	     WHERE   phone_num = ?
             AND     phone_type = ?
             AND     phone_location = ?
        ");
	$sth->execute($phone_num,  $phone_type, $phone_location);
	my $phone_no = $sth->fetchrow;
        if (!$phone_no) {
	     $sth = $dbh->prepare("
                  INSERT INTO CGM_DDB.phone(phone_no, phone_num, 
                              phone_type, phone_location, created_by)
                  VALUES(CGM_DDB.phoneno_seq.nextval, ?,?,?,?) 
             ");
	     $sth->execute($phone_num,  $phone_type, $phone_location, 
			   $self->user);
	     $dbh->commit;
	     $sth = $dbh->prepare("
                  INSERT INTO CGM_DDB.coll_phone (colleague_no, phone_no) 
                  VALUES (?, CGM_DDB.phoneno_seq.currval)
             ");
	     $sth->execute($colleague_no);
	}
        else {
	     $sth = $dbh->prepare("
                  INSERT INTO CGM_DDB.coll_phone (colleague_no, phone_no) 
                  VALUES (?,?)
             ");
	     $sth->execute($colleague_no, $phone_no);
	}
    }
    if ($update != 0) {
        print "Phone info has been inserted into ".font({-color=>'red'}, b("phone"))." and ".font({-color=>'red>'}, b("coll_phone"))." tables.", br;
    }

    ###### insert info into pi table
    foreach my $pi (param('PILIST0'), param('PILIST1')) {
        &DeleteUnwantedChar(\$pi);
        if (!$pi) { next; }
        &insertPi($dbh, $colleague_no, $self->user, $pi);
        print "The pi '$pi' has been inserted into ".font({-color=>'red'}, b("pi"))." table", br;
    }
        
    ###### insert into coll_email and email tables
    my $email = param('email');
    &DeleteUnwantedChar(\$email);
    $email =~ s/[,;:]/ /g;
    $email =~ s/ +/ /g;
    my @email = split(/ /, $email); 
    if (@email) {
	&insertEmails($dbh, $colleague_no, $self->user, @email);
        print "The email has been inserted into ".font({-color=>'red'}, b("email"))." and ".font({-color=>'red'}, b("coll_email"))." tables.", br;
    }

    ###### insert into coll_url and url tables
    for (my $urlNo = 0; $urlNo < $maxNumURLs; $urlNo++) {
	my $url = param("url$urlNo");
	my $urlTitle = param("WebTitle$urlNo");
	my $urlType = param("url_type$urlNo");

	# $url =~ s/[;,:]/ /g;
	$url =~ s/ +/ /g;
	&DeleteUnwantedChar(\$url);

	if (length($urlTitle) == 0) {
	    my $ua = new LWP::UserAgent;
	    my $reqUrl = new HTTP::Request GET => $url;
	    $reqUrl->content_type('application/x-www-form-urlencoded');
	    my $resUrl = $ua->request($reqUrl);
	    if ($resUrl->is_success) {
		my $output = $resUrl->content;
		if ($output =~ /<title>(.+)<\/title>/i) {
		    $urlTitle = $1;
	        }
		else { $urlTitle = ""; }
	    }
	    else { $urlTitle = ""; }
	}
	my $urlObject = "$url\t$urlTitle\t$urlType";

	# print $urlObject;

	&insertUrls($dbh, $colleague_no, $self->user, $urlObject);
    }
    print "The url has been inserted into "
	 .font({-color=>'red'}, b("url"))
	 ." and ".font({-color=>'red'}, 
		       b("coll_url"))." tables.", br;

    ###### insert into pi and associate tables
    my @assocsbyPi;
    for (my $i = 1; $i <= 10; $i++) {
        my $assoc = param("associate$i");
        &DeleteUnwantedChar(\$assoc);
        if (!$assoc) { next; }
        push(@assocsbyPi, $assoc);
    }
    if (@assocsbyPi) {
	&insertAssocsBYpi($dbh, $colleague_no, $self->user, @assocsbyPi);
        print "The associates by Pi info have been inserted into ".font({-color=>'red'}, b("colleague"))." and ".font({-color=>'red'}, b("pi"))." tables.", br;
    }
    else {
	my $setGenAssoc = "false";
	if (defined param('newPIOnly')) {
	    $setGenAssoc = "true";
	}
	my @newAssoc = ();
	&updateGenericAssociate($dbh, $colleague_no, 
				$self->user, $setGenAssoc,
				@newAssoc);
    }

    ###### insert into associate table
    my @assocs;
    for (my $i = 11; $i <= 20; $i++) {
        my $assoc = param("associate$i");
        &DeleteUnwantedChar(\$assoc);
        if (!$assoc) { next; }
        push(@assocs, $assoc);
    }
    if (@assocs) {
	&insertAssocs($dbh, $colleague_no, $self->user, @assocs);
        print "The associates' info have been inserted into ".font({-color=>'red'}, b("colleague"))." and ".font({-color=>'red'}, b("associate"))." tables.", br;
    }

    ###### insert research topics into coll_keyword table ######
    my $researchTopicList = &getCDKeywords($dbh);
    foreach my $rt (split(/\t/, $researchTopicList)) {
	my $value = param($rt);
	if ($value eq "on") {
	    if (length($rt) < 1) { next; }
	    my $keywordNo;
	    eval {
		my $sthGetRTNo = $dbh->prepare("
                   SELECT keyword_no
                   FROM   CGM_DDB.keyword
                   WHERE  source = 'Curator-defined'
                   AND    keyword = ?
                ");
		$sthGetRTNo->execute($rt);
		$keywordNo = ($sthGetRTNo->fetchrow_array)[0];
		my $sthAddCollRT = $dbh->prepare("
                   INSERT INTO CGM_DDB.coll_keyword(colleague_no, keyword_no)
                   VALUES (?, ?)
                ");
		$sthAddCollRT->execute($colleague_no,
				       $keywordNo);
	    };
	    if ($@) {
		print "An error has occurred while attempting to insert a",
		     "new research topic/colleague association ($rt [$keywordNo], ",
			  "$colleague_no): $@\n",p;
	    }
	}
    }

    ###### insert into coll_keyword and keyword tables
    my @keyword;
    for(my $i = 1; $i <= 10; $i++) {
        my $keyword = param("keyword$i");
        &DeleteUnwantedChar(\$keyword);
        if (!$keyword) { next;}
        push(@keyword, $keyword);
    }
    if (@keyword) {
	&insertKeywords($dbh, $colleague_no, $self->user, @keyword);
        print "The keyword info has been inserted into ".font({-color=>'red'}, b("keyword"))." and ".font({-color=>'red'}, b("coll_keyword"))." tables.", br;
    }
 
    my $comment = param('Comments');
    &DeleteUnwantedChar(\$comment);
    if ($comment) {
        &insertComments($dbh, $colleague_no, $self->user, $comment, 
                        "Announcement");
        print "The user comment has been inserted into ".font({-color=>'red'}, b("colleague_remark"))." table.", br;
    }

    my $interest = param('interests');
    &DeleteUnwantedChar(\$interest);
    if ($interest) {
        &insertComments($dbh, $colleague_no, $self->user, $interest, 
                        "Research Interest");
        print "The research interest has been inserted into ".font({-color=>'red'}, b("colleague_remark"))." table.", br;
    }
    my $note = param('note');
    &DeleteUnwantedChar(\$note);
    if ($note) {
       my $idList = param('idList');
       &DeleteUnwantedChar(\$idList);
       $idList =~ s/\, */\,/g;
       $idList =~ s/ +/ /g;
       if (!$idList) {
           $idList = "Colleague,$colleague_no";
       }
       elsif ($idList !~ /Colleague,$colleague_no/i) {
	   $idList .= " Colleague,${colleague_no}"; 
       }
       &insertNote4idList($dbh, $self->user, $note, param('isPublic'), 
                          $idList);
       print "The note has been inserted into curator_note table.<br>";
    }

    ###### insert associated loci information into coll_locus table
    my $userLociList = param('associatedLoci');
    $userLociList =~ s/\s+/\|/g;
    my @validatedLociNoList = ();
    if (defined $userLociList) {
	foreach my $locus (split(/\|/, $userLociList)) {
	    if (length($locus) <= 1) { next; }
	    my $locusObj;
	    undef $locusObj;
	    if ($locus =~ /^(\d+)$/) {
		# otherwise, assume $locus contains a locus number
		$locusObj = Locus->new(dbh=>$dbh,
				       locus_no=>$locus
				      );
	    }
	    else {
		# $locus contains a locus name and needs to be looked up
		# print "Trying locus named: \"$locus\"", br;
		$locusObj = Locus->new(dbh=>$dbh,
				       locus_name=>uc($locus)
				      );
	    }

	    if (defined $locusObj) {
		push(@validatedLociNoList, $locusObj->locus_no());
	    }
	    else {
		print "Unable to locate the locus specified by \"", $locus,
		       "\" in the list of associated loci.\n", br;
	    }
	}

	eval {
	    &insertAssociatedLoci($dbh, $colleague_no, @validatedLociNoList);
	};
	if ($@) {
	    print "An error was encountered while attempting to insert associated loci information into the ".font({-color=>'red'}, b("coll_loci"))." table.", p;
	    print $@, br, DBI::errstr, p;

	    print "For precautionary purposes, all updates to the ".font({-color=>'red'}, b("coll_loci"))." table have been rolled back.", p;

	    print "This is the list of associated loci ", code($userLociList),
		  ".  You may wish to copy this information for future use.";

	    $dbh->rollback();

	    $commitInfoErrorCount++;
	}
	else {
	    print "Associated loci information has been inserted into ".font({-color=>'red'}, b("coll_locus"))." table.", br;
	    $dbh->commit();
	}

    }

    print p, b("All updates completed."), p;

    my $id = param('id');

    system("/bin/mv $dir/$id.newEntry $dir/archive/");

    &returnForm($self->user, $dblink);
    &printEndPage;
}

#######################################################################
sub processFile {
#######################################################################
    my ($self) = shift;

    &printStartPage($self->database, $self->title, $self->help);

    if (!$self->id) { $self->{'_id'} = param('id'); }

    if (param('delayCB') =~ /on/i) { 
	&delayFile($self->id, $dir, param('delayComment'), $self->user);
    }
    elsif (param('deleteCB') =~ /on/i) {
	&deleteFile($dir, $self->id.".newEntry");
    }
    elsif (param('convertCB') =~ /on/i) {
        if (param('DBid')) {
	    $self->convert2update;
       	}
	else {
	    $self->err_report("If you want to convert this entry to update, please go back, enter the colleague_no and submit again; otherwise go back, uncheck the 'Convert this entry to update' checkbox and press submit button to commit this entry into database. Thanks");
	    exit;
	}
    }
    &returnForm($self->user, $dblink);
    &printEndPage;
}


#######################################################################
sub convert2update {
#######################################################################
    my ($self) = shift;
    my $DBid = param('DBid');
    my $id = $self->id;
   
    my @oldlist;
    $self->getFullCollInfoBYid($DBid, \@oldlist);
  
    my @newlist;
    my %foundNewItem;
    my %foundOldItem;
    if (-e "$dir/$id.newEntry") {
	open(IN, "$dir/$id.newEntry") || die "colleagueNewEntry: Can't open '$id.newEntry' for reading:$!", br;
    }
    else {
	print "There is no file under $dir with the id ".font({-color=>'red'},$id), p;
	return;
    }
    while(<IN>) {
	chomp;
	if (/(locus|orf)/i) {
	    next;
	}
	push(@newlist, $_);
	$foundNewItem{$_}++;
    }
    close(IN);

    my @deleteList;
    my @commentList;
    my ($colleague_id, $lname, $fname);
    foreach my $list (@oldlist) {
	    my ($name, $value) = split(/=/, $list);
	    &DeleteUnwantedChar(\$name);
	    &DeleteUnwantedChar(\$value);
	    if ($name =~ /Colleague_no/i) {
		$colleague_id = $value;
	    }
	    elsif ($name =~ /Last_Name/i) {
		$lname = $value;
	    }
	    elsif ($name =~ /First_Name/i) {
		$fname = $value;
	    }
	    elsif ($foundNewItem{$list} && $list !~ /Address/i) {
		push(@commentList, $list);
		$foundOldItem{$list}++;
	    }
	    else {
		push(@deleteList, $list);
	    }
    }

    my $updatefile = "$dir/$$.MajorUpdate";
    if (open(OUT, ">$updatefile")) {
	    print OUT "Colleague_no = ${colleague_id}\n";
	    print OUT "\/\/ Last_Name = ${lname}\n";
	    print OUT "\/\/ First_Name = ${fname}\n";
	    foreach my $list (@deleteList) {
		print OUT "-D ${list}\n";
	    }
	    foreach my $list (@newlist) {
		if ($list !~ /Last_Name/i && $list !~ /First_Name/i && 
		    !$foundOldItem{$list}) {
		    print OUT "$list\n";
		}
	    }
	    foreach my $list (@commentList) {
		print OUT "\/\/ $list\n";
	    }
	    close (OUT);
	    system("/bin/mv $dir/${id}.newEntry $dir/archive/${id}.newEntry");
	    system("/bin/mv $dir/${id}.GeneRegistry4ace $dir/$$.GeneRegistry4ace");
	    print "This entry has been converted into update entry <font color=red>$$.MajorUpdate</font>. Please use login page to commit this update into database.<p>";
	    print "The original entry <font color=red>${id}.newEntry</font> has been moved to old directory.<p>";
    }
    else {
	    print "The entry can't be converted into update entry.", br;
    }

}

#######################################################################
sub populateInfo {
#######################################################################
    ## KD:  added stuff to deal with dictyBase contact
    my ($self, $phoneRef, $addrRef, $piRef, 
	$assocsByPiRef, $assocsRef, $keywordRef, $urlRef) = @_;

    my @tempURL;
    my $id = param('id');
    if (-e "$dir/$id.newEntry") {
	open(IN, "$dir/$id.newEntry") || die "colleagueNewEntry: Can't open '$id.newEntry' for reading:$!\n";
    }
    elsif (-e "$dir/$id.GeneRegistryNewEntry") {
	open(IN, "$dir/$id.GeneRegistryNewEntry") || die "colleagueNewEntry: Can't open '$id.GeneRegistryNewEntry' for reading:$!\n";
    }
    else {
	print "There is no file under $dir with the id ".font({-color=>'red'}, $id), p;
	exit;
    }
    while(<IN>) {
	#KD: adding dictyBase contact
	    chomp;
	    my ($name, $value) = split(/ = /);
	    if ($name =~ /Last_Name/i) {
		$self->{'_lname'} = $value;
	    }
	    elsif ($name =~ /CONTACT4dictyBase/i) {
		$self->{'_dictyBasecontact'} = $value;
	    }
	    elsif ($name =~ /First_Name/i) {
		$self->{'_fname'} = $value;
	    }
	    elsif ($name =~ /Suffix/i) {
		$self->{'_suffix'} = $value;
	    }
	    elsif ($name =~ /Other_Name/i) {
		$self->{'_oname'} = $value;
	    }
	    elsif ($name =~ /Internet/i) {
		$self->{'_email'} = $value;
		$self->{'_email'} =~ s/[,;]/ /g;
	    }
	    elsif ($name =~ /Organization/i) {
		$self->{'_institution'} = $value;
	    }
	    elsif ($name =~ /(phone|fax|mobile|pager|telex)/i) {
		push(@$phoneRef, $_);
	    }
	    elsif ($name =~ /url(\d+)/i) {
		$value =~ s/[,;]/ /g;
		$value =~ s/ +/ /g;
		$tempURL[$1][0] = $value;
	    }
	    elsif ($name =~ /webtitle(\d+)/i) {
		$tempURL[$1][1] = $value;
	    }
	    elsif ($name =~ /urltype(\d+)/i) {
		$tempURL[$1][2] = $value;
	    }
	    elsif ($name =~ /address/i) {
		push(@$addrRef, $value);
	    }
	    elsif ($name =~ /city/i) {
		$self->{'_city'} = $value;
	    }
	    elsif ($name =~ /state/i) {
		$self->{'_state'} = $value;
	    }
	    elsif ($name =~ /region/i) {
		$self->{'_region'} = $value;
	    }
	    elsif ($name =~ /country/i) {
		$self->{'_country'} = $value;
	    }
	    elsif ($name =~ /zip_code/i) {
		$self->{'_zip_code'} = $value;
	    }
	    elsif ($name =~ /jobtitle/i) {
		$self->{'_jobtitle'} = $value;
	    }
	    elsif ($name =~ /profession/i) {
		$self->{'_profession'} = $value;
	    }
	    elsif ($name =~ /head_of_lab/i) {
		push(@$piRef, $value);
	    }
	    elsif ($name =~ /PI *\((.+)\)/i) {
		push(@$assocsByPiRef, $1);
	    }
	    elsif ($name =~ /Associate/i) {
		push(@$assocsRef, $value);
	    }
	    elsif ($name =~ /interest/i) {
		$self->{'_interests'} = $value;
	    }
	    elsif ($name =~ /researchTopics/i) {
		$self->{'_researchTopics'} = $value;
	    }
	    elsif ($name =~ /keyword/i) {
		push(@$keywordRef, $value);
	    }
	    elsif ($name =~ /Comments/i) {
		$self->{'_Comments'} = $value;
	    }
    }
    for (my $counter = 0; $counter < $maxNumURLs; $counter++) {
	push (@$urlRef, $tempURL[$counter][0]
	      . "\t" . $tempURL[$counter][1]
	      . "\t" . $tempURL[$counter][2]);
    }
    close(IN);
}

########################################################################
sub processData {
########################################################################
    my ($self) = @_;

   ## KD: adding contact stuff ***
    my $value = param('dictyBasecontact');
    &DeleteUnwantedChar(\$value);
    $self->{'_dictyBasecontact'} = $value;

    my $value = param('lname');
    &DeleteUnwantedChar(\$value);
    $self->{'_lname'} = $value;
    
    $value = param('fname');
    &DeleteUnwantedChar(\$value);
    $self->{'_fname'} = $value;
 
    $value = param('suffix');
    &DeleteUnwantedChar(\$value);
    $self->{'_suffix'} = $value;
 
    $value = param('oname');
    &DeleteUnwantedChar(\$value);
    $self->{'_oname'} = $value;

    $value = param('profession');
    &DeleteUnwantedChar(\$value);
    $self->{'_profession'} = $value;

    $value = param('jobtitle');
    &DeleteUnwantedChar(\$value);
    $self->{'_jobtitle'} = $value;

    $value = param('institution');
    &DeleteUnwantedChar(\$value);
    $self->{'_institution'} = $value;
    my $j;
    for (my $i = 1; $i <= 5; $i++) {
	$value = param("addr$i");
	&DeleteUnwantedChar(\$value);
	if ($value) {
	    $j++;
	    $self->{"_address$j"} = $value;
	}
    }
    $value = param('city');
    &DeleteUnwantedChar(\$value);
    $self->{'_city'} = $value;

    if (param('USAstate')) {
	$self->{'_state'} = param('USAstate');
    }
    else {
	$self->{'_state'} = param('CanadaPro');
    }
    if ($self->{'_state'} =~ /USA State/i ||
	$self->{'_state'} =~ /Canadian Province/i) {
	$self->{'_state'} = "";
    }
    $value = param('region');
    &DeleteUnwantedChar(\$value);
    $self->{'_region'} = $value;
 
    $value = param('country');
    &DeleteUnwantedChar(\$value);
    $self->{'_country'} = $value;

    $value = param('zip_code');
    &DeleteUnwantedChar(\$value);
    $self->{'_zip_code'} = $value;
}

#########################################################################
sub getFullCollInfoBYid {
#########################################################################
    my ($self, $collNo, $CollInfoRef) = @_;
    my $collObject = sdevColleague->new('database'=>$self->database,
					colleagueNo=>$collNo);
    if (!$collObject->lname || !$collObject->fname) { return;}
    push(@$CollInfoRef, "Colleague_no = ".$collObject->colleagueNo);
    push(@$CollInfoRef, "Last_Name = ".$collObject->lname);
    push(@$CollInfoRef, "First_Name = ".$collObject->fname);
    push(@$CollInfoRef, "is_subscribed = ".$collObject->dictyBasecontact);
    if ($collObject->suffix) {
	push(@$CollInfoRef, "Suffix = ".$collObject->suffix);
    }
    if ($collObject->oname) {
	push(@$CollInfoRef, "Other_Name = ".$collObject->oname);
    }
    my $emailRef = $collObject->emailRef;
    foreach my $rowRef (@$emailRef) {
	my ($email) = @$rowRef;
	push(@$CollInfoRef, "Internet = $email");
    }
    if ($collObject->institution) {
	push(@$CollInfoRef, "Organization = ".$collObject->institution);
    }
    my $phoneRef = $collObject->phoneRef;
    foreach my $rowRef (@$phoneRef) { 
	my ($phoneNum, $phoneType, $phoneLoc) = @$rowRef;
	if ($phoneLoc !~ /Unspecified/i) {
	    $phoneType = $phoneLoc." ".$phoneType;
	}
	push(@$CollInfoRef, $phoneType." = ".$phoneNum);
    }
    my $urlRef = $collObject->urlRef; 
    foreach my $rowRef (@$urlRef) {
	my ($url, $wwwNm) = @$rowRef;
	push(@$CollInfoRef, "URL = $url");
    }
    if ($collObject->address1) {
	push(@$CollInfoRef, "Address = ".$collObject->address1);
    }
    if ($collObject->address2) {
	push(@$CollInfoRef, "Address = ".$collObject->address2);
    }
    if ($collObject->address3) {
	push(@$CollInfoRef, "Address = ".$collObject->address3);
    }
    if ($collObject->address4) {
	push(@$CollInfoRef, "Address = ".$collObject->address4);
    }  
    if ($collObject->address5) {
	push(@$CollInfoRef, "Address = ".$collObject->address5);
    } 
    if ($collObject->city) {
	push(@$CollInfoRef, "City = ".$collObject->city);
    }
    if ($collObject->state) {
	push(@$CollInfoRef, "State = ".$collObject->state);
    }
    if ($collObject->region) {
	push(@$CollInfoRef, "Region = ".$collObject->region);
    }
    if ($collObject->country) {
	push(@$CollInfoRef, "Country = ".$collObject->country);
    } 
    if ($collObject->postalCode) {
	push(@$CollInfoRef, "Zip_code = ".$collObject->postalCode);
    } 
    if ($collObject->jobTitle) {
	push(@$CollInfoRef, "JobTitle = ".$collObject->jobTitle);
    }
    if ($collObject->profession) {
	push(@$CollInfoRef, "Profession = ".$collObject->profession);
    }
    my $piRef = $collObject->piRef;
    foreach my $rowRef (@$piRef) {
	my($colleagueNo, $lname, $fname, $suffix) = @$rowRef;
	push(@$CollInfoRef, "Head_of_Lab = ".$lname.", ".$fname);
    }
    my $associate4piRef = $collObject->associate4piRef; 
    foreach my $rowRef (@$associate4piRef) {
	my($colleagueNo, $lname, $fname, $suffix) = @$rowRef;
	my $assoc = $lname.", ".$fname;
	push(@$CollInfoRef, "PI ($assoc) = ".$collObject->lname.", ".$collObject->fname);
    }

    my $pi_without_lab_members = $collObject->isPIWithoutAssocs;
    if ($pi_without_lab_members eq "1") {
	push(@$CollInfoRef, "PI_without_Lab_Members = true");
    }

    my $associateRef = $collObject->associateRef; 
    foreach my $rowRef (@$associateRef) { 
	my($colleagueNo, $lname, $fname, $suffix) = @$rowRef;
	push(@$CollInfoRef, "Associate = ".$lname.", ".$fname);
    }
    if ($collObject->interest) {
	push(@$CollInfoRef, "Interests = ".$collObject->interest);
    }
    my $rtRef = $collObject->researchTopicsRef;
    my $rtList = "";
    foreach my $rowRef (@$rtRef) {
	my ($rt) = @$rowRef;
	$rtList = "\t$rt";
    }
    $rtList .= "\t";
    push(@$CollInfoRef, "ResearchTopics = $rtList");

    my $keywordRef = $collObject->keywordRef;
    foreach my $rowRef (@$keywordRef) { 
	my ($keyword) = @$rowRef;
	push(@$CollInfoRef, "Keyword = $keyword");
    }
    if ($collObject->comment) {
	push(@$CollInfoRef, "Comments = ".$collObject->comment);
    }
}

########################################################################
sub err_report {
########################################################################
    my ($self, $err) = @_;

    &printStartPage($self->database, $self->title, $self->help);
   
    print b($err);
    
    &printEndPage;

    exit;
}

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









