Index: gsdl/trunk/perllib/cgiactions/metadataaction.pm
===================================================================
--- gsdl/trunk/perllib/cgiactions/metadataaction.pm	(revision 20537)
+++ gsdl/trunk/perllib/cgiactions/metadataaction.pm	(revision 20538)
@@ -45,25 +45,32 @@
 my $action_table =
 { 
-    "get-live-metadata"    => { 'compulsory-args' => [ "d", "metaname" ],
-			        'optional-args'   => [] }, 
-
-    "get-metadata"         => { 'compulsory-args' => [ "d", "metaname" ],
-			        'optional-args'   => [ "metapos" ] }, 
-
-    "set-live-metadata"    => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
-			        'optional-args'   => [ ] },
-
-    "set-metadata"         => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
-			        'optional-args'   => [ "metapos" ] },
-
-    "set-import-metadata"  => { 'compulsory-args' => [ "metaname", "metavalue" ],
-			        'optional-args'   => [ "d", "f", "metamode" ]
-				   # metamode can be "accumulate", "override", or "unique-id"
+    "get-live-metadata"     => { 'compulsory-args' => [ "d", "metaname" ],
+			         'optional-args'   => [] }, 
+
+    "get-metadata"          => { 'compulsory-args' => [ "d", "metaname" ],
+			         'optional-args'   => [ "metapos" ] }, 
+
+    "set-live-metadata"     => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
+			         'optional-args'   => [ ] },
+
+    "set-metadata"          => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
+			         'optional-args'   => [ "metapos" ] },
+
+    "set-archives-metadata" => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
+			         'optional-args'   => [ "metapos" ]
+
 				},
-    "remove-live-metadata" => { 'compulsory-args' => [ "d", "metaname" ],
-			        'optional-args'   => [ ] },
-
-    "remove-metadata"      => { 'compulsory-args' => [ "d", "metaname" ],
-			        'optional-args'   => [ "metapos" ] }
+
+    "set-import-metadata"   => { 'compulsory-args' => [ "metaname", "metavalue" ],
+			         'optional-args'   => [ "d", "f", "metamode" ]
+				    # metamode can be "accumulate", "override", or "unique-id"
+				 },
+
+
+    "remove-live-metadata"  => { 'compulsory-args' => [ "d", "metaname" ],
+			         'optional-args'   => [ ] },
+
+    "remove-metadata"       => { 'compulsory-args' => [ "d", "metaname" ],
+			         'optional-args'   => [ "metapos" ] }
 };
 
@@ -353,8 +360,5 @@
 
 
-
-
-
-sub mxml_metadata
+sub dxml_metadata
 {
 
@@ -383,5 +387,5 @@
 
 
-sub mxml_description
+sub dxml_description
 {
 
@@ -414,9 +418,182 @@
 }
 
+sub edit_xml_file
+{
+    my $self = shift @_;
+
+    my ($gsdl_cgi, $filename, $rules, $options) = @_;
+
+    # use XML::Rules to add it in (read in and out again)
+
+    my $parser = XML::Rules->new(rules => $rules, 
+				 style => 'filter' );
+
+    my $xml_in = "";
+    if (!open(MIN,"<$filename")) {
+	$gsdl_cgi->generate_error("Unable to read in $filename: $!");
+    }
+    else {
+	my $line;
+	while (defined ($line=<MIN>)) {
+	    $xml_in .= $line;
+	}
+	close(MIN);
+	
+	
+	my $xml_out = "";
+	$parser->filter($xml_in,\$xml_out, $options);
+    
+	if (!open(MOUT,">$filename")) {
+	    $gsdl_cgi->generate_error("Unable to write out to $filename: $!");
+	}
+	else {
+	    print MOUT $xml_out;
+	    close(MOUT);
+	    
+	}
+    }
+}
+
+
+
+sub edit_doc_xml
+{
+    my $self = shift @_;
+
+    my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos) = @_;
+
+    # use XML::Rules to add it in (read in and out again)
+
+    my @rules = 
+	( _default => 'raw extended',
+	  'Metadata' => \&dxml_metadata,
+	  'Description' => \&dxml_description );
+	  
+    my $options = { 'metaname'  => $metaname,
+		    'metapos'   => $metapos,
+		    'metavalue' => $metavalue };
+
+    $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@rules,$options);
+}
+
+
+sub set_archives_metadata
+{
+    my $self = shift @_;
+
+    my $username  = $self->{'username'};
+    my $collect   = $self->{'collect'};
+    my $gsdl_cgi  = $self->{'gsdl_cgi'};
+    my $gsdlhome  = $self->{'gsdlhome'};
+
+
+    # don't user authenticate for now
+    if ($baseaction::authentication_enabled) {
+	# Ensure the user is allowed to edit this collection
+	$self->authenticate_user($username, $collect);
+    }
+
+    my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+#    $gsdl_cgi->checked_chdir($collect_dir);
+
+    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 $metapos    = $self->{'metapos'};
+
+    $metapos = 0 if (!defined $metapos);
+
+    my $doc_db = "archiveinf-doc.gdb";
+    my $arcinfo_doc_filename = &util::filename_cat ($archive_dir, $doc_db);
+
+    my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$docid);
+
+    my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
+
+    
+    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
+    
+    my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
+
+    
+    $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
+			$metaname,$metavalue,$metapos);
+
+}
+
+
+
+
+sub mxml_metadata
+{
+
+    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
+
+
+    my $metaname = $parser->{'parameters'}->{'metaname'};
+    my $metamode = $parser->{'parameters'}->{'metamode'};
+
+    my $name_attr = $attrHash->{'name'};
+
+    if (($name_attr eq $metaname) && ($metamode eq "override")) {
+
+	my $metavalue = $parser->{'parameters'}->{'metavalue'};
+
+	$attrHash->{'_content'} = $metavalue;
+
+	# Don't want it to wipe out any other pieces of metadata
+	$parser->{'parameters'}->{'metamode'} = "done";
+
+    }
+
+    # raw extended
+    return (':'.$tagname => $attrHash, [$tagname => $attrHash]);
+}
+
+
+sub mxml_description
+{
+
+    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
+
+    my $metamode = $parser->{'parameters'}->{'metamode'};
+
+
+    if ($metamode eq "accumulate") {
+	# tack a new metadata tag on to the end of the <Metadata>+ block
+
+	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_content = $attrHash->{'_content'};
+
+	push(@$description_content,"    ", $append_metadata ,"\n        ");
+
+    }
+
+    # raw
+    return $tagname => $attrHash;
+}
+
 sub edit_metadata_xml
 {
     my $self = shift @_;
 
-    my ($gsdl_cgi, $metadata_xml_filename, $file, $metaname, $metavalue, $metamode) = @_;
+    my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metavalue, $metamode) = @_;
 
     # use XML::Rules to add it in (read in and out again)
@@ -458,8 +635,6 @@
 	}
     }
-
-
-
-}
+}
+
 
 sub set_import_metadata
@@ -524,9 +699,4 @@
     my $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
 
-
-    # Assuming that the metadata field is being indexed, then
-    # **** "touch" (in the Unix sense) $import_filename so we know it needs to
-    # be reindexed?
-    # (to be implemented)
     
     # figure out correct metadata.xml file
@@ -537,5 +707,5 @@
     my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
 
-    $self->edit_metadata_xml($gsdl_cgi,$metadata_xml_filename,$import_tailname,
+    $self->edit_metadata_xml($gsdl_cgi,$metadata_xml_filename,
 			     $metaname,$metavalue,$metamode);
 
