package EPrints::Plugin::Export::REF;

# HEFCE REF Export - Abstract class
#
# generic class that can take REF1a/b/c and REF2 data and initialise the appropriate data structures prior to exporting to CSV, XML, ...

use EPrints::Plugin::Export;

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

use strict;

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

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

	$self->{name} = "REF2014 - Abstract Exporter class";
	$self->{accept} = [ 'report/*' ];
	$self->{advertise} = 0;
	$self->{enable} = 1;

	return $self;
}


sub initialise_fh
{
        my( $plugin, $fh ) = @_;

        binmode($fh, ":utf8" );
}

# Turns a REF2014 (EPrints) subject id into:
# 1- the HEFCE code for the UoA
# 2- whether this is part of a multiple submission or not
sub parse_uoa
{
        my( $plugin, $uoa_id ) = @_;

        my ( $hefce_uoa_id, $is_multiple );

	# multiple submission: on EPrints, those UoAs are encoded with an extra 'b' ('bis') at the end e.g. ref2014_a1b for A1
        if( $uoa_id =~ /^ref2014_(\w)(\d+)(b?)$/ )
        {
                $hefce_uoa_id = uc($1).$2;
                $is_multiple = EPrints::Utils::is_set( $3 );
        }
        
        return( $hefce_uoa_id, $is_multiple );
}

# Extracts the UoA from different types of data objects. Exporters (XML, CSV...) need to know the UoA since it's a field for REF.
sub get_current_uoa
{
	my( $plugin, $object ) = @_;

	my $report = $plugin->get_report() or return undef;
	return undef unless( EPrints::Utils::is_set( $report ) );

	if( $report =~ /^ref1[abc]$/ )	# ref1a, ref1b, ref1c
	{
		# $object is EPrints::DataObj::User
		return $object->value( 'ref_uoa' );
	}
	elsif( $report eq 'ref2' )
	{
		# $object is EPrints::DataObj::REFSelection
		return $object->current_uoa();
	}

	return undef;
}

# Which report are we currently exporting? values are set by the calling Screen::Report plugin and are: ref1a, ref1b, ref1c and ref2
sub get_report { shift->{report} }

# Generating a Report usually requires a few data objects (because data's stored in different places in EPrints).
sub get_related_objects
{
	my( $plugin, $dataobj ) = @_;

	my $report = $plugin->get_report();
	return {} unless( EPrints::Utils::is_set( $report ) && defined $dataobj );

	my $objects = {};
	my $session = $plugin->{session};

	if( $report =~ /^ref1[abc]$/ )	# ref1a, ref1b, ref1c
	{
		# we receive a user object and need to give back a "ref circumstance" object
	        $objects = {
        	        user => $dataobj,
                	ref_circ => EPrints::DataObj::REFCirc->new_from_user( $session, $dataobj->get_id ),
	        };
	}
	elsif( $report eq 'ref2' )
	{
		# we receive a ref_selection object, and need to give back a user & eprint object
		$objects = {
			ref_selection => $dataobj,
			eprint => $session->dataset( 'eprint' )->dataobj( $dataobj->value( 'eprint_id' ) ),
			user => $session->dataset( 'user' )->dataobj( $dataobj->value( 'user_id' ) ),
		};
	}

	return $objects;
}

# Returns a list of (HEFCE/REF) fields in the order expected by HEFCE. The defaults are defined in the local configuration (zz_ref_reports.pl)
sub ref_fields_order
{
	my( $plugin ) = @_;

	my $report = $plugin->get_report();

	return [] unless( defined $report );

	return $plugin->{session}->config( 'ref', $report, 'fields' );
}

# Returns mappings between HEFCE/REF fields and EPrints' own fields. Look in zz_ref_reports.pl for more explanation on how this works.
sub ref_fields
{
	my( $plugin ) = @_;

	my $report = $plugin->get_report();
	return [] unless( defined $report );

	return $plugin->{session}->config( 'ref', $report, 'mappings' );
}

1;
