
sub _get_value_from_child 
{
  my ($parentnode, $nodename)=@_;
  my $value="";
  my @elements=$parentnode->getElementsByTagName($nodename) unless (!$parentnode);
  if($elements[0]) { my $child=$elements[0]->getFirstChild(); if($child) {$value=$child->getData;}};
  return $value;
}


sub _set_eprint_field_from_value
{
  my ($eprint, $eprintfield, $value)=@_;
  $eprint->set_value($eprintfield, $value);
}

sub _set_eprint_field_from_node
{
  my ($eprint, $eprintfield, $parentnode, $nodename)=@_;
  my $value=_get_value_from_child($parentnode, $nodename);
  _set_eprint_field_from_value($eprint, $eprintfield, $value) unless (!$value);

}


sub import_pubmed
{
	my($eprint, $pmid) =@_;

	my %months= ( 	Jan => "01", Feb => "02", Mar => "03", Apr => "04",
			May => "05", Jun => "06", Jul => "07", Aug => "08",
			Sep => "09", Oct => "10", Nov => "11", Dec => "12" );

	if(!$eprint) {
#			open(LOG , ">/tmp/pmid.log");
#			print LOG "No eprint";
#			close(LOG);
			return;
		}

	# first, fetch from pubmed
	my $pubmed_xml=`wget -O - http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed\\\&retmode=xml\\\&rettype=abstract\\\&id=$pmid 2>/dev/null`;


	# then parse,
	my $doc=EPrints::XML::parse_xml_string($pubmed_xml);
	if (!$doc) {return;}
	my $root = $doc->getDocumentElement();
	my @pubmedarticle=$root->getElementsByTagName("PubmedArticle"); 
	my @citation=$pubmedarticle[0]->getElementsByTagName("MedlineCitation");


	my @articles=$citation[0]->getElementsByTagName("Article");
 	my $article=$articles[0];
	if ( lc($article->getNodeName ) ne "article" ) { die "Error parsing pubmed info"; }

	for my $journal ($article->getElementsByTagName("Journal"))
	{
	       _set_eprint_field_from_node($eprint, "issn",$journal,"ISSN");
	       for my $issue ($journal->getElementsByTagName("JournalIssue"))
	       {
		       _set_eprint_field_from_node($eprint, "volume", $issue, "Volume");
		       _set_eprint_field_from_node($eprint, "number", $issue, "Issue");
			my @pubdates=$issue->getElementsByTagName("PubDate");
			if(@pubdates) {
				my $issue_date=_get_value_from_child($pubdates[0],"Year")."-".
					$months{_get_value_from_child($pubdates[0],"Month")}."-".
					_get_value_from_child($pubdates[0],"Day");

				_set_eprint_field_from_value($eprint, "date_issue", $issue_date);
			}
		}		
	}

        _set_eprint_field_from_node($eprint, "title", $article, "ArticleTitle");

	for my $pagination ($article->getElementsByTagName("Pagination"))
	{
		_set_eprint_field_from_node($eprint, "pagerange", $pagination, "MedlinePgn");
	}

	for my $abstract ($article->getElementsByTagName("Abstract"))
	{
        	_set_eprint_field_from_node($eprint, "abstract", $abstract, "AbstractText");
	}

	my @creators=();
	my @authorlist=$article->getElementsByTagName("AuthorList");
	if(@authorlist) {
		for my $author ($authorlist[0]->getElementsByTagName("Author"))
		{
		  my $creator={}; 
		  $creator->{family}=_get_value_from_child($author,"LastName");
		  $creator->{given}=_get_value_from_child($author,"FirstName");
		  if(!$creator->{given}) { $creator->{given} =_get_value_from_child($author,"Initials"); }
		  if(!$creator->{given}) { $creator->{given} =_get_value_from_child($author,"ForeName"); }
		  push @creators,{id=>"", main=>$creator};
		};
	};

	_set_eprint_field_from_value($eprint, "creators", \@creators);

	for my $medlinejournalinfo ($citation[0]->getElementsByTagName("MedlineJournalInfo"))
	{
        	_set_eprint_field_from_node($eprint, "publication", $medlinejournalinfo, "MedlineTA");
	}

}



