# OME/ImportEngine/BioFormats.pm

#-------------------------------------------------------------------------------
#
# Copyright (C) 2003 Open Microscopy Environment
#       Massachusetts Institute of Technology,
#       National Institutes of Health,
#       University of Dundee
#
#
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library 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
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#-------------------------------------------------------------------------------




#-------------------------------------------------------------------------------
#
# Written by:    Ilya Goldberg <igg@nih.gov>
#
#-------------------------------------------------------------------------------


package OME::ImportEngine::BioFormats;

use strict;
use OME;
our $VERSION = $OME::VERSION;
use Carp;
use Log::Agent;

use OME::Image::Server;
use OME::Tasks::OMEImport;

use base qw(OME::ImportEngine::AbstractFormat);


# We call OMEIS to tell us if files are recognized by BioFormats (IsBioFormats)
# It returns a list of FIleIDs that BioFormats can potentially interpret.
sub getGroups {
my $self = shift;
my $fhash = shift;
my @inlist = keys %$fhash; # an array of file IDs
my $file;

	my $result;
	# we're wrapping this in an eval because an error will be generated by OMEIS if
	# BioFormats isn't installed.  Here, we'll ignore this silently.
	eval {
		$result = OME::Image::Server->isBioFormats ( \@inlist );
	};
	logdbg "debug", ref ($self)."->getGroups: IsBioFormats error: $@" if $@;
	return [] if $@;
	
	logdbg "debug", ref ($self)."->getGroups: IsBioFormats result: $result";

	# Result is a string containing line-delimited groups containing space-delimited FileIDs
	my $grouplist = [];
	my @outlist;
	my @lines = split (/\n/,$result);
	for (my $i=0; $i < scalar @lines; $i++) {
		my @grFiles = split (/\s/,$lines[$i]);
		foreach my $fileID (@grFiles) {
	    	next unless exists $fhash->{$fileID};
			logdbg "debug", ref ($self)."->getGroups: BioFormats recognized: ".$fhash->{$fileID}->getFilename();
		    push (@outlist,$fhash->{$fileID});
		    push (@{$grouplist->[$i]},$fhash->{$fileID});
		}
	}
	
    # Clean out the file list.
    $self->removeFiles($fhash,\@outlist);

	# and return our grouplist
	return $grouplist;
}



sub importGroup {
    my ($self,$files, $callback) = @_;
    my $session = $self->Session();
    my @images;
    my $object;
    my $omeImport = OME::Tasks::OMEImport-> new(
	    session => $session,
	    # XXX: Debugging off.
	    #debug => 1
	);
	
	my $file;
	my @FileIDs;
	foreach $file (@$files) {
		logdbg "debug", ref ($self)."->importGroup: Importing BioFormats file: ".$file->getFilename();
		push (@FileIDs,$file->getFileID());
	}

	my $objects = $omeImport->importXMLstring (
		OME::Image::Server->ImportBioFormats( \@FileIDs ),
		NoDuplicates           => 0,
		IgnoreAlterTableErrors => 1,
	) or return (undef);

	logdbg "debug", ref ($self)."->importGroup: XML objects: ".scalar @$objects;
	
	my @OrigFIles;
	foreach $file (@$files) {
		push (@OrigFIles,$self->touchOriginalFile ($file,'BioFormats'));
	}

	foreach $object (@$objects) {
		if (UNIVERSAL::isa($object,'OME::Image')) {
			foreach my $pixels ($object->pixels() ) {
				$self->storeDisplayOptions($object);
				OME::Tasks::PixelsManager->saveThumb( $pixels );
			}
			OME::Tasks::ImportManager->markImageFiles(
				$object,
				\@OrigFIles);
			logdbg "debug", ref ($self)."->importGroup: Image Name: ".$object->name();
			push (@images,$object) ;
		}
	}
	
	$self->doSliceCallback($callback);
	
	return \@images;
}

sub getSHA1 {
    my $self = shift;
    my $file = shift;
    return $file->getSHA1();
}


1;
