Index: main/trunk/greenstone2/perllib/extrametautil.pm
===================================================================
--- main/trunk/greenstone2/perllib/extrametautil.pm	(revision 24951)
+++ main/trunk/greenstone2/perllib/extrametautil.pm	(revision 24951)
@@ -0,0 +1,130 @@
+###########################################################################
+#
+# util.pm -- various useful utilities
+# 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 extrametautil;
+
+use strict;
+
+use util;
+#use Encode;
+#use File::Copy;
+use File::Basename;
+
+
+#******************* ADD: add extrametakey, add extrametadata *********************#
+sub addmetakey {
+	my ($extrametakeys, $filename_re_for_metadata) = @_;
+	push(@$extrametakeys, $filename_re_for_metadata);
+}
+
+# Unused. Added for symmetry
+sub addmetadata {
+	my ($extrametadata, $filename_re_for_metadata, $value) = @_;	
+	my $metanames = $extrametadata->{$filename_re_for_metadata};
+	push(@$metanames, $value);
+}
+
+# Unused. Added for symmetry
+sub addmetafile {
+	my ($extrametafile, $filename_re_for_metadata, $file) = @_;	
+	my $metafiles = $extrametafile->{$filename_re_for_metadata};
+	push(@$metafiles, $file);
+}
+
+sub addmetadata_for_named_metaname { # e.g. push(@{$extrametadata->{$filename_re_for_metadata}->{$field_name}}, $value);
+	my ($extrametadata, $filename_re_for_metadata, $field_name, $value) = @_;
+	my $metaname_vals = $extrametadata->{$filename_re_for_metadata}->{$field_name};
+	push(@$metaname_vals, $value); 
+}
+
+# Unused. Added for symmetry
+sub addmetafile_for_named_file {	
+	my ($extrametafile, $filename_re_for_metadata, $file, $filename_full_path) = @_;
+	my $metafile_vals = $extrametafile->{$filename_re_for_metadata}->{$file};
+	push(@$metafile_vals, $filename_full_path); 
+}
+
+
+#**************** In future, may be useful to expand this utility file 
+#**************** by having remove methods to mirror the add methods
+
+
+#******************* GET methods
+sub getmetadata {
+	my ($extrametadata, $filename_re_for_metadata) = @_;
+	return $extrametadata->{$filename_re_for_metadata};
+}
+
+sub getmetafile {
+	my ($extrametafile, $filename_re_for_metadata) = @_;
+	return $extrametafile->{$filename_re_for_metadata};
+}
+
+sub getmetadata_for_named_metaname {
+	my ($extrametadata, $filename_re_for_metadata, $field_name) = @_;
+	return $extrametadata->{$filename_re_for_metadata}->{$field_name}; # e.g. $extrametadata->{$filename_re_for_metadata}->{$field_name}
+}
+
+# Unused. Added for symmetry
+sub getmetadata_for_named_file {
+	my ($extrametafile, $filename_re_for_metadata, $file) = @_;
+	return $extrametafile->{$filename_re_for_metadata}->{$file};
+}
+
+sub getmetadata_for_named_pos {
+	my ($extrametadata, $filename_re_for_metadata, $metaname, $index) = @_;
+	return $extrametadata->{$filename_re_for_metadata}->{$metaname}->[$index]; # e.g. $extrametadata->{$filename_re_for_metadata}->{"dc.Identifier"}->[0]
+}
+
+
+#******************* SET methods
+sub setmetadata {
+	my ($extrametadata, $filename_re_for_metadata, $value) = @_;
+	$extrametadata->{$filename_re_for_metadata} = $value;
+}
+
+sub setmetafile { # e.g. $extrametafile{$filename_re_for_metadata} = $file;
+	my ($extrametafile, $filename_re_for_metadata, $file) = @_;
+	$extrametafile->{$filename_re_for_metadata} = $file; 
+}
+
+sub setmetadata_for_named_metaname {
+	my ($extrametadata, $filename_re_for_metadata, $field_name, $value) = @_;
+	$extrametadata->{$filename_re_for_metadata}->{$field_name} = $value;
+}
+
+sub setmetafile_for_named_file {
+	my ($extrametafile, $filename_re_for_metadata, $file, $filename_full_path) = @_;
+	$extrametafile->{$filename_re_for_metadata}->{$file} = $filename_full_path;
+}
+
+# Unused. Added for symmetry
+sub setmetadata_for_named_pos {
+	my ($extrametadata, $filename_re_for_metadata, $metaname, $index, $value) = @_;
+	$extrametadata->{$filename_re_for_metadata}->{$metaname}->[$index] =  $value;
+}
+
+
+1;
Index: main/trunk/greenstone2/perllib/plugins/DSpacePlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/DSpacePlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/DSpacePlugin.pm	(revision 24951)
@@ -47,4 +47,5 @@
 package DSpacePlugin;
 
+use extrametautil;
 use ReadTextFile;
 use plugin;
@@ -298,5 +299,5 @@
 	}
     }
-    push(@$extrametakeys,$file_re);
+    &extrametautil::addmetakey($extrametakeys, $file_re);
 
     # See Format's Extent section in http://dublincore.org/documents/usageguide/qualifiers.shtml
@@ -331,5 +332,5 @@
     }
     
-    $extrametadata->{$file_re} = $self->{'saved_metadata'};
+    &extrametautil::setmetadata($extrametadata, $file_re, $self->{'saved_metadata'});
 
     return 1;
Index: main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/DirectoryPlugin.pm	(revision 24951)
@@ -30,4 +30,5 @@
 package DirectoryPlugin;
 
+use extrametautil;
 use PrintInfo;
 use plugin;
@@ -443,7 +444,7 @@
 		my $extrakeys_md  = $ek->{'md'};
 		my $extrakeys_mf  = $ek->{'mf'};
-		push(@extrametakeys,$extrakeys_re);
-		$extrametadata{$extrakeys_re} = $extrakeys_md;
-		$extrametafile{$extrakeys_re} = $extrakeys_mf;
+		&extrametautil::addmetakey(\@extrametakeys, $extrakeys_re);
+		&extrametautil::setmetadata(\%extrametadata, $extrakeys_re, $extrakeys_md);
+		&extrametautil::setmetafile(\%extrametafile, $extrakeys_re, $extrakeys_mf);
 	    }
 	    delete($self->{'subdir_extrametakeys'}->{$local_dirname});
@@ -489,6 +490,6 @@
 	my $ek_non_re = &util::unregex_filename($ek);
 	if ($ek_non_re =~ m/$dirsep_re/) { # specifies at least one directory
-	    my $md = $extrametadata{$ek};
-	    my $mf = $extrametafile{$ek};
+	    my $md = &extrametautil::getmetadata(\%extrametadata, $ek);
+	    my $mf = &extrametautil::getmetafile(\%extrametafile, $ek);
 
 	    my $subdir_extrametakeys = $self->{'subdir_extrametakeys'};
@@ -496,5 +497,5 @@
 	    my $subdir_rec = { 're' => $subdir_re, 'md' => $md, 'mf' => $mf };
 
-	    # when its looked up, it must be relative to the base dir
+	    # when it's looked up, it must be relative to the base dir
 	    push(@{$subdir_extrametakeys->{"$local_dirname$extrakey_dir"}},$subdir_rec);
 	    #push(@{$subdir_extrametakeys->{"$extrakey_dir"}},$subdir_rec);
@@ -606,6 +607,6 @@
 		    print $outhandle "File \"$subfile\" matches filespec \"$filespec\"\n" 
 			if ($verbosity > 2);
-		    my $mdref = $extrametadata{$filespec};
-		    my $mfref = $extrametafile{$filespec};
+		    my $mdref = &extrametautil::getmetadata(\%extrametadata, $filespec);
+		    my $mfref = &extrametautil::getmetafile(\%extrametafile, $filespec);
 
 		    # Add the list files where the metadata came from
Index: main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm	(revision 24951)
@@ -29,4 +29,5 @@
 
 use BasePlugin;
+use extrametautil;
 
 use Encode;
@@ -325,12 +326,12 @@
 
     # Protect windows directory chars \
-    $file = &util::filename_to_regex($file);
+    $file = &util::filename_to_regex($file); #### 
     
     # Associate the metadata now
 
-    if (defined $extrametadata->{$file}) {
+    if (defined &extrametautil::getmetadata($extrametadata, $file)) {
 	print STDERR "\n****  Need to merge new metadata with existing stored metadata: file = $file\n" if $verbosity > 2;
 
-	my $file_metadata_table =     $extrametadata->{$file};
+	my $file_metadata_table = &extrametautil::getmetadata($extrametadata, $file);
 
 	foreach my $metaname (keys %exif_metadata) {
@@ -342,6 +343,6 @@
     }
     else {
-	$extrametadata->{$file} = \%exif_metadata;
-	push(@$extrametakeys, $file);
+	&extrametautil::setmetadata($extrametadata, $file, \%exif_metadata);
+	&extrametautil::addmetakey($extrametakeys, $file);
     }
 
Index: main/trunk/greenstone2/perllib/plugins/LOMPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/LOMPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/LOMPlugin.pm	(revision 24951)
@@ -32,4 +32,5 @@
 package LOMPlugin;
 
+use extrametautil;
 use ReadTextFile;
 use MetadataPass;
@@ -168,13 +169,13 @@
     $self->{'lom_srcdoc'} = undef; # reset for next file to be processed
 
-    push(@$extrametakeys,$file_re);
-    $extrametadata->{$file_re} = $self->{'saved_metadata'};
+    &extrametautil::addmetakey($extrametakeys, $file_re);
+    &extrametautil::setmetadata($extrametadata, $file_re, $self->{'saved_metadata'});
     if (defined $lom_srcdoc) {
 	# copied from oaiplugin
-	if (!defined $extrametafile->{$file_re}) {
-	    $extrametafile->{$file_re} = {};
+	if (!defined &extrametautil::getmetafile($extrametafile, $file_re)) {
+		&extrametautil::setmetafile($extrametafile, $file_re, {});
 	}
 	 #maps the file to full path
-	$extrametafile->{$file_re}->{$file} = $filename_full_path;
+	&extrametautil::setmetafile_for_named_file($extrametafile, $file_re, $file, $filename_full_path);
     }
 	
Index: main/trunk/greenstone2/perllib/plugins/MetadataCSVPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/MetadataCSVPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/MetadataCSVPlugin.pm	(revision 24951)
@@ -33,4 +33,6 @@
 use strict;
 no strict 'refs';
+
+use extrametautil;
 use multiread;
 
@@ -205,12 +207,12 @@
 	$csv_line_filename = &util::filename_to_regex($csv_line_filename);
 
- 	$extrametadata->{$csv_line_filename} = \%csv_line_metadata;
- 	push(@$extrametakeys, $csv_line_filename);
+ 	&extrametautil::setmetadata($extrametadata, $csv_line_filename, \%csv_line_metadata);
+ 	&extrametautil::addmetakey($extrametakeys, $csv_line_filename);
 	# record which file the metadata came from 
-	if (!defined $extrametafile->{$csv_line_filename}) {
-	    $extrametafile->{$csv_line_filename} = {};
+	if (!defined &extrametautil::getmetafile($extrametafile, $csv_line_filename)) {
+	    &extrametautil::setmetafile($extrametafile, $csv_line_filename, {});
 	}
 	# maps the file to full path
-	$extrametafile->{$csv_line_filename}->{$file} = $filename;
+	&extrametautil::setmetafile_for_named_file($extrametafile, $csv_line_filename, $file, $filename);
     }
 }
Index: main/trunk/greenstone2/perllib/plugins/MetadataXMLPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/MetadataXMLPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/MetadataXMLPlugin.pm	(revision 24951)
@@ -93,4 +93,5 @@
 
 use BasePlugin;
+use extrametautil;
 use util;
 use metadatautil;
@@ -327,12 +328,12 @@
     if ($element eq "FileSet") {
 	foreach my $target (@{$self->{'saved_targets'}}) {
-	    my $file_metadata = $self->{'metadataref'}->{$target};
+		my $file_metadata = &extrametautil::getmetadata($self->{'metadataref'}, $target);
 	    my $saved_metadata = $self->{'saved_metadata'};
 
 	    if (!defined $file_metadata) {
-		$self->{'metadataref'}->{$target} = $saved_metadata;
+		&extrametautil::setmetadata($self->{'metadataref'}, $target, $saved_metadata);
 
 		# not had target before
-		push (@{$self->{'metakeysref'}}, $target);
+		&extrametautil::addmetakey($self->{'metakeysref'}, $target);
 	    }
 	    else {
@@ -346,9 +347,9 @@
 	    my $filename = $self->{'metadata-filename'};
 
-	    if (!defined $self->{'metafileref'}->{$target}) {
-	    	$self->{'metafileref'}->{$target} = {};
+	    if (!defined &extrametautil::getmetafile($self->{'metafileref'}, $target)) {
+	    	&extrametautil::setmetafile($self->{'metafileref'}, $target, {});
 	    }
 
-	    $self->{'metafileref'}->{$target}->{$file} = $filename
+	    &extrametautil::setmetafile_for_named_file($self->{'metafileref'}, $target, $file, $filename);
 	}
     }
Index: main/trunk/greenstone2/perllib/plugins/OAIMetadataXMLPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/OAIMetadataXMLPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/OAIMetadataXMLPlugin.pm	(revision 24951)
@@ -33,4 +33,5 @@
 no strict 'refs';
 
+use extrametautil;
 use MetadataXMLPlugin;
 
@@ -125,5 +126,6 @@
   {
     # Don't harvest file sets that don't have dc.Identifier set, "dc.Identifier" is usde as the key between Greenstone and OAI Server!
-    next if (!defined($extrametadata->{$one_file}->{"dc.Identifier"}->[0]) || $extrametadata->{$one_file}->{"dc.Identifier"}->[0] eq "");
+	my $dc_identifier = &extrametautil::getmetadata_for_named_pos($extrametadata, $one_file, "dc.Identifier", 0);
+	next if (!defined($dc_identifier) || $dc_identifier eq "");
 
     #======================================================================#
@@ -131,5 +133,5 @@
     #======================================================================#
     # The dc.Identifier has to be the same as the OAI record identifier 
-    my $oai_identifier = $extrametadata->{$one_file}->{"dc.Identifier"}->[0];
+    my $oai_identifier = $dc_identifier;
     
     # Now, let's get the OAI metadata
@@ -195,7 +197,8 @@
         # This might need change over once they update the Koha OAI server
         $value =~ s/\/opac\/opac\-detail\.pl\?bib\=/\/catalogue\/detail\.pl\?biblionumber\=/;
-      }
-      $extrametadata->{$one_file}->{$field_name} = [] if (!defined($extrametadata->{$one_file}->{$field_name}));
-      push(@{$extrametadata->{$one_file}->{$field_name}}, $value); 
+      }	  
+	  
+      &extrametautil::setmetadata_for_named_metaname($extrametadata, $one_file, $field_name, []) if (!defined (&extrametautil::getmetadata_for_named_metaname($extrametadata, $one_file, $field_name)));
+	  &extrametautil::addmetadata_for_named_metaname($extrametadata, $one_file, $field_name, $value);
     }
     #======================================================================#
Index: main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm	(revision 24950)
+++ main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm	(revision 24951)
@@ -27,4 +27,5 @@
 package OAIPlugin;
 
+use extrametautil;
 use unicode;
 use util;
@@ -256,12 +257,13 @@
     # extrametadata keys should be regular expressions
     $filename_for_metadata = &util::filename_to_regex($filename_for_metadata);
-    $extrametadata->{$filename_for_metadata} = $new_metadata;
-    push(@$extrametakeys, $filename_for_metadata);
-    if ($srcdoc_exists) {
-	if (!defined $extrametafile->{$filename_for_metadata}) {
-	    $extrametafile->{$filename_for_metadata} = {};
+    &extrametautil::setmetadata($extrametadata, $filename_for_metadata, $new_metadata);
+    &extrametautil::addmetakey($extrametakeys, $filename_for_metadata);
+    if ($srcdoc_exists) {	
+	if (!defined &extrametautil::getmetafile($extrametafile, $filename_for_metadata)) {
+		&extrametautil::setmetafile($extrametafile, $filename_for_metadata, {});
 	}
 	 #maps the file to full path
-	$extrametafile->{$filename_for_metadata}->{$file} = $filename_full_path;
+	&extrametautil::setmetafile_for_named_file($extrametafile, $filename_for_metadata, $file, $filename_full_path);
+	
     }
     return 1;
