package GO::Object::Xref;

=head1 NAME

GO::Object::Xref       - represents a basic GO::Object::Xref object

=cut

use strict;
use Data::Dumper;
use Exporter;
use lib '/Users/gwg/go/scratch/tools';
use vars qw(@ISA);
use base qw(GO::Object::Generic); #GO::MiniTests);
use GO::TestSet qw(dfv_test);


sub _specification {
	my $self = shift;
	
	return (
		# required
		"db", {
			test => dfv_test('is_a_string_p'),
#			required => 1,
		},
		"local_id", {
			test => dfv_test('is_a_string_p'),
			dependencies => [ 'db' ],
#			required => 1,
		},
		"href", {
			automatic => 1,
		#	requires the xref_abbs file
#			test => 'is_an_url_p',
		},
		"xref", {
			test => dfv_test('is_xref_like_p'),
#			required => 1,
		},
	);
}


sub _dfv_data {
	return {
#		require_some => [ 1, 'local_id', 'xref' ],
		debug => 1,
#		constraint_methods => {
#			xref => 
#		}
	};
}


sub href {
	my $self = shift;
	return $self->{href} if $self->{href};
	if (lc $self->{db} eq 'pmid')
	{	return "http://www.ncbi.nlm.nih.gov/pubmed/".$self->{local_id};
	}
	elsif (lc $self->{db} eq 'doi')
	{	return "http://www.doi.org/DOI:".$self->{local_id};
	}
	
	return;
}

sub xref {
	my $self = shift;
	if (!$self->{xref})
	{	$self->{xref} = ( $self->{db} || "" ) . ":" . ( $self->{local_id} || "" );
	}
	return $self->{xref};
}

sub db {
	my $self = shift;
	$self->_set_db_and_local_id if ! defined $self->{db};
	return $self->{db};
}

sub local_id {
	my $self = shift;
	$self->_set_db_and_local_id if ! defined $self->{local_id};
	return $self->{local_id};
}

sub _set_db_and_local_id {
	my $self = shift;
	if ($self->{xref} && $self->{xref} =~ /(.+?):(.*)/)
	{	$self->{db} = $1;
		$self->{local_id} = $2;
	}
}

sub all_params_to_text {
	my $self = shift;
	my $obj = shift;
	my $str = shift || '';
	
	return $str . $self->xref . "\n";
}


# initialise the DB and local_id if they don't already exist
sub _initialise {
	my $self = shift;
	my $results = $self->SUPER::_initialise(@_);
	if ($results)
	{	if (ref($results) eq 'HASH' && $results->{OBJECT})
		{	$results->{OBJECT}->_set_db_and_local_id();
		}
		else
		{	$results->_set_db_and_local_id();
		}
	}
	return $results;
}


=cut
## when we initialise the object, create the db and local_ids
sub transform_parsed_data {
	my $self = shift;
	my $arg_h = shift;
	my $data_h = $arg_h->{data};

	my $object_spec = $arg_h->{object_spec} || $self->get_spec;
	
	## do any transformations here
	if ($data_h->{xref})
	{	if (!$data_h->{db} || !$data_h->{local_id})
		{	if ($data_h->{xref} =~ /(.+?):(.*)/)
			{	$data_h->{db} = $1;
				$data_h->{local_id} = $2;
			}
		}
	}
	elsif ($data_h->{db} && $data_h->{local_id})
	{	$data_h->{xref} = $data_h->{db} . ":" . $data_h->{local_id};
	}
	return $data_h;
}
=cut


1;