###########################################################################
#
# NpepaList.pm -- website plugin
#
# 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.
#
###########################################################################

# There should probably be a general classifier to do this kind of stuff

package NPepaList;

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

    return bless {
	'list'=>{},
	'metaname' => 'Series',
	'sortname' => 'Date'
	}, $class;
}

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

    $self->{'list'} = {};
}


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

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

    # want to ignore abstracts
    my $doctype = $doc_obj->get_metadata_element($doc_obj->get_top_section(), 'doctype');
    return if (defined $doctype && $doctype eq 'Description');

    my $date = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
					      $self->{'sortname'});
    # commentaries have no date but we want them to sort to the top
    $date = '00000000' unless defined $date;

    my $series = $doc_obj->get_metadata_element ($doc_obj->get_top_section(),
						 $self->{'metaname'});

    if (defined $series) {
	if (!defined $self->{'list'}->{$series}) {
	    $self->{'list'}->{$series} = {};
	}
	if (defined $self->{'list'}->{$series}->{$doc_OID}) {
	    print STDERR "WARNING: NPepaList::classify called multiple times for $doc_OID\n";
	} 
	$self->{'list'}->{$series}->{$doc_OID} = $date;
    } else {
	print STDERR "WARNING: NpepaList::classify $doc_OID has no Series metadata\n";
    }
}


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

    my $classifyinfo = $self->get_entry ('Series', 'VList', 'Invisible');

    foreach $series (sort keys %{$self->{'list'}}) {
	my $cinfo = $self->get_entry ($series, 'VList');
	foreach $OID (sort {$self->{'list'}->{$series}->{$a} 
			    cmp $self->{'list'}->{$series}->{$b};} keys %{$self->{'list'}->{$series}}) {
	    push (@{$cinfo->{'contains'}}, {'OID'=>$OID});
	}
	push (@{$classifyinfo->{'contains'}}, $cinfo);
    }

    return $classifyinfo;
}

sub get_entry {
    my $self = shift (@_);
    my ($title, $childtype, $thistype) = @_;
    
    # organise into classification structure
    my %classifyinfo = ('childtype'=>$childtype,
			'Title'=>$title,
			'contains'=>[]);
    $classifyinfo{'thistype'} = $thistype 
	if defined $thistype && $thistype =~ /\w/;

    return \%classifyinfo;
}

1;
