[metacat-cvs] metacat/src/perl register-dataset.cgi
Saurabh Garg
sgarg at ecoinformatics.org
Mon Nov 28 14:47:30 PST 2005
sgarg 05/11/28 14:47:30
Modified: src/perl register-dataset.cgi
Log:
New functions in the dataset registry to support moderation feature needed for ESA
Revision Changes Path
1.60 +675 -69 metacat/src/perl/register-dataset.cgi
Index: register-dataset.cgi
===================================================================
RCS file: /cvs/metacat/src/perl/register-dataset.cgi,v
retrieving revision 1.59
retrieving revision 1.60
diff -u -r1.59 -r1.60
--- register-dataset.cgi 4 Oct 2005 19:20:45 -0000 1.59
+++ register-dataset.cgi 28 Nov 2005 22:47:30 -0000 1.60
@@ -4,8 +4,8 @@
# Copyright: 2000 Regents of the University of California
#
# '$Author: sgarg $'
-# '$Date: 2005/10/04 19:20:45 $'
-# '$Revision: 1.59 $'
+# '$Date: 2005/11/28 22:47:30 $'
+# '$Revision: 1.60 $'
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -35,6 +35,7 @@
use Template;
use Net::SMTP;
use CGI qw/:standard :html3/;
+use CGI::Session;
use strict;
# Global configuration paramters
@@ -84,9 +85,10 @@
$config->define("hasMethod", { DEFAULT => 'true'} );
$config->define("temporalRequired", { DEFAULT => 'true'} );
$config->define("spatialRequired", { DEFAULT => 'true'} );
+$config->define("contactEmailAddressRequired", { DEFAULT => 'false'} );
$config->define("publicReadable", { DEFAULT => 'true'} );
-$config->define("accessPrincipals", { DEFAULT => ''} );
-$config->define("accessType", { DEFAULT => ''} );
+$config->define("moderators", { DEFAULT => ''} );
+$config->define("moderatorPermission", { DEFAULT => ''} );
$config->define("mailhost");
$config->define("sender");
$config->define("recipient");
@@ -99,8 +101,10 @@
$config->define("responseTemplate", { DEFAULT => 'crap.tmpl'} );
$config->define("entryFormTemplate", { DEFAULT => 'crap.tmpl'} );
$config->define("guideTemplate", { DEFAULT => 'crap.tmpl'} );
+$config->define("loginFormTemplate", { DEFAULT => 'crap.tmpl'} );
$config->define("confirmDataTemplate", { DEFAULT => 'crap.tmpl'} );
$config->define("deleteDataTemplate", { DEFAULT => 'crap.tmpl'} );
+$config->define("modNotificationTemplate", { DEFAULT => 'crap.tmpl'} );
$config->define("debug", { DEFAULT => '0'} );
$config->define("lat", { ARGCOUNT => ARGCOUNT_HASH} );
$config->define("lon", { ARGCOUNT => ARGCOUNT_HASH} );
@@ -127,7 +131,9 @@
my $entryFormTemplate = $config->entryFormTemplate();
my $deleteDataTemplate = $config->deleteDataTemplate();
my $guideTemplate = $config->guideTemplate();
+my $loginFormTemplate = $config->loginFormTemplate();
my $confirmDataTemplate = $config->confirmDataTemplate();
+my $modNotificationTemplate = $config->modNotificationTemplate();
my $accesspubid = $config->accesspubid();
my $accesssysid = $config->accesssysid();
my $datasetpubid = $config->datasetpubid();
@@ -144,9 +150,10 @@
my $hasMethod = $config->hasMethod();
my $temporalRequired = $config->temporalRequired();
my $spatialRequired = $config->spatialRequired();
+my $contactEmailAddressRequired = $config->contactEmailAddressRequired();
my $publicReadable = $config->publicReadable();
-my $accessPrincipals = $config->accessPrincipals();
-my $accessType = $config->accessType();
+my $moderators = $config->moderators();
+my $moderatorPermission = $config->moderatorPermission();
my $mailhost = $config->mailhost();
my $sender = $config->sender();
my $recipient = $config->recipient();
@@ -162,7 +169,7 @@
my $debug = $config->debug();
my $lat = $config->get('lat');
my $lon = $config->get('lon');
-
+$debug = 1;
# Convert the lat and lon configs into usable data structures
my @sitelist;
my %siteLatDMS;
@@ -185,8 +192,6 @@
# create an instance of the template processor
my $template = Template->new($ttConfig) || die $Template::ERROR, "\n";
-print "Content-type: text/html\n\n";
-
# Set up the template information that is common to all forms
$$templateVars{'cfg'} = $FORM::cfg;
$$templateVars{'recipient'} = $recipient;
@@ -198,6 +203,38 @@
debug("Registry: Initialized");
# Process the form based on stage parameter.
+if ($FORM::stage =~ "loginform") {
+ print "Content-type: text/html\n\n";
+ # Send back the login form.....
+ my $session = CGI::Session->load() or die CGI::Session->errstr();
+ if ( $session->is_empty ) {
+ # no session found ... redirect to login page template
+ $template->process( $loginFormTemplate, $templateVars);
+ } else {
+ # session found ... delete the session....
+ $session->delete();
+ $template->process( $loginFormTemplate, $templateVars);
+ }
+ exit(0);
+} elsif ($FORM::stage =~ "logout") {
+ handleLogoutRequest();
+ exit(0);
+} elsif ($FORM::stage =~ "login") {
+ handleLoginRequest();
+ exit(0);
+} elsif ($FORM::stage =~ "mod_accept") {
+ handleModAccept();
+ exit(0);
+} elsif ($FORM::stage =~ "mod_decline") {
+ handleModDecline();
+ exit(0);
+} elsif ($FORM::stage =~ "mod_revise") {
+ handleModRevise();
+ exit(0);
+}
+
+print "Content-type: text/html\n\n";
+
if ($FORM::stage =~ "guide") {
# Send back the information on how to fill the form
$$templateVars{'section'} = "Guide on How to Complete Registry Entries";
@@ -277,6 +314,7 @@
$$templateVars{'hasMethod'} = $hasMethod;
$$templateVars{'temporalRequired'} = $temporalRequired;
$$templateVars{'spatialRequired'} = $spatialRequired;
+ $$templateVars{'contactEmailAddressRequired'} = $contactEmailAddressRequired;
$$templateVars{'siteList'} = \@sortedSites;
$$templateVars{'section'} = "Entry Form";
@@ -516,6 +554,7 @@
my $xmldoc = shift;
my $docid = shift;
+ debug("Trying to insert the following document");
my $notunique = "SUCCESS";
debug("Registry: Starting insert (D1)");
my $response = $metacat->insert($docid, $xmldoc);
@@ -751,6 +790,8 @@
push(@invalidParams, "Last name of data set contact is missing.")
unless (hasContent($FORM::origNamelastContact) ||
$FORM::useOrigAddress);
+ push(@invalidParams, "Email address of data set contact is missing.")
+ unless (hasContent($FORM::origEmailContact) || $FORM::contactEmailAddressRequired ne 'true' );
push(@invalidParams, "Data medium is missing.")
unless (hasContent($FORM::dataMedium) || $FORM::dataMedium =~ /elect/);
push(@invalidParams, "Usage rights are missing.")
@@ -1425,20 +1466,31 @@
$doc .= "<principal>$username</principal>\n";
$doc .= "<permission>all</permission>\n";
$doc .= "</allow>\n";
- $doc .= "<allow>\n";
- $doc .= "<principal>uid=$FORM::username,o=$FORM::organization,dc=ecoinformatics,dc=org</principal>\n";
- $doc .= "<permission>all</permission>\n";
- $doc .= "</allow>\n";
- if($publicReadable eq "true"){
+
+ if($moderators eq ''){
$doc .= "<allow>\n";
- $doc .= "<principal>public</principal>\n";
- $doc .= "<permission>read</permission>\n";
+ $doc .= "<principal>uid=$FORM::username,o=$FORM::organization,dc=ecoinformatics,dc=org</principal>\n";
+ $doc .= "<permission>all</permission>\n";
$doc .= "</allow>\n";
+ } else {
+ foreach(split(":",$moderators)){
+ $doc .= "<allow>\n";
+ $doc .= "<principal>".$_."</principal>\n";
+ $doc .= "<permission>".$moderatorPermission."</permission>\n";
+ $doc .= "</allow>\n";
+ }
+
+ $doc .= "<allow>\n";
+ $doc .= "<principal>uid=$FORM::username,o=$FORM::organization,dc=ecoinformatics,dc=org</principal>\n";
+ $doc .= "<permission>read</permission>\n";
+ $doc .= "<permission>write</permission>\n";
+ $doc .= "</allow>\n";
}
- foreach(split(":",$accessPrincipals)){
+
+ if($publicReadable eq "true"){
$doc .= "<allow>\n";
- $doc .= "<principal>".$_."</principal>\n";
- $doc .= "<permission>".$accessType."</permission>\n";
+ $doc .= "<principal>public</principal>\n";
+ $doc .= "<permission>read</permission>\n";
$doc .= "</allow>\n";
}
$doc .= "</access>\n";
@@ -1449,54 +1501,11 @@
}
-################################################################################
-#
-# send an email message notifying the moderator of a new submission
-#
-################################################################################
-sub sendNotification {
- my $identifier = shift;
- my $mailhost = shift;
- my $sender = shift;
- my $recipient = shift;
+sub readDocumentFromMetacat(){
- my $smtp = Net::SMTP->new($mailhost);
- $smtp->mail($sender);
- $smtp->to($recipient);
-
- my $message = <<" ENDOFMESSAGE";
- To: $recipient
- From: $sender
- Subject: New data submission
-
- Data was submitted to the data registry.
- The identifying information for the new data set is:
-
- Identifier: $identifier
- Title: $FORM::title
- Submitter: $FORM::providerGivenName $FORM::providerSurName
-
- Please review the submmission and grant public read access if appropriate.
- Thanks
-
- ENDOFMESSAGE
- $message =~ s/^[ \t\r\f]+//gm;
-
- $smtp->data($message);
- $smtp->quit;
-}
-
-
-################################################################################
-#
-# read the eml document and send back a form with values filled in.
-#
-################################################################################
-sub modifyData {
-
+ my $docid = $FORM::docid;
# create metacat instance
my $metacat;
- my $docid = $FORM::docid;
my $httpMessage;
my $doc;
my $xmldoc;
@@ -1602,17 +1611,28 @@
#open (TFILE,">$tempfile") || die ("Cant open xml file...\n");
#print TFILE $stylesheet->output_string($resultsheet);
#close(TFILE);
-
- getFormValuesFromEml2($resultsheet);
# Delete the files written earlier.
unlink @fileArray;
- } else {
- getFormValuesFromEml2($xmldoc);
+ return $resultsheet;
+
}
- }
+ }
+ return $xmldoc;
+}
+
+################################################################################
+#
+# read the eml document and send back a form with values filled in.
+#
+################################################################################
+sub modifyData {
+
+ my $xmldoc = readDocumentFromMetacat();
+ getFormValuesFromEml2($xmldoc);
+
if (scalar(@errorMessages)) {
# if any errors, print them in the response template
$$templateVars{'status'} = 'failure_no_resubmit';
@@ -1656,6 +1676,7 @@
$$templateVars{'hasTaxonomic'} = $hasTaxonomic;
$$templateVars{'hasMethod'} = $hasMethod;
$$templateVars{'spatialRequired'} = $spatialRequired;
+ $$templateVars{'contactEmailAddressRequired'} = $contactEmailAddressRequired;
$$templateVars{'temporalRequired'} = $temporalRequired;
# find out the tag <alternateIdentifier>.
@@ -2395,6 +2416,575 @@
}
}
+################################################################################
+#
+# function to handle login request
+#
+################################################################################
+sub handleLoginRequest(){
+ # Check if a session already exists
+ my $session = CGI::Session->load() or die CGI::Session->errstr();
+ if ( $session->is_empty ) {
+ # no session found ... check if the login is correct
+ my $username = $FORM::username;
+ my $password = $FORM::password;
+
+ debug($username);
+ debug($password);
+ my $metacat = Metacat->new();
+ if ($metacat) {
+ $metacat->set_options( metacatUrl => $metacatUrl );
+ } else {
+ #die "failed during metacat creation\n";
+ push(@errorMessages, "Failed during metacat creation.");
+ }
+ my $returnVal = $metacat->login($username,$password);
+ debug($returnVal);
+ if($returnVal > 0){
+ # valid username and passwd
+ # create a new session and store username and passswd
+ $session = new CGI::Session();
+
+ $session->param('username', $username);
+ $session->param('password', $password);
+
+ if($returnVal == 2 || $returnVal == 4){
+ # is a moderator. store this information in the session
+ $session->param('moderator', 'true');
+ }
+
+ # send redirect to metacat and action = login
+ my $html = "<html><head>";
+ $html .= "</head><body onload=\"document.loginForm.submit()\">";
+ $html .= "<form name=\"loginForm\" method=\"post\" action=\"@systemidserver@@servlet-path@\">";
+ $html .= "<input type=\"hidden\" name=\"action\" value=\"login\" />";
+ $html .= "<input type=\"hidden\" name=\"username\" value=\"".$FORM::username."\" />";
+ $html .= "<input type=\"hidden\" name=\"password\" value=\"".$FORM::password."\" />";
+ $html .= "<input type=\"hidden\" name=\"qformat\" value=\"".$FORM::cfg."\" />";
+ $html .= "</form></body></html>";
+ debug($html);
+ debug($session->header());
+ print $session->header();
+ print $html;
+ } else {
+ # send back the error template with error message as wrong username password
+ # push(@errorMessages, $metacat->getMessage());
+ print "Content-type: text/html\n\n";
+ push(@errorMessages, "Failed during login.\n");
+ $$templateVars{'status'} = 'login_failure';
+ $$templateVars{'errorMessages'} = \@errorMessages;
+ $$templateVars{'cfg'} = $FORM::cfg;
+ $$templateVars{'section'} = "Login Status";
+ $template->process( $responseTemplate, $templateVars);
+ }
+
+ exit(0);
+ } else {
+ # session found ... redirect to index page
+ my $url = "@systemidserver@@style-skins-path@/".$FORM::cfg."/index.html";
+ redirect($url);
+ exit(0);
+ }
+}
+
+################################################################################
+#
+# function to handle logout request
+#
+################################################################################
+sub handleLogoutRequest(){
+ print "Content-type: text/html\n\n";
+ # Check if the session exists
+ debug("Stage is logout");
+ my $session = CGI::Session->load();
+ if ( $session->is_empty || $session->is_expired) {
+ # no session found ... send back to index.html page ...
+ debug("Session empty or not found");
+ my $url = "@systemidserver@@style-skins-path@/".$FORM::cfg."/index.html";
+ redirect($url);
+ # should logout request be sent to metacat???
+ } else {
+ # get the user name and delete the session
+ debug("Session found");
+ my $uname = $session->param("username");
+ $session->delete();
+ # send redirect to metacat and action = login
+ my $html = "<html><head>";
+ $html .= "</head><body onload=\"document.loginForm.submit()\">";
+ $html .= "<form name=\"loginForm\" method=\"post\" action=\"@systemidserver@@servlet-path@\">";
+ $html .= "<input type=\"hidden\" name=\"action\" value=\"logout\" />";
+ $html .= "<input type=\"hidden\" name=\"username\" value=\"".$uname."\" />";
+ $html .= "<input type=\"hidden\" name=\"qformat\" value=\"".$FORM::cfg."\" />";
+ $html .= "</form></body></html>";
+ debug($html);
+ print($html);
+ }
+}
+
+################################################################################
+#
+# function to handle accept request for moderator
+#
+################################################################################
+sub handleModAccept(){
+
+ my $metacat;
+ $metacat = Metacat->new();
+ if ($metacat) {
+ $metacat->set_options( metacatUrl => $metacatUrl );
+ } else {
+ #die "failed during metacat creation\n";
+ push(@errorMessages, "Failed during metacat creation.");
+ }
+
+ print "Content-type: text/html\n\n";
+
+ # Check if the session exists
+ debug("Stage is mod_accept");
+
+ my $session = CGI::Session->load();
+ if ( $session->is_empty || $session->is_expired) {
+ # no session found ... send back to index.html page ...
+ push(@errorMessages, "Failed during login.\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # check if logged in user is moderator
+ my $moderator = $session->param("moderator");
+ if ( $moderator ne 'true') {
+ # no session found ... send back to index.html page ...
+ debug("Logged in user is not moderator");
+ push(@errorMessages, "Logged in user is not moderator. Please login again as moderator\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # change the access rules
+
+ ## create the access block
+ my $accessblock = '';
+ $accessblock .= "<access authSystem=\"knb\" order=\"denyFirst\">\n";
+ $accessblock .= "<allow>\n";
+ $accessblock .= "<principal>$username</principal>\n";
+ $accessblock .= "<permission>all</permission>\n";
+ $accessblock .= "</allow>\n";
+
+ foreach(split(":",$moderators)){
+ $accessblock .= "<allow>\n";
+ $accessblock .= "<principal>".$_."</principal>\n";
+ $accessblock .= "<permission>".$moderatorPermission."</permission>\n";
+ $accessblock .= "</allow>\n";
+ }
+
+ $accessblock .= "<allow>\n";
+ $accessblock .= "<principal>public</principal>\n";
+ $accessblock .= "<permission>read</permission>\n";
+ $accessblock .= "</allow>\n";
+ $accessblock .= "</access>\n";
+
+ ## read the document from
+ my $docFromMetacat = readDocumentFromMetacat();
+ my ($emldoc, $temp) = split('<access authSystem=', $docFromMetacat);
+
+ $emldoc .= $accessblock;
+ $emldoc .= "</dataset>\n</eml:eml>\n";
+
+ debug("Document created by handleModAccept is " . $emldoc);
+
+ # update the document
+ my $docid = $FORM::docid;
+
+ my $x;
+ my $y;
+ my $z;
+
+ ($x, $y, $z) = split(/\./, $docid);
+ $z++;
+ $docid = "$x.$y.$z";
+
+ $emldoc =~ s/packageId=\"docid\"/packageId=\"$docid\"/;
+
+ my $response = $metacat->update($docid, $emldoc);
+
+ if (! $response) {
+ debug("Error while updating in handleModAccept.");
+
+ push(@errorMessages, $metacat->getMessage());
+ push(@errorMessages, "Failed while updating.\n");
+
+ ############ TODO: need for proper error message
+ }
+
+ # get the contact email address from the
+ my $parser = XML::LibXML->new();
+ my $xmldoc = $parser->parse_string($emldoc);
+
+ if ($xmldoc eq "" || $doc=~ /<error/) {
+ # not able to parse
+ push(@errorMessages, "Error in reading the docid:".$docid."\n");
+ } else {
+ my $findNodes = $xmldoc->findnodes('//dataset/contact');
+ if($findNodes->size() > 0){
+ # found contact email address
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $contactEmailAddress = findValue($node,'electronicMailAddress');
+ my $surName = findValue($node,'individualName/surName');
+ my $givenName = findValue($node,'individualName/givenName');
+ my $organizationName = findValue($node,'organizaitionName');
+
+ if($surName ne ''){
+ $contactName = $givenName.' '.$surName;
+ } else {
+ $contactName = $organizationName;
+ }
+ }
+ } else {
+ $contactEmailAddress = '';
+ }
+ $findNodes = $xmldoc->findnodes('//dataset/title');
+ if($findNodes->size() > 0){
+ # found title
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $title = findValue($node,'../title');
+ }
+ } else {
+ $title = '';
+ }
+ }
+
+ # send notification to the user and the moderator
+ if (!scalar(@errorMessages)) {
+ # send notifications
+ mod_sendNotification($title, $contactEmailAddress, $contactName, 'Document '.$FORM::docid.' Accepted');
+
+ $$templateVars{'function'} = "deleted";
+ $$templateVars{'section'} = "Deletion Status";
+ $template->process( $responseTemplate, $templateVars);
+ } else {
+ ############ TODO: need for proper error message
+ processErrorTemplate('failure', '', 'Unable to delete the document');
+ }
+}
+
+################################################################################
+#
+# function to handle decline request for moderator
+#
+################################################################################
+sub handleModDecline(){
+
+ my $metacat;
+ $metacat = Metacat->new();
+ if ($metacat) {
+ $metacat->set_options( metacatUrl => $metacatUrl );
+ } else {
+ #die "failed during metacat creation\n";
+ push(@errorMessages, "Failed during metacat creation.");
+ }
+
+ print "Content-type: text/html\n\n";
+
+ # Check if the session exists
+ debug("Stage is mod_decline");
+
+ my $session = CGI::Session->load();
+ if ( $session->is_empty || $session->is_expired) {
+ # no session found ... send back to index.html page ...
+ push(@errorMessages, "Failed during login.\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # check if logged in user is moderator
+ my $moderator = $session->param("moderator");
+ if ( $moderator ne 'true') {
+ # no session found ... send back to index.html page ...
+ debug("Logged in user is not moderator");
+ push(@errorMessages, "Logged in user is not moderator. Please login again as moderator\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # variables for contact information
+ my $contactEmailAddress;
+ my $contactName;
+ my $title;
+
+ # Log into metacat
+ my $username = $session->param("username");
+ my $password = $session->param("password");
+ my $response = $metacat->login($username, $password);
+
+ if (! $response) {
+ # Could not login
+ push(@errorMessages, $metacat->getMessage());
+ push(@errorMessages, "Failed during login.\n");
+
+ } else {
+ # read the document and get the contact name and address
+ my $parser = XML::LibXML->new();
+ my $docid = $FORM::docid;
+
+ $response = $metacat->read($docid);
+ my $doc = $response->content();
+ my $xmldoc = $parser->parse_string($doc);
+
+ if ($xmldoc eq "" || $doc=~ /<error/) {
+ # not able to parse
+ push(@errorMessages, "Error in reading the docid:".$docid."\n");
+ } else {
+ my $findNodes = $xmldoc->findnodes('//dataset/contact');
+ if($findNodes->size() > 0){
+ # found contact email address
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $contactEmailAddress = findValue($node,'electronicMailAddress');
+
+ my $surName = findValue($node,'individualName/surName');
+ my $givenName = findValue($node,'individualName/givenName');
+ my $organizationName = findValue($node,'organizaitionName');
+
+ if($surName ne ''){
+ $contactName = $givenName.' '.$surName;
+ } else {
+ $contactName = $organizationName;
+ }
+ }
+ } else {
+ $contactEmailAddress = '';
+ }
+ $findNodes = $xmldoc->findnodes('//dataset/title');
+ if($findNodes->size() > 0){
+ # found title
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $title = findValue($node,'../title');
+ }
+ } else {
+ $title = '';
+ }
+
+ }
+
+ #$response = $metacat->delete($docid);
+ if (!$response) {
+ debug("Error while deleting document in handleModDecline.");
+ push(@errorMessages, $metacat->getMessage());
+ push(@errorMessages, "Failed during deleting $docid. Please check if you are authorized to delete this document.\n");
+ } else {
+ debug("Document deleted by handleModDecline is " . $docid);
+ }
+ }
+
+ if (!scalar(@errorMessages)) {
+ # send notifications
+ mod_sendNotification($title, $contactEmailAddress, $contactName, 'Document '.$FORM::docid.' Rejected');
+
+ $$templateVars{'function'} = "deleted";
+ $$templateVars{'section'} = "Deletion Status";
+ $template->process( $responseTemplate, $templateVars);
+ } else {
+ ############ TODO: need for proper error message
+ processErrorTemplate('failure', '', 'Unable to delete the document');
+ }
+}
+
+################################################################################
+#
+# function to handle revise request for moderator
+#
+################################################################################
+sub handleModRevise(){
+
+ my $metacat;
+ $metacat = Metacat->new();
+ if ($metacat) {
+ $metacat->set_options( metacatUrl => $metacatUrl );
+ } else {
+ #die "failed during metacat creation\n";
+ push(@errorMessages, "Failed during metacat creation.");
+ }
+
+ print "Content-type: text/html\n\n";
+
+ # Check if the session exists
+ debug("Stage is mod_revise");
+
+ my $session = CGI::Session->load();
+ if ( $session->is_empty || $session->is_expired) {
+ # no session found ... send back to index.html page ...
+ push(@errorMessages, "Failed during login.\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # check if logged in user is moderator
+ my $moderator = $session->param("moderator");
+ if ( $moderator ne 'true') {
+ # no session found ... send back to index.html page ...
+ debug("Logged in user is not moderator");
+ push(@errorMessages, "Logged in user is not moderator. Please login again as moderator\n");
+ processErrorTemplate('login_failure', 'loginform', 'Login Status');
+ return;
+ }
+
+ # variables for contact information
+ my $contactEmailAddress;
+ my $contactName;
+ my $title;
+
+ # Log into metacat
+ my $username = $session->param("username");
+ my $password = $session->param("password");
+ my $response = $metacat->login($username, $password);
+
+ if (! $response) {
+ # Could not login
+ push(@errorMessages, $metacat->getMessage());
+ push(@errorMessages, "Failed during login.\n");
+
+ } else {
+ # read the document and get the contact name and address
+ my $parser = XML::LibXML->new();
+ my $docid = $FORM::docid;
+
+ $response = $metacat->read($docid);
+ my $doc = $response->content();
+ my $xmldoc = $parser->parse_string($doc);
+
+ if ($xmldoc eq "" || $doc=~ /<error/) {
+ # not able to parse
+ push(@errorMessages, "Error in reading the docid:".$docid."\n");
+ } else {
+ my $findNodes = $xmldoc->findnodes('//dataset/contact');
+ if($findNodes->size() > 0){
+ # found contact email address
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $contactEmailAddress = findValue($node,'electronicMailAddress');
+
+ my $surName = findValue($node,'individualName/surName');
+ my $givenName = findValue($node,'individualName/givenName');
+ my $organizationName = findValue($node,'organizaitionName');
+
+ if($surName ne ''){
+ $contactName = $givenName.' '.$surName;
+ } else {
+ $contactName = $organizationName;
+ }
+ }
+ } else {
+ $contactEmailAddress = '';
+ }
+ $findNodes = $xmldoc->findnodes('//dataset/title');
+ if($findNodes->size() > 0){
+ # found title
+ my $node = '';
+ foreach $node ($findNodes->get_nodelist) {
+ $title = findValue($node,'../title');
+ }
+ } else {
+ $title = '';
+ }
+
+ }
+ }
+
+ # send notifications
+ mod_sendNotification($title, $contactEmailAddress, $contactName, "Revise document: ".$FORM::docid);
+
+ $$templateVars{'function'} = "deleted";
+ $$templateVars{'section'} = "Deletion Status";
+ $template->process( $responseTemplate, $templateVars);
+}
+
+
+
+################################################################################
+#
+# send an email message to the moderator and the contact
+#
+################################################################################
+sub mod_sendNotification {
+ my $title = shift;
+ my $contactEmailAddress = shift;
+ my $contactName = shift;
+ my $subject = shift;
+
+ debug($subject);
+ # send notification to the user and the moderator
+ my $templateVars = { 'stage' => $FORM::stage };
+ $$templateVars{'recipient'} = $recipient;
+ $$templateVars{'sender'} = $sender;
+ $$templateVars{'docid'} = $FORM::docid;
+ $$templateVars{'comment'} = $FORM::comment;
+ $$templateVars{'contactName'} = $contactName;
+ $$templateVars{'dpTitle'} = $title;
+
+ $$templateVars{'recipient_status'} = 'moderator';
+ sendNotification($mailhost, $sender, $recipient, $subject,
+ $modNotificationTemplate, $templateVars);
+ if($contactEmailAddress ne ''){
+ # send notification to contact email address specified in cfg
+ $$templateVars{'recipient'} = $contactEmailAddress;
+ $$templateVars{'recipient_status'} = 'user';
+ sendNotification($mailhost, $sender, $contactEmailAddress,
+ $subject, $modNotificationTemplate, $templateVars);
+ }
+}
+
+################################################################################
+#
+# send an email message notifying the moderator of a new submission
+#
+################################################################################
+sub sendNotification {
+ my $mailhost = shift;
+ my $sender = shift;
+ my $recipient = shift;
+ my $subject = shift;
+ my $templateForm = shift;
+ my $templateVars = shift;
+
+ debug($mailhost);
+ debug($sender);
+ debug($recipient);
+ debug($subject);
+
+ my $smtp = Net::SMTP->new($mailhost);
+ $smtp->mail($sender);
+ $smtp->to($recipient);
+
+ $smtp->data;
+ $smtp->datasend("From: $sender\n");
+ $smtp->datasend("To: <$recipient>\n");
+ $smtp->datasend("Subject: $subject\n");
+ $smtp->datasend("\n");
+
+ my $message;
+ $template->process($templateForm, $templateVars,\$message);
+
+ $smtp->datasend($message);
+ $smtp->dataend();
+ $smtp->quit;
+}
+
+sub processErrorTemplate(){
+ my $status = shift;
+ my $function = shift;
+ my $section = shift;
+
+ $$templateVars{'status'} = $status;
+ $$templateVars{'function'} = $function;
+ $$templateVars{'errorMessages'} = \@errorMessages;
+ $$templateVars{'cfg'} = $FORM::cfg;
+ $$templateVars{'section'} = $section;
+ $template->process( $responseTemplate, $templateVars);
+}
+
################################################################################
#
@@ -2594,6 +3184,7 @@
$$templateVars{'hasTaxonomic'} = $FORM::hasTaxonomic;
$$templateVars{'hasMethod'} = $FORM::hasMethod;
$$templateVars{'spatialRequired'} = $FORM::spatialRequired;
+ $$templateVars{'contactEmailAddressRequired'} = $FORM::contactEmailAddressRequired;
$$templateVars{'temporalRequired'} = $FORM::temporalRequired;
$$templateVars{'docid'} = $FORM::docid;
@@ -2791,6 +3382,7 @@
$$templateVars{'hasTaxonomic'} = $FORM::hasTaxonomic;
$$templateVars{'hasMethod'} = $FORM::hasMethod;
$$templateVars{'spatialRequired'} = $FORM::spatialRequired;
+ $$templateVars{'contactEmailAddressRequired'} = $FORM::contactEmailAddressRequired;
$$templateVars{'temporalRequired'} = $FORM::temporalRequired;
}
@@ -2907,6 +3499,20 @@
# print $deg." ".$min." ".$sec;
# print "\n";
#}
+
+
+################################################################################
+#
+# print redirect html code
+#
+################################################################################
+sub redirect(){
+ my $url = shift;
+
+ print "<head>";
+ print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=".$url."\">";
+ print "</head><body></body></html>";
+}
################################################################################
More information about the Metacat-cvs
mailing list