Index: main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27156)
+++ main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27157)
@@ -8,5 +8,5 @@
 # Copyright (C) 2009 New Zealand Digital Library Project
 #
-# This program is free software; you can redistribute it and/or modify
+# This program is free software; you can redistr   te 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
@@ -54,7 +54,11 @@
 		'optional-args'   => [ "metapos" ] },
 	
-	"get-metadata" => { 
+	"get-index-metadata" => { 
 		'compulsory-args' => [ "d", "metaname" ],
 		'optional-args'   => [ "metapos" ] }, 
+
+	"get-metadata" => { # alias for get-index-metadata
+	    'compulsory-args' => [ "d", "metaname" ],
+	    'optional-args'   => [ "metapos" ] },
 
 	"get-live-metadata" => { 
@@ -67,5 +71,9 @@
 		'optional-args'   => [ ] },
 
-	"set-metadata" => { 
+	"set-metadata" => { # generic set-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
+		'compulsory-args' => [ "metaname", "metavalue" ],
+		'optional-args'   => [ "where", "metapos", "metamode", "prevmetavalue", "d", "f" ] }, 
+
+	"set-index-metadata" => { 
 		'compulsory-args' => [ "d", "metaname", "metavalue" ],
 		'optional-args'   => [ "metapos" ] },
@@ -105,7 +113,11 @@
 		'optional-args'   => [ ] },
 
-	"remove-metadata" => {
+	"remove-index-metadata" => {
 		'compulsory-args' => [ "d", "metaname" ],
 		'optional-args'   => [ "metapos", "metavalue" ] },
+
+	"remove-metadata" => { # generic remove-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
+		'compulsory-args' => [ "d", "metaname" ],
+		'optional-args'   => [ "where", "metapos", "metavalue" ] },
 
 	#INSERT METHODS
@@ -202,6 +214,12 @@
 }
 
-
+# just calls the index version
 sub get_metadata
+{
+    my $self = shift @_;
+    $self->get_index_metadata(@_);
+}
+
+sub get_index_metadata
 {
     my $self = shift @_;
@@ -493,26 +511,18 @@
 }
 
-sub set_live_metadata
+sub _set_live_metadata
 {
     my $self = shift @_;
 
-    my $username  = $self->{'username'};
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
+    #my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
-  
-    if ($baseaction::authentication_enabled) {
-	# Ensure the user is allowed to edit this collection
-	&authenticate_user($gsdl_cgi, $username, $collect);
-    }
 
     # Obtain the collect dir
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
-    # Make sure the collection isn't locked by someone else
-    $self->lock_collection($username, $collect);
 
     # look up additional args
@@ -552,5 +562,26 @@
 	$gsdl_cgi->generate_ok_message("set-live-metadata successful: Key[$metaname]=$metavalue");
     }
-    
+
+    #return $status; # in case calling functions have any further use for this
+}
+
+sub set_live_metadata
+{
+    my $self = shift @_;
+
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+  
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	&authenticate_user($gsdl_cgi, $username, $collect);
+    }
+
+    # Make sure the collection isn't locked by someone else
+    $self->lock_collection($username, $collect);
+
+    $self->_set_live_metadata(@_);
+
     # Release the lock once it is done
     $self->unlock_collection($username, $collect);
@@ -589,28 +620,150 @@
 }
 
-sub set_metadata
+sub _set_import_metadata
 {
     my $self = shift @_;
 
-    my $username  = $self->{'username'};
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
-
-    if ($baseaction::authentication_enabled) {
-	# Ensure the user is allowed to edit this collection
-	&authenticate_user($gsdl_cgi, $username, $collect);
-    }
-
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-	
-    $gsdl_cgi->checked_chdir($collect_dir);
-
-    # Obtain the collect dir
+    my $infodbtype = $self->{'infodbtype'};
+#    my $gsdlhome  = $self->{'gsdlhome'};
+     
+    # Obtain the collect and archive dir    
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
-
-    # Make sure the collection isn't locked by someone else
-    $self->lock_collection($username, $collect);
+    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
+   
+    # look up additional args
+    # want either d= or f=
+    my $docid  = $self->{'d'};
+    my $import_file  = $self->{'f'};
+    if ((!defined $docid) && (!defined $import_file)) {
+	$gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified.");
+    } 
+
+    # Get the parameters and set default mode to "accumulate"
+    my $metaname   = $self->{'metaname'};
+    my $metavalue  = $self->{'metavalue'};
+##    $metavalue =~ s/&amp;lt;(.*?)&amp;gt;/<$1>/g;
+    $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
+    
+    my $metamode   = $self->{'metamode'};
+    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
+	# make "accumulate" the default (less destructive, as won't actually 
+	# delete any existing values)
+	$metamode = "accumulate";
+    }
+
+    # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
+    # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
+    my $metadata_xml_file;
+    my $import_filename = undef;
+    if (defined $docid) {
+	my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
+	my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
+
+	# This now stores the full pathname
+	$import_filename = $doc_rec->{'src-file'}->[0];	
+    }
+    else {
+        $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
+    }
+
+    # figure out correct metadata.xml file [?]
+    # Assuming the metadata.xml file is next to the source file
+    # Note: This will not work if it is using the inherited metadata from the parent folder
+    my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
+    my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
+
+    # Edit the metadata.xml
+    # Modified by Jeffrey from DL Consulting
+    # Handle the case where there is one metadata.xml file for multiple FileSets
+    # The XML filter needs to know whether it is in the right FileSet
+    # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
+    # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
+    $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename,
+                             $metaname, $metavalue, $metamode, $import_tailname);
+
+
+    my $mess = "set-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
+    $mess .= "  $metaname";
+    $mess .= " = $metavalue";
+    $mess .= " ($metamode)\n";
+
+    $gsdl_cgi->generate_ok_message($mess);
+
+    #return $status; # in case calling functions have any further use for this
+}
+
+# the version of set_index_meta that doesn't do authentication
+sub _set_archives_metadata
+{
+    my $self = shift @_;
+
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+    my $infodbtype = $self->{'infodbtype'};
+    
+    # Obtain the collect and archive dir   
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);	
+    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
+
+    # look up additional args
+    my $docid  = $self->{'d'};
+    my $metaname   = $self->{'metaname'};
+    my $metavalue  = $self->{'metavalue'};
+    my $prevmetavalue = $self->{'prevmetavalue'};
+    
+    my $metapos    = $self->{'metapos'};
+    $metapos = 0 if (!defined $metapos);
+
+    my $metamode   = $self->{'metamode'};
+    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
+	# make "accumulate" the default (less destructive, as won't actually 
+	# delete any existing values)
+	$metamode = "accumulate";
+    } 
+	
+    my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
+				$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
+   
+    if ($status == 0) {
+	my $mess = "set-archives-metadata successful: Key[$docid]\n";
+	$mess .= "  $metaname";
+	$mess .= "->[$metapos]" if (defined $metapos);
+	$mess .= " = $metavalue";
+	$mess .= " ($metamode)\n";
+	
+	$gsdl_cgi->generate_ok_message($mess);	
+    }
+    else {
+	my $mess .= "Failed to set archives metadata key: $docid\n";
+	$mess .= "Exit status: $status\n";
+	if(defined $self->{'error_msg'}) {
+	    $mess .= "Error Message: $self->{'error_msg'}\n";
+	} else {
+	    $mess .= "System Error Message: $!\n";
+	}
+	$mess .= "-" x 20 . "\n";
+	
+	$gsdl_cgi->generate_error($mess);
+    }
+
+    #return $status; # in case calling functions have any further use for this
+}
+
+
+# the version of set_index_meta that doesn't do authentication
+sub _set_index_metadata
+{
+    my $self = shift @_;
+
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 
     # look up additional args
@@ -634,11 +787,41 @@
     }
     else {
-	my $mess = "set-metadata successful: Key[$docid]\n";
+	my $mess = "set-index-metadata successful: Key[$docid]\n";
 	$mess .= "  $metaname";
 	$mess .= "->[$metapos]" if (defined $metapos);
-	$mess .= " = $metavalue";
+	$mess .= " = $metavalue\n";
 	
 	$gsdl_cgi->generate_ok_message($mess);
     }
+
+    #return $status; # in case calling functions have any further use for this
+}
+
+sub set_index_metadata
+{
+    my $self = shift @_;
+
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+    #my $gsdlhome  = $self->{'gsdlhome'};
+
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	&authenticate_user($gsdl_cgi, $username, $collect);
+    }
+
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+	
+    $gsdl_cgi->checked_chdir($collect_dir);
+
+    # Obtain the collect dir
+    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+
+    # Make sure the collection isn't locked by someone else
+    $self->lock_collection($username, $collect);
+
+    $self->_set_index_metadata(@_);
     
     # Release the lock once it is done
@@ -646,4 +829,84 @@
 }
 
+# call this to set the metadata for a combination of dirs archives, import or index, or live
+# if none specified, defaults to index which was the original behaviour of set_metadata.
+sub set_metadata
+{
+    my $self = shift @_;
+
+    # Testing that not defining a variable, setting it to "" or to "  " all return false
+    # >perl -e 'my $whichdirs=""; if($whichdirs) {print "$whichdirs\n"};'
+
+    my $where = $self->{'where'};
+    if(!$where) {	
+	$self->set_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
+	return;
+    } 
+
+    # authenticate and lock collection once, even if processing multiple dirs
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+    
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	#&authenticate_user($gsdl_cgi, $username, $collect);
+	$self->authenticate_user($username, $collect);
+    }
+    
+    if($where =~ m/index/) {
+	my $site = $self->{'site'};
+	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+	$gsdl_cgi->checked_chdir($collect_dir);
+    }
+
+    # Make sure the collection isn't locked by someone else
+    $self->lock_collection($username, $collect);
+
+
+    # now at last can set the metadata. $where can specify multiple
+    # $where is of the form: import|archives|index, or a subset thereof
+
+    #my @whichdirs = split('\|', $where);
+
+    # just check whether $where contains import/archives/index/live in turn, and 
+    # for each case, process it accordingly
+    if($where =~ m/import/) {
+	$self->_set_import_metadata(@_);	    
+    }
+
+    if($where =~ m/archives/) {
+
+	# look up docID arg which is optional to set_metadata because it's optional 
+	# to set_import, but which is compulsory to set_archives_metadata
+	my $docid     = $self->{'d'};
+	if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
+	    $gsdl_cgi->generate_error("No docid (d=...) specified.");
+	} 
+	else { # we have a docid, so can set archives meta
+	    $self->_set_archives_metadata(@_);
+	}
+    }
+
+    if($where =~ m/index/) {
+	
+	# look up docID arg which is optional to set_metadata because it's optional 
+	# to set_import, but which is compulsory to set_archives_metadata
+	my $docid     = $self->{'d'};
+	if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
+	    $gsdl_cgi->generate_error("No docid (d=...) specified.");
+	}
+	else { # we have a docid, so can set index meta
+	    $self->_set_index_metadata(@_);
+	}
+    }	
+
+    if($where =~ m/live/) {
+	$self->_set_live_metadata(@_); # docid param, d, is compulsory, but is checked for in subroutine
+    }
+
+    # Release the lock once it is done
+    $self->unlock_collection($username, $collect);
+}
 
 sub set_metadata_array
@@ -1070,6 +1333,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
-    my $infodbtype = $self->{'infodbtype'};
 	
     if ($baseaction::authentication_enabled) {
@@ -1078,57 +1339,11 @@
     }
 
-	my $site = $self->{'site'};
-		
-	# Obtain the collect and archive dir   
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);	
-	
-    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
-
     # Make sure the collection isn't locked by someone else
     $self->lock_collection($username, $collect);
 
-    # look up additional args
-    my $docid  = $self->{'d'};
-    my $metaname   = $self->{'metaname'};
-    my $metavalue  = $self->{'metavalue'};
-	my $prevmetavalue = $self->{'prevmetavalue'};
-	
-    my $metapos    = $self->{'metapos'};
-    $metapos = 0 if (!defined $metapos);
-
-    my $metamode   = $self->{'metamode'};
-    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
-	# make "accumulate" the default (less destructive, as won't actually 
-	# delete any existing values)
-	$metamode = "accumulate";
-    } 
-	
-	my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
-				$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
-   
+    $self->_set_archives_metadata(@_);
+
     # Release the lock once it is done
     $self->unlock_collection($username, $collect);
-
-	if ($status == 0) {
-		my $mess = "set-archives-metadata successful: Key[$docid]\n";
-		$mess .= "  $metaname";
-		$mess .= "->[$metapos]" if (defined $metapos);
-		$mess .= " = $metavalue";
-		$mess .= " ($metamode)\n";
-	
-		$gsdl_cgi->generate_ok_message($mess);	
-	}
-	else {
-		my $mess .= "Failed to set archives metadata key: $docid\n";
-		$mess .= "Exit status: $status\n";
-		if(defined $self->{'error_msg'}) {
-		    $mess .= "Error Message: $self->{'error_msg'}\n";
-		} else {
-		    $mess .= "System Error Message: $!\n";
-		}
-		$mess .= "-" x 20 . "\n";
-		
-		$gsdl_cgi->generate_error($mess);
-	}
 }
 
@@ -1219,19 +1434,12 @@
 }
 
-sub remove_archives_metadata
+sub _remove_archives_metadata
 {
 	my $self = shift @_;
 
-	my $username  = $self->{'username'};
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-	my $gsdlhome  = $self->{'gsdlhome'};
+#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
-	
-	if ($baseaction::authentication_enabled) 
-	{
-		# Ensure the user is allowed to edit this collection
-		&authenticate_user($gsdl_cgi, $username, $collect);
-	}
 	
 	my $site = $self->{'site'};
@@ -1242,7 +1450,4 @@
 	my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
 
-	# Make sure the collection isn't locked by someone else
-	$self->lock_collection($username, $collect);
-	
 	# look up additional args
 	my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
@@ -1260,7 +1465,4 @@
 	my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, undef, $docid_secnum);
 	
-	# Release the lock once it is done
-	$self->unlock_collection($username, $collect);
-
 	if ($status == 0) 
 	{
@@ -1280,4 +1482,29 @@
 		$gsdl_cgi->generate_error($mess);
 	}
+	
+	#return $status; # in case calling functions have a use for this
+}
+
+sub remove_archives_metadata
+{
+	my $self = shift @_;
+
+	my $username  = $self->{'username'};
+	my $collect   = $self->{'collect'};
+	my $gsdl_cgi  = $self->{'gsdl_cgi'};
+	
+	if ($baseaction::authentication_enabled) 
+	{
+		# Ensure the user is allowed to edit this collection
+		&authenticate_user($gsdl_cgi, $username, $collect);
+	}
+
+	# Make sure the collection isn't locked by someone else
+	$self->lock_collection($username, $collect);
+
+	$self->_remove_archives_metadata(@_);
+
+	# Release the lock once it is done
+	$self->unlock_collection($username, $collect);
 }
 
@@ -1594,6 +1821,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
-    my $infodbtype = $self->{'infodbtype'};
 	
     if ($baseaction::authentication_enabled) {
@@ -1602,75 +1827,11 @@
     }
 
-
-    # Obtain the collect and archive dir    
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
-    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
-
     # Make sure the collection isn't locked by someone else
     $self->lock_collection($username, $collect);
-    
-    # look up additional args
-    # want either d= or f=
-    my $docid  = $self->{'d'};
-    my $import_file  = $self->{'f'};
-    if ((!defined $docid) && (!defined $import_file)) {
-	$gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified.");
-    } 
-
-    # Get the parameters and set default mode to "accumulate"
-    my $metaname   = $self->{'metaname'};
-    my $metavalue  = $self->{'metavalue'};
-##    $metavalue =~ s/&amp;lt;(.*?)&amp;gt;/<$1>/g;
-    $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
-    
-    my $metamode   = $self->{'metamode'};
-    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
-	# make "accumulate" the default (less destructive, as won't actually 
-	# delete any existing values)
-	$metamode = "accumulate";
-    }
-
-    # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
-    # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
-    my $metadata_xml_file;
-    my $import_filename = undef;
-    if (defined $docid) {
-	my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
-	my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
-
-	# This now stores the full pathname
-	$import_filename = $doc_rec->{'src-file'}->[0];	
-    }
-    else {
-        $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
-    }
-
-    # figure out correct metadata.xml file [?]
-    # Assuming the metadata.xml file is next to the source file
-    # Note: This will not work if it is using the inherited metadata from the parent folder
-    my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
-    my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
-
-    # Edit the metadata.xml
-    # Modified by Jeffrey from DL Consulting
-    # Handle the case where there is one metadata.xml file for multiple FileSets
-    # The XML filter needs to know whether it is in the right FileSet
-    # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
-    # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
-    $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename,
-                             $metaname, $metavalue, $metamode, $import_tailname);
+ 
+    $self->_set_import_metadata(@_);
 
     # Release the lock once it is done
     $self->unlock_collection($username, $collect);
-
-    my $mess = "set-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
-    $mess .= "  $metaname";
-    $mess .= " = $metavalue";
-    $mess .= " ($metamode)\n";
-	
-    $gsdl_cgi->generate_ok_message($mess);
     
 }
@@ -1754,17 +1915,10 @@
 }
 
-sub remove_import_metadata
+sub _remove_import_metadata
 {
 	my $self = shift @_;
-	
-	my $username = $self->{'username'};
+
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-	
-	if ($baseaction::authentication_enabled) {
-		# Ensure the user is allowed to edit this collection
-		&authenticate_user($gsdl_cgi, $username, $collect);
-	}
-
 	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
@@ -1777,8 +1931,5 @@
 	## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 	my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
-	
-	# Make sure the collection isn't locked by someone else
-	$self->lock_collection($username, $collect);
-	
+
 	# look up additional args
 	my $docid = $self->{'d'};
@@ -1818,7 +1969,4 @@
 	$self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metavalue, $import_tailname);
 	
-	# Release the lock once it is done
-	$self->unlock_collection($username, $collect);
-
 	my $mess = "remove-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
 	$mess .= "  $metaname";
@@ -1826,4 +1974,29 @@
 	
 	$gsdl_cgi->generate_ok_message($mess);
+
+	#return $status; # in case calling functions have a use for this
+}
+
+sub remove_import_metadata
+{
+	my $self = shift @_;
+	
+	my $username = $self->{'username'};
+	my $collect   = $self->{'collect'};
+	my $gsdl_cgi  = $self->{'gsdl_cgi'};
+	
+	if ($baseaction::authentication_enabled) {
+		# Ensure the user is allowed to edit this collection
+		&authenticate_user($gsdl_cgi, $username, $collect);
+	}
+
+	# Make sure the collection isn't locked by someone else
+	$self->lock_collection($username, $collect);
+	
+	$self->_remove_import_metadata(@_);
+
+	# Release the lock once it is done
+	$self->unlock_collection($username, $collect);
+
 }
 
@@ -1890,27 +2063,19 @@
 }
 
-sub remove_live_metadata
+sub _remove_live_metadata
 {
     my $self = shift @_;
 
-    my $username  = $self->{'username'};
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
+#    my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
-	
-    if ($baseaction::authentication_enabled) {
-	# Ensure the user is allowed to edit this collection
-	&authenticate_user($gsdl_cgi, $username, $collect);
-    }
 
     # Obtain the collect dir
     ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-
-    # Make sure the collection isn't locked by someone else
-    $self->lock_collection($username, $collect);
-
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+
+    
     # look up additional args
     my $docid     = $self->{'d'};
@@ -1948,10 +2113,7 @@
     }
 
-    # Release the lock once it is done
-    $self->unlock_collection($username, $collect);
-}
-
-
-sub remove_metadata
+}
+
+sub remove_live_metadata
 {
     my $self = shift @_;
@@ -1961,5 +2123,4 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $gsdlhome  = $self->{'gsdlhome'};
-    my $infodbtype = $self->{'infodbtype'};
 	
     if ($baseaction::authentication_enabled) {
@@ -1968,13 +2129,66 @@
     }
 
-    # Obtain the collect dir
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
-
     # Make sure the collection isn't locked by someone else
     $self->lock_collection($username, $collect);
 
-    # look up additional args
+    $self->_remove_live_metadata(@_);
+
+    $self->unlock_collection($username, $collect);
+}
+
+sub remove_metadata
+{
+    my $self = shift @_;
+
+    my $where = $self->{'where'};
+    if(!$where) {
+	$self->remove_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
+	return;
+    }
+
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	&authenticate_user($gsdl_cgi, $username, $collect);
+    }
+
+    # Make sure the collection isn't locked by someone else
+    $self->lock_collection($username, $collect);
+
+    # check which directories need to be processed, specified in $where as 
+    # any combination of import|archives|index|live
+    if($where =~ m/import/) {
+	$self->_remove_import_metadata(@_);	    
+    }
+    if($where =~ m/archives/) {
+	$self->_remove_archives_metadata(@_);	    
+   } 
+    if($where =~ m/index/) {
+	$self->_remove_index_metadata(@_);	    
+    }
+
+    # Release the lock once it is done
+    $self->unlock_collection($username, $collect);
+}
+
+# the internal version, without authentication
+sub _remove_index_metadata
+{    
+    my $self = shift @_;
+
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+#    my $gsdlhome  = $self->{'gsdlhome'};
+    my $infodbtype = $self->{'infodbtype'};
+	
+    # Obtain the collect dir
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+
+        # look up additional args
     my $docid     = $self->{'d'};
     if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
@@ -2062,4 +2276,31 @@
 	$gsdl_cgi->generate_ok_message($mess);
     }
+
+    #return $status; # in case calling functions have a use for this
+}
+
+sub remove_index_metadata
+{
+    my $self = shift @_;
+
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+#    my $gsdlhome  = $self->{'gsdlhome'};
+	
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	&authenticate_user($gsdl_cgi, $username, $collect);
+    }
+
+    # Obtain the collect dir
+	my $site = $self->{'site'};
+	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+
+    # Make sure the collection isn't locked by someone else
+    $self->lock_collection($username, $collect);
+
+    $self->_remove_index_metadata(@_);
 
     # Release the lock once it is done
