Project

General

Profile

1
#!/usr/bin/perl
2
#
3
#  '$RCSfile$'
4
#  Copyright: 2000 Regents of the University of California 
5
#
6
#   '$Author: brooke $'
7
#     '$Date: 2003-11-21 15:05:54 -0800 (Fri, 21 Nov 2003) $'
8
# '$Revision: 1929 $' 
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
        push(@errorMessages, $metacat->getMessage());
285
        push(@errorMessages, "Failed during login.\n");
286
    }
287

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

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

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

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

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

    
351
	    exit(0);
352
    }
353
}
354

    
355

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

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

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

    
371
exit(0);
372

    
373

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

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

    
401
  return $notunique;
402
}
403

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

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

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

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

    
453
  return $acl;
454
}
455

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

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

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

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

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

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

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

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

    
567
    if ($FORM::useSiteCoord) {
568
	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.")
569
	    unless(hasContent($latDeg1) && hasContent($longDeg1));
570
    }else{
571
	push(@invalidParams, "Latitude degrees are missing.")
572
	    unless hasContent($latDeg1);
573
	push(@invalidParams, "Longitude degrees are missing.")
574
	    unless hasContent($longDeg1);
575
    }
576

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

    
593
    return \@invalidParams;
594
}
595

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

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

    
618

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

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

    
636

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
943

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

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

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

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

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

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

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

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

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

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

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

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

    
1107
    return $doc;
1108
}
1109

    
1110

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

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

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

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

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

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

    
1148

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

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

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

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

    
1231
	    # Read the main document. 
1232

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

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

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

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

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

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

    
1290

    
1291

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

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

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

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

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

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

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

    
1442

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

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

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

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

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

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

    
1535

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

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

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

    
1578

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

    
1590

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

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

    
1607

    
1608
    $results = $doc->findnodes('//dataset/coverage');
1609
    if($results->size() > 1){
1610
	errMoreThanOne("coverage");
1611
    } else {
1612
	foreach $node ($results->get_nodelist){
1613
	    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");
1614

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

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

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

    
1652
		    my $coord;
1653

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

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

    
1685

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

    
1701

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

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

    
1724

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

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

    
1800

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

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

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

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

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

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

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

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

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

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

    
1925

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

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

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

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

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

    
2162

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

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

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

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

    
2312

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

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

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

    
2337

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

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

    
2370

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

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

    
2389

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

    
2402

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

    
2415

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

    
2427

    
    (1-1/1)