Project

General

Profile

1
#!/usr/bin/perl
2
#
3
#  '$RCSfile$'
4
#  Copyright: 2000 Regents of the University of California 
5
#
6
#   '$Author: jones $'
7
#     '$Date: 2003-11-24 10:44:03 -0800 (Mon, 24 Nov 2003) $'
8
# '$Revision: 1936 $' 
9
# 
10
# This program is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation; either version 2 of the License, or
13
# (at your option) any later version.
14
#
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
#
20
# You should have received a copy of the GNU General Public License
21
# along with this program; if not, write to the Free Software
22
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
#
24

    
25
#
26
# This is a CGI application for inserting metadata documents into
27
# the Metacat database.  It utilizes the Metacat.pm module for most work.
28
# In this script, we process the form fields passed in from a POST, insert a
29
# metadata document and an ACL document.
30

    
31
use Metacat;
32
use AppConfig qw(:expand :argcount);
33
use XML::LibXML;
34
use XML::LibXSLT;
35
use Template;
36
use Net::LDAP;
37
use Net::SMTP;
38
use CGI qw/:standard :html3/;
39
use strict;
40

    
41
# Global configuration paramters
42
#my $cfgdir = "@install-dir@";
43
#my $cfgdir = "/usr/local/devtools/tomcat/webapps/knb/style/skins";
44
my $cfgdir = "@install-dir@@style-skins-relpath@";
45
my $tmpdir = "@temp-dir@";
46
my $templatesdir = "@install-dir@@style-common-relpath@/templates";
47
my $now = time;
48
my $xslConvDir = "$cfgdir/lib/style/";
49

    
50
# Import all of the HTML form fields as variables
51
import_names('FORM');
52

    
53
# Set up the hash for returning data to the HTML templates
54
my $templateVars = { 'status' => 'success' };
55
my $error = 0;
56
my @errorMessages;
57

    
58
# create a new AppConfig object and load our config parameters
59
# note that this requires the form submission to have a "cfg" paramter
60
# to determine which config file to load
61
my $config = AppConfig->new({
62
    GLOBAL => { ARGCOUNT => ARGCOUNT_ONE, } });
63

    
64
$config->define("metacatUrl");
65
$config->define("username");
66
$config->define("password");
67
$config->define("ldapUrl");
68
$config->define("defaultScope");
69
$config->define("organization");
70
$config->define("orgabbrev");
71
$config->define("orgurl");
72
$config->define("accesspubid");
73
$config->define("accesssysid");
74
$config->define("datasetpubid");
75
$config->define("datasetsysid");
76
$config->define("mailhost");
77
$config->define("sender");
78
$config->define("recipient");
79
$config->define("adminname");
80
$config->define("responseTemplate", { DEFAULT => 'crap.tmpl'} );
81
$config->define("entryFormTemplate", { DEFAULT => 'crap.tmpl'} );
82
$config->define("guideTemplate", { DEFAULT => 'crap.tmpl'} );
83
$config->define("confirmDataTemplate", { DEFAULT => 'crap.tmpl'} );
84
$config->define("deleteDataTemplate", { DEFAULT => 'crap.tmpl'} );
85
$config->define("lat", { ARGCOUNT => ARGCOUNT_HASH} );
86
$config->define("lon", { ARGCOUNT => ARGCOUNT_HASH} );
87

    
88
if (! hasContent($FORM::cfg)) {
89
    $error = "Application misconfigured.  Please contact the administrator.";
90
    push(@errorMessages, $error);
91
} else {
92
    my $cfgfile = $cfgdir . "/" . $FORM::cfg . "/" . $FORM::cfg . ".cfg";
93
    $config->file($cfgfile);
94
}
95

    
96
my $metacatUrl = $config->metacatUrl();
97
my $username = $config->username();
98
my $password = $config->password();
99
my $ldapUrl = $config->ldapUrl();
100
my $defaultScope = $config->defaultScope();
101
my $organization = $config->organization();
102
my $orgabbrev = $config->orgabbrev();
103
my $orgurl = $config->orgurl();
104
my $orgfilter = $organization;
105
   $orgfilter =~ s/ /%20/g;
106
my $responseTemplate = $config->responseTemplate();
107
my $entryFormTemplate = $config->entryFormTemplate();
108
my $deleteDataTemplate = $config->deleteDataTemplate();
109
my $guideTemplate = $config->guideTemplate();
110
my $confirmDataTemplate = $config->confirmDataTemplate();
111
my $accesspubid = $config->accesspubid();
112
my $accesssysid = $config->accesssysid();
113
my $datasetpubid = $config->datasetpubid();
114
my $datasetsysid = $config->datasetsysid();
115
my $mailhost = $config->mailhost();
116
my $sender = $config->sender();
117
my $recipient = $config->recipient();
118
my $adminname = $config->adminname();
119
my $lat = $config->get('lat');
120
my $lon = $config->get('lon');
121

    
122
# Convert the lat and lon configs into usable data structures
123
my @sitelist;
124
my %siteLatDMS;
125
my %siteLongDMS;
126
foreach my $newsite (keys %$lat) {
127
    my ($latd, $latm, $lats, $latdir) = split(':', $lat->{$newsite});
128
    my ($lond, $lonm, $lons, $londir) = split(':', $lon->{$newsite});
129
    push(@sitelist, $newsite);
130
    $siteLatDMS{$newsite} = [ $latd, $latm, $lats, $latdir ];
131
    $siteLongDMS{$newsite} = [ $lond, $lonm, $lons, $londir ];
132
}
133

    
134
# set some configuration options for the template object
135
my $ttConfig = {
136
             INCLUDE_PATH => $templatesdir, 
137
             INTERPOLATE  => 0,                    
138
             POST_CHOMP   => 1,                   
139
             };
140

    
141
# create an instance of the template processor
142
my $template = Template->new($ttConfig) || die $Template::ERROR, "\n";
143

    
144
print "Content-type: text/html\n\n";
145

    
146
# Set up the template information that is common to all forms
147
$$templateVars{'cfg'} = $FORM::cfg;
148
$$templateVars{'recipient'} = $recipient;
149
$$templateVars{'adminname'} = $adminname;
150
$$templateVars{'organization'} = $organization;
151
$$templateVars{'orgabbrev'} = $orgabbrev;
152
$$templateVars{'orgurl'} = $orgurl;
153
$$templateVars{'orgfilter'} = $orgfilter;
154

    
155

    
156
# Process the form based on stage parameter. 
157
if ($FORM::stage =~ "guide") {
158
    # Send back the information on how to fill the form
159
    $$templateVars{'section'} = "Guide on How to Complete Registry Entries";
160
    $template->process( $guideTemplate, $templateVars);
161
    exit(0);
162

    
163
} elsif ($FORM::stage =~ "insert") {
164
    # The user has entered the data. Do data validation and send back data 
165
    # to confirm the data that has been entered. 
166
    toConfirmData();
167
    exit(0);
168

    
169
}elsif ($FORM::dataWrong =~ "No, I want to change it!!" && $FORM::stage =~ "confirmed") {
170
    # The user wants to correct the data that he has entered. 
171
    # Hence show the data again in entryData form. 
172
    confirmDataToReEntryData();
173
    exit(0);
174

    
175
}elsif ($FORM::stage =~ "modify") {
176
    # Modification of a file has been requested. 
177
    # Show the form will all the values filled in.
178
    my @sortedSites;
179
    foreach my $site (sort @sitelist) {
180
        push(@sortedSites, $site);
181
    }
182
    $$templateVars{'siteList'} = \@sortedSites;
183
    $$templateVars{'section'} = "Modification Form";
184
    $$templateVars{'docid'} = $FORM::docid;
185
    modifyData();
186
    exit(0);
187

    
188
}elsif ($FORM::stage =~ "delete_confirm") {
189

    
190
    # Result from deleteData form. 
191
    if($FORM::deleteData =~ "Delete data"){
192
	# delete Data
193
	deleteData(1);    
194
	exit(0);
195
    } else {
196
	# go back to search page. 
197
	exit(0);
198
    }
199

    
200
}elsif ($FORM::stage =~ "delete") {
201
    # Deletion of a file has been requested. 
202
    # Ask for username and password using deleteDataForm
203
    $$templateVars{'docid'} = $FORM::docid;
204
    $template->process( $deleteDataTemplate, $templateVars);
205
    exit(0);
206

    
207
}elsif ($FORM::stage !~ "confirmed") {
208
    # None of the stages have been reached and data is not being confirmed. 
209
    # Hence, send back entry form for entry of data.  
210
    my @sortedSites;
211
    foreach my $site (sort @sitelist) {
212
        push(@sortedSites, $site);
213
    }
214
    $$templateVars{'siteList'} = \@sortedSites;
215
    $$templateVars{'section'} = "Entry Form";
216
    $$templateVars{'docid'} = "";
217
    $template->process( $entryFormTemplate, $templateVars);
218
    exit(0);
219
}
220

    
221
# Confirm stage has been reached. Enter the data into metacat. 
222

    
223
# Initialize some global vars
224
my $latDeg1 = "";
225
my $latMin1 = "";
226
my $latSec1 = "";
227
my $hemisphLat1 = "";
228
my $longDeg1 = "";
229
my $longMin1 = "";
230
my $longSec1 = "";
231
my $hemisphLong1 = "";
232
my $latDeg2 = "";
233
my $latMin2 = "";
234
my $latSec2 = "";
235
my $hemisphLat2 = "";
236
my $longDeg2 = "";
237
my $longMin2 = "";
238
my $longSec2 = "";
239
my $hemisphLong2 = "";
240

    
241
# validate the input form parameters
242
my $invalidParams;
243

    
244
if (! $error) {
245
    $invalidParams = validateParameters(1);
246
    if (scalar(@$invalidParams)) {
247
        $$templateVars{'status'} = 'failure';
248
        $$templateVars{'invalidParams'} = $invalidParams;
249
        $error = 1;
250
    }
251
}
252

    
253

    
254
my $metacat;
255
my $docid;
256
if (! $error) {
257
    # Parameters have been validated and Create the XML document
258

    
259
    my $xmldoc = createXMLDocument();
260

    
261
    # Write out the XML file for debugging purposes
262
    #my $testFile = $tmpdir . "/test.xml";
263

    
264
    # Create a  metacat object
265
    $metacat = Metacat->new();
266
    if ($metacat) {
267
        $metacat->set_options( metacatUrl => $metacatUrl );
268
    } else {
269
        #die "failed during metacat creation\n";
270
        push(@errorMessages, "Failed during metacat creation.");
271
    }
272

    
273
    # Login to metacat
274
    my $userDN = $FORM::username;
275
    my $userOrg = $FORM::organization;
276
    my $userPass = $FORM::password;
277
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
278
    
279
    my $errorMessage = "";
280
    my $response = $metacat->login($dname, $userPass);
281
    if (! $response) {
282
        #print $metacat->getMessage();
283
        #die "failed during login\n";
284
        #print STDERR "Login response is: $metacat->getMessage()\n";
285
        push(@errorMessages, $metacat->getMessage());
286
        push(@errorMessages, "Failed during login.\n");
287
    }
288

    
289
    if($FORM::docid eq ""){
290
	    # document is being inserted 
291
	    my $notunique = "NOT_UNIQUE";
292
	    while ($notunique eq "NOT_UNIQUE") {
293
	        $docid = newAccessionNumber($defaultScope);
294
	        
295
	        $xmldoc =~ s/docid/$docid/;
296
    
297
            #my $testFile = "/usr/local/apache2/htdocs/xml/test.xml";
298
            #open (TFILE,">$testFile") || die ("Cant open xml file...\n");
299
            #print TFILE $xmldoc;
300
            #close(TFILE);
301
    
302
	        $notunique = insertMetadata($xmldoc, $docid);
303
	        #  if (!$notunique) {
304
	        # Write out the XML file for debugging purposes
305
	        #my $testFile = $tmpdir . "/test-new.xml";
306
	        #open (TFILE,">$testFile") || die ("Cant open xml file...\n");
307
	        #print TFILE $newdoc;
308
	        #close(TFILE);
309
	        #   }
310
	    }
311
	    if (! ($notunique eq "SUCCESS")) {
312
            push(@errorMessages, $notunique);
313
	    }
314
    } else {
315
	    # document is being modified
316
	    $docid = $FORM::docid;
317
	
318
	    my $x;
319
	    my $y;
320
	    my $z;
321

    
322
	    ($x, $y, $z) = split(/\./, $docid); 
323
	    $z++;
324
	    $docid = "$x.$y.$z";
325
	
326
	    $xmldoc =~ s/docid/$docid/;
327
        
328
	    my $response = $metacat->update($docid, $xmldoc);
329

    
330
	    if (! $response) {
331
	        push(@errorMessages, $metacat->getMessage());
332
	        push(@errorMessages, "Failed while updating.\n");  
333
	    } else {
334
	        #deleteData(0);
335
	    }
336

    
337
	    if (scalar(@errorMessages)) {
338
	        $$templateVars{'status'} = 'failure';
339
	        $$templateVars{'errorMessages'} = \@errorMessages;
340
	        $error = 1;
341
	    }
342

    
343
#if (! $error) {
344
#sendNotification($docid, $mailhost, $sender, $recipient);
345
#}
346
	
347
        # Create our HTML response and send it back
348
	    $$templateVars{'function'} = "modified";
349
	    $$templateVars{'section'} = "Modification Status";
350
	    $template->process( $responseTemplate, $templateVars);
351

    
352
	    exit(0);
353
    }
354
}
355

    
356

    
357
if (scalar(@errorMessages)) {
358
    $$templateVars{'status'} = 'failure';
359
    $$templateVars{'errorMessages'} = \@errorMessages;
360
    $error = 1;
361
}
362

    
363
#if (! $error) {
364
#sendNotification($docid, $mailhost, $sender, $recipient);
365
#}
366

    
367
# Create our HTML response and send it back
368
$$templateVars{'function'} = "submitted";
369
$$templateVars{'section'} = "Submission Status";
370
$template->process( $responseTemplate, $templateVars);
371

    
372
exit(0);
373

    
374

    
375
################################################################################
376
#
377
# Subroutine for inserting a document to metacat
378
#
379
################################################################################
380
sub insertMetadata {
381
  my $xmldoc = shift;
382
  my $docid = shift;
383

    
384
  my $notunique = "SUCCESS";
385
  my $response = $metacat->insert($docid, $xmldoc);
386
  if (! $response) {
387
    my $errormsg = $metacat->getMessage();
388
    if ($errormsg =~ /is already in use/) {
389
      $notunique = "NOT_UNIQUE";
390
      #print "Accession number already used: $docid\n";
391
    } elsif ($errormsg =~ /<login>/) {
392
      $notunique = "SUCCESS";
393
    } else {
394
      #print "<p>Dumping error on failure...</p>\n";
395
      #print "<p>", $errormsg, "</p>\n";
396
      #die "Failed during insert\n";
397
      #print "<p>Failed during insert</p>\n";
398
      $notunique = $errormsg;
399
    }
400
  }
401

    
402
  return $notunique;
403
}
404

    
405
################################################################################
406
#
407
# Subroutine for generating a new accession number
408
#  Note: this is not threadsafe, assumes only one running process at a time
409
#  Also: need to check metacat for max id # used in this scope already
410
################################################################################
411
sub newAccessionNumber {
412
  my $scope = shift;
413
    
414
  my $docrev = 1;
415
  my $lastid = 1;
416

    
417
  my $scopeFile = $cfgdir . "/" . $FORM::cfg . "/" . $scope . ".lastid";
418
  if (-e $scopeFile) {
419
    open(LASTID, "<$scopeFile") or die "Failed to generate accession number!";
420
    $lastid = <LASTID>;
421
    chomp($lastid);
422
    $lastid++;
423
    close(LASTID);
424
  }
425
  open(LASTID, ">$scopeFile") or die "Failed to open lastid file for writing!";
426
  print LASTID $lastid, "\n";
427
  close(LASTID);
428

    
429
  my $docroot = "$scope.$lastid.";
430
  my $docid = $docroot . $docrev;
431
  return $docid;
432
}
433

    
434
################################################################################
435
#
436
# Subroutine for generating a new ACL document
437
#
438
################################################################################
439
sub newAccessDocument {
440
  my $aclid = shift;
441
  my $acl = "<?xml version=\"1.0\"?>\n";
442
  $acl .= "<!DOCTYPE acl ";
443
  $acl .= "PUBLIC \"$accesspubid\" \"$accesssysid\">\n";
444
  $acl .= "<acl authSystem=\"ldap://ldap.ecoinformatics.org\" ";
445
  $acl .= "order=\"denyFirst\">\n";
446
  $acl .= "<identifier system=\"knb\">$aclid</identifier>\n";
447
  #$acl .= "<identifier>$aclid</identifier>\n";
448
  $acl .= "<allow><principal>$username</principal>" .
449
          "<permission>all</permission></allow>\n";
450
  #$acl .= "<allow><principal>public</principal>" .
451
  #        "<permission>read</permission></allow>\n";
452
  $acl .= "</acl>\n";
453

    
454
  return $acl;
455
}
456

    
457
################################################################################
458
#
459
# Subroutine for inserting identifers to the metadata document passed to us
460
#
461
################################################################################
462
sub insertIdentifiers {
463
  my $docstring = shift;
464
  my $aclid = shift;
465
  my $docid = shift;
466

    
467
  my $parser = XML::LibXML->new();
468
  my $dom = $parser->parse_string($docstring);
469

    
470
  my $root = $dom->documentElement;
471
  my $name = $root->getName();
472

    
473
  my $addedIdentifier = 0;
474
  my $currentElement = $root->getFirstChild();
475
  $name = $currentElement->getName();
476
  while ("$name" !~ "triple" &&
477
         "$name" !~ "temporalCov" && 
478
         "$name" !~ "geographicCov" && 
479
         "$name" !~ "taxonomicCov"
480
        ) {
481
    if ("$name" =~ "identifier" ||
482
        "$name" =~ "title") {
483
      if (! $addedIdentifier) {
484
      my $idelement = $dom->createElement( "identifier" );
485
        $addedIdentifier = 1;
486
        $idelement->setAttribute("system", "knb");
487
        $idelement->appendTextNode($docid);
488
        $root->insertBefore($idelement, $currentElement);
489
      }
490
    }
491
    $currentElement = $currentElement->getNextSibling();
492
    $name = $currentElement->getName();
493
  }
494
  # Link the document to the access doc
495
  my $element = $dom->createElement( "triple" );
496
  $element->appendTextChild( "subject", $aclid);
497
  $element->appendTextChild( "relationship", 
498
                             "describes access control rules for");
499
  $element->appendTextChild( "object", $docid);
500
  $root->insertBefore($element, $currentElement);
501
  # Link the access doc to the access doc
502
  $element = $dom->createElement( "triple" );
503
  $element->appendTextChild( "subject", $aclid);
504
  $element->appendTextChild( "relationship", 
505
                             "describes access control rules for");
506
  $element->appendTextChild( "object", $aclid);
507
  $root->insertBefore($element, $currentElement);
508

    
509
  return $dom->toString();
510
}
511

    
512
################################################################################
513
# 
514
# Validate the parameters to make sure that required params are provided
515
#
516
################################################################################
517
sub validateParameters {
518
    my $chkUser = shift;
519
    my @invalidParams;
520

    
521
    push(@invalidParams, "Provider's first name is missing.")
522
        unless hasContent($FORM::providerGivenName);
523
    push(@invalidParams, "Provider's last name is missing.")
524
        unless hasContent($FORM::providerSurName);
525
    push(@invalidParams, "Name of site is missing.")
526
        unless (hasContent($FORM::site) || $FORM::site =~ /elect/);
527
    push(@invalidParams, "Data set title is missing.")
528
        unless hasContent($FORM::title);
529
    push(@invalidParams, "Originator's first name is missing.")
530
        unless hasContent($FORM::origNamefirst0);
531
    push(@invalidParams, "Originator's last name is missing.")
532
        unless hasContent($FORM::origNamelast0);
533
    push(@invalidParams, "Abstract is missing.")
534
        unless hasContent($FORM::abstract);
535
    push(@invalidParams, "Beginning year of data set is missing.")
536
        unless hasContent($FORM::beginningYear);
537

    
538
    # If the "use site" coord. box is checked and if the site is in 
539
    # the longitude hash ...  && ($siteLatDMS{$FORM::site})
540
   
541
    if (($FORM::useSiteCoord) && ($siteLatDMS{$FORM::site}) ) {
542
        
543
        $latDeg1 = $siteLatDMS{$FORM::site}[0];
544
        $latMin1 = $siteLatDMS{$FORM::site}[1];
545
        $latSec1 = $siteLatDMS{$FORM::site}[2];
546
        $hemisphLat1 = $siteLatDMS{$FORM::site}[3];
547
        $longDeg1 = $siteLongDMS{$FORM::site}[0];
548
        $longMin1 = $siteLongDMS{$FORM::site}[1];
549
        $longSec1 = $siteLongDMS{$FORM::site}[2];
550
        $hemisphLong1 = $siteLongDMS{$FORM::site}[3];
551
     
552
    }  else {
553
    
554
        $latDeg1 = $FORM::latDeg1;
555
        $latMin1 = $FORM::latMin1;
556
        $latSec1 = $FORM::latSec1;
557
        $hemisphLat1 = $FORM::hemisphLat1;
558
        $longDeg1 = $FORM::longDeg1;
559
        $longMin1 = $FORM::longMin1;
560
        $longSec1 = $FORM::longSec1;
561
        $hemisphLong1 = $FORM::hemisphLong1;
562
    }
563
    
564
    # Check if latDeg1 and longDeg1 has values if useSiteCoord is used. 
565
    # This check is required because some of the sites dont have lat 
566
    # and long mentioned in the config file. 
567

    
568
    if ($FORM::useSiteCoord) {
569
	push(@invalidParams, "The Data Registry doesn't have latitude and longitude information for the site that you choose. Please go back and enter the spatial information.")
570
	    unless(hasContent($latDeg1) && hasContent($longDeg1));
571
    }else{
572
	push(@invalidParams, "Latitude degrees are missing.")
573
	    unless hasContent($latDeg1);
574
	push(@invalidParams, "Longitude degrees are missing.")
575
	    unless hasContent($longDeg1);
576
    }
577

    
578
    push(@invalidParams, "Contact first name is missing.")
579
	unless (hasContent($FORM::origNamefirstContact) || 
580
		$FORM::useOrigAddress);
581
    push(@invalidParams, "Contact last name is missing.")
582
	unless (hasContent($FORM::origNamelastContact) || 
583
		$FORM::useOrigAddress);
584
    push(@invalidParams, "Data medium is missing.")
585
	unless (hasContent($FORM::dataMedium) || $FORM::dataMedium =~ /elect/);
586
    
587
    if($chkUser){
588
	my $errorUserPass = validateUserPass();
589
	if($errorUserPass ne ""){
590
	    push(@invalidParams, $errorUserPass);
591
	}
592
    }
593

    
594
    return \@invalidParams;
595
}
596

    
597
################################################################################
598
# 
599
# Validate the parameters username and password.
600
#
601
################################################################################
602
sub validateUserPass {
603
    my $userDN = $FORM::username;
604
    my $userOrg = $FORM::organization;
605
    my $userPass = $FORM::password;
606
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
607

    
608
    my $errorMessage = "";
609
    my $ldap = Net::LDAP->new($ldapUrl) or die "$@";
610
    my $bindresult = $ldap->bind( version => 3, dn => $dname, 
611
                                  password => $userPass );
612
    if ($bindresult->code) {
613
        $errorMessage = "Failed to log into metacat. Please check the username, organization and password entered";
614
        return $errorMessage;
615
    }
616
    return $errorMessage;
617
}
618

    
619

    
620
################################################################################
621
# 
622
# utility function to determine if a paramter is defined and not an empty string
623
#
624
################################################################################
625
sub hasContent {
626
    my $param = shift;
627

    
628
    my $paramHasContent;
629
    if (!defined($param) || $param eq '') { 
630
        $paramHasContent = 0;
631
    } else {
632
        $paramHasContent = 1;
633
    }
634
    return $paramHasContent;
635
}
636

    
637

    
638
################################################################################
639
# 
640
# Create the XML document from the HTML form input
641
# returns the XML document as a string
642
#
643
################################################################################
644
sub createXMLDocument {
645

    
646
    my $orig  = "";
647
    my $role  = "associatedParty";
648
    my $creat = "";
649
    my $metaP = "";
650
    my $apart = "";
651
    my $cont  = "";
652
    my $publ  = "";
653
    my $dso   = "";
654
    my $gmt = gmtime($now);
655
    my $doc =  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
656

    
657
   $doc .= "<eml:eml\n 
658
                     \t packageId=\"docid\" system=\"knb\"\n 
659
                     \t xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"\n
660
                     \t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n 
661
                     \t xmlns:ds=\"eml://ecoinformatics.org/dataset-2.0.0\"\n 
662
                     \t xmlns:stmml=\"http://www.xml-cml.org/schema/stmml\"\n 
663
                     \t xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\">\n";
664

    
665
    $doc .= "<!-- Person who filled in the catalog entry form: ";
666
    $doc .= "$FORM::providerGivenName $FORM::providerSurName -->\n";
667
    $doc .= "<!-- Form filled out at $gmt GMT -->\n";
668
    $doc .= "<dataset>\n";
669
    
670
    if (hasContent($FORM::identifier)) {
671
        $doc .= "<alternateIdentifier system=\"$FORM::site\">";
672
        $doc .= $FORM::identifier . "</alternateIdentifier>\n";
673
    }
674
    
675
    if (hasContent($FORM::title)) {
676
        $doc .= "<title>$FORM::title</title>\n";
677
    }
678

    
679
    if (hasContent($FORM::origNamelast0)) {
680
	$role = "creator";
681
        $orig .= "<individualName>\n";
682
        $orig .= "<givenName>$FORM::origNamefirst0</givenName>\n";
683
        $orig .= "<surName>$FORM::origNamelast0</surName>\n";
684
        $orig .= "</individualName>\n";
685
    }
686

    
687
    if (hasContent($FORM::origNameOrg)) {
688
        $orig .= "<organizationName>$FORM::origNameOrg</organizationName>\n";
689
    }
690

    
691
    if (hasContent($FORM::origDelivery) || hasContent($FORM::origCity) ||
692
        (hasContent($FORM::origState   ) &&
693
        ($FORM::origState !~ "Select state here.")) ||
694
        hasContent($FORM::origStateOther) ||
695
        hasContent($FORM::origZIP ) || hasContent($FORM::origCountry)) {
696
        $orig .= "<address>\n";
697

    
698
        if (hasContent($FORM::origDelivery)) {
699
            $orig .= "<deliveryPoint>$FORM::origDelivery</deliveryPoint>\n";
700
        }
701
        if (hasContent($FORM::origCity)) {
702
            $orig .= "<city>$FORM::origCity</city>\n";
703
        }
704

    
705
	if (hasContent($FORM::origState) && 
706
            ($FORM::origState !~ "Select state here.")) {
707
            $orig .= "<administrativeArea>$FORM::origState";
708
            $orig .= "</administrativeArea>\n";
709
        } elsif (hasContent($FORM::origStateOther)) {
710
            $orig .= "<administrativeArea>$FORM::origStateOther";
711
            $orig .= "</administrativeArea>\n";
712
        }
713
        if (hasContent($FORM::origZIP)) {
714
            $orig .= "<postalCode>$FORM::origZIP</postalCode>\n";
715
        }
716
        if (hasContent($FORM::origCountry)) {
717
            $orig .= "<country>$FORM::origCountry</country>\n";
718
        }
719
        $orig .= "</address>\n";
720
    }
721

    
722
    if (hasContent($FORM::origPhone)) {
723
        $orig .= "<phone>$FORM::origPhone</phone>\n";
724
    }
725
    if (hasContent($FORM::origFAX)) {
726
        $orig .= "<phone phonetype=\"Fax\">$FORM::origFAX</phone>\n";
727
    }
728
    if (hasContent($FORM::origEmail)) {
729
        $orig .= "<electronicMailAddress>$FORM::origEmail";
730
        $orig .= "</electronicMailAddress>\n";
731
    }
732
    $dso = "<$role>\n$orig</$role>\n";
733
    
734
    $creat .= $dso;
735

    
736
    if ($FORM::useOrigAddress) {
737
        # Add a contact originator like the original with a different role
738
            $cont .= "<contact>\n";
739
	    $cont .= $orig;
740
	    $cont .= "</contact>\n";
741
    } else {
742
        $cont .= "<contact>\n";
743

    
744
        $cont .= "<individualName>\n";
745
        $cont .= "<givenName>$FORM::origNamefirstContact</givenName>\n";
746
        $cont .= "<surName>$FORM::origNamelastContact</surName>\n";
747
        $cont .= "</individualName>\n";
748
 
749
	if (hasContent($FORM::origNameOrgContact)) {
750
	    $cont .= "<organizationName>$FORM::origNameOrgContact</organizationName>\n";
751
	}
752

    
753
        if (hasContent($FORM::origDeliveryContact) || 
754
            hasContent($FORM::origCityContact) ||
755
            (hasContent($FORM::origStateContact) &&
756
            ($FORM::origStateContact !~ "Select state here.")) ||
757
            hasContent($FORM::origStateOtherContact) ||
758
            hasContent($FORM::origZIPContact) || 
759
            hasContent($FORM::origCountryContact)) {
760
            $cont .= "<address>\n";
761
            if (hasContent($FORM::origDeliveryContact)) {
762
                $cont .= "<deliveryPoint>$FORM::origDeliveryContact";
763
                $cont .= "</deliveryPoint>\n";
764
            }
765
            if (hasContent($FORM::origCityContact)) {
766
                $cont .= "<city>$FORM::origCityContact</city>\n";
767
            }
768
            if (hasContent($FORM::origStateContact) && 
769
                ($FORM::origStateContact !~ "Select state here.")) {
770
                $cont .= "<administrativeArea>$FORM::origStateContact";
771
                $cont .= "</administrativeArea>\n";
772
            } elsif (hasContent($FORM::origStateOtherContact)) {
773
                $cont .= "<administrativeArea>$FORM::origStateOtherContact";
774
                $cont .= "</administrativeArea>\n";
775
            }
776
            if (hasContent($FORM::origZIPContact)) {
777
                $cont .= "<postalCode>$FORM::origZIPContact</postalCode>\n";
778
            }
779
            if (hasContent($FORM::origCountryContact)) {
780
                $cont .= "<country>$FORM::origCountryContact</country>\n";
781
            }
782
            $cont .= "</address>\n";
783
        }
784
        if (hasContent($FORM::origPhoneContact)) {
785
            $cont .= "<phone>$FORM::origPhoneContact</phone>\n";
786
        }
787
	if (hasContent($FORM::origFAXContact)) {
788
	    $cont .= "<phone phonetype=\"Fax\">$FORM::origFAXContact</phone>\n";
789
	}
790
        if (hasContent($FORM::origEmailContact)) {
791
            $cont .= "<electronicMailAddress>$FORM::origEmailContact";
792
            $cont .= "</electronicMailAddress>\n";
793
        }
794
	$cont .= "</contact>\n";
795
    }
796

    
797
    $metaP .= "<metadataProvider>\n";
798
    $metaP .= "<individualName>\n";
799
    $metaP .= "<givenName>$FORM::providerGivenName</givenName>\n";
800
    $metaP .= "<surName>$FORM::providerSurName</surName>\n";
801
    $metaP .= "</individualName>\n";
802
    $metaP .= "</metadataProvider>\n";
803

    
804
    # Additional originators
805
    foreach my $tmp (param()) {
806
        if ($tmp =~ /origNamelast/){
807
            my $tmp1 = $tmp;
808
            $tmp1 =~ s/origNamelast//; # get the index of the parameter 0 to 10
809
            if ( $tmp1 eq '1' 
810
                 || $tmp1 eq '2'
811
                 || $tmp1 eq '3'
812
                 || $tmp1 eq '4'
813
                 || $tmp1 eq '5'
814
                 || $tmp1 eq '6'
815
                 || $tmp1 eq '7'
816
                 || $tmp1 eq '8'
817
                 || $tmp1 eq '9'
818
                 || $tmp1 eq '10'
819
                 ) {
820
     
821
                # do not generate XML for empty originator fields 
822
                if (hasContent(param("origNamefirst" . $tmp1))) {    
823

    
824
		    my $add = "";
825
		    $add .= "<individualName>\n";
826
		    $add .= "<givenName>";
827
		    $add .= param("origNamefirst" . $tmp1);
828
		    $add .= "</givenName>\n";
829
		    $add .= "<surName>";
830
		    $add .= param("origNamelast" . $tmp1);
831
		    $add .= "</surName>\n";
832
		    $add .= "</individualName>\n";
833
		    
834
		    if(param("origRole" . $tmp1) eq "Originator"){
835
			$creat .= "<creator>\n";
836
			$creat .= $add;
837
			$creat .= "</creator>\n";
838
		    }
839
		    elsif(param("origRole" . $tmp1) eq "Metadata Provider"){
840
			$metaP .= "<metadataProvider>\n";
841
			$metaP .= $add;
842
			$metaP .= "</metadataProvider>\n";
843
		    }
844
		    elsif((param("origRole" . $tmp1) eq "Publisher")  && ($publ eq "")){
845
			$publ .= "<publisher>\n";
846
			$publ .= $add;
847
			$publ .= "</publisher>\n";
848
		    }
849
		    else{
850
			$apart .= "<associatedParty>\n";
851
			$apart .= $add;
852
			$apart .= "<role>";
853
			$apart .= param("origRole" . $tmp1);
854
			$apart .= "</role>\n";
855
			$apart .= "</associatedParty>\n";
856
		    }
857
		}
858
            }
859
        }
860
    }
861

    
862
    $creat .= "<creator>\n";
863
    $creat .= "<organizationName>$FORM::site</organizationName>\n";
864
    $creat .= "</creator>\n";
865

    
866
    $creat .= "<creator>\n";
867
    $creat .= "<organizationName>$organization</organizationName>\n";
868
    $creat .= "</creator>\n";
869

    
870
    $doc .= $creat;
871
    $doc .= $metaP;
872
    $doc .= $apart;
873

    
874
    $doc .= "<abstract>\n";
875
    $doc .= "<para>$FORM::abstract</para>\n";
876
    $doc .= "</abstract>\n";
877

    
878
    # Keyword information
879
    foreach my $tmp (param()) {
880
        if ($tmp =~ /keyword/) {
881
            my $tmp1 = $tmp;
882
            $tmp1 =~ s/keyword//; # get the index of the parameter 0, ..., 10
883
            if ( $tmp1 =~ /[0-9]/ ){
884
                # don't generate xml for empty keyword fields
885
                # don't generate taxonomic keyword fields, those go in taxonomic coverage
886
                if (hasContent(param($tmp))) {
887
                    $doc .= "<keywordSet>\n";
888
                    $doc .= "<keyword ";
889
                    if (hasContent(param("kwType" . $tmp1)) &&
890
                       (param("kwType" . $tmp1) !~ "none") ) {
891
                         $doc .= "keywordType=\"";
892
                         $doc .= param("kwType" . $tmp1);
893
                         $doc .= "\"";
894
                    }
895
                    $doc .= ">";
896
                    $doc .= param("keyword" . $tmp1);
897
                    $doc .= "</keyword>\n";
898
                    $doc .= "<keywordThesaurus>";
899
                    $doc .= param("kwTh" . $tmp1);
900
                    $doc .= "</keywordThesaurus>\n";
901
                    $doc .= "</keywordSet>\n";
902
                }
903
            }
904
        }
905
    }
906

    
907
    if (hasContent($FORM::addComments)) {
908
        $doc .= "<additionalInfo>\n";
909
        $doc .= "<para>$FORM::addComments</para>\n";
910
        $doc .= "</additionalInfo>\n";
911
    }
912

    
913
    if (hasContent($FORM::useConstraints) || 
914
        hasContent($FORM::useConstraintsOther)) {
915
        $doc .= "<intellectualRights>\n";
916
        if (hasContent($FORM::useConstraints)) {
917
            $doc .= "<para>$FORM::useConstraints</para>\n";
918
        }
919
        if (hasContent($FORM::useConstraintsOther)) {
920
            $doc .= "<para>$FORM::useConstraintsOther</para>\n";
921
        }
922
        $doc .= "</intellectualRights>\n";
923
    }
924

    
925
    
926
    if (hasContent($FORM::url)) {
927
	$doc .= "<distribution>\n";
928
        $doc .= "<online>\n";
929
	$doc .= "<url>$FORM::url</url>\n";
930
	$doc .= "</online>\n";
931
	$doc .= "</distribution>\n";
932
    }
933
    
934
    $doc .= "<distribution>\n";
935
    $doc .= "<offline>\n";
936
    $doc .= "<mediumName>" . "$FORM::dataMedium   $FORM::dataMediumOther";
937
    $doc .= "</mediumName>\n";
938
    $doc .= "</offline>\n";
939
    $doc .= "</distribution>\n";
940
            
941
    $doc .= "<coverage>\n";
942
    $doc .= "<temporalCoverage>\n";
943

    
944

    
945
    if (hasContent($FORM::endingYear)) {
946
	$doc .= "<rangeOfDates>\n";
947
	if (hasContent($FORM::beginningMonth)) {
948
	    my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
949
			 "JUL","AUG","SEP","OCT","NOV","DEC")
950
		[$FORM::beginningMonth - 1];
951
	    $doc .= "<beginDate>\n";
952
	    $doc .= "<calendarDate>";
953
	    $doc .= "$FORM::beginningYear-$FORM::beginningMonth-$FORM::beginningDay";
954
	    $doc .= "</calendarDate>\n";
955
	    $doc .= "</beginDate>\n";
956
	} else {
957
	    $doc .= "<beginDate>\n";
958
	    $doc .= "<calendarDate>";
959
	    $doc .= "$FORM::beginningYear";
960
	    $doc .= "</calendarDate>\n";
961
	    $doc .= "</beginDate>\n";
962
	}
963

    
964
	if (hasContent($FORM::endingMonth)) {
965
	    my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
966
			 "JUL","AUG","SEP","OCT","NOV","DEC")
967
		[$FORM::endingMonth - 1];
968
	    $doc .= "<endDate>\n";
969
	    $doc .= "<calendarDate>";
970
	    $doc .= "$FORM::endingYear-$FORM::endingMonth-$FORM::endingDay";
971
	    $doc .= "</calendarDate>\n";
972
	    $doc .= "</endDate>\n";
973
	} else {
974
	    $doc .= "<endDate>\n";
975
	    $doc .= "<calendarDate>";
976
	    $doc .= "$FORM::endingYear";
977
	    $doc .= "</calendarDate>\n";
978
	    $doc .= "</endDate>\n";
979
	}
980
	$doc .= "</rangeOfDates>\n";
981
    } else {
982
	$doc .= "<singleDateTime>\n";
983
	if (hasContent($FORM::beginningMonth)) {
984
	    my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
985
			 "JUL","AUG","SEP","OCT","NOV","DEC")
986
		[$FORM::beginningMonth - 1];
987
	    $doc .= "<calendarDate>";
988
	    $doc .= "$FORM::beginningYear-$FORM::beginningMonth-$FORM::beginningDay";
989
	    $doc .= "</calendarDate>\n";
990
	} else {
991
	    $doc .= "<calendarDate>";
992
	    $doc .= "$FORM::beginningYear";
993
	    $doc .= "</calendarDate>\n";
994
	}
995
	$doc .= "</singleDateTime>\n";
996
    }
997

    
998
    $doc .= "</temporalCoverage>\n";
999
    
1000
    $doc .= "<geographicCoverage>\n";
1001
    $doc .= "<geographicDescription></geographicDescription>\n";
1002
    $doc .= "<boundingCoordinates>\n";
1003

    
1004
    # if the second latitude is missing, then set the second lat/long pair 
1005
    # equal to the first this makes a point appear like a rectangle 
1006
    if ($FORM::latDeg2 == 0 && $FORM::latMin2 == 0 && $FORM::latSec2 == 0) {
1007
    
1008
        $latDeg2 = $latDeg1;
1009
        $latMin2 = $latMin1;
1010
        $latSec2 = $latSec1;
1011
        $hemisphLat2 = $hemisphLat1;
1012
        $longDeg2 = $longDeg1;
1013
        $longMin2 = $longMin1;
1014
        $longSec2 = $longSec1;
1015
        $hemisphLong2 = $hemisphLong1;
1016
    }
1017
    else
1018
    {
1019
        $latDeg2 = $FORM::latDeg2;
1020
        $latMin2 = $FORM::latMin2;
1021
        $latSec2 = $FORM::latSec2;
1022
        $hemisphLat2 = $FORM::hemisphLat2;
1023
        $longDeg2 = $FORM::longDeg2;
1024
        $longMin2 = $FORM::longMin2;
1025
        $longSec2 = $FORM::longSec2;
1026
        $hemisphLong2 = $FORM::hemisphLong2;
1027
    } 
1028
    
1029
   
1030
    my $hemisph;
1031
    $hemisph = ($hemisphLong1 eq "W") ? -1 : 1;
1032
    $doc .= "<westBoundingCoordinate>";
1033
    $doc .= $hemisph * ($longDeg1 + (60*$longMin1+$longSec1)/3600);
1034
    $doc .= "</westBoundingCoordinate>\n";
1035

    
1036
    $hemisph = ($hemisphLong2 eq "W") ? -1 : 1;
1037
    $doc .= "<eastBoundingCoordinate>";
1038
    $doc .= $hemisph * ($longDeg2 + (60*$longMin2+$longSec2)/3600);
1039
    $doc .= "</eastBoundingCoordinate>\n";
1040

    
1041
    $hemisph = ($hemisphLat1 eq "S") ? -1 : 1;
1042
    $doc .= "<northBoundingCoordinate>";
1043
    $doc .= $hemisph * ($latDeg1 + (60*$latMin1+$latSec1)/3600);
1044
    $doc .= "</northBoundingCoordinate>\n";
1045

    
1046
    $hemisph = ($hemisphLat2 eq "S") ? -1 : 1;
1047
    $doc .= "<southBoundingCoordinate>";
1048
    $doc .= $hemisph * ($latDeg2 + (60*$latMin2+$latSec2)/3600);
1049
    $doc .= "</southBoundingCoordinate>\n";
1050

    
1051
    $doc .= "</boundingCoordinates>\n";
1052
    $doc .= "</geographicCoverage>\n";
1053

    
1054
    # Taxonomic coverage information
1055
    # my $foundFirstTaxKeyword = 0;
1056
    # foreach my $tmp (param()) {
1057
    #    if ($tmp =~ /keyword/) {
1058
    #        my $tmp1 = $tmp;
1059
    #        $tmp1 =~ s/keyword//; # get the index of the parameter 0, ..., 10
1060
    #        if ( $tmp1 =~ /[0-9]/ ){
1061
    #            # don't generate xml for empty keyword fields
1062
    #            if (hasContent(param($tmp))) {
1063
    #                if (hasContent(param("kwType" . $tmp1)) &&
1064
    #                   (param("kwType" . $tmp1) eq "taxonomic") ) {
1065
    #                    # Opening tags before first taxonomic. keyword.
1066
    #                    if ($foundFirstTaxKeyword == 0) {
1067
    #                        $foundFirstTaxKeyword = 1;
1068
    #                        $doc .= "<taxonomicCoverage>\n";
1069
    #                    }
1070
    #                    $doc .= "<keywtax>\n";
1071
    #                    $doc .= "<taxonkt>".param("kwTh".$tmp1)."</taxonkt>\n";
1072
    #                    $doc .= "<taxonkey>".param("keyword".$tmp1).
1073
    #                            "</taxonkey>\n";
1074
    #                    $doc .= "</keywtax>\n";
1075
    #                }
1076
    #            }
1077
    #        }
1078
    #    }
1079
    # }
1080
    # Closing tag for taxonomic coverage, if there was a tax. keyword
1081
    # if ($foundFirstTaxKeyword == 1) {
1082
    #    $doc .= "<taxoncl></taxoncl>\n";
1083
    #    $doc .= "</taxonomicCoverage>\n";
1084
    # }
1085

    
1086
    $doc .= "</coverage>\n";
1087

    
1088
    $doc .= $cont;
1089
    $doc .= $publ;
1090

    
1091
    $doc .= "<access authSystem=\"knb\" order=\"denyFirst\">\n";
1092
    $doc .= "<allow>\n";
1093
    $doc .= "<principal>uid=obfsadmin,o=LTER,dc=ecoinformatics,dc=org</principal>\n";
1094
    $doc .= "<permission>all</permission>\n";
1095
    $doc .= "</allow>\n";
1096
    $doc .= "<allow>\n";
1097
    $doc .= "<principal>uid=$FORM::username,o=$FORM::organization,dc=ecoinformatics,dc=org</principal>\n";
1098
    $doc .= "<permission>all</permission>\n";
1099
    $doc .= "</allow>\n";
1100
    $doc .= "<allow>\n";
1101
    $doc .= "<principal>public</principal>\n";
1102
    $doc .= "<permission>read</permission>\n";
1103
    $doc .= "</allow>\n";
1104
    $doc .= "</access>\n";
1105
    
1106
    $doc .= "</dataset>\n</eml:eml>\n";
1107

    
1108
    return $doc;
1109
}
1110

    
1111

    
1112
################################################################################
1113
# 
1114
# send an email message notifying the moderator of a new submission 
1115
#
1116
################################################################################
1117
sub sendNotification {
1118
    my $identifier = shift;
1119
    my $mailhost = shift;
1120
    my $sender = shift;
1121
    my $recipient = shift;
1122

    
1123
    my $smtp = Net::SMTP->new($mailhost);
1124
    $smtp->mail($sender);
1125
    $smtp->to($recipient);
1126

    
1127
    my $message = <<"    ENDOFMESSAGE";
1128
    To: $recipient
1129
    From: $sender
1130
    Subject: New data submission
1131
    
1132
    Data was submitted to the data registry.  
1133
    The identifying information for the new data set is:
1134

    
1135
    Identifier: $identifier
1136
    Title: $FORM::title
1137
    Submitter: $FORM::providerGivenName $FORM::providerSurName
1138

    
1139
    Please review the submmission and grant public read access if appropriate.
1140
    Thanks
1141
    
1142
    ENDOFMESSAGE
1143
    $message =~ s/^[ \t\r\f]+//gm;
1144

    
1145
    $smtp->data($message);
1146
    $smtp->quit;
1147
}
1148

    
1149

    
1150
################################################################################
1151
# 
1152
# read the eml document and send back a form with values filled in. 
1153
#
1154
################################################################################
1155
sub modifyData {
1156
    
1157
    # create metacat instance
1158
    my $metacat;
1159
    my $docid = $FORM::docid;
1160
    my $httpMessage;
1161
    my $doc;
1162
    my $xmldoc;
1163
    my $findType;
1164
    my $parser = XML::LibXML->new();
1165
    my @fileArray;
1166
    my $pushDoc;
1167
    my $alreadyInArray;
1168
    my $node;
1169
    my $response; 
1170
    my $element;
1171
    my $tempfile;
1172

    
1173
    $metacat = Metacat->new();
1174
    if ($metacat) {
1175
        $metacat->set_options( metacatUrl => $metacatUrl );
1176
    } else {
1177
        #die "failed during metacat creation\n";
1178
        push(@errorMessages, "Failed during metacat creation.");
1179
    }
1180
    
1181
    $httpMessage = $metacat->read($docid);
1182
    $doc = $httpMessage->content();
1183
    $xmldoc = $parser->parse_string($doc);
1184

    
1185
#    $tempfile = $xslConvDir.$docid;
1186
#    push (@fileArray, $tempfile);
1187

    
1188
    if ($xmldoc eq "") {
1189
	$error ="Error in parsing the eml document";
1190
	push(@errorMessages, $error);
1191
    } else {
1192
	$findType = $xmldoc->findnodes('//dataset/identifier');
1193
	if($findType->size() > 0){
1194
	    # This is a eml beta6 document
1195
	    # Read the documents mentioned in triples also
1196
	    
1197
	    $findType = $xmldoc->findnodes('//dataset/triple');
1198
	    if($findType->size() > 0){
1199
		foreach $node ($findType->get_nodelist){
1200
		    $pushDoc = findValue($node, 'subject');
1201
		    
1202
		    # If the file is already in the @fileArray then do not add it 
1203
		    $alreadyInArray = 0;
1204
		    foreach $element (@fileArray){
1205
			$tempfile = $tmpdir."/".$pushDoc;
1206
			if($element eq $pushDoc){
1207
			    $alreadyInArray = 1;
1208
			}
1209
		    }
1210
		    
1211
		    if(!$alreadyInArray){
1212
			$tempfile = $tmpdir."/".$pushDoc; #= $xslConvDir.$pushDoc;
1213
			$response = "";
1214
			$response = $metacat->read($pushDoc);    
1215
			if (! $response) {
1216
			    # could not read
1217
			    #push(@errorMessages, $response);
1218
			    push(@errorMessages, $metacat->getMessage());
1219
			    push(@errorMessages, "Failed during reading.\n");
1220
			} else {
1221
			    my $xdoc = $response->content();
1222
			    #$tempfile = $xslConvDir.$pushDoc;
1223
			    open (TFILE,">$tempfile") || die ("Cant open xml file... $tempfile\n");
1224
			    print TFILE $xdoc;
1225
			    close(TFILE);
1226
			    push (@fileArray, $tempfile);
1227
			}
1228
		    }
1229
		}
1230
	    }
1231

    
1232
	    # Read the main document. 
1233

    
1234
	    $tempfile = $tmpdir."/".$docid; #= $xslConvDir.$docid;
1235
	    open (TFILE,">$tempfile") || die ("Cant open xml file...\n");
1236
	    print TFILE $doc;
1237
	    close(TFILE);
1238
	    
1239
	    # Transforming beta6 to eml 2
1240
	    my $xslt;
1241
	    my $triplesheet;
1242
	    my $results;
1243
	    my $stylesheet;
1244
	    my $resultsheet;
1245
	    
1246
	    $xslt = XML::LibXSLT->new();
1247
#	    $tempfile = $xslConvDir."triple_info.xsl";
1248
	    $tempfile = $tmpdir."/"."triple_info.xsl";
1249

    
1250
	    $triplesheet = $xslt->parse_stylesheet_file($tempfile);
1251

    
1252
#	    $results = $triplesheet->transform($xmldoc, packageDir => "\'$tmpdir/\'", packageName => "\'$docid\'");
1253
	    $results = $triplesheet->transform($xmldoc, packageDir => "\'$tmpdir/\'", packageName => "\'$docid\'");
1254

    
1255
#	    $tempfile = $xslConvDir."emlb6toeml2.xsl";
1256
	    $tempfile = $tmpdir."/"."emlb6toeml2.xsl";
1257
	    $stylesheet = $xslt->parse_stylesheet_file($tempfile);
1258
	    $resultsheet = $stylesheet->transform($results);
1259
	    
1260
	    #$tempfile = "/usr/local/apache2/htdocs/xml/test.xml";;
1261
	    #open (TFILE,">$tempfile") || die ("Cant open xml file...\n");
1262
	    #print TFILE $stylesheet->output_string($resultsheet);
1263
	    #close(TFILE);
1264

    
1265
	    getFormValuesFromEml2($resultsheet);
1266
	    
1267
	    # Delete the files written earlier. 
1268
	    unlink @fileArray;
1269

    
1270
	} else {
1271
	    getFormValuesFromEml2($xmldoc);
1272
	}
1273
    }   
1274
    
1275
    if (scalar(@errorMessages)) {
1276
	# if any errors, print them in the response template 
1277
	$$templateVars{'status'} = 'failure';
1278
	$$templateVars{'errorMessages'} = \@errorMessages;
1279
	$error = 1;
1280
	$$templateVars{'function'} = "modification";
1281
	$$templateVars{'section'} = "Modification Status";
1282
	$template->process( $responseTemplate, $templateVars); 
1283
    } else {
1284
        $$templateVars{'form'} = 're_entry';
1285
	$template->process( $entryFormTemplate, $templateVars);
1286
    }
1287
    
1288
    # process the response template
1289
}
1290

    
1291

    
1292

    
1293
sub getFormValuesFromEml2 {
1294
    
1295
    my $doc = shift;
1296
    my $results;
1297
    my $error;
1298
    my $node;
1299
    my $tempResult;
1300
    my $tempNode;
1301
    my $aoCount = 1;
1302
    my $foundDSO;
1303

    
1304
    # find out the tag <alternateIdentifier>. 
1305
    $results = $doc->findnodes('//dataset/alternateIdentifier');
1306
    if($results->size() > 1){
1307
	errMoreThanOne("alternateIdentifier");
1308
    } else {
1309
	foreach $node ($results->get_nodelist){
1310
	    $$templateVars{'identifier'} = findValue($node, '../alternateIdentifier');
1311
	}
1312
    }
1313

    
1314
    # find out the tag <title>. 
1315
    $results = $doc->findnodes('//dataset/title');
1316
    if($results->size() > 1){
1317
	errMoreThanOne("title");
1318
    } elsif($results->size() < 1){
1319
	$error ="Following tag not found: title. Please use Morpho to edit this document";
1320
	push(@errorMessages, $error."\n");
1321
	#if ($DEBUG == 1){ print $error;}
1322
    } else {
1323
	foreach $node ($results->get_nodelist){
1324
	$$templateVars{'title'} = findValue($node, '../title');
1325
	}
1326
    }
1327

    
1328
    # find out the tag <creator>. 
1329
    $results = $doc->findnodes('//dataset/creator/individualName');
1330
    if($results->size() > 11){
1331
	errMoreThanN("creator/individualName");
1332
    } else {
1333
	foreach $node ($results->get_nodelist){
1334
	    dontOccur($node, "../positionName|../onlineURL|../userId", 
1335
		      "positionName, onlineURL, userId");
1336
	    
1337
	    dontOccur($node, "./saluation", "saluation");			    
1338
	    
1339
	    $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress|../organizationName');
1340
	    if($tempResult->size > 0){
1341
		if($foundDSO == 0){
1342
		    $foundDSO = 1;
1343
     
1344
		    $$templateVars{'origNamefirst0'} = findValue($node, 'givenName');
1345
		    $$templateVars{'origNamelast0'} = findValue($node, 'surName');
1346
			
1347
		    my $tempResult2 = $node->findnodes('../address');
1348
		    if($tempResult2->size > 1){
1349
			errMoreThanOne("address");
1350
		    } else {
1351
			foreach my $tempNode2 ($tempResult2->get_nodelist){
1352
			    $$templateVars{'origDelivery'} = findValue($tempNode2, 'deliveryPoint');
1353
			    $$templateVars{'origCity'} = findValue($tempNode2, 'city');
1354
			    $$templateVars{'origState'} = findValue($tempNode2, 'administrativeArea');
1355
			    $$templateVars{'origZIP'} = findValue($tempNode2, 'postalCode');
1356
			    $$templateVars{'origCountry'} = findValue($tempNode2, 'country');
1357
			}
1358
		    }
1359
		    
1360
		    my $tempResult3 = $node->findnodes('../phone');
1361
		    if($tempResult3->size > 2){
1362
			errMoreThanN("phone");
1363
		    } else {
1364
			foreach my $tempNode2 ($tempResult3->get_nodelist){
1365
			    if($tempNode2->hasAttributes()){
1366
				my @attlist = $tempNode2->attributes();
1367
				if($attlist[0]->value eq "Fax"){
1368
				    $$templateVars{'origFAX'} = $tempNode2->textContent();
1369
				} else {
1370
				    $$templateVars{'origPhone'} = $tempNode2->textContent();
1371
				}
1372
			    } else{
1373
				$$templateVars{'origPhone'} = $tempNode2->textContent();
1374
			    }
1375
			}
1376
		    }
1377
		    $$templateVars{'origEmail'} = findValue($node, '../electronicMailAddress');
1378
		    $$templateVars{'origNameOrg'} = findValue($node, '../organizationName');
1379
		} else {
1380
		    errMoreThanN("address, phone and electronicMailAddress");
1381
		}
1382
	    }
1383
	}
1384
	foreach $node ($results->get_nodelist){
1385
	    $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress');
1386
	    if($tempResult->size == 0){
1387
		if($foundDSO == 0){
1388
		    $foundDSO = 1;
1389
		    $$templateVars{'origNamefirst0'} = findValue($node, 'givenName');
1390
		    $$templateVars{'origNamelast0'} = findValue($node, 'surName');
1391
		    $$templateVars{'origNameOrg'} = findValue($node, '../organizationName');
1392
		} else {
1393
		    $$templateVars{"origNamefirst$aoCount"} =  findValue($node, './givenName');
1394
		    $$templateVars{"origNamelast$aoCount"} =  findValue($node, './surName');
1395
		    $$templateVars{"origRole$aoCount"} = "Originator";
1396
		    $aoCount++;
1397
		}
1398
	    }
1399
	}
1400
    }
1401

    
1402
    $results = $doc->findnodes('//dataset/creator/organizationName');
1403
    if($results->size() > 3){
1404
	errMoreThanN("creator/organizationName");	
1405
    } else {
1406
	foreach $node ($results->get_nodelist){
1407

    
1408
	    my $tempValue = findValue($node,'../organizationName');
1409
	    $tempResult = $node->findnodes('../individualName');
1410
	    if($tempResult->size == 0 && $tempValue ne $organization){
1411
		$$templateVars{'site'} = $tempValue;
1412
	    }
1413
	}
1414
    }
1415

    
1416
    $results = $doc->findnodes('//dataset/metadataProvider');
1417
    if($results->size() > 11){
1418
	errMoreThanN("metadataProvider");	
1419
    } else {
1420
	foreach $node ($results->get_nodelist){
1421
	    dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address", 
1422
		      "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in metadataProvider");
1423
	    
1424
	    $tempResult = $node->findnodes('./individualName');
1425
	    if($tempResult->size > 1){
1426
		errMoreThanOne("metadataProvider/indvidualName");
1427
	    }else{
1428
		foreach $tempNode ($tempResult->get_nodelist){
1429
		    if($$templateVars{'providerGivenName'} ne ""){
1430
			$$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1431
			$$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1432
			$$templateVars{"origRole$aoCount"} = "Metadata Provider";
1433
			$aoCount++;
1434
		    } else {
1435
			$$templateVars{'providerGivenName'} =  findValue($tempNode, './givenName');
1436
			$$templateVars{'providerSurName'} =  findValue($tempNode, './surName');
1437
		    }
1438
		}
1439
	    }
1440
	}
1441
    }
1442

    
1443

    
1444
    $results = $doc->findnodes('//dataset/associatedParty');
1445
    if($results->size() > 10){
1446
	errMoreThanN("associatedParty");
1447
    } else {
1448
	foreach $node ($results->get_nodelist){
1449
	    dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address", 
1450
		      "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in associatedParty");
1451
	   
1452
	    $tempResult = $node->findnodes('./individualName');
1453
	    if($tempResult->size > 1){
1454
		errMoreThanOne("associatedParty/indvidualName");
1455
	    }else{
1456
		foreach $tempNode ($tempResult->get_nodelist){
1457
		    $$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1458
		    $$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1459
		    $$templateVars{"origRole$aoCount"} = findValue($tempNode, '../role');
1460
		    $aoCount++;		   
1461
		}
1462
	    }
1463
	}
1464
    }
1465

    
1466
    $results = $doc->findnodes('//dataset/publisher');
1467
    if($results->size() > 10){
1468
	errMoreThanN("publisher");
1469
    } else {
1470
	foreach $node ($results->get_nodelist){
1471
	    dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address", 
1472
		      "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in associatedParty");
1473
	   
1474
	    $tempResult = $node->findnodes('./individualName');
1475
	    if($tempResult->size > 1){
1476
		errMoreThanOne("publisher/indvidualName");
1477
	    }else{
1478
		foreach $tempNode ($tempResult->get_nodelist){
1479
		    $$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1480
		    $$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1481
		    $$templateVars{"origRole$aoCount"} = "Publisher";
1482
		    $aoCount++;		   
1483
		}
1484
	    }
1485
	}
1486
    }
1487

    
1488
    if($aoCount > 11){
1489
	errMoreThanN("Additional Originators");
1490
    } 
1491

    
1492
    dontOccur($doc, "./pubDate", "pubDate");
1493
    dontOccur($doc, "./language", "language");
1494
    dontOccur($doc, "./series", "series");
1495
    
1496

    
1497
    $results = $doc->findnodes('//dataset/abstract');
1498
    if($results->size() > 1){
1499
	errMoreThanOne("abstract");
1500
    } else {
1501
	foreach my $node ($results->get_nodelist){
1502
	    dontOccur($node, "./section", "section");
1503
	    $$templateVars{'abstract'} = findValueNoChild($node, "para");
1504
	}
1505
    }
1506

    
1507
    $results = $doc->findnodes('//dataset/keywordSet');
1508
    if($results->size() > 10){
1509
	errMoreThanN("keywordSet");
1510
    } else {
1511
	my $count = 0;
1512
	foreach $node ($results->get_nodelist){
1513
	    $tempResult = $node->findnodes('./keyword');
1514
	    if($tempResult->size() > 1){
1515
		errMoreThanOne("keyword");
1516
	    } else {
1517
		foreach $tempNode ($tempResult->get_nodelist){
1518
		    $$templateVars{"keyword$count"} = $tempNode->textContent();
1519
		    if($tempNode->hasAttributes()){
1520
			my @attlist = $tempNode->attributes();
1521
			$$templateVars{"kwType$count"} = $attlist[0]->value;
1522
		    }  
1523
	 	} 
1524
	    }
1525
	    $$templateVars{"kwTh$count"} = findValue($node, "keywordThesaurus");
1526
	    $count++;
1527
	}
1528
	
1529
	while($count<11){
1530
	    $$templateVars{"kwType$count"} = "none";
1531
	    $$templateVars{"kwTh$count"} = "none";
1532
	    $count++;
1533
	}
1534
    }
1535

    
1536

    
1537
    $results = $doc->findnodes('//dataset/additionalInfo');
1538
    if($results->size() > 1){
1539
	errMoreThanOne("additionalInfo");
1540
    } else {
1541
	foreach $node ($results->get_nodelist){
1542
	    dontOccur($node, "./section", "section");
1543
	    $$templateVars{'addComments'} = findValueNoChild($node, "para");
1544
	}
1545
    }
1546

    
1547
    $$templateVars{'useConstraints'} = "";
1548
    $results = $doc->findnodes('//dataset/intellectualRights');
1549
    if($results->size() > 1){
1550
	errMoreThanOne("intellectualRights");
1551
    } else {
1552
	foreach $node ($results->get_nodelist){
1553
	    dontOccur($node, "./section", "section in intellectualRights");
1554

    
1555
	    $tempResult = $node->findnodes("para");
1556
	    if($tempResult->size > 2){
1557
	   	errMoreThanN("para");
1558
	    } else {
1559
		foreach $tempNode ($tempResult->get_nodelist){
1560
		    my $childNodes = $tempNode->childNodes;
1561
		    if($childNodes->size() > 1){
1562
			$error ="The tag para in intellectualRights has children which cannot be shown using the form. Please use Morpho to edit this document";	
1563
			push(@errorMessages, $error);
1564
			#if ($DEBUG == 1){ print $error."\n";}
1565
		    }else{
1566
			#print $tempNode->nodeName().":".$tempNode->textContent();
1567
			#print "\n";
1568
			if($$templateVars{'useConstraints'} eq ""){
1569
			    $$templateVars{'useConstraints'} = $tempNode->textContent();
1570
			} else {
1571
			    $$templateVars{'useConstraintsOther'} = $tempNode->textContent();
1572
			}
1573
		    }
1574
		}
1575
	    }
1576
	}
1577
    }
1578

    
1579

    
1580
    $results = $doc->findnodes('//dataset/distribution/online');
1581
    if($results->size() > 1){
1582
	errMoreThanOne("distribution/online");
1583
    } else {
1584
	foreach my $tempNode ($results->get_nodelist){
1585
	    $$templateVars{'url'} = findValue($tempNode, "url");
1586
	    dontOccur($tempNode, "./connection", "/distribution/online/connection");
1587
	    dontOccur($tempNode, "./connectionDefinition", "/distribution/online/connectionDefinition");
1588
	}
1589
    }
1590

    
1591

    
1592
    $results = $doc->findnodes('//dataset/distribution/offline');
1593
    if($results->size() > 1){
1594
	errMoreThanOne("distribution/online");
1595
    } else {
1596
	foreach my $tempNode ($results->get_nodelist){
1597
	    $$templateVars{'dataMedium'} = findValue($tempNode, "mediumName");
1598
	    dontOccur($tempNode, "./mediumDensity", "/distribution/offline/mediumDensity");
1599
	    dontOccur($tempNode, "./mediumDensityUnits", "/distribution/offline/mediumDensityUnits");
1600
	    dontOccur($tempNode, "./mediumVolume", "/distribution/offline/mediumVolume");
1601
	    dontOccur($tempNode, "./mediumFormat", "/distribution/offline/mediumFormat");
1602
	    dontOccur($tempNode, "./mediumNote", "/distribution/offline/mediumNote");
1603
	}
1604
    }
1605

    
1606
    dontOccur($doc, "./inline", "//dataset/distribution/inline");
1607

    
1608

    
1609
    $results = $doc->findnodes('//dataset/coverage');
1610
    if($results->size() > 1){
1611
	errMoreThanOne("coverage");
1612
    } else {
1613
	foreach $node ($results->get_nodelist){
1614
	    dontOccur($node, "./temporalCoverage/rangeOfDates/beginDate/time|./temporalCoverage/rangeOfDates/beginDate/alternativeTimeScale|./temporalCoverage/rangeOfDates/endDate/time|./temporalCoverage/rangeOfDates/endDate/alternativeTimeScale|./taxonomicCoverage|./geographicCoverage/datasetGPolygon|./geographicCoverage/boundingCoordinates/boundingAltitudes", "temporalCoverage/rangeOfDates/beginDate/time, /temporalCoverage/rangeOfDates/beginDate/alternativeTimeScale, /temporalCoverage/rangeOfDates/endDate/time, /temporalCoverage/rangeOfDates/endDate/alternativeTimeScale, /taxonomicCoverage, /geographicCoverage/datasetGPolygon, /geographicCoverage/boundingCoordinates/boundingAltitudes");
1615

    
1616
	    $tempResult = $node->findnodes('./temporalCoverage');
1617
	    if($tempResult->size > 1){
1618
	       	errMoreThanOne("temporalCoverage");
1619
	    }else{
1620
		foreach $tempNode ($tempResult->get_nodelist){
1621
		    my $x;
1622
		    my $y;
1623
		    my $z;
1624
		    my $tempdate = findValue($tempNode, "rangeOfDates/beginDate/calendarDate");
1625
		    ($x, $y, $z) = split("-", $tempdate); 
1626
		    $$templateVars{'beginningYear'} = $x;
1627
		    $$templateVars{'beginningMonth'} = $y;
1628
		    $$templateVars{'beginningDay'} = $z;
1629

    
1630
		    $tempdate = findValue($tempNode, "rangeOfDates/endDate/calendarDate");
1631
		    ($x, $y, $z) = split("-", $tempdate);
1632
		    $$templateVars{'endingYear'} = $x;
1633
		    $$templateVars{'endingMonth'} = $y;
1634
		    $$templateVars{'endingDay'} = $z;
1635
		    
1636
		    $tempdate = "";
1637
		    $tempdate = findValue($tempNode, "singleDateTime/calendarDate");
1638
		    if($tempdate ne ""){
1639
			($x, $y, $z) = split("-", $tempdate);
1640
			$$templateVars{'beginningYear'} = $x;
1641
			$$templateVars{'beginningMonth'} = $y;
1642
			$$templateVars{'beginningDay'} = $z;
1643
		    }  
1644
		}
1645
	    }
1646

    
1647
	    $tempResult = $node->findnodes('./geographicCoverage');
1648
	    if($tempResult->size > 1){
1649
		errMoreThanOne("geographicCoverage");
1650
	    }else{
1651
		foreach $tempNode ($tempResult->get_nodelist){
1652

    
1653
		    my $coord;
1654

    
1655
		    $coord = findValue($tempNode, "boundingCoordinates/westBoundingCoordinate");
1656
		    if($coord > 0){
1657
			#print "+";
1658
			$$templateVars{'hemisphLong1'} = "E";
1659
		    } else {
1660
			#print "-";
1661
			eval($coord = $coord * -1);
1662
			$$templateVars{'hemisphLong1'} = "W";
1663
		    }
1664
		    eval($$templateVars{'longDeg1'} = int($coord));
1665
		    eval($coord = ($coord - int($coord))*60);
1666
		    eval($$templateVars{'longMin1'} = int($coord));
1667
		    eval($coord = ($coord - int($coord))*60);
1668
		    eval($$templateVars{'longSec1'} = int($coord));
1669
		    
1670

    
1671
		    $coord = findValue($tempNode, "boundingCoordinates/southBoundingCoordinate");
1672
		    if($coord > 0){
1673
			#print "+";
1674
			$$templateVars{'hemisphLat2'} = "N";
1675
		    } else {
1676
			#print "-";
1677
			eval($coord = $coord * -1);
1678
			$$templateVars{'hemisphLat2'} = "S";
1679
		    }
1680
		    eval($$templateVars{'latDeg2'} = int($coord));
1681
		    eval($coord = ($coord - int($coord))*60);
1682
		    eval($$templateVars{'latMin2'} = int($coord));
1683
		    eval($coord = ($coord - int($coord))*60);
1684
		    eval($$templateVars{'latSec2'} = int($coord));
1685

    
1686

    
1687
		    $coord = findValue($tempNode, "boundingCoordinates/northBoundingCoordinate");
1688
		    if($coord > 0){
1689
			#print "+";
1690
			$$templateVars{'hemisphLat1'} = "N";
1691
		    } else {
1692
			#print "-";
1693
			eval($coord = $coord * -1);
1694
			$$templateVars{'hemisphLat1'} = "S";
1695
		    }
1696
		    eval($$templateVars{'latDeg1'} = int($coord));
1697
		    eval($coord = ($coord - int($coord))*60);
1698
		    eval($$templateVars{'latMin1'} = int($coord));
1699
		    eval($coord = ($coord - int($coord))*60);
1700
		    eval($$templateVars{'latSec1'} = int($coord));
1701

    
1702

    
1703
		    $coord = findValue($tempNode, "boundingCoordinates/eastBoundingCoordinate");
1704
		    if($coord > 0){
1705
			#print "+";
1706
			$$templateVars{'hemisphLong2'} = "E";
1707
		    } else {
1708
			#print "-";
1709
			eval($coord = $coord * -1);
1710
			$$templateVars{'hemisphLong2'} = "W";
1711
		    }
1712
		    eval($$templateVars{'longDeg2'} = int($coord));
1713
		    eval($coord = ($coord - int($coord))*60);
1714
		    eval($$templateVars{'longMin2'} = int($coord));
1715
		    eval($coord = ($coord - int($coord))*60);
1716
		    eval($$templateVars{'longSec2'} = int($coord));
1717
		}
1718
	    }
1719
	}
1720
    }
1721

    
1722
    dontOccur($doc, "./purpose", "purpose");
1723
    dontOccur($doc, "./maintenance", "maintnance");
1724

    
1725

    
1726
    $results = $doc->findnodes('//dataset/contact/individualName');
1727
    if($results->size() > 1){
1728
	errMoreThanOne("contact/individualName");
1729
    } else {
1730
	foreach $node ($results->get_nodelist){
1731
	    dontOccur($node, "../positionName|../onlineURL|../userId", 
1732
		      "positionName, onlineURL, userId in contact tag");
1733
	    
1734
	    dontOccur($node, "./saluation", "saluation in contact tag");			    
1735
	    
1736
	    $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress|../organizationName');
1737
	    if($tempResult->size > 0){
1738
		
1739
		$$templateVars{'origNamefirstContact'} = findValue($node, 'givenName');
1740
		$$templateVars{'origNamelastContact'} = findValue($node, 'surName');
1741
	
1742
		my $tempResult2 = $node->findnodes('../address');
1743
		if($tempResult2->size > 1){
1744
		    errMoreThanOne("address");
1745
		} else {
1746
		    foreach my $tempNode2 ($tempResult2->get_nodelist){
1747
			$$templateVars{'origDeliveryContact'} = findValue($tempNode2, 'deliveryPoint');
1748
			$$templateVars{'origCityContact'} = findValue($tempNode2, 'city');
1749
			$$templateVars{'origStateContact'} = findValue($tempNode2, 'administrativeArea');
1750
			$$templateVars{'origZIPContact'} = findValue($tempNode2, 'postalCode');
1751
			$$templateVars{'origCountryContact'} = findValue($tempNode2, 'country');
1752
		    }
1753
		}
1754
		
1755
		my $tempResult3 = $node->findnodes('../phone');
1756
		if($tempResult3->size > 2){
1757
		    errMoreThanN("phone");
1758
		} else {
1759
		    foreach my $tempNode2 ($tempResult3->get_nodelist){
1760
			if($tempNode2->hasAttributes()){
1761
			    my @attlist = $tempNode2->attributes();
1762
			    if($attlist[0]->value eq "Fax"){
1763
				$$templateVars{'origFAXContact'} = $tempNode2->textContent();
1764
			    } else {
1765
				$$templateVars{'origPhoneContact'} = $tempNode2->textContent();
1766
			    }
1767
			}else{
1768
			    $$templateVars{'origPhoneContact'} = $tempNode2->textContent();
1769
			}
1770
		    }
1771
		}
1772
		$$templateVars{'origEmailContact'} = findValue($node, '../electronicMailAddress');
1773
		$$templateVars{'origNameOrgContact'} = findValue($node, '../organizationName');
1774
	    } else {
1775
		$$templateVars{'origNamefirstContact'} = findValue($node, 'givenName');
1776
		$$templateVars{'origNamelastContact'} = findValue($node, 'surName');
1777
		$$templateVars{'origNameOrgContact'} = findValue($node, '../organizationName');
1778
	    }
1779
	}
1780
    }
1781

    
1782
    
1783
    dontOccur($doc, "./pubPlace", "pubPlace");
1784
    dontOccur($doc, "./methods", "methods");
1785
    dontOccur($doc, "./project", "project");
1786
    
1787
    dontOccur($doc, "./dataTable", "dataTable");
1788
    dontOccur($doc, "./spatialRaster", "spatialRaster");
1789
    dontOccur($doc, "./spatialVector", "spatialVector");
1790
    dontOccur($doc, "./storedProcedure", "storedProcedure");
1791
    dontOccur($doc, "./view", "view");
1792
    dontOccur($doc, "./otherEntity", "otherEntity");
1793
    dontOccur($doc, "./references", "references");
1794
    
1795
    dontOccur($doc, "//citation", "citation");
1796
    dontOccur($doc, "//software", "software");
1797
    dontOccur($doc, "//protocol", "protocol");
1798
    dontOccur($doc, "//additionalMetadata", "additionalMetadata");    
1799
}
1800

    
1801

    
1802
################################################################################
1803
# 
1804
# Delete the eml file that has been requested for deletion. 
1805
#
1806
################################################################################
1807
sub deleteData {
1808
    my $deleteAll = shift;
1809
    
1810
    # create metacat instance
1811
    my $metacat;
1812
    my $docid = $FORM::docid;
1813
    
1814
    $metacat = Metacat->new();
1815
    if ($metacat) {
1816
        $metacat->set_options( metacatUrl => $metacatUrl );
1817
    } else {
1818
        #die "failed during metacat creation\n";
1819
        push(@errorMessages, "Failed during metacat creation.");
1820
    }
1821

    
1822
    # Login to metacat
1823
    my $userDN = $FORM::username;
1824
    my $userOrg = $FORM::organization;
1825
    my $userPass = $FORM::password;
1826
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
1827
    
1828
    my $errorMessage = "";
1829
    my $response = $metacat->login($dname, $userPass);
1830

    
1831
    if (! $response) {
1832
	# Could not login
1833
        push(@errorMessages, $metacat->getMessage());
1834
        push(@errorMessages, "Failed during login.\n");
1835

    
1836
    } else {
1837
	#Able to login - try to delete the file	
1838

    
1839
	my $parser;
1840
	my @fileArray;
1841
	my $httpMessage;
1842
	my $xmldoc;
1843
	my $doc;
1844
	my $pushDoc;
1845
	my $alreadyInArray;
1846
	my $findType;
1847
        my $node;
1848
	my $response; 
1849
	my $element;
1850

    
1851
	push (@fileArray, $docid);
1852
	$parser = XML::LibXML->new();
1853

    
1854
        $httpMessage = $metacat->read($docid);
1855
	$doc = $httpMessage->content();	
1856
	$xmldoc = $parser->parse_string($doc);
1857

    
1858
	if ($xmldoc eq "") {
1859
	    $error ="Error in parsing the eml document";
1860
	    push(@errorMessages, $error);
1861
	} else {
1862

    
1863
	    $findType = $xmldoc->findnodes('//dataset/identifier');
1864
	    if($findType->size() > 0){
1865
		# This is a eml beta6 document
1866
		# Delete the documents mentioned in triples also
1867
		
1868
		$findType = $xmldoc->findnodes('//dataset/triple');
1869
		if($findType->size() > 0){
1870
		    foreach $node ($findType->get_nodelist){
1871
			$pushDoc = findValue($node, 'subject');
1872
			
1873
			# If the file is already in the @fileArray then do not add it 
1874
			$alreadyInArray = 0;
1875
			foreach $element (@fileArray){
1876
			    if($element eq $pushDoc){
1877
				$alreadyInArray = 1;
1878
			    }
1879
			}
1880
			
1881
			if(!$alreadyInArray){
1882
			    # If not already in array then delete the file. 
1883
			    push (@fileArray, $pushDoc);
1884
			    $response = $metacat->delete($pushDoc);
1885
			    
1886
			    if (! $response) {
1887
				# Could not delete
1888
				#push(@errorMessages, $response);
1889
				push(@errorMessages, $metacat->getMessage());
1890
				push(@errorMessages, "Failed during deleting $pushDoc. Please check if you are authorized to delete this document.\n");
1891
			    }
1892
			}
1893
		    }
1894
		}
1895
	    }
1896
	}
1897
	
1898
	# Delete the main document. 
1899
	if($deleteAll){
1900
	    $response = $metacat->delete($docid);  
1901
	    if (! $response) {
1902
		# Could not delete
1903
		#push(@errorMessages, $response);
1904
		push(@errorMessages, $metacat->getMessage());
1905
		push(@errorMessages, "Failed during deleting $docid. Please check if you are authorized to delete this document.\n");
1906
	    }
1907
	}
1908
    }
1909
    
1910
    if (scalar(@errorMessages)) {
1911
	# If any errors, print them in the response template 
1912
	$$templateVars{'status'} = 'failure';
1913
	$$templateVars{'errorMessages'} = \@errorMessages;
1914
	$error = 1;
1915
    }
1916
    
1917
    # Process the response template
1918
    if($deleteAll){
1919

    
1920
	$$templateVars{'function'} = "deleted";
1921
	$$templateVars{'section'} = "Deletion Status";
1922
	$template->process( $responseTemplate, $templateVars);
1923
    }
1924
}
1925

    
1926

    
1927
################################################################################
1928
# 
1929
# Do data validation and send the data to confirm data template.
1930
#
1931
################################################################################
1932
sub toConfirmData{
1933
    # Check if any invalid parameters
1934
 
1935
    my $invalidParams;
1936
    if (! $error) {
1937
	$invalidParams = validateParameters(0);
1938
	if (scalar(@$invalidParams)) {
1939
	    $$templateVars{'status'} = 'failure';
1940
	    $$templateVars{'invalidParams'} = $invalidParams;
1941
	    $error = 1;
1942
	}
1943
    }
1944

    
1945
    if (! $error) {
1946
	# If no errors, then print out data in confirm Data template
1947

    
1948
	$$templateVars{'providerGivenName'} = $FORM::providerGivenName;
1949
	$$templateVars{'providerSurName'} = $FORM::providerSurName;
1950
	if($FORM::site eq "Select your station here."){
1951
	    $$templateVars{'site'} = "";
1952
	}else{
1953
	    $$templateVars{'site'} = $FORM::site;
1954
	}
1955
	$$templateVars{'identifier'} = $FORM::identifier;
1956
	$$templateVars{'title'} = $FORM::title;
1957
	$$templateVars{'origNamefirst0'} = $FORM::origNamefirst0;
1958
	$$templateVars{'origNamelast0'} = $FORM::origNamelast0;
1959
	$$templateVars{'origNameOrg'} = $FORM::origNameOrg;
1960
	# $$templateVars{'origRole0'} = $FORM::origRole0;
1961
	$$templateVars{'origDelivery'} = $FORM::origDelivery;
1962
	$$templateVars{'origCity'} = $FORM::origCity;
1963
	if($FORM::origState eq "Select State Here."){
1964
	    $$templateVars{'origState'} = "";
1965
	}else{
1966
	    $$templateVars{'origState'} = $FORM::origState;
1967
	}
1968
	$$templateVars{'origStateOther'} = $FORM::origStateOther;
1969
	$$templateVars{'origZIP'} = $FORM::origZIP;
1970
	$$templateVars{'origCountry'} = $FORM::origCountry;
1971
	$$templateVars{'origPhone'} = $FORM::origPhone;
1972
	$$templateVars{'origFAX'} = $FORM::origFAX;
1973
	$$templateVars{'origEmail'} = $FORM::origEmail;
1974
	$$templateVars{'useOrigAddress'} = $FORM::useOrigAddress;
1975
	if($FORM::useOrigAddress eq "on"){
1976
	    $$templateVars{'origNamefirstContact'} = $FORM::origNamefirst0;
1977
	    $$templateVars{'origNamelastContact'} = $FORM::origNamelast0;
1978
	    $$templateVars{'origNameOrgContact'} = $FORM::origNameOrg;
1979
	    $$templateVars{'origDeliveryContact'} = $FORM::origDelivery; 
1980
	    $$templateVars{'origCityContact'} = $FORM::origCity;
1981
	    if($FORM::origState eq "Select State Here."){
1982
		$$templateVars{'origStateContact'} = "";
1983
	    }else{
1984
		$$templateVars{'origStateContact'} = $FORM::origState;
1985
	    }
1986
	    $$templateVars{'origStateOtherContact'} = $FORM::origStateOther;
1987
	    $$templateVars{'origZIPContact'} = $FORM::origZIP;
1988
	    $$templateVars{'origCountryContact'} = $FORM::origCountry;
1989
	    $$templateVars{'origPhoneContact'} = $FORM::origPhone;
1990
	    $$templateVars{'origFAXContact'} = $FORM::origFAX;
1991
	    $$templateVars{'origEmailContact'} = $FORM::origEmail;
1992
	}else{
1993
	    $$templateVars{'origNamefirstContact'} = $FORM::origNamefirstContact;
1994
	    $$templateVars{'origNamelastContact'} = $FORM::origNamelastContact;
1995
	    $$templateVars{'origNameOrgContact'} = $FORM::origNameOrgContact;
1996
	    $$templateVars{'origDeliveryContact'} = $FORM::origDeliveryContact; 
1997
	    $$templateVars{'origCityContact'} = $FORM::origCityContact;
1998
	    if($FORM::origStateContact eq "Select State Here."){
1999
		$$templateVars{'origStateContact'} = "";
2000
	    }else{
2001
		$$templateVars{'origStateContact'} = $FORM::origStateContact;
2002
	    }
2003
	    $$templateVars{'origStateOtherContact'} = $FORM::origStateOtherContact;
2004
	    $$templateVars{'origZIPContact'} = $FORM::origZIPContact;
2005
	    $$templateVars{'origCountryContact'} = $FORM::origCountryContact;
2006
	    $$templateVars{'origPhoneContact'} = $FORM::origPhoneContact;
2007
	    $$templateVars{'origFAXContact'} = $FORM::origFAXContact;
2008
	    $$templateVars{'origEmailContact'} = $FORM::origEmailContact;    
2009
	}
2010
	$$templateVars{'origNamefirst1'} = $FORM::origNamefirst1;
2011
	$$templateVars{'origNamelast1'} = $FORM::origNamelast1;
2012
	if($FORM::origNamefirst1 eq "" && $FORM::origNamelast1 eq ""){
2013
	    $$templateVars{'origRole1'} = "";
2014
	}else{
2015
	    $$templateVars{'origRole1'} = $FORM::origRole1;
2016
	}
2017
	$$templateVars{'origNamefirst2'} = $FORM::origNamefirst2;
2018
	$$templateVars{'origNamelast2'} = $FORM::origNamelast2;
2019
	if($FORM::origNamefirst2 eq "" && $FORM::origNamelast2 eq ""){
2020
	    $$templateVars{'origRole2'} = "";
2021
	}else{
2022
	    $$templateVars{'origRole2'} = $FORM::origRole2;
2023
	}
2024
	$$templateVars{'origNamefirst3'} = $FORM::origNamefirst3;
2025
	$$templateVars{'origNamelast3'} = $FORM::origNamelast3;
2026
	if($FORM::origNamefirst3 eq "" && $FORM::origNamelast3 eq ""){
2027
	    $$templateVars{'origRole3'} = "";
2028
	}else{
2029
	    $$templateVars{'origRole3'} = $FORM::origRole3;
2030
	}
2031
	$$templateVars{'origNamefirst4'} = $FORM::origNamefirst4;
2032
	$$templateVars{'origNamelast4'} = $FORM::origNamelast4;
2033
	if($FORM::origNamefirst4 eq "" && $FORM::origNamelast4 eq ""){
2034
	    $$templateVars{'origRole4'} = "";
2035
	}else{
2036
	    $$templateVars{'origRole4'} = $FORM::origRole4;
2037
	}
2038
	$$templateVars{'origNamefirst5'} = $FORM::origNamefirst5;
2039
	$$templateVars{'origNamelast5'} = $FORM::origNamelast5;
2040
	if($FORM::origNamefirst5 eq "" && $FORM::origNamelast5 eq ""){
2041
	    $$templateVars{'origRole5'} = "";
2042
	}else{
2043
	    $$templateVars{'origRole5'} = $FORM::origRole5;
2044
	}
2045
	$$templateVars{'origNamefirst6'} = $FORM::origNamefirst6;
2046
	$$templateVars{'origNamelast6'} = $FORM::origNamelast6;
2047
	if($FORM::origNamefirst6 eq "" && $FORM::origNamelast6 eq ""){
2048
	    $$templateVars{'origRole6'} = "";
2049
	}else{
2050
	    $$templateVars{'origRole6'} = $FORM::origRole6;
2051
	}
2052
	$$templateVars{'origNamefirst7'} = $FORM::origNamefirst7;
2053
	$$templateVars{'origNamelast7'} = $FORM::origNamelast7;
2054
	if($FORM::origNamefirst7 eq "" && $FORM::origNamelast7 eq ""){
2055
	    $$templateVars{'origRole7'} = "";
2056
	}else{
2057
	    $$templateVars{'origRole7'} = $FORM::origRole7;
2058
	}
2059
	$$templateVars{'origNamefirst8'} = $FORM::origNamefirst8;
2060
	$$templateVars{'origNamelast8'} = $FORM::origNamelast8;
2061
	if($FORM::origNamefirst8 eq "" && $FORM::origNamelast8 eq ""){
2062
	    $$templateVars{'origRole8'} = "";
2063
	}else{
2064
	    $$templateVars{'origRole8'} = $FORM::origRole8;
2065
	}
2066
	$$templateVars{'origNamefirst9'} = $FORM::origNamefirst9;
2067
	$$templateVars{'origNamelast9'} = $FORM::origNamelast9;
2068
	if($FORM::origNamefirst9 eq "" && $FORM::origNamelast9 eq ""){
2069
	    $$templateVars{'origRole9'} = "";
2070
	}else{
2071
	    $$templateVars{'origRole9'} = $FORM::origRole9;
2072
	}
2073
	$$templateVars{'origNamefirst10'} = $FORM::origNamefirst10;
2074
	$$templateVars{'origNamelast10'} = $FORM::origNamelast10;
2075
	if($FORM::origNamefirst10 eq "" && $FORM::origNamelast10 eq ""){
2076
	    $$templateVars{'origRole10'} = "";
2077
	}else{
2078
	    $$templateVars{'origRole10'} = $FORM::origRole10;
2079
	}
2080
	$$templateVars{'abstract'} = $FORM::abstract;
2081
	$$templateVars{'keyword0'} = $FORM::keyword0;
2082
	$$templateVars{'kwType0'} = $FORM::kwType0;
2083
	$$templateVars{'kwTh0'} = $FORM::kwTh0;
2084
	$$templateVars{'kwType1'} = $FORM::kwType1;
2085
	$$templateVars{'keyword1'} = $FORM::keyword1;
2086
	$$templateVars{'kwTh1'} = $FORM::kwTh1;
2087
	$$templateVars{'kwType2'} = $FORM::kwType2;
2088
	$$templateVars{'keyword2'} = $FORM::keyword2;
2089
	$$templateVars{'kwTh2'} = $FORM::kwTh2;
2090
	$$templateVars{'kwType3'} = $FORM::kwType3;
2091
	$$templateVars{'keyword3'} = $FORM::keyword3;
2092
	$$templateVars{'kwTh3'} = $FORM::kwTh3;
2093
	$$templateVars{'kwType4'} = $FORM::kwType4;
2094
	$$templateVars{'keyword4'} = $FORM::keyword4;
2095
	$$templateVars{'kwTh4'} = $FORM::kwTh4;
2096
	$$templateVars{'kwType5'} = $FORM::kwType5;
2097
	$$templateVars{'keyword5'} = $FORM::keyword5;
2098
	$$templateVars{'kwTh5'} = $FORM::kwTh5;
2099
	$$templateVars{'kwType6'} = $FORM::kwType6;
2100
	$$templateVars{'keyword6'} = $FORM::keyword6;
2101
	$$templateVars{'kwTh6'} = $FORM::kwTh6;
2102
	$$templateVars{'kwType7'} = $FORM::kwType7;
2103
	$$templateVars{'keyword7'} = $FORM::keyword7;
2104
	$$templateVars{'kwTh7'} = $FORM::kwTh7;
2105
	$$templateVars{'kwType8'} = $FORM::kwType8;
2106
	$$templateVars{'keyword8'} = $FORM::keyword8;
2107
	$$templateVars{'kwTh8'} = $FORM::kwTh8;
2108
	$$templateVars{'kwType9'} = $FORM::kwType9;
2109
	$$templateVars{'keyword9'} = $FORM::keyword9;
2110
	$$templateVars{'kwTh9'} = $FORM::kwTh9;
2111
	$$templateVars{'addComments'} = $FORM::addComments;
2112
	$$templateVars{'useConstraints'} = $FORM::useConstraints;
2113
	$$templateVars{'useConstraintsOther'} = $FORM::useConstraintsOther;
2114
	$$templateVars{'url'} = $FORM::url;
2115
	if($FORM::dataMedium eq "Select type of medium here."){
2116
	    $$templateVars{'dataMedium'} = "";
2117
	}else{
2118
	    $$templateVars{'dataMedium'} = $FORM::dataMedium;
2119
	}    
2120
	$$templateVars{'dataMediumOther'} = $FORM::dataMediumOther;
2121
	$$templateVars{'beginningYear'} = $FORM::beginningYear;
2122
	$$templateVars{'beginningMonth'} = $FORM::beginningMonth;
2123
	$$templateVars{'beginningDay'} = $FORM::beginningDay;
2124
	$$templateVars{'endingYear'} = $FORM::endingYear;
2125
	$$templateVars{'endingMonth'} = $FORM::endingMonth;
2126
	$$templateVars{'endingDay'} = $FORM::endingDay;
2127
	$$templateVars{'useSiteCoord'} = $FORM::useSiteCoord;
2128
	$$templateVars{'latDeg1'} = $FORM::latDeg1;
2129
	$$templateVars{'latMin1'} = $FORM::latMin1;
2130
	$$templateVars{'latSec1'} = $FORM::latSec1;
2131
	$$templateVars{'hemisphLat1'} = $FORM::hemisphLat1;
2132
	$$templateVars{'longDeg1'} = $FORM::longDeg1;
2133
	$$templateVars{'longMin1'} = $FORM::longMin1;
2134
	$$templateVars{'longSec1'} = $FORM::longSec1;
2135
	$$templateVars{'hemisphLong1'} = $FORM::hemisphLong1;
2136
	$$templateVars{'latDeg2'} = $FORM::latDeg2;
2137
	$$templateVars{'latMin2'} = $FORM::latMin2;
2138
	$$templateVars{'latSec2'} = $FORM::latSec2;
2139
	$$templateVars{'hemisphLat2'} = $FORM::hemisphLat2;
2140
	$$templateVars{'longDeg2'} = $FORM::longDeg2;
2141
	$$templateVars{'longMin2'} = $FORM::longMin2;
2142
	$$templateVars{'longSec2'} = $FORM::longSec2;
2143
	$$templateVars{'hemisphLong2'} = $FORM::hemisphLong2;
2144
	$$templateVars{'docid'} = $FORM::docid;
2145

    
2146
	$$templateVars{'section'} = "Confirm Data";
2147
	$template->process( $confirmDataTemplate, $templateVars);
2148

    
2149
    } else{	
2150
	# Errors from validation function. print the errors out using the response template
2151
	if (scalar(@errorMessages)) {
2152
	    $$templateVars{'status'} = 'failure';
2153
	    $$templateVars{'errorMessages'} = \@errorMessages;
2154
	    $error = 1;
2155
	}
2156
        # Create our HTML response and send it back
2157
	$$templateVars{'function'} = "submitted";
2158
	$$templateVars{'section'} = "Submission Status";
2159
	$template->process( $responseTemplate, $templateVars);
2160
    }
2161
}
2162

    
2163

    
2164
################################################################################
2165
# 
2166
# From confirm Data template - user wants to make some changes.
2167
#
2168
################################################################################
2169
sub confirmDataToReEntryData{
2170
    my @sortedSites;
2171
    foreach my $site (sort @sitelist) {
2172
        push(@sortedSites, $site);
2173
    }
2174

    
2175
    $$templateVars{'siteList'} = \@sortedSites;
2176
    $$templateVars{'section'} = "Re-Entry Form";
2177

    
2178
    $$templateVars{'providerGivenName'} = $FORM::providerGivenName;
2179
    $$templateVars{'providerSurName'} = $FORM::providerSurName;
2180
    $$templateVars{'site'} = $FORM::site;
2181
    $$templateVars{'identifier'} = $FORM::identifier;
2182
    $$templateVars{'title'} = $FORM::title;
2183
    $$templateVars{'origNamefirst0'} = $FORM::origNamefirst0;
2184
    $$templateVars{'origNamelast0'} = $FORM::origNamelast0;
2185
    $$templateVars{'origNameOrg'} = $FORM::origNameOrg;
2186
 #   $$templateVars{'origRole0'} = $FORM::origRole0;
2187
    $$templateVars{'origDelivery'} = $FORM::origDelivery;
2188
    $$templateVars{'origCity'} = $FORM::origCity;
2189
    $$templateVars{'origState'} = $FORM::origState;
2190
    $$templateVars{'origStateOther'} = $FORM::origStateOther;
2191
    $$templateVars{'origZIP'} = $FORM::origZIP;
2192
    $$templateVars{'origCountry'} = $FORM::origCountry;
2193
    $$templateVars{'origPhone'} = $FORM::origPhone;
2194
    $$templateVars{'origFAX'} = $FORM::origFAX;
2195
    $$templateVars{'origEmail'} = $FORM::origEmail;
2196
    if($FORM::useSiteCoord ne ""){
2197
	$$templateVars{'useOrigAddress'} = "CHECKED";
2198
    }else{
2199
	$$templateVars{'useOrigAddress'} = $FORM::useOrigAddress;
2200
    }
2201
    $$templateVars{'origNamefirstContact'} = $FORM::origNamefirstContact;
2202
    $$templateVars{'origNamelastContact'} = $FORM::origNamelastContact;
2203
    $$templateVars{'origNameOrgContact'} = $FORM::origNameOrgContact;
2204
    $$templateVars{'origDeliveryContact'} = $FORM::origDeliveryContact; 
2205
    $$templateVars{'origCityContact'} = $FORM::origCityContact;
2206
    $$templateVars{'origStateContact'} = $FORM::origStateContact;
2207
    $$templateVars{'origStateOtherContact'} = $FORM::origStateOtherContact;
2208
    $$templateVars{'origZIPContact'} = $FORM::origZIPContact;
2209
    $$templateVars{'origCountryContact'} = $FORM::origCountryContact;
2210
    $$templateVars{'origPhoneContact'} = $FORM::origPhoneContact;
2211
    $$templateVars{'origFAXContact'} = $FORM::origFAXContact;
2212
    $$templateVars{'origEmailContact'} = $FORM::origEmailContact;    
2213
    $$templateVars{'origNamefirst1'} = $FORM::origNamefirst1;
2214
    $$templateVars{'origNamelast1'} = $FORM::origNamelast1;
2215
    $$templateVars{'origRole1'} = $FORM::origRole1;
2216
    $$templateVars{'origNamefirst2'} = $FORM::origNamefirst2;
2217
    $$templateVars{'origNamelast2'} = $FORM::origNamelast2;
2218
    $$templateVars{'origRole2'} = $FORM::origRole2;
2219
    $$templateVars{'origNamefirst3'} = $FORM::origNamefirst3;
2220
    $$templateVars{'origNamelast3'} = $FORM::origNamelast3;
2221
    $$templateVars{'origRole3'} = $FORM::origRole3;
2222
    $$templateVars{'origNamefirst4'} = $FORM::origNamefirst4;
2223
    $$templateVars{'origNamelast4'} = $FORM::origNamelast4;
2224
    $$templateVars{'origRole4'} = $FORM::origRole4;
2225
    $$templateVars{'origNamefirst5'} = $FORM::origNamefirst5;
2226
    $$templateVars{'origNamelast5'} = $FORM::origNamelast5;
2227
    $$templateVars{'origRole5'} = $FORM::origRole5;
2228
    $$templateVars{'origNamefirst6'} = $FORM::origNamefirst6;
2229
    $$templateVars{'origNamelast6'} = $FORM::origNamelast6;
2230
    $$templateVars{'origRole6'} = $FORM::origRole6;
2231
    $$templateVars{'origNamefirst7'} = $FORM::origNamefirst7;
2232
    $$templateVars{'origNamelast7'} = $FORM::origNamelast7;
2233
    $$templateVars{'origRole7'} = $FORM::origRole7;
2234
    $$templateVars{'origNamefirst8'} = $FORM::origNamefirst8;
2235
    $$templateVars{'origNamelast8'} = $FORM::origNamelast8;
2236
    $$templateVars{'origRole8'} = $FORM::origRole8;
2237
    $$templateVars{'origNamefirst9'} = $FORM::origNamefirst9;
2238
    $$templateVars{'origNamelast9'} = $FORM::origNamelast9;
2239
    $$templateVars{'origRole9'} = $FORM::origRole9;
2240
    $$templateVars{'origNamefirst10'} = $FORM::origNamefirst10;
2241
    $$templateVars{'origNamelast10'} = $FORM::origNamelast10;
2242
    $$templateVars{'origRole10'} = $FORM::origRole10;
2243
    $$templateVars{'abstract'} = $FORM::abstract;
2244
    $$templateVars{'keyword0'} = $FORM::keyword0;
2245
    $$templateVars{'kwType0'} = $FORM::kwType0;
2246
    $$templateVars{'kwTh0'} = $FORM::kwTh0;
2247
    $$templateVars{'kwType1'} = $FORM::kwType1;
2248
    $$templateVars{'keyword1'} = $FORM::keyword1;
2249
    $$templateVars{'kwTh1'} = $FORM::kwTh1;
2250
    $$templateVars{'kwType2'} = $FORM::kwType2;
2251
    $$templateVars{'keyword2'} = $FORM::keyword2;
2252
    $$templateVars{'kwTh2'} = $FORM::kwTh2;
2253
    $$templateVars{'kwType3'} = $FORM::kwType3;
2254
    $$templateVars{'keyword3'} = $FORM::keyword3;
2255
    $$templateVars{'kwTh3'} = $FORM::kwTh3;
2256
    $$templateVars{'kwType4'} = $FORM::kwType4;
2257
    $$templateVars{'keyword4'} = $FORM::keyword4;
2258
    $$templateVars{'kwTh4'} = $FORM::kwTh4;
2259
    $$templateVars{'kwType5'} = $FORM::kwType5;
2260
    $$templateVars{'keyword5'} = $FORM::keyword5;
2261
    $$templateVars{'kwTh5'} = $FORM::kwTh5;
2262
    $$templateVars{'kwType6'} = $FORM::kwType6;
2263
    $$templateVars{'keyword6'} = $FORM::keyword6;
2264
    $$templateVars{'kwTh6'} = $FORM::kwTh6;
2265
    $$templateVars{'kwType7'} = $FORM::kwType7;
2266
    $$templateVars{'keyword7'} = $FORM::keyword7;
2267
    $$templateVars{'kwTh7'} = $FORM::kwTh7;
2268
    $$templateVars{'kwType8'} = $FORM::kwType8;
2269
    $$templateVars{'keyword8'} = $FORM::keyword8;
2270
    $$templateVars{'kwTh8'} = $FORM::kwTh8;
2271
    $$templateVars{'kwType9'} = $FORM::kwType9;
2272
    $$templateVars{'keyword9'} = $FORM::keyword9;
2273
    $$templateVars{'kwTh9'} = $FORM::kwTh9;
2274
    $$templateVars{'addComments'} = $FORM::addComments;
2275
    $$templateVars{'useConstraints'} = $FORM::useConstraints;
2276
    $$templateVars{'useConstraintsOther'} = $FORM::useConstraintsOther;
2277
    $$templateVars{'url'} = $FORM::url;
2278
    $$templateVars{'dataMedium'} = $FORM::dataMedium;
2279
    $$templateVars{'dataMediumOther'} = $FORM::dataMediumOther;
2280
    $$templateVars{'beginningYear'} = $FORM::beginningYear;
2281
    $$templateVars{'beginningMonth'} = $FORM::beginningMonth;
2282
    $$templateVars{'beginningDay'} = $FORM::beginningDay;
2283
    $$templateVars{'endingYear'} = $FORM::endingYear;
2284
    $$templateVars{'endingMonth'} = $FORM::endingMonth;
2285
    $$templateVars{'endingDay'} = $FORM::endingDay;
2286
    if($FORM::useSiteCoord ne ""){
2287
	$$templateVars{'useSiteCoord'} = "CHECKED";
2288
    }else{
2289
	$$templateVars{'useSiteCoord'} = "";
2290
    }
2291
    $$templateVars{'latDeg1'} = $FORM::latDeg1;
2292
    $$templateVars{'latMin1'} = $FORM::latMin1;
2293
    $$templateVars{'latSec1'} = $FORM::latSec1;
2294
    $$templateVars{'hemisphLat1'} = $FORM::hemisphLat1;
2295
    $$templateVars{'longDeg1'} = $FORM::longDeg1;
2296
    $$templateVars{'longMin1'} = $FORM::longMin1;
2297
    $$templateVars{'longSec1'} = $FORM::longSec1;
2298
    $$templateVars{'hemisphLong1'} = $FORM::hemisphLong1;
2299
    $$templateVars{'latDeg2'} = $FORM::latDeg2;
2300
    $$templateVars{'latMin2'} = $FORM::latMin2;
2301
    $$templateVars{'latSec2'} = $FORM::latSec2;
2302
    $$templateVars{'hemisphLat2'} = $FORM::hemisphLat2;
2303
    $$templateVars{'longDeg2'} = $FORM::longDeg2;
2304
    $$templateVars{'longMin2'} = $FORM::longMin2;
2305
    $$templateVars{'longSec2'} = $FORM::longSec2;
2306
    $$templateVars{'hemisphLong2'} = $FORM::hemisphLong2;
2307
    $$templateVars{'docid'} = $FORM::docid;
2308

    
2309
    $$templateVars{'form'} = 're_entry';
2310
    $template->process( $entryFormTemplate, $templateVars);
2311
}
2312

    
2313

    
2314
################################################################################
2315
# 
2316
# check if there is multiple occurence of the given tag and find its value.
2317
#
2318
################################################################################
2319

    
2320
sub findValue {
2321
    my $node = shift;
2322
    my $value = shift;
2323
    my $result;
2324
    my $tempNode;
2325

    
2326
    $result = $node->findnodes("./$value");
2327
    if($result->size > 1){
2328
	errMoreThanOne("$value");
2329
    } else {
2330
	foreach $tempNode ($result->get_nodelist){
2331
	    #print $tempNode->nodeName().":".$tempNode->textContent();
2332
	    #print "\n";
2333
	    return $tempNode->textContent();
2334
	}
2335
    }
2336
}
2337

    
2338

    
2339
################################################################################
2340
# 
2341
# check if given tags has any children. if not return the value
2342
#
2343
################################################################################
2344
sub findValueNoChild {
2345
    my $node = shift;
2346
    my $value = shift;
2347
    my $tempNode;
2348
    my $childNodes;
2349
    my $result;
2350
    my $error;
2351

    
2352
    $result = $node->findnodes("./$value");
2353
	    if($result->size > 1){
2354
	   	errMoreThanOne("$value");
2355
	    } else {
2356
		foreach $tempNode ($result->get_nodelist){
2357
		    $childNodes = $tempNode->childNodes;
2358
		    if($childNodes->size() > 1){
2359
			$error ="The tag $value has children which cannot be shown using the form. Please use Morpho to edit this document";	
2360
			push(@errorMessages, $error);
2361
			#if ($DEBUG == 1){ print $error."\n";}
2362
		    }else{
2363
			#print $tempNode->nodeName().":".$tempNode->textContent();
2364
			#print "\n";
2365
			return $tempNode->textContent();
2366
		    }
2367
		}
2368
	    }
2369
}
2370

    
2371

    
2372
################################################################################
2373
# 
2374
# check if given tags are children of given node.
2375
#
2376
################################################################################
2377
sub dontOccur {
2378
    my $node = shift;
2379
    my $value = shift;
2380
    my $errVal = shift;
2381

    
2382
    my $result = $node->findnodes("$value");
2383
    if($result->size > 0){
2384
	$error ="One of the following tags found: $errVal. Please use Morpho to edit this document";
2385
	push(@errorMessages, $error."\n");
2386
	#if ($DEBUG == 1){ print $error;}
2387
    } 
2388
}
2389

    
2390

    
2391
################################################################################
2392
# 
2393
# print out error for more than one occurence of a given tag
2394
#
2395
################################################################################
2396
sub errMoreThanOne {
2397
    my $value = shift;
2398
    my $error ="More than one occurence of the tag $value found. Please use Morpho to edit this document";
2399
    push(@errorMessages, $error."\n");
2400
    # if ($DEBUG == 1){ print $error;}
2401
}
2402

    
2403

    
2404
################################################################################
2405
# 
2406
# print out error for more than given number of occurences of a given tag
2407
#
2408
################################################################################
2409
sub errMoreThanN {
2410
    my $value = shift;
2411
    my $error ="More occurences of the tag $value found than that can be shown in the form. Please use Morpho to edit this document";
2412
    push(@errorMessages, $error);
2413
    #if ($DEBUG == 1){ print $error."\n";}
2414
}
2415

    
2416

    
2417
################################################################################
2418
# 
2419
# convert coord to degrees, minutes and seconds form. 
2420
#
2421
################################################################################
2422
#sub convertCoord {
2423
#    my $wx = shift;
2424
#    print $deg." ".$min." ".$sec;
2425
#    print "\n";
2426
#}
2427

    
2428

    
    (1-1/1)