###########################################################################
#
# CSTRList.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.
#
###########################################################################

# hacked about version of List classifier for use with cstr collection. 
# Creates list of Source's (ftp sites), each of which contains a list of
# those documents originating from Source.

# note that 'Source' metadata contains entire path to document so
# we strip it down to get the ftp site.

package CSTRList;

use sorttools;

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

    my $listname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/classify/List.pm"); 
    if (-e $listname) { require $listname; }
    else { die "CSTRList ERROR - couldn't find classifier \"$listname\"\n"; }

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

sub init {
    my $self = shift (@_);
    $self->{'list'} = {};
}

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

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

    my $source = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Source');

    if (defined $source) {
	my ($ftp_site) = $source =~ /^((ftp:\/\/)?([^\/\\]*))/;
	print STDERR "CSTRList: Warning: ftp_site undefined\n" unless defined $ftp_site;
	if (!defined ($self->{'list'}->{$ftp_site})) {
	    my ($listclassobj);
	    eval ("\$listclassobj = new List(\"sort=Date\", \"title=\$ftp_site\")");
	    die "$@" if $@;
	    $listclassobj->init();
	    $self->{'list'}->{$ftp_site} = $listclassobj;
	    $self->{'list'}->{$ftp_site}->classify ($doc_obj);
	} else {
	    $self->{'list'}->{$ftp_site}->classify ($doc_obj);
	}	    
    }
}

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

    # organise into classification structure
    my %classifyinfo = ('thistype'=>'Invisible',
			'childtype'=>'VList',
			'Title'=>'Browse',
			'contains'=>[]);
    foreach $ftp_site (sort keys %{$self->{'list'}}) {
	push (@{$classifyinfo{'contains'}}, 
	      $self->{'list'}->{$ftp_site}->get_classify_info());
    }
    
    return \%classifyinfo;
}


1;
