######################################################################
#
# EPrints::Plugin::Sword::Import::Png
#
######################################################################
#
#
# Copyright 2000-2008 University of Southampton. All Rights Reserved.
# 
#  This file is part of GNU EPrints 3.
#  
#  Copyright (c) 2000-2008 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 3 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 3 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 3; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

######################################################################
#
# PURPOSE:
#
#	This handles JPEG files and attaches them to an empty EPrint object.
#	This is a copy of the Pdf module.
#
# METHODS:
#
# input_files( $plugin, %opts ):
#       The method called by DepositHandler. The %opts hash contains
#       information on which files to import.
#
######################################################################

package EPrints::Plugin::Sword::Import::Png;

use strict;

use EPrints::Plugin::Import::TextFile;

our @ISA = qw/ EPrints::Plugin::Import::TextFile /;

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

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

	$self->{name} = "SWORD Importer - PNG";
	$self->{visible} = "all";
	
	$self->{accept} = "image/x-png";

	return $self;
}


sub input_file
{
	my ( $plugin, %opts ) = @_;

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

	# We need to get ALL the files of type 'image/png'
	my $f = EPrints::Sword::Utils::get_file_to_import( $session, $opts{files}, "image/x-png", 1 );

	return unless( defined $f );

	my @files = @{$f};

	my $dir = $opts{dir};

	my $dataset_id = $opts{dataset_id};
	my $owner_id = $opts{owner_id};
	my $depositor_id = $opts{depositor_id};

	my $dataset = $session->get_archive()->get_dataset( $dataset_id );

	if(!defined $dataset)
        {
                print STDERR "\n[SWORD-PNG] [INTERNAL-ERROR] Failed to open the dataset '$dataset_id'.";
                return;
        }

	my $epdata = {};
	if(defined $depositor_id)
	{
		$epdata->{userid} = $owner_id;
		$epdata->{sword_depositor} = $depositor_id;
	}
	else
	{
		$epdata->{userid} = $owner_id;
	}
	
	$epdata->{eprint_status} = $dataset_id;


	# minimal amount of metadata!
	my $eprint = $dataset->create_object( $plugin->{session}, $epdata );
		
	return unless( defined $eprint );
	
	# Attaching every PNG found:
	foreach my $infile (@files)
	{
	       my $unpack_dir;
	       my $fn;

	       $fn = $infile;

	       if( $fn =~ /^(.*)\/(.*)$/ )
	       {
             		$unpack_dir = $1;
	                $fn = $2;
	       }

	       my %doc_data;
	       $doc_data{eprintid} = $eprint->get_id;
	       $doc_data{format} = "image/x-png";

		$doc_data{main} = $fn;

		my %file_data;

	        $file_data{filename} = $fn;
	        $file_data{data} = $infile;

        	$doc_data{files} = [ \%file_data ];

		my $doc_dataset = $session->get_repository->get_dataset( "document" );

		my $document = EPrints::DataObj::Document->create_from_data( $session, \%doc_data, $doc_dataset );

                if(!defined $document)
                {
                        print STDERR "\n[SWORD-PNG] [ERROR] Failed to add the attached file to the eprint.";
	        }
		else
		{
			$document->make_thumbnails();			
		}

	}

	$eprint->generate_static();

	return $eprint->get_id;
}



1;
