Index: main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27168)
+++ main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 27173)
@@ -317,5 +317,5 @@
     if (!defined $docid) 
 	{
-		$gsdl_cgi->generate_error_message("No docid (d=...) specified.\n");
+		$gsdl_cgi->generate_error("No docid (d=...) specified.\n");
 	} 
 
@@ -660,4 +660,14 @@
     # want either d= or f=
     my $docid  = $self->{'d'};
+    my ($docid_root,$docid_secnum);
+    if(defined $docid) {	
+	($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);	
+	# as yet no support for setting subsection metadata in metadata.xml
+	if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
+	    $gsdl_cgi->generate_ok_message("*** Unable to set import metadata for subsections at this stage.\n");
+	    return;
+	}
+    }
+
     my $import_file  = $self->{'f'};
     if ((!defined $docid) && (!defined $import_file)) {
@@ -684,5 +694,5 @@
     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);
+	my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
 
 	# This now stores the full pathname
@@ -706,5 +716,5 @@
     # 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);
+			     $metaname, $metavalue, $metamode, $import_tailname);
 
 
@@ -1300,5 +1310,5 @@
 
 	my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
-	my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
+	my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'} || "";
 	
 	print STDERR "**** Processing a closing </Description> tag \n";
@@ -1329,5 +1339,5 @@
 	    }
 
-	    # If get to here and metamode is override, the this means there 
+	    # If we get to here and metamode is override, this means there 
 	    # was no existing value to overide => treat as an append operation
 	    
@@ -1809,5 +1819,7 @@
 		rules => \@rules, 
 		style => 'filter',
-		output_encoding => 'utf8'
+		output_encoding => 'utf8',
+#	 normalisespaces => 1, # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
+	 	stripspaces => 2|0|0 # ineffectual
 	);
 	
@@ -1849,5 +1861,9 @@
 	my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
 
-	if (!($parser->{'parameters'}->{'secid'} eq $parser->{'parameters'}->{'curr_section_num'}))
+	# For comparisons, toplevel section is indicated by ""
+	my $curr_sec_num = $parser->{'parameters'}->{'curr_section_num'} || "";
+	my $secid = $parser->{'parameters'}->{'secid'} || "";
+
+	if (!($secid eq $curr_sec_num))
 	{
 		# RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
@@ -1893,5 +1909,5 @@
 
     # Report error if we don't see FileName tag before this
-    die "Fatal Error: Unexpected metadata.xml structure. Undefind current_file, possiblely encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
+    die "Fatal Error: Unexpected metadata.xml structure. Undefined current_file, possibly encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
     
     # Don't do anything if we are not in the right FileSet
@@ -1901,4 +1917,5 @@
 	# file.  
 	# So, skip anything with a .* in it as it is too general
+#	print STDERR "@@@@ Skipping entry in metadata.xml where FileName=.* as it is too general\n";
 	return [$tagname => $attrHash];
     }
@@ -1916,5 +1933,5 @@
 	$attrHash->{'_content'} = $metavalue;
 
-##	print STDERR "**** overrideing metadata.xml\n";
+##	print STDERR "**** overriding metadata.xml\n";
 
 	# Don't want it to wipe out any other pieces of metadata
@@ -2016,4 +2033,70 @@
 }
 
+sub mxml_directorymetadata
+{
+    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
+
+    # if we haven't processed the metadata when we reach the end of metadata.xml
+    # it's because there's no particular FileSet element whose FileName matched
+    # In which case, add a new FileSet for this FileName
+    my $metamode = $parser->{'parameters'}->{'metamode'};
+    if($metamode ne "done") {
+	
+	if ($metamode eq "override") {
+	    print "No metadata value to override.  Switching 'metamode' to accumulate\n";
+	}
+
+	# If we get to here and metamode is override, this means there 
+	# was no existing value to overide => treat as an append operation
+	
+	# Create a new FileSet element and append to DirectoryMetadata
+	# <FileSet>
+	# <FileName>src_file</FileName>
+	# <Description>
+	# <Metadata mode="" name="">metavalue</Metadata>
+	# </Description>
+	# </FileSet>
+	my $src_file = $parser->{'parameters'}->{'src_file'};
+	my $metaname = $parser->{'parameters'}->{'metaname'};
+	my $metavalue = $parser->{'parameters'}->{'metavalue'};
+	my $metadata_attr = { 
+	    '_content' => $metavalue, 
+	    'name' => $metaname, 
+	    'mode' => "accumulate" 
+	};
+	my $append_metadata = [ "Metadata" => $metadata_attr ];
+	my $description_attr->{'_content'} = [ "\n\t\t   ", $append_metadata, "\n\t\t"];
+	my $description_element = [ "Description" => $description_attr ];
+
+	#_content is not an attribute, it's special and holds the children of this element
+	# including the textnode value embedded in this element if any.
+	my $filename_attr = {'_content' => $src_file};
+	my $filename_element = [ "FileName" => $filename_attr ];
+
+	my $fileset_attr = {};
+	$fileset_attr->{'_content'} = [ "\n\t\t", $filename_element,"\n\t\t",$description_element ,"\n\t" ];
+	my $fileset = [ "FileSet" => $fileset_attr ]; #my $fileset = [ "FileSet" => {} ];
+	
+	
+	# get children of dirmeta, and push the new FileSet element onto it
+	print "Appending metadata to metadata.xml\n";
+	my $dirmeta_content = $attrHash->{'_content'};
+	if (ref($dirmeta_content)) {
+	    # got some existing interesting nested content
+	    #push(@$dirmeta_content, "    ", $fileset ,"\n        ");
+	    push(@$dirmeta_content, "\t", $fileset ,"\n");
+	}
+	else {
+	    #description_content is most likely a string such as "\n"
+	    #$attrHash->{'_content'} = [$dirmeta_content, "    ", $fileset ,"\n" ];
+	    $attrHash->{'_content'} = [$dirmeta_content, "\t", $fileset ,"\n" ];
+	}	
+
+	$parser->{'parameters'}->{'metamode'} = "done";
+    }
+    # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
+    return [$tagname => $attrHash];
+}
+
 
 sub edit_metadata_xml
@@ -2028,10 +2111,12 @@
 	  'Metadata' => \&mxml_metadata,
 	  'Description' => \&mxml_description,
-          'FileSet' => \&mxml_fileset);
+          'FileSet' => \&mxml_fileset,
+	  'DirectoryMetadata' => \&mxml_directorymetadata);
 
     # use XML::Rules to add it in (read in and out again)
     my $parser = XML::Rules->new(rules => \@rules, 
 				 style => 'filter',
-                                 output_encoding => 'utf8');
+                                 output_encoding => 'utf8',
+				 stripspaces => 2|0|0); # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
 
 	if (!-e $metadata_xml_filename) {
@@ -2276,5 +2361,5 @@
 	if ((!defined $docid) || ($docid =~ m/^\s*$/)) 
 	{
-		$gsdl_cgi->generate_error_message("No docid (d=...) specified.\n");
+		$gsdl_cgi->generate_error("No docid (d=...) specified.\n");
 	}
 	
@@ -2298,5 +2383,5 @@
 	if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
 	{
-		$gsdl_cgi->generate_error_message("There is no metadata\n");
+		$gsdl_cgi->generate_error("There is no metadata\n");
 	}
 	
@@ -2358,5 +2443,7 @@
 		rules => \@rules, 
 		style => 'filter',
-		output_encoding => 'utf8'
+		output_encoding => 'utf8',
+	 #normalisespaces => 1,
+	        stripspaces => 2|0|0 # ineffectual
 	);
 	
@@ -2396,5 +2483,5 @@
 	if ((defined $parser->{'parameters'}->{'metavalue'}) && ($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'}) && ($attrHash->{'name'} eq $parser->{'parameters'}->{'metaname'}) && ($attrHash->{'_content'} eq $parser->{'parameters'}->{'metavalue'}))
 	{
-		return [];
+	    return [];
 	}
 
