###########################################################################
#
# AutoHierarchy.pm --classifier to create a Hierarchy without the need for a 
#  hierarchy file (like Hierarchy)
# created by Imene, modified by Katherine
# 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 Hierarchical classifier plugin 
# to see the options, run "perl -S classinfo.pl AutoHierarchy"

package AutoHierarchy;

use BasClas;
use sorttools;

sub BEGIN {
    @ISA = ('BasClas');
}

my $arguments = 
    [ { 'name' => "metadata",
	'desc' => "{AZCompactList.metadata}",
	'type' => "metadata",
	'reqd' => "yes" },
      { 'name' => "firstvalueonly",
	'desc' => "{AZCompactList.firstvalueonly}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "allvalues",
	'desc' => "{AZCompactList.allvalues}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "buttonname",
  	'desc' => "{BasClas.buttonname}",
	'type' => "string",
	'deft' => "{BasClas.metadata.deft}",
	'reqd' => "no" },
      { 'name' => "sort",
	'desc' => "{Hierarchy.sort}",
	'type' => "string",
	'deft' => "{BasClas.metadata.deft}",
	'reqd' => "no" },
      { 'name' => "separator",
	'desc' => "{AutoHierarchy.separator}",
	'type' => "regexp",
	'deft' => "[\\\\\\\/|\\\\\\\|]",
	'reqd' => "no" },
      { 'name' => "suppresslastlevel",
	'desc' => "{AutoHierarchy.suppresslastlevel}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "hlist_at_top",
	'desc' => "{Hierarchy.hlist_at_top}",
	'type' => "flag",
	'reqd' => "no" } ];

my $options = { 'name'     => "AutoHierarchy",
		'desc'     => "{AutoHierarchy.desc}",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };


sub new {
    my $class = shift (@_);
    my $self = new BasClas($class, @_);

    # 14-05-02 To allow for proper inheritance of arguments - John Thompson
    my $option_list = $self->{'option_list'};
    push( @{$option_list}, $options );

    if ($self->{'info_only'}) {
	# created from classinfo.pl - don't need to parse the arguments
	return bless $self, $class;
    }

    my ($metadata, $buttonname, $sortname, $separator);
    my $firstvalueonly = 0;
    my $allvalues = 0;
    my $suppresslastlevel = 0;

    if (!parsargv::parse(\@_, 
			 q^metadata/.*/^, \$metadata,
			 q^buttonname/.*/^, \$buttonname,
			 q^sort/.*/^, \$sortname,
			 q^separator/.*/^, \$separator,
			 q^firstvalueonly^, \$firstvalueonly,
			 q^allvalues^, \$allvalues,
			 q^suppresslastlevel^, \$suppresslastlevel,
			 q^hlist_at_top^, \$hlist_at_top,
			 "allow_extra_options")) {
	
	print STDERR "\nIncorrect options passed to $class, check your collect.cfg file\n";
	$self->print_txt_usage("");  # Use default resource bundle
	die "\n";
    }

    if (!$metadata) {
	print STDERR "$class Error: required option -metadata not supplied\n";
	$self->print_txt_usage("");  # Use default resource bundle
	
	die "$class Error: required option -metadata not supplied\n";
    }
    my @meta_list = split(/,/, $metadata);
    $self->{'meta_list'} = \@meta_list;
    
    if (!$separator) {
	$separator = "[\\\/|\\\|]";
    }
    $self->{'separator'} = $separator;

    $buttonname = $self->generate_title_from_metadata($metadata) unless ($buttonname);
    $self->{'title'} = $buttonname;
    $self->{'firstvalueonly'} = $firstvalueonly;
    $self->{'allvalues'} = $allvalues;
    $self->{'suppresslastlevel'} = $suppresslastlevel;
    $self->{'hlist_at_top'} = $hlist_at_top;

    # sortname is handled a bit differently - kjdon
    # undef means to sort, but use the metadata value from -metadata
    # because there is no one metadata value to get for sorting when 
    # we have a list of possible metadata
    # to get no sorting, set sortname = 'nosort'
    if (!$sortname) {
	if (defined ($metadata)) {
	    $sortname = undef;
	} else {
	    $sortname = "nosort";
	}
    }
    $self->{'sortname'} = $sortname;
    
    # the hash that we use to build up the hierarchy
    $self->{'path_hash'}={};
    
    return bless $self, $class;
}

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

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

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

    # are we sorting the list??
    my $nosort = 0;
    if (defined $self->{'sortname'} && $self->{'sortname'} eq "nosort") {
	$nosort = 1;
    }

    my $metavalues = [];
    # find all the metadata values
    foreach $m (@{$self->{'meta_list'}}) {
	my $mvalues = $doc_obj->get_metadata($doc_obj->get_top_section(), $m);
	next unless (@{$mvalues});
	if ($self->{'onlyfirst'}) {
	    # we only want the first metadata value
	    push (@$metavalues, $mvalues[0]);
	    last;
	}
	push (@$metavalues, @$mvalues);
	last if (!$self->{'allvalues'}); # we don't want to try other elements
 	                                 # cos we have already found some 
    } 
    
    return unless (@$metavalues);
    
    #check for a sort element other than our metadata
    my $sortmeta = undef;
    if (!$nosort && defined $self->{'sortname'}) {
	    
	if ($self->{'sortname'} =~ /^filename$/i) {
	    $sortmeta = $doc_obj->get_source_filename();
	} else {
	    $sortmeta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'sortname'});
	    if (defined $sortmeta) {
		$sortmeta = &sorttools::format_metadata_for_sorting($self->{'sortname'}, $sortmeta, $doc_obj);
	    }
	}
	$sortmeta = "" unless defined $sortmeta;
    }

    
    #Add all the metadata values to the hash
    my $path_hash;
    my $current_pos;
    
    foreach $metavalue (@$metavalues) {
	$path_hash = $self->{'path_hash'};
	my @chunks = split (/$self->{'separator'}/, $metavalue);
	if ($self->{'suppresslastlevel'}) {
	    pop(@chunks); # remove the last element from the end
	}
	
	foreach $folderName (@chunks) 
	{
	    if ($folderName ne ""){ #sometimes the tokens are empty
		$current_pos = $self->add_To_Hash($path_hash, $folderName, $nosort);
		$path_hash = $current_pos->{'nodes'};
	    } 
	}
	# now add the document, with sort meta if needed
	if ($nosort) {
	    push(@{$current_pos->{'docs'}}, $doc_OID);
	} else {
	    if (defined $sortmeta) {
		# can you ever get the same doc twice in one classification??
		$current_pos->{'docs'}->{$doc_OID} = $sortmeta;
	    } else {
		$current_pos->{'docs'}->{$doc_OID} = $metavalue;
	    }
	}
    } # foreach metadata
}

sub add_To_Hash {
    my $self = shift (@_);
    my ($myhash, $k, $nosort) = @_;
    
    if (!defined $myhash->{$k}){
	$myhash->{$k}={};
	$myhash->{$k}->{'nodes'}={};
	if ($nosort) {
	    $myhash->{$k}->{'docs'}=[];
	} else {
	    $myhash->{$k}->{'docs'} = {};
	}
    }
    return $myhash->{$k}; 
}

sub print_Hash{
    my $self = shift (@_); 
    my ($myHash, $num_spaces) = @_;

    foreach my $key (keys %{$myHash}){
	print "\n";
	$self->print_spaces($num_spaces);
	print STDERR "$key*";
	$self->print_Hash($myHash->{$key}, $num_spaces + 2);
    }    
}

sub print_spaces{
    my $self = shift (@_);
    my ($num_spaces) = @_;
    
    for ($i = 0; $i < $num_spaces; $i++){
	print STDERR " ";
    }
}

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;
}

sub process_hash {
    my $self = shift (@_);
    my ($top_hash, $top_entry) = @_;   
    my ($entry);
    
    my $hash = {};
    foreach my $key (sort keys %{$top_hash}) {
	$entry = $self->get_entry($key,"VList","VList");
	my $has_content = 0;
	my @doc_list;
	# generate a sorted list of doc ids
	if ($nosort && scalar(@{$top_hash->{$key}->{'docs'}})) {
	    @doc_list = @{$top_hash->{$key}->{'docs'}};
	} elsif (!$nosort && (keys %{$top_hash->{$key}->{'docs'}})) {
	    @doc_list = sort {$top_hash->{$key}->{'docs'}->{$a} 
			      cmp $top_hash->{$key}->{'docs'}->{$b};} keys %{$top_hash->{$key}->{'docs'}};
	    
	}
	# if this key has documents, add them
	if (@doc_list) {
	    $has_content = 1;
	    foreach $d(@doc_list) {
		push (@{$entry->{'contains'}}, {'OID'=>$d});
	    }    
	}
	# if this key has nodes, add them
	if (scalar(keys %{$top_hash->{$key}->{'nodes'}})) {
	    $has_content = 1;
	    $self->process_hash($top_hash->{$key}->{'nodes'}, $entry); 
	}
	# if we have found some content, add the new entry for this key into the parent node
	if ($has_content) {
	    push (@{$top_entry->{'contains'}}, $entry);
	}

    }    
}

sub get_classify_info {
    my $self = shift (@_);
    my ($no_thistype) = @_;
    $no_thistype = 0 unless defined $no_thistype;

    my ($classification);
    my $top_h = $self->{'path_hash'};

    if ($self->{'path_hash'}) {
	if ($self->{'hlist_at_top'}) {
	    $classification = $self->get_entry ($self->{'title'}, "HList", "Invisible");
	}
	else {
	    $classification = $self->get_entry ($self->{'title'}, "VList", "Invisible");
	}
    }

    $self->process_hash($top_h, $classification);
   
    return  $classification;

}


1;



