package EPrints::Plugin::Export::OAI_DRIVER_Export;

# OAI DRIVER export base plug-in, extends EPrints::Plugin::Export
#
# Version 1.1
#
# 10-01-2008, Arnoud Jippes, University of Twente, NL
#
# Waiting for validating report
# See: http://www.driver-support.eu/documents/DRIVER_guidelines_annexes_1_1_1.pdf

use Unicode::String qw( utf8 );
use MIME::Types;
use EPrints::Plugin::Export;

@ISA = ( "EPrints::Plugin::Export" );

use strict;

sub ep_to_dc_type
# Map your publication types here to DRIVER types:
# Article/Book/Conference lecture/Conference report/Contribution for newspaper or weekly magazine
# Doctoral thesis/Master thesis/Bachelor thesis/External research report/Lecture (inaugural or other)
# Internal report/Newsletter/Part of book or chapter of book/Research paper

{
	my( $plugin, $ep_type ) = @_;
	my %types_ep_to_dc = ( #map EP types to DARE/DRIVER defined types
		'article' => 'Article',
		'article_news' => 'Newsletter',
		'article_period' => 'Contribution for newspaper or weekly magazine',
		'book' => 'Book',
		'book_edit' => 'Part of book or chapter of book',
		'book_review' => 'Part of book or chapter of book',
		'book_section' => 'Part of book or chapter of book',
		'conference_item' => 'Conference lecture',
		'extreport' => 'External research report',
		'inaugural_lecture' => 'Lecture (inaugural or other)',
		'intreport' => 'Internal report',
		'thesis' => 'Master thesis',
		#'referee' => 'Referee',
		#'still' => 'Still image (photo, video, movie)',
		#'other' => 'Other',
	);
	return $types_ep_to_dc{ $ep_type };
}

sub mimetype
# Get mime-type of main or sub-file (if unknown, make a guess based on file extension)
{
	my( $plugin, $doc, $filename ) = @_;
	
	my $format;
	if ( $filename eq $doc->get_value("main") ) { # respect the assigned mime-type
		my $oai_mimetypes = $plugin->{session}->get_repository->get_conf( "oai", "mime_types" );
		$format = $oai_mimetypes->{ $doc->get_value( "format" ) }; # use mapping in /cfg.d/oai.pl (don't map 'other')
		$format = $doc->get_value( "format" ) unless defined $format;
	}
	if ( !defined $format || $format eq "other" ) {
		my $mimetypes = MIME::Types->new;
		my MIME::Type $format_type = $mimetypes->mimeTypeOf($filename);
		$format  = $format_type->type if defined $format_type;
	 	$format = "application/octet-stream" if !defined $format; # fallback to octet if all else fails
	}

	return $format;
}


sub xml_dc
{
	my( $plugin, $dataobj ) = @_;

	my $data = $plugin->convert_dc( $dataobj );

	my $dc = $plugin->{session}->make_element(
        	"oai_dc:dc",
		"xmlns:oai_dc" => "http://www.openarchives.org/OAI/2.0/oai_dc/",
        	"xmlns:dc" => "http://purl.org/dc/elements/1.1/",
        	"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
		"xsi:schemaLocation" =>
 	"http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd" );

	# turn the list of pairs into XML blocks (indented by 8) and add them
	# them to the DC element.
	foreach( @{$data} )
	{
		$dc->appendChild(  $plugin->{session}->render_data_element( 8, "dc:".$_->[0], $_->[1] ) );
		# produces <key>value</key>
	}

	return $dc;
}

sub convert_dc
{
	my( $plugin, $eprint ) = @_;

	my @dcdata = ();
	push @dcdata, [ "title", $eprint->get_value( "title" ) ] if( $eprint->exists_and_set( "title" ) ); 

	# grab the creators without the ID parts so if the site admin
	# sets or unsets creators to having and ID part it will make
	# no difference to this bit.

  #DC:CREATOR
	if( $eprint->exists_and_set( "creators_name" ) )
	{
		my $creators = $eprint->get_value( "creators_name" );
		if( defined $creators )
		{
			foreach my $creator ( @{$creators} )
			{	
				next if !defined $creator;
				push @dcdata, [ "creator", EPrints::Utils::make_name_string( $creator ) ];
			}
		}
	}


  #DC:DESCRIPTION
	push @dcdata, [ "description", $eprint->get_value( "abstract" ) ] if( $eprint->exists_and_set( "abstract" ) ); 
	
	#DC:PUBLISHER
	push @dcdata, [ "publisher", $eprint->get_value( "publisher" ) ] if( $eprint->exists_and_set( "publisher" ) ); 

  #DC:CONTRIBUTER
	if( $eprint->exists_and_set( "editors_name" ) )
	{
		my $editors = $eprint->get_value( "editors_name" );
		if( defined $editors )
		{
			foreach my $editor ( @{$editors} )
			{
				push @dcdata, [ "contributor", EPrints::Utils::make_name_string( $editor ) ];
			}
		}
	}

	#DC:DATE
	# Date for discovery. For a month/day we don't have, assume 01.
	if( $eprint->exists_and_set( "date" ) )
	{
		my $date = $eprint->get_value( "date" );
		if( defined $date )
		{
			$date =~ s/(-0+)+$//;
			push @dcdata, [ "date", $date ];
		}
	}

  #DC:FORMAT
  #DC:RELATION
  #TODO: Mimetypes of all files? (this is only main files)
	my @documents = $eprint->get_all_documents();
	my $mimetypes = $plugin->{session}->get_repository->get_conf( "oai", "mime_types" );
	foreach( @documents )
	{
		push @dcdata, [ "format", $plugin->mimetype( $_, $_->get_value("main")) ];
		push @dcdata, [ "relation", $_->get_url() ];
	}

	#DC:IDENTIFIER
	# Persistent URL can be defined in /cfg.d
	my $PURL = $eprint->get_url;
	my $base_purl = $plugin->{session}->get_repository->get_conf( "base_purl" );
	$PURL = $base_purl . $eprint->get_value( "eprintid" ) if defined $base_purl;

	push @dcdata, [ "identifier", $PURL ];

  #DC.RIGHTS
	push @dcdata, [ "rights", $eprint->get_value( "rights" ) ] if( $eprint->exists_and_set( "rights" ) ); 
  
	#DC:TYPE
	if( $eprint->exists_and_set( "type" ) )
	{
		my $type = $plugin->ep_to_dc_type( $eprint->get_value( "type") );
		push @dcdata, [ "type", $type ] if defined $type; 
#		push @dcdata, [ "type", EPrints::Utils::tree_to_utf8( $eprint->render_value( "type" ) ) ];
	}

	return \@dcdata;
}



1;
