###########################################################################
#
# List.pm --
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

# simple list classifier plugin 
# options are:
# metadata=Metaname -- (optional) all documents with Metaname metadata
#                      will be included in list, list will be sorted
#                      by this element. if not included all documents
#                      will be included in list in build (random) order
# title=Title       -- (optional) the title field for this classification.
#                      if not included title field will be Metaname. 
#                      if metadata is also not included title will be 'List'

package List;

use sorttools;

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

    my $list = [];
    my ($metaname, $title);

    foreach $option (@options) {
	if ($option =~ /^metadata=(.*)$/i) {
	    $metaname = $1;
	    $list = {};
	} elsif ($option =~ /^title=(.*)$/i) {
	    $title = $1;
	}
    }

    if (!defined $title) {
	if (defined $metaname) {
	    $title = $metaname;
	} else {
	    $title = 'List';
	}
    }

    return bless {
	'list'=>$list,
	'metaname' => $metaname,
	'title' => $title
	}, $class;
}

sub init {
    my $self = shift (@_);

    if (defined $self->{'metaname'}) {
	$self->{'list'} = {};
    } else {
	$self->{'list'} = [];
    }
}

sub classify {
    my $self = shift (@_);
    my ($doc_obj) = @_;

    my $doc_OID = $doc_obj->get_OID();

    if (defined $self->{'metaname'}) {
	my $metavalue = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
						     $self->{'metaname'});
	if (defined $metavalue) {
	    if ($self->{'metaname'} eq 'Creator') {
		&sorttools::format_string_name_english (\$metavalue);
	    } else {
		&sorttools::format_string_english (\$metavalue);
	    }
	    if (defined $self->{'list'}->{$doc_OID}) {
		print STDERR "metavalue: $metavalue\n";
		print STDERR "title: $self->{'title'}\n";
		print STDERR "WARNING: List::classify called multiple times for $doc_OID\n";
	    } 
	    $self->{'list'}->{$doc_OID} = $metavalue;
	}
    } else {
	push (@{$self->{'list'}}, $doc_OID);
    }
}

sub get_classify_info {
    my $self = shift (@_);

    my $contains = "";
    my @list = ();
    if (defined $self->{'metaname'}) {
	if (keys %{$self->{'list'}}) {
	    @list = sort {$self->{'list'}->{$a} 
			  cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};
	}
    } else {
	@list = @{$self->{'list'}};
    }

    # organise into classification structure
    my %classifyinfo = ('classifytype'=>'List',
			'Title'=>$self->{'title'},
			'contains'=>[]);
    foreach $OID (@list) {
	push (@{$classifyinfo{'contains'}}, {'OID'=>$OID});
    }

    return \%classifyinfo;
}


1;
