# classifier plugin for generating hierarchical classifications

package Hierarchy;

use util;
use cfgread;

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

    if (!defined @options || (scalar @options < 2)) {
	die "Error in options passed to Hierarchy classification\n" .
	    "Usage: Hierarchy subjectfile metadata\n";
    }

    my $subjectfile = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $options[0]);
    if (!-e $subjectfile) {
	my $collfile = $subjectfile;
	$subjectfile = &util::filename_cat($ENV{'GSDLHOME'},"etc", $options[0]);
	if (!-e $subjectfile) {
	    die "Error: Can't locate subject file $options[0]\n" .
		"This file should be in $collfile or $subjectfile\n";
	}
    }

    return bless {
	'descriptorlist'=>{}, # first field in subject file
	'locatorlist'=>{},    # second field in subject file
	'subjectfile' => $subjectfile,
	'metaname' => $options[1]
	}, $class;
}

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

    # read in the subject file
    my $list = &cfgread::read_cfg_file ($self->{'subjectfile'}, undef, '^[^#]?\w');
    
    # $list is a hash that is indexed by the descriptor. The contents of this
    # hash is a list of two items. The first item is the OID and the second item
    # is the title
    foreach $descriptor (keys (%$list)) {
	$self->{'descriptorlist'}->{$descriptor} = $list->{$descriptor}->[0];
	unless (defined $self->{'locatorlist'}->{$list->{$descriptor}->[0]}) {
	    $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'title'} = $list->{$descriptor}->[1];
	    $self->{'locatorlist'}->{$list->{$descriptor}->[0]}->{'contents'} = [];
	}
    }
}

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

    my $doc_OID = $doc_obj->get_OID();
    my $metadata = $doc_obj->get_metadata ($doc_obj->get_top_section(),
					   $self->{'metaname'});

    foreach $metaelement (@$metadata) {
	if ((defined $self->{'descriptorlist'}->{$metaelement}) &&
	    (defined $self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}})) {
	    push (@{$self->{'locatorlist'}->{$self->{'descriptorlist'}->{$metaelement}}->{'contents'}}, $doc_OID);
	}
    }
}


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

    my $classifyinfo = $self->get_entry ($self->{'metaname'}, "Hierarchy");
    foreach $OID (keys (%$list)) {
	my $tempinfo = $self->get_OID_entry ($OID, $classifyinfo, $list->{$OID}->{'title'}, "Hierarchy");
	foreach $subOID (@{$list->{$OID}->{'contents'}}) {
	    push (@{$tempinfo->{'contains'}}, {'OID'=>$subOID});
	}
    }

    return $classifyinfo;
}


sub get_OID_entry {
    my $self = shift (@_);
    my ($OID, $classifyinfo, $title, $classifytype) = @_;

    $OID = "" unless defined $OID;
    $OID =~ s/^\.+//;
    
    my ($headOID, $tailOID) = $OID =~ /^(\d+)(.*)$/;
    $tailOID = "" unless defined $tailOID;


    if (!defined $headOID) {
	$classifyinfo->{'Title'} = $title;
	$classifyinfo->{'classifytype'} = $classifytype;
	return $classifyinfo;
    }

    $classifyinfo->{'contains'} = [] unless defined $classifyinfo->{'contains'};
    
    # add entries to the contains list until we have one for headOID
    while (scalar(@{$classifyinfo->{'contains'}}) < $headOID) {
	push (@{$classifyinfo->{'contains'}}, $self->get_entry("", $classifytype));
    }

    return $self->get_OID_entry ($tailOID, $classifyinfo->{'contains'}->[($headOID-1)], $title, $classifytype);
}

sub get_entry {
    my $self = shift (@_);
    my ($title, $classifytype) = @_;
    
    # organise into classification structure
    my %classifyinfo = ('classifytype'=>$classifytype,
			'Title'=>$title,
			'contains'=>[]);

    return \%classifyinfo;
}

1;
