Index: /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuilder.pm
===================================================================
--- /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuilder.pm	(revision 38290)
+++ /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuilder.pm	(revision 38290)
@@ -0,0 +1,136 @@
+###########################################################################
+#
+# sasOpenAnnotationBuilder.pm -- topup builder that gets sasOpenAnnotation 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 sasOpenAnnotationBuilder;
+
+use strict;
+no strict 'refs'; # allow filehandles to be variables and viceversa
+
+use util;
+use FileUtils;
+
+use extrabuilder;
+
+sub BEGIN {
+    @sasOpenAnnotationBuilder::ISA = ('extrabuilder');
+}
+
+
+sub new {
+    my $class = shift(@_);
+    my $self = new extrabuilder (@_);
+    $self = bless $self, $class;
+
+    $self->{'buildtype'} = "sasOpenAnnotation";
+
+    return $self;
+}
+
+
+sub default_buildproc {
+    my $self  = shift (@_);
+
+    return "sasOpenAnnotationBuildproc";
+}
+
+
+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*** SimpleAnnotationStore (SAS) Indexing/Storing OpenAnnotations\n"  if ($verbosity >= 1);
+
+#    my $sasOpenAnnotation_dir = &util::filename_cat($build_dir, "sasOpenAnnotation");
+
+#    if (! -d $sasOpenAnnotation_dir) {
+#	&FileUtils::makeAllDirectories($sasOpenAnnotation_dir);
+#    }
+    
+    my $opt_create_index = ($self->{'incremental'}) ? "" : "-removeold";
+
+
+    if ($opt_create_index) {
+
+	# init an sasOpenAnnotation database
+	
+#	my $adb_filename = &util::filename_cat($sasOpenAnnotation_dir,"lsh-features.adb");
+
+#	print $outhandle "\n    creating sasOpenAnnotation dataset for collection\n"  if ($verbosity >= 1);
+
+#	my $init_cmd = "sasOpenAnnotation -N -d $adb_filename";
+#	my $init_status = system($init_cmd);
+#	if ($init_status != 0) {
+#	    print STDERR "Error: failed to initialize the sasOpenAnnotation 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 sasOpenAnnotation document processor
+
+    $self->{'buildproc'}->set_mode ('text');
+    $self->{'buildproc'}->reset();
+
+    # If '-removeold' in effect (i.e. *not* keepold), then clear out
+    # the IIIFManifest graphs for this collection
+
+    if (!$self->{'keepold'}) {
+	print STDERR "**** Find out if it is possible to delete (for each document in the last build) \n";
+	print STDERR "***** all exising openannotation-list<num>.json entries\n";
+	
+
+#	my $collection = $self->{'collection'};
+#	my $cmd = "gs-triplestore-reset3 $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;
Index: /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuildproc.pm
===================================================================
--- /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuildproc.pm	(revision 38290)
+++ /gs3-extensions/structured-image/trunk/perllib/sasOpenAnnotationBuildproc.pm	(revision 38290)
@@ -0,0 +1,177 @@
+##########################################################################
+#
+# sasOpenAnnotationBuildproc.pm -- called by sasOpenAnnotationBuidler, per doc
+# 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 sasOpenAnnotationBuildproc;
+
+use strict; 
+no strict 'refs'; # allow filehandles to be variables and viceversa
+
+use util;
+
+use extrabuildproc;
+
+
+BEGIN {
+    @sasOpenAnnotationBuildproc::ISA = ('extrabuildproc');
+}
+
+sub new()
+  {
+    my $class = shift @_;
+
+    my $self = new extrabuildproc (@_);
+
+    return bless $self, $class;
+}
+
+
+sub textedit {
+    my $self = shift (@_);
+    my ($doc_obj,$file,$mode) = @_;
+
+    # Code written on the assumption that that sasOpenAnnotation does a replace
+    # operation when presented with a docid that already extis.
+    # => don't need to do anything special to distinguish between
+    #    a mode of "add" and "update"
+
+    # **** !!!! Need to check the above is true!!
+    
+    my $outhandle = $self->{'outhandle'};
+
+    my $source_dir = $self->{'source_dir'}; # typically the archives dir
+    my $build_dir  = $self->{'build_dir'};
+
+    # full path to adb database
+#    my $adb_filename 
+#	= &util::filename_cat($build_dir, "sasOpenAnnotation", "lsh-features.adb");
+
+    # get doc id
+    my $doc_oid = $doc_obj->get_OID();
+
+    # work out if we need to process it
+    my $top_section = $doc_obj->get_top_section();
+    my $tl_contains_openannotation_json = $doc_obj->get_metadata_element($top_section,"ContainsOpenAnnotationJSON");
+	
+    if ((defined $tl_contains_openannotation_json) && ($tl_contains_openannotation_json eq "1")) {
+	print STDERR "*** ### Processing $doc_oid for its OpenAnnotation associated files\n";
+	
+	# map to assoc dir
+	my $assoc_file  = $doc_obj->get_metadata_element ($top_section,"assocfilepath");
+	# my $assoc_filename = &util::filename_cat($source_dir,$assoc_file);
+
+	my $tl_root_oa_id = "openannotation-list";
+
+	my $gs_http_port = "8383";
+	    
+	my $internal_url_base = "http://localhost:${gs_http_port}/simpleAnnotationStore/annotation";
+	my $post_url="${internal_url_base}/populate";
+
+	my $gs_library_url = "http://localhost:${gs_http_port}/greenstone3/library";
+	my $site           = $self->{'site'};    	
+	my $collect        = $ENV{'GSDLCOLLECTION'};
+	
+	my $iuri_prefix = "$gs_library_url/sites/$site/collect/$collect/index/assoc/$assoc_file/";
+
+	
+
+	# For each page:
+	#    send the openannotation-list<n>.json to SAS endpoint
+	
+	my $section = $doc_obj->get_top_section();
+	while (defined $section)
+	{
+	    my $has_openannotation_json = $doc_obj->get_metadata_element($section,"HasOpenAnnotationJSON");
+	    if ((!defined $has_openannotation_json) || ($has_openannotation_json eq "0")) {
+		$section = $doc_obj->get_next_section($section);
+		next;
+	    }
+		
+	    my $sec_oa_id = "${tl_root_oa_id}${section}";
+
+	    my $sec_oa_file = $sec_oa_id;
+	    $sec_oa_file =~ s/\./-/g;
+	    $sec_oa_file .= ".json";
+
+	    #my $sec_oa_filename = &util::filename_cat($assoc_filename,$sec_oa_file);
+	    	    
+	    print $outhandle "  Inserting Open Annotation for $doc_oid.$section\n";
+
+	    my $iuri_tail   = $sec_oa_file;
+	    my $iuri="${iuri_prefix}${iuri_tail}";
+
+	    my $cmd = "curl -X POST -d \"uri=${iuri}\" \"$post_url\"";
+	    print STDERR "!!!!! cmd = $cmd\n";
+	    
+	    
+	    my $status = system($cmd);
+	    if ($status != 0) {
+		print STDERR "Error: failed to run:\n  $cmd\n$!\n";
+	    }
+	    
+	    $section = $doc_obj->get_next_section($section);
+	}
+    }
+    
+    
+#    my $chr12_filename = &util::filename_cat($assoc_filename,"doc.chr12");
+#    my $powerlog_filename = &util::filename_cat($assoc_filename,"doc.power");
+
+#    print $outhandle "  Inserting features for $doc_oid\n";
+
+#    my $cmd = "sasOpenAnnotation -d \"$adb_filename\" -I -k \"$doc_oid\" -f \"$chr12_filename\" -w \"$powerlog_filename\"";
+
+#    my $status = system($cmd);
+#    if ($status != 0) {
+#	print STDERR "Error: failed to run:\n  $cmd\n$!\n";
+#    }
+}
+
+sub text {
+    my $self = shift (@_);
+    my ($doc_obj,$file) = @_;
+
+    $self->textedit($doc_obj,$file,"add");
+}
+
+sub textreindex
+{
+    my $self = shift @_;
+    my ($doc_obj,$file) = @_;
+
+    $self->textedit($doc_obj,$file,"update");
+}
+
+sub textdelete
+{
+    my $self = shift @_;
+
+    my ($doc_obj,$file) = @_;
+
+    print STDERR "Warning: sasOpenAnnotation command-line does not currently support delete operation\n";
+
+    # $self->textedit($doc_obj,$file,"delete");
+}
+
+1;
