Index: main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27323)
+++ main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27324)
@@ -267,5 +267,5 @@
     }
     elsif($where =~ m/archives/) {
-#	$self->_get_archives_metadata_array(@_);
+	$self->_get_archives_metadata_array(@_);
     }
     elsif($where =~ m/import/) {
@@ -275,4 +275,107 @@
 #    	$self->_get_live_metadata_array(@_);
     }
+}
+
+
+# Unused method, but included for completion. Tested, works. Takes a JSON string and returns a JSON string.
+# For more information on the format of the output, see _get_index_metadata_array, which is in use.
+sub _get_archives_metadata_array {
+
+    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    
+    my $infodbtype = $self->{'infodbtype'};
+
+    my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
+    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
+
+    my $json_str      = $self->{'json'};
+    my $doc_array = decode_json $json_str;
+
+    my $json_result_str = "[";
+    my $first_doc_rec = 1;
+    foreach my $doc_array_rec ( @$doc_array ) {
+	
+	my $docid     = $doc_array_rec->{'docid'};
+	
+	if($first_doc_rec) {
+	    $first_doc_rec = 0;
+	} else {
+	    $json_result_str .= ",";
+	}
+	$json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";	
+
+	my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
+	$json_result_str = $json_result_str . ",\"metatable\":[";
+
+	my $first_rec = 1;
+	foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps	    
+
+	    my ($docid, $docid_secnum) = ($doc_array_rec->{'docid'} =~ m/^(.*?)(\..*)?$/);
+	    $docid_secnum = "" if (!defined $docid_secnum);
+
+	    # Read the docid entry	    
+	    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
+	    # This now stores the full pathname
+	    my $doc_filename = $doc_rec->{'doc-file'}->[0];	
+	    $doc_filename = &util::filename_cat($archive_dir, $doc_filename);
+
+	    if($first_rec) {
+		$first_rec = 0;
+	    } else {
+		$json_result_str .= ",";		
+	    }
+	    
+	    my $metaname  = $metatable_rec->{'metaname'};
+	    $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
+
+	    my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
+	    if(!defined $metapos) {
+		$metapos = 0;
+	    }
+
+
+	    # Obtain the specified metadata value(s)
+	    my $metavalue;
+
+	    if($metapos ne "all") { # get the value at a single metapos
+
+		$metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
+		#print STDERR "**** Docname, metaname, metapos, sec_num: $doc_filename, $metaname, $metapos, $docid_secnum\n"; 
+		
+		$json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
+
+	    } else {
+		my $first_metaval = 1;
+		$metapos = 0;
+		$metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
+
+		while (defined $metavalue && $metavalue ne "") {
+		    if($first_metaval) {		
+			$first_metaval = 0;
+		    } else {
+			$json_result_str .= ",";
+		    }
+	    
+		    $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
+
+		    $metapos++;
+		    $metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
+		}
+	    }
+
+	    $json_result_str .= "]}"; # close metavals array and metatable record
+	}
+	
+	$json_result_str .= "]}"; # close metatable array and docid record
+    }
+
+    $json_result_str .= "]"; # close array of docids
+    $gsdl_cgi->generate_ok_message($json_result_str."\n");
 }
 
@@ -383,5 +486,5 @@
     $json_result_str .= "]"; # close array of docids
 
-    $gsdl_cgi->generate_ok_message($json_result_str);    
+    $gsdl_cgi->generate_ok_message($json_result_str."\n");    
 }
 
@@ -565,5 +668,5 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-	my $gsdlhome  = $self->{'gsdlhome'};
+#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
 
@@ -1494,5 +1597,5 @@
     my $new_depth = scalar(@$contextArray);
 
-	print STDERR "**** START SECTION \n";
+#	print STDERR "**** START SECTION \n";
 	
     if ($new_depth == 1) {
