# classifier plugin for sorting by date

# no options - always sorts by 'Date' metadata

# date is assumed to be in the form yyyymmdd

# at present dates are split by year - this should change

package DateList;

use sorttools;

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

    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 $date = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Date');

    # if this document doesn't contain Date element we won't 
    # include it in this classification
    if (defined $date && $date =~ /\d/) {
	if (defined $self->{'list'}->{$doc_OID}) {
	    print STDERR "WARNING: DateList::classify called multiple times for $doc_OID\n";
	} 
	$self->{'list'}->{$doc_OID} = $date;
    }
}


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

    my @classlist = sort {$self->{'list'}->{$a} cmp $self->{'list'}->{$b};} keys %{$self->{'list'}};

    return $self->splitlist (\@classlist);
}


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

    return \%classifyinfo;
}

# splitlist takes an ordered list of classifications (@$classlistref) and splits it
# up into sub-sections by date
sub splitlist {
    my $self = shift (@_);
    my ($classlistref) = @_;
    my $classhash = {};

    # top level
    my $classifyinfo = $self->get_entry ("Date", "DateList");

    # don't need to do any splitting if there are less than 20 classifications
    if ((scalar @$classlistref) <= 20) {
	foreach $subOID (@$classlistref) {
	    push (@{$classifyinfo->{'contains'}}, {'OID'=>$subOID});
	}
	return $classifyinfo;
    }

    # first split up the list into separate year classifications
    foreach $classification (@$classlistref) {
	my $date = $self->{'list'}->{$classification};
	$date =~ s/^(\d\d\d\d).*$/$1/;
	$classhash->{$date} = [] unless defined $classhash->{$date};
	push (@{$classhash->{$date}}, $classification);
    }

    $classhash = $self->compactlist ($classhash);

    foreach $subclass (sort keys %$classhash) {
	my $tempclassify = $self->get_entry($subclass, "DateList");
	foreach $subsubOID (@{$classhash->{$subclass}}) {
	    push (@{$tempclassify->{'contains'}}, {'OID'=>$subsubOID});
	}
	push (@{$classifyinfo->{'contains'}}, $tempclassify);
    }

    return $classifyinfo;
}

sub compactlist {
    my $self = shift (@_);
    my ($classhashref) = @_;
    my $compactedhash = {};
    my @currentOIDs = ();
    my $currentfirstletter = "";
    my $currentlastletter = "";
    my $lastkey = "";

    # minimum and maximum documents to be displayed per page.
    # the actual maximum will be max + (min-1).
    # the smallest sub-section is a single letter at present
    # so in this case there may be many times max documents
    # displayed on a page.
    my $min = 10; 
    my $max = 30;

    foreach $subsection (sort keys %$classhashref) {
	$currentfirstletter = $subsection if $currentfirstletter eq "";
	if ((scalar (@currentOIDs) < $min) ||
	    ((scalar (@currentOIDs) + scalar (@{$classhashref->{$subsection}})) <= $max)) {
	    push (@currentOIDs, @{$classhashref->{$subsection}});
	    $currentlastletter = $subsection;
	} else {

	    if ($currentfirstletter eq $currentlastletter) {
		@{$compactedhash->{$currentfirstletter}} = @currentOIDs;
		$lastkey = $currentfirstletter;
	    } else {
		@{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
		$lastkey = "$currentfirstletter-$currentlastletter";
	    } 
	    if (scalar (@{$classhashref->{$subsection}}) >= $max) {
		$compactedhash->{$subsection} = $classhashref->{$subsection};
		@currentOIDs = ();
		$currentfirstletter = "";
	    } else {
		@currentOIDs = @{$classhashref->{$subsection}};
		$currentfirstletter = $subsection;
		$currentlastletter = $subsection;
	    }
	}
    }

    # add final OIDs to last sub-classification if there aren't many otherwise
    # add final sub-classification
    if (scalar (@currentOIDs) < $min) {
	my ($newkey) = $lastkey =~ /^(.)/;
	@currentOIDs = (@{$compactedhash->{$lastkey}}, @currentOIDs);
	delete $compactedhash->{$lastkey};
	@{$compactedhash->{"$newkey-$currentlastletter"}} = @currentOIDs;	
    } else {
	if ($currentfirstletter eq $currentlastletter) {
	    @{$compactedhash->{$currentfirstletter}} = @currentOIDs;
	} else {
	    @{$compactedhash->{"$currentfirstletter-$currentlastletter"}} = @currentOIDs;
	} 
    }

    return $compactedhash;
}

1;
