Index: /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27335)
+++ /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27336)
@@ -259,5 +259,5 @@
     }
 
-    # Only when setting metadata do we perform authentication and do we lock the collection
+    # Only when setting metadata do we perform authentication and do we lock the collection,
     # not when getting metadata
 
@@ -273,8 +273,9 @@
     }
     elsif($where =~ m/live/) {
-#    	$self->_get_live_metadata_array(@_);
-    }
-}
-
+    	$self->_get_live_metadata_array(@_);
+    }
+}
+
+# Unused at present. Added for completion. Tested.
 sub _get_import_metadata_array {
     
@@ -480,8 +481,7 @@
 }
 
-# takes a JSON string and returns a JSON string
-# resulting string is of the form:
-# [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
-sub _get_index_metadata_array
+
+# Unused at present. Added for completion. Tested, but not sure if it retrieves metadata in the manner it's expected to.
+sub _get_live_metadata_array
 {
     my $self = shift @_;
@@ -498,7 +498,7 @@
     # Obtain the path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
-    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
 
     my $json_str      = $self->{'json'};
@@ -518,8 +518,8 @@
 	}
 	$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	    
@@ -532,4 +532,111 @@
 	    my $metaname  = $metatable_rec->{'metaname'};
 	    $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
+	    
+	    # Generate the dbkey
+	    my $dbkey = "$docid.$metaname";
+	    
+	    # metapos for get_live_metadata is always assumed to be "all". 
+	    # It's always going to get all the lines of metavalues associated with a metaname
+	    # (It's the metaname itself that should contain an increment number, if there are to be multiple values).
+	    #my $metapos = "all";
+	    my $metapos = $metatable_rec->{'metapos'} || 0; # Can be 0... 1|all|undefined. Defaults to 0 if undefined/false
+	    my $metavalue = "";
+	    
+	    # Obtain the content of the key
+	    my $cmd = "gdbmget $infodb_file_path $dbkey";
+	    if (open(GIN,"$cmd |") != 0) { # Success. 
+		
+		binmode(GIN,":utf8");
+		# Read everything in and concatenate them into $metavalue		
+		my $line;
+		my $first_metaval = 1;
+		my $pos = 0;
+		while (defined ($line=<GIN>)) {
+		    chomp($line); # Get rid off the tailing newlines
+		    
+		    if($metapos eq "all") {
+			if($first_metaval) {		
+			    $first_metaval = 0;
+			} else {
+			    $json_result_str .= ",";
+			}			
+			$metavalue = $line;
+			$json_result_str .= "{\"metapos\":\"$pos\",\"metavalue\":\"$metavalue\"}";
+		    } elsif($metapos == $pos) {
+			$metavalue = $line;
+			$json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
+			last;
+		    } # else, the current $pos is not the required $metapos
+		    $pos += 1;
+		}
+		close(GIN);
+	    } # else open cmd == 0 (failed) and metavals array will be empty [] for this metaname
+	    
+	    $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");    
+}
+
+
+# Takes a JSON string and returns a JSON string
+# Request string is of the form:
+# http://localhost:8283/greenstone/cgi-bin/metadata-server.pl?a=get-metadata-array&c=demo&where=index&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metapos":"all"},{"metaname":"usertimestamp","metapos":"all"}, {"metaname":"usercomment","metapos":"all"}]}]
+# Resulting string is of the form:
+# [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
+sub _get_index_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'};
+    
+    # To people who know $collect_tail please add some comments
+    # Obtain the path to the database
+    my $collect_tail = $collect;
+    $collect_tail =~ s/^.*[\/|\\]//;
+    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
+    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
+
+    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	    
+	    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
@@ -613,5 +720,5 @@
     # Obtain path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
@@ -898,5 +1005,5 @@
     # Obtain path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
@@ -953,5 +1060,5 @@
     # Obtain path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
@@ -1434,5 +1541,5 @@
     # Obtain path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
@@ -2978,5 +3085,5 @@
     # Obtain the live gdbm_db path 
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
@@ -3089,5 +3196,5 @@
     # Obtain the path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
@@ -3285,5 +3392,5 @@
     # Obtain path to the database
     my $collect_tail = $collect;
-    $collect_tail =~ s/^.*[\/\\]//;
+    $collect_tail =~ s/^.*[\/|\\]//;
     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
