Index: src/perl/register-dataset.cgi
===================================================================
--- src/perl/register-dataset.cgi	(revision 4757)
+++ src/perl/register-dataset.cgi	(revision 4758)
@@ -67,9 +67,11 @@
 
 # url configuration
 my $server = $properties->splitToTree(qr/\./, 'server');
-my $contextUrl = 'http://' . $properties->getProperty('server.name') . ':' . 
-                 $properties->getProperty('server.httpPort') . '/' . 
-                 $properties->getProperty('application.context');
+my $contextUrl = 'http://' . $properties->getProperty('server.name');
+if ($properties->getProperty('server.httpPort') ne '80') {
+    $contextUrl = $contextUrl . ':' . $properties->getProperty('server.httpPort');
+}
+$contextUrl = $contextUrl . '/' .  $properties->getProperty('application.context');
 
 my $metacatUrl = $contextUrl . "/metacat";
 my $cgiPrefix = "/" . $properties->getProperty('application.context') . "/cgi-bin";
@@ -428,7 +430,7 @@
             # document is being inserted 
             my $docStatus = "INCOMPLETE";
             while ($docStatus eq "INCOMPLETE") {
-                $docid = newAccessionNumber($config->{'scope'});
+                $docid = newAccessionNumber($config->{'scope'}, $metacat);
 
                 $xmldocWithDocID =~ s/docid/$docid/;
                 debugDoc($xmldocWithDocID);
@@ -554,10 +556,10 @@
 ################################################################################
 sub newAccessionNumber {
     my $scope = shift;
+    my $metacat = shift;
     my $errormsg = 0;
 
     my $docid = $metacat->getLastId($scope);
-
     if (!$docid) {
         $docid = "$scope.1.1";
         debug("Error in newAccessionNumber: " . $metacat->getMessage());
@@ -957,7 +959,7 @@
     }
 
     if (!$docid) {
-        $docid = newAccessionNumber($config->{'scope'});
+        $docid = newAccessionNumber($config->{'scope'}, $metacat);
         my $uploadReturn = uploadData($outFile, $docid);
         if (!$uploadReturn) {
             debug("Uploading the data failed.");
@@ -2950,7 +2952,7 @@
           <frameset rows="150,*" cols="*" frameborder="NO" border="0" framespacing="0">
             <frame src="$skinsDir/$skinName/header.jsp" marginwidth="40" name="topFrame" scrolling="NO" noresize>
             <frameset cols="200,*" frameborder="NO" border="0" framespacing="0">
-              <body></body><frame src="$cgiPrefix/register-dataset.cgi?cfg=$skinName&stage=review_frame&docid="$docid"" name="rightFrame" scrolling="NO" noresize></frame>
+              <body></body><frame src="$cgiPrefix/register-dataset.cgi?cfg=$skinName&stage=review_frame&docid=$docid" name="rightFrame" scrolling="NO" noresize></frame>
               <frame src="$metacatUrl?action=read&qformat=$skinName&docid=$docid&insertTemplate=0" name="mainFrame">
             </frameset>
           </frameset>
@@ -2968,7 +2970,6 @@
     }
 
     my $moderator = $session->param("moderator");
-
     $$templateVars{'reviewdocid'} = getReviewHistoryHTML();
     $$templateVars{'docid'} = $FORM::docid;
     if ($moderator eq 'true') {
@@ -2984,9 +2985,13 @@
     my $parser = XML::LibXML->new();
     my $docid = $FORM::docid;
     my ($x, $y, $z) = split(/\./, $docid);
+    if ($x eq "") {
+        return "Error: Unable to find review for invalid docid.";
+    }
     my $docidWithoutRev = $x.".".$y;
     my $reviewDocumentId = '';
-    my $response = $metacat->squery("<pathquery><querytitle>Moderator-Search</querytitle><querygroup operator='INTERSECT'><queryterm searchmode='contains' casesensitive='false'><value>$docidWithoutRev</value><pathexpr>/reviewHistory/review/packageId</pathexpr></queryterm></querygroup></pathquery>");
+    my $reviewQuery = "<pathquery><querytitle>Moderator-Search</querytitle><querygroup operator='INTERSECT'><queryterm searchmode='contains' casesensitive='false'><value>$docidWithoutRev</value><pathexpr>/reviewHistory/review/packageId</pathexpr></queryterm></querygroup></pathquery>";
+    my $response = $metacat->squery($reviewQuery);
     my $doc = $response->content();
     my $xmldoc = $parser->parse_string($doc);
     my $reviewHTML = '';
@@ -2993,7 +2998,7 @@
 
     if ($xmldoc eq "" || $doc=~ /<error/) {
         # not able to parse
-        return " Unable to search for review for the docid:".$docid;
+        return "Error: Unable to search for review for the docid:".$docid;
     } else {
         my $findNodes = $xmldoc->findnodes('//resultset/document');
         if ($findNodes->size() > 0) {
@@ -3067,9 +3072,9 @@
 
             #debug("Document created by handleModAccept is " . $emldoc);
             # update the document
-            my $newDocid = incrementRevision($docid);
+            my $docid = incrementRevision($docid);
 
-            $emldoc =~ s/packageId=\"$docid\"/packageId=\"$newDocid\"/;
+            $emldoc =~ s/packageId="docid"/packageId=\"$docid\"/;
             debugDoc($emldoc);
             $response = $metacat->update($docid, $emldoc);
 
@@ -3304,7 +3309,7 @@
         $response = $metacat->read($docid);
         my $doc = $response->content();
         my $xmldoc = $parser->parse_string($doc);
-        
+        $xmldoc = transformEml($xmldoc); 
         if ($xmldoc eq "" || $doc=~ /<error/) {
             # not able to parse
             $errorMessage = $errorMessage." Error in reading the docid:".$docid;
@@ -3362,12 +3367,12 @@
         my $accessElem = '';
 
         # pull out existing dataset entry 
-        my $results = $xmldoc->findnodes('//dataset');
+        my $results = $xmldoc->findnodes('/eml:eml/dataset');
         foreach my $r ($results->get_nodelist) {
             $dataElem .= $r->toString();
         }
         # also grab the access element
-        $results = $xmldoc->findnodes('//access');
+        $results = $xmldoc->findnodes('/eml:eml/access');
         foreach my $r ($results->get_nodelist) {
           $accessElem .= $r->toString();
         }
@@ -3385,16 +3390,16 @@
         $emldoc .= $addlMetadata;
         $emldoc .= EMLEnd();
         
-        debugDoc($emldoc);
         # update the document
-        my $newDocid = incrementRevision($docid);
+        my $docid = incrementRevision($docid);
 
-        $emldoc =~ s/packageId=\"$docid\"/packageId=\"$newDocid\"/;
-        $response = $metacat->update($newDocid, $emldoc);
+        $emldoc =~ s/packageId="docid"/packageId="$docid"/;
+        debugDoc($emldoc);
+        $response = $metacat->update($docid, $emldoc);
 
         if (!$response) {
             debug("Error while updating in handleModAccept.".$metacat->getMessage());
-            $errorMessage = $errorMessage." Failed while updating the document with additional Metadata.";
+            $errorMessage = $errorMessage." Failed while updating the document with additional metadata.";
         }
 
     }
@@ -3434,9 +3439,10 @@
 
     # find out if a docid already exists...
     my ($x, $y, $z) = split(/\./, $docid);
-    my $docidWithoutRev = $x.$y;
+    my $docidWithoutRev = $x . "." . $y;
+    debug("docid without rev: $docidWithoutRev");
     my $reviewDocumentId = '';
-    my $response = $metacat->squery("<pathquery><querytitle>Moderator-Search</querytitle><querygroup operator='INTERSECT'><queryterm searchmode='contains' casesensitive='false'><value>".$x.".".$y."</value><pathexpr>/reviewHistory/review/packageId</pathexpr></queryterm></querygroup></pathquery>"); 
+    my $response = $metacat->squery("<pathquery><querytitle>Moderator-Search</querytitle><querygroup operator='INTERSECT'><queryterm searchmode='contains' casesensitive='false'><value>$docidWithoutRev</value><pathexpr>/reviewHistory/review/packageId</pathexpr></queryterm></querygroup></pathquery>"); 
 
     my $doc = $response->content();
     my $xmldoc = $parser->parse_string($doc);
@@ -3443,7 +3449,8 @@
 
     if ($xmldoc eq "" || $doc=~ /<error/) {
         # not able to parse
-        return " Unable to store review for the docid:".$docid;
+        debug("Unable to parse results for $docidWithoutRev query.");
+        return "Error: Unable to store review for the docid:".$docid;
     } else {
         my $findNodes = $xmldoc->findnodes('//resultset/document');
         if ($findNodes->size() > 0) {
@@ -3464,6 +3471,8 @@
                 my ($reviewdoc, $temp) = split('</reviewHistory>', $doc);
                 $reviewdoc .= $review."\n</reviewHistory>\n";
                 $reviewDocumentId = incrementRevision($reviewDocumentId);
+                debug("Generating review history document for $reviewDocumentId");
+                debugDoc($reviewdoc);
                 $response = $metacat->update($reviewDocumentId, $reviewdoc);
                 if ($response != '1') {
                     return " Unable to update the review on Metacat for the docid:".$docid;
@@ -3482,7 +3491,9 @@
             }
         } else {
             #insert a new document
-            my $id = newAccessionNumber('esa_reviews');
+            debug("no review history document found, generating a new one.");
+
+            my $id = newAccessionNumber('esa_reviews', $metacat);
             my $reviewDoc = '';
             my $failMessage = " Unable to insert the review on Metacat for the docid:".$docid;
 
@@ -3541,7 +3552,7 @@
     $$templateVars{'comment'} = $FORM::review;
     $$templateVars{'contactName'} = $contactName;
     $$templateVars{'dpTitle'} = $title;
-
+    $$templateVars{'registryUrl'} = $contextUrl . "/cgi-bin/register-dataset.cgi";
     $$templateVars{'recipient_status'} = 'moderator';
     sendNotification($email->{'sender'}, $email->{'recipient'}, $subject, 
                             $templates->{'modEmailNotification'}, $templateVars);
@@ -3564,7 +3575,9 @@
     my $subject = shift;
     my $templateForm = shift;
     my $templateVars = shift;
-
+    
+    # When testing, set recipient to your email address
+    # $recipient = 'walbridge@nceas.ucsb.edu';
     my $smtp = Net::SMTP->new($email->{'mailhost'});
     $smtp->mail($sender);
     $smtp->to($recipient);
