###########################################################################
#
# jenaTDBBuilder.pm -- topup builder that gets jenaTDB initialized correctly
#
# 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.
#
###########################################################################

package jenaTDBBuilder;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa

use util;
use FileUtils;

use extrabuilder;

sub BEGIN {
    @jenaTDBBuilder::ISA = ('extrabuilder');
}


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

    $self->{'buildtype'} = "jenaTDB";

    return $self;
}


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

    return "jenaTDBBuildproc";
}


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

    my $outhandle = $self->{'outhandle'};
    my $build_dir = $self->{'build_dir'};
    my $verbosity = $self->{'verbosity'};

    print $outhandle "\n*** Apache Jena TDB-configured Tripple store\n"  if ($verbosity >= 1);

    my $jenaTDB_dir = &util::filename_cat($build_dir, "jenaTDB");

    if (! -d $jenaTDB_dir) {
	&FileUtils::makeAllDirectories($jenaTDB_dir);
    }
    
    my $opt_create_index = ($self->{'incremental'}) ? "" : "-removeold";


    if ($opt_create_index) {

	# init an jenaTDB database
	
#	my $adb_filename = &util::filename_cat($jenaTDB_dir,"lsh-features.adb");

	print $outhandle "\n    creating jenaTDB dataset for collection\n"  if ($verbosity >= 1);

#	my $init_cmd = "jenaTDB -N -d $adb_filename";
#	my $init_status = system($init_cmd);
#	if ($init_status != 0) {
#	    print STDERR "Error: failed to initialize the jenaTDB database\n";
#	    print STDERR "         $adb_filename\n";
#	    print STDERR "       $!\n";
#	    if ($verbosity>=2) {
#		print STDERR "       cmd: $init_cmd\n";
#	    }
#	    return;
#	}

    }
    
    # Run the docs through the jenaTDB document processor

    $self->{'buildproc'}->set_mode ('text');
    $self->{'buildproc'}->reset();

    # If '-removeold' in effect (i.e. *not* keepold), then clear out
    # the triple-store graph for this collection

    if (!$self->{'keepold'}) {
	my $collection = $self->{'collection'};
	my $cmd = "gs-triplestore-reset $collection";
		
	my $status = system($cmd);
	if ($status == 0) {
	    print $outhandle "    => removeold: Deleting triplestore graph for $collection\n";
	}
	else {
	    print STDERR "Error: failed to run:\n  $cmd\n$!\n";
	    print STDERR "       Graph has not been deleted\n";
	}
    }

    &plugin::begin($self->{'pluginfo'}, $self->{'source_dir'},
		   $self->{'buildproc'}, $self->{'maxdocs'});
    &plugin::read ($self->{'pluginfo'}, $self->{'source_dir'},
		   "", {}, {}, $self->{'buildproc'}, $self->{'maxdocs'}, 0, $self->{'gli'});
    &plugin::end($self->{'pluginfo'});


}

1;

