package EPrints::Plugin::Export::OAI_DARE_DIDL;

# DARE DIDL 2.3.1
# Extends OAI_DRIVER_DIDL class with a DAI mini-mods and different dc:types

use Unicode::String qw( utf8 );

use EPrints::Plugin::Export::OAI_DRIVER_DIDL;

@ISA = qw(EPrints::Plugin::Export::OAI_DRIVER_DIDL);

use strict;


sub new
{
	my( $class, %opts ) = @_;

	my $self = $class->SUPER::new( %opts );

	$self->{name} = "OAI DARE_DIDL v2.3.1";
	$self->{accept} = [ 'dataobj/eprint' ];
	$self->{visible} = "all";
	$self->{suffix} = ".xml";
	$self->{mimetype} = "text/xml";
	
	return $self;
}


sub ep_to_dc_type
# Map your publication types here.
# (the current DARE use of DC (types) is object of discussion)
# 
{
	my( $plugin, $ep_type ) = @_;
	my %types_ep_to_dc = ( #map EP types to DARE/DRIVER defined types
		'article' => 'Article / Letter to editor',
		'article_news' => 'Newspaper article',
		'article_period' => 'Contribution for newspaper or weekly magazine',
		'book' => 'Book (monograph)',
		'book_edit' => 'Book editorial',
		'book_review' => 'Book Review',
		'book_section' => 'Part of book or chapter of book',
		'conference_item' => 'Article in monograph or in proceedings',
		'extreport' => 'External research report',
		'inaugural_lecture' => 'Inaugural lecture',
		'intreport' => 'Internal report',
		#'referee' => 'Referee',
		'still' => 'Still image (photo, video, movie)',
		'thesis' => 'Doctoral thesis',
		#'other' => 'Other',
	);
	return $types_ep_to_dc{ $ep_type };
}


sub fill_didl_element
{
	my ( $plugin, $eprint, $didl, $PURL, $URN ) = @_;
	
	my $top_item = $plugin->{session}->make_element( "didl:Item" );
	$didl->appendChild( $top_item );
	
	$top_item->appendChild( $plugin->desc_identifier( $eprint, $URN ) );
	$top_item->appendChild( $plugin->desc_modified( $eprint ) );
	$top_item->appendChild( $plugin->com_resource( $eprint, $PURL ) );
	$top_item->appendChild( $plugin->item_metadata( $eprint ) );
	$top_item->appendChild( $plugin->item_intermediate_page( $eprint, $PURL ) );
	# DARE-DAI
	$top_item->appendChild( $plugin->item_mods_dai( $eprint ) );
	foreach my $doc ( $eprint->get_all_documents() )	{
		my %files = $doc->files; 
		foreach my $filename ( keys %files ) {
			$top_item->appendChild( $plugin->item_resource_location( $eprint, $doc, $filename) )
		}
	}
}


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

	my $item = $plugin->{session}->make_element( "didl:Item" );

	#humanStartPage
	my $desc = $plugin->{session}->make_element( "didl:Descriptor" );
	my $stat = $plugin->{session}->make_element( "didl:Statement", mimeType=>"application/xml" );
	my $elem = $plugin->{session}->make_element( "dip:ObjectType" );
	$elem->appendChild( $plugin->{session}->make_text( "info:eu-repo/semantics/descriptiveMetadata" ) );
	$stat->appendChild( $elem );
	$desc->appendChild( $stat );
	$item->appendChild( $desc );

	#creators
	
	my $comp = $plugin->{session}->make_element( "didl:Component" );
	my $res = $plugin->{session}->make_element( "didl:Resource", mimeType=>"application/xml" );
	my $mods = $plugin->{session}->make_element( 
	"mods",
	"version"=>"3.2",
	"xmlns"=>"http://www.loc.gov/mods/v3",
	"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
	"xsi:schemaLocation"=>"http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-2.xsd");
	$res->appendChild( $mods );
	$comp->appendChild( $res );
	$item->appendChild( $comp );

	my $title_info = $plugin->{session}->make_element( "titleInfo" );
	#if( $eprint->exists_and_set( "language" ) ) { $res->setAttribute("xml:lang", $eprint->get_value( "language" )); #iso639-2b/639-3!!
	my $title = $plugin->{session}->make_element( "title" );
	$title->appendChild( $plugin->{session}->make_text( $eprint->get_value("title") ) ) if( $eprint->exists_and_set( "title" ) );
	$title_info->appendChild( $title );
	$mods->appendChild( $title_info );
	
	my $ext = $plugin->{session}->make_element( "extension" );
	my $dai_list = $plugin->{session}->make_element(
	"daiList",
	"xmlns:dai"=>"info:eu-repo/dai",
	"xsi:schemaLocation"=>"info:eu-repo/dai http://www.surfgroepen.nl/sites/oai/metadata/Shared%20Documents/dai-extension.xsd");
	
	if( $eprint->exists_and_set( "creators_name" ) )
	{
		my $creators = $eprint->get_value( "creators_name" );
		my $creators_dai = $eprint->get_value( "creators_dai" ) if ( $eprint->exists_and_set( "creators_dai" ) );
		if( defined $creators )
		{
			for ( my $i = 0; $i < @{$creators}; $i++ )
			{	
				next if !defined $creators->[$i];
				my $id = "n" . $eprint->get_value( "eprintid" ). "-" . $i;
				my $name = $plugin->{session}->make_element( "name", type=>"personal", ID=>$id );
				$mods->appendChild( $name );
				
				my $name_part = $plugin->{session}->make_element( "namePart", type=>"given" );
				$name_part->appendChild( $plugin->{session}->make_text( $creators->[$i]->{given} ) );
				$name->appendChild( $name_part );

				$name_part = $plugin->{session}->make_element( "namePart", type=>"family" );
				my $family = $creators->[$i]->{family};
				$family .= ", " . $creators->[$i]->{lineage} if (defined $creators->[$i]->{lineage} && $creators->[$i]->{lineage} ne '');
				$name_part->appendChild( $plugin->{session}->make_text( $family ) );
				$name->appendChild( $name_part );

				my $role = $plugin->{session}->make_element( "role" );
				my $role_term = $plugin->{session}->make_element( "roleTerm", authority=>"marcrelator", type=>"code" );
				$role_term->appendChild( $plugin->{session}->make_text( 'aut' ) );
				$role->appendChild( $role_term );
				$name->appendChild( $role );
				
				my $ident = $plugin->{session}->make_element( "identifier", IDref=>$id,  authority=>"info:eu-repo/dai/nl" );
				my $dai = $creators_dai->[$i] if defined $creators_dai;
					if (defined $dai && $dai ne '') {
						$ident->appendChild( $plugin->{session}->make_text( $dai ) );
						$dai_list->appendChild( $ident );
					}
			}
		}
	}
	
	$ext->appendChild( $dai_list ); #extension at the end, just for readability
	$mods->appendChild( $ext );

	return $item;
}



1;
