Project

General

Profile

1 1929 brooke
#!/usr/bin/perl
2
#
3
#  '$RCSfile$'
4
#  Copyright: 2000 Regents of the University of California
5
#
6
#   '$Author$'
7
#     '$Date$'
8
# '$Revision$'
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 1973 jones
if ($FORM::cfg eq 'nceas') {
81
    $config->define("nceas_db");
82
    $config->define("nceas_db_user");
83
    $config->define("nceas_db_password");
84
}
85 1929 brooke
$config->define("responseTemplate", { DEFAULT => 'crap.tmpl'} );
86
$config->define("entryFormTemplate", { DEFAULT => 'crap.tmpl'} );
87
$config->define("guideTemplate", { DEFAULT => 'crap.tmpl'} );
88
$config->define("confirmDataTemplate", { DEFAULT => 'crap.tmpl'} );
89
$config->define("deleteDataTemplate", { DEFAULT => 'crap.tmpl'} );
90 1955 jones
$config->define("debug", { DEFAULT => '0'} );
91 1929 brooke
$config->define("lat", { ARGCOUNT => ARGCOUNT_HASH} );
92
$config->define("lon", { ARGCOUNT => ARGCOUNT_HASH} );
93
94
if (! hasContent($FORM::cfg)) {
95
    $error = "Application misconfigured.  Please contact the administrator.";
96
    push(@errorMessages, $error);
97
} else {
98
    my $cfgfile = $cfgdir . "/" . $FORM::cfg . "/" . $FORM::cfg . ".cfg";
99
    $config->file($cfgfile);
100
}
101
102
my $metacatUrl = $config->metacatUrl();
103
my $username = $config->username();
104
my $password = $config->password();
105
my $ldapUrl = $config->ldapUrl();
106
my $defaultScope = $config->defaultScope();
107
my $organization = $config->organization();
108
my $orgabbrev = $config->orgabbrev();
109
my $orgurl = $config->orgurl();
110
my $orgfilter = $organization;
111
   $orgfilter =~ s/ /%20/g;
112
my $responseTemplate = $config->responseTemplate();
113
my $entryFormTemplate = $config->entryFormTemplate();
114
my $deleteDataTemplate = $config->deleteDataTemplate();
115
my $guideTemplate = $config->guideTemplate();
116
my $confirmDataTemplate = $config->confirmDataTemplate();
117
my $accesspubid = $config->accesspubid();
118
my $accesssysid = $config->accesssysid();
119
my $datasetpubid = $config->datasetpubid();
120
my $datasetsysid = $config->datasetsysid();
121
my $mailhost = $config->mailhost();
122
my $sender = $config->sender();
123
my $recipient = $config->recipient();
124
my $adminname = $config->adminname();
125 1973 jones
my $nceas_db;
126
my $nceas_db_user;
127
my $nceas_db_password;
128
if ($FORM::cfg eq 'nceas') {
129
    $nceas_db = $config->nceas_db();
130
    $nceas_db_user = $config->nceas_db_user();
131
    $nceas_db_password = $config->nceas_db_password();
132
}
133 1955 jones
my $debug = $config->debug();
134 1929 brooke
my $lat = $config->get('lat');
135
my $lon = $config->get('lon');
136
137
# Convert the lat and lon configs into usable data structures
138
my @sitelist;
139
my %siteLatDMS;
140
my %siteLongDMS;
141
foreach my $newsite (keys %$lat) {
142
    my ($latd, $latm, $lats, $latdir) = split(':', $lat->{$newsite});
143
    my ($lond, $lonm, $lons, $londir) = split(':', $lon->{$newsite});
144
    push(@sitelist, $newsite);
145
    $siteLatDMS{$newsite} = [ $latd, $latm, $lats, $latdir ];
146
    $siteLongDMS{$newsite} = [ $lond, $lonm, $lons, $londir ];
147
}
148
149
# set some configuration options for the template object
150
my $ttConfig = {
151
             INCLUDE_PATH => $templatesdir,
152
             INTERPOLATE  => 0,
153
             POST_CHOMP   => 1,
154
             };
155
156
# create an instance of the template processor
157
my $template = Template->new($ttConfig) || die $Template::ERROR, "\n";
158
159
print "Content-type: text/html\n\n";
160
161
# Set up the template information that is common to all forms
162
$$templateVars{'cfg'} = $FORM::cfg;
163
$$templateVars{'recipient'} = $recipient;
164
$$templateVars{'adminname'} = $adminname;
165
$$templateVars{'organization'} = $organization;
166
$$templateVars{'orgabbrev'} = $orgabbrev;
167
$$templateVars{'orgurl'} = $orgurl;
168
$$templateVars{'orgfilter'} = $orgfilter;
169
170 1983 jones
debug("Registry: Initialized");
171 1929 brooke
# Process the form based on stage parameter.
172
if ($FORM::stage =~ "guide") {
173
    # Send back the information on how to fill the form
174
    $$templateVars{'section'} = "Guide on How to Complete Registry Entries";
175
    $template->process( $guideTemplate, $templateVars);
176
    exit(0);
177
178
} elsif ($FORM::stage =~ "insert") {
179
    # The user has entered the data. Do data validation and send back data
180
    # to confirm the data that has been entered.
181
    toConfirmData();
182
    exit(0);
183
184
}elsif ($FORM::dataWrong =~ "No, I want to change it!!" && $FORM::stage =~ "confirmed") {
185
    # The user wants to correct the data that he has entered.
186
    # Hence show the data again in entryData form.
187
    confirmDataToReEntryData();
188
    exit(0);
189
190
}elsif ($FORM::stage =~ "modify") {
191
    # Modification of a file has been requested.
192
    # Show the form will all the values filled in.
193
    my @sortedSites;
194
    foreach my $site (sort @sitelist) {
195
        push(@sortedSites, $site);
196
    }
197
    $$templateVars{'siteList'} = \@sortedSites;
198
    $$templateVars{'section'} = "Modification Form";
199
    $$templateVars{'docid'} = $FORM::docid;
200
    modifyData();
201
    exit(0);
202
203
}elsif ($FORM::stage =~ "delete_confirm") {
204
205
    # Result from deleteData form.
206
    if($FORM::deleteData =~ "Delete data"){
207 1977 jones
    # delete Data
208
    deleteData(1);
209
    exit(0);
210 1929 brooke
    } else {
211 1977 jones
    # go back to search page.
212
    exit(0);
213 1929 brooke
    }
214
215
}elsif ($FORM::stage =~ "delete") {
216
    # Deletion of a file has been requested.
217
    # Ask for username and password using deleteDataForm
218
    $$templateVars{'docid'} = $FORM::docid;
219
    $template->process( $deleteDataTemplate, $templateVars);
220
    exit(0);
221
222
}elsif ($FORM::stage !~ "confirmed") {
223
    # None of the stages have been reached and data is not being confirmed.
224
    # Hence, send back entry form for entry of data.
225 1983 jones
    debug("Registry: Sending form");
226 1929 brooke
    my @sortedSites;
227
    foreach my $site (sort @sitelist) {
228
        push(@sortedSites, $site);
229
    }
230 1973 jones
231
    if ($FORM::cfg eq 'nceas') {
232
        my $projects = getProjectList();
233 1974 jones
        $$templateVars{'projects'} = $projects;
234
        $$templateVars{'wg'} = \@FORM::wg;
235 1973 jones
    }
236 1974 jones
237 1929 brooke
    $$templateVars{'siteList'} = \@sortedSites;
238
    $$templateVars{'section'} = "Entry Form";
239
    $$templateVars{'docid'} = "";
240 1983 jones
    debug("Registry: Sending form: ready to process template");
241 1929 brooke
    $template->process( $entryFormTemplate, $templateVars);
242 1983 jones
    debug("Registry: Sending form: template processed");
243 1929 brooke
    exit(0);
244
}
245
246
# Confirm stage has been reached. Enter the data into metacat.
247
248
# Initialize some global vars
249
my $latDeg1 = "";
250
my $latMin1 = "";
251
my $latSec1 = "";
252
my $hemisphLat1 = "";
253
my $longDeg1 = "";
254
my $longMin1 = "";
255
my $longSec1 = "";
256
my $hemisphLong1 = "";
257
my $latDeg2 = "";
258
my $latMin2 = "";
259
my $latSec2 = "";
260
my $hemisphLat2 = "";
261
my $longDeg2 = "";
262
my $longMin2 = "";
263
my $longSec2 = "";
264
my $hemisphLong2 = "";
265
266
# validate the input form parameters
267
my $invalidParams;
268
269
if (! $error) {
270
    $invalidParams = validateParameters(1);
271
    if (scalar(@$invalidParams)) {
272
        $$templateVars{'status'} = 'failure';
273
        $$templateVars{'invalidParams'} = $invalidParams;
274
        $error = 1;
275
    }
276
}
277
278
279
my $metacat;
280
my $docid;
281
if (! $error) {
282
    # Parameters have been validated and Create the XML document
283
284
    my $xmldoc = createXMLDocument();
285
286
    # Write out the XML file for debugging purposes
287
    #my $testFile = $tmpdir . "/test.xml";
288
289
    # Create a  metacat object
290
    $metacat = Metacat->new();
291
    if ($metacat) {
292
        $metacat->set_options( metacatUrl => $metacatUrl );
293
    } else {
294
        #die "failed during metacat creation\n";
295
        push(@errorMessages, "Failed during metacat creation.");
296
    }
297
298
    # Login to metacat
299
    my $userDN = $FORM::username;
300
    my $userOrg = $FORM::organization;
301
    my $userPass = $FORM::password;
302
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
303
304
    my $errorMessage = "";
305
    my $response = $metacat->login($dname, $userPass);
306
    if (! $response) {
307
        push(@errorMessages, $metacat->getMessage());
308
        push(@errorMessages, "Failed during login.\n");
309
    }
310
311 1982 jones
    debug( "Registry: A");
312 1955 jones
    if ($FORM::docid eq "") {
313
        debug( "Registry: B1");
314 1977 jones
        # document is being inserted
315
        my $notunique = "NOT_UNIQUE";
316
        while ($notunique eq "NOT_UNIQUE") {
317
            $docid = newAccessionNumber($defaultScope);
318
319
            $xmldoc =~ s/docid/$docid/;
320 1929 brooke
321 1992 jones
            #my $testFile = "/tmp/test.xml";
322
            #open (TFILE,">$testFile") || die ("Cant open xml file...\n");
323
            #print TFILE $xmldoc;
324
            #close(TFILE);
325 1929 brooke
326 1977 jones
            $notunique = insertMetadata($xmldoc, $docid);
327
            #  if (!$notunique) {
328
            # Write out the XML file for debugging purposes
329
            #my $testFile = $tmpdir . "/test-new.xml";
330
            #open (TFILE,">$testFile") || die ("Cant open xml file...\n");
331
            #print TFILE $newdoc;
332
            #close(TFILE);
333
            #   }
334 1955 jones
335
            # The id wasn't unique, so update our lastid file
336 1977 jones
            if ($notunique eq "NOT_UNIQUE") {
337 1989 jones
                debug( "Registry: Updating lastid (B1.1)");
338 1955 jones
                updateLastId($defaultScope);
339
            }
340 1977 jones
        }
341 1955 jones
        debug("Registry: B2");
342 1977 jones
        if ($notunique ne "SUCCESS") {
343 1955 jones
            debug("Registry: NO SUCCESS");
344
            debug("Message is: $notunique");
345 1929 brooke
            push(@errorMessages, $notunique);
346 1977 jones
        }
347 1955 jones
        debug("Registry: B3");
348 1929 brooke
    } else {
349 1977 jones
        # document is being modified
350
        $docid = $FORM::docid;
351
352
        my $x;
353
        my $y;
354
        my $z;
355 1929 brooke
356 1977 jones
        ($x, $y, $z) = split(/\./, $docid);
357
        $z++;
358
        $docid = "$x.$y.$z";
359
360
        $xmldoc =~ s/docid/$docid/;
361 1929 brooke
362 1977 jones
        my $response = $metacat->update($docid, $xmldoc);
363 1929 brooke
364 1977 jones
        if (! $response) {
365
            push(@errorMessages, $metacat->getMessage());
366
            push(@errorMessages, "Failed while updating.\n");
367
        }
368 1929 brooke
369 1977 jones
        if (scalar(@errorMessages)) {
370 1955 jones
            debug("Registry: ErrorMessages defined in modify.");
371 1977 jones
            $$templateVars{'status'} = 'failure';
372
            $$templateVars{'errorMessages'} = \@errorMessages;
373
            $error = 1;
374
        }
375 1929 brooke
376 1955 jones
        #if (! $error) {
377
            #sendNotification($docid, $mailhost, $sender, $recipient);
378
        #}
379 1977 jones
380 1929 brooke
        # Create our HTML response and send it back
381 1977 jones
        $$templateVars{'function'} = "modified";
382
        $$templateVars{'section'} = "Modification Status";
383
        $template->process( $responseTemplate, $templateVars);
384 1929 brooke
385 1977 jones
        exit(0);
386 1929 brooke
    }
387
}
388
389 1955 jones
debug("Registry: C");
390 1929 brooke
391
if (scalar(@errorMessages)) {
392 1955 jones
    debug("Registry: ErrorMessages defined.");
393 1929 brooke
    $$templateVars{'status'} = 'failure';
394
    $$templateVars{'errorMessages'} = \@errorMessages;
395
    $error = 1;
396
}
397
398 1936 jones
#if (! $error) {
399
#sendNotification($docid, $mailhost, $sender, $recipient);
400
#}
401 1929 brooke
402
# Create our HTML response and send it back
403
$$templateVars{'function'} = "submitted";
404
$$templateVars{'section'} = "Submission Status";
405
$template->process( $responseTemplate, $templateVars);
406
407
exit(0);
408
409
410
################################################################################
411
#
412 1955 jones
# Subroutine for updating a metacat id for a given scope to the highest value
413
#
414
################################################################################
415
sub updateLastId {
416
  my $scope = shift;
417
418
  my $errormsg = 0;
419
  my $docid = $metacat->getLastId($scope);
420
421
  if ($docid =~ /null/) {
422
      # No docids with this scope present, so do nothing
423
  } elsif ($docid) {
424
      # Update the lastid file for this scope
425
      (my $foundScope, my $id, my $rev) = split(/\./, $docid);
426
      debug("Docid is: $docid\n");
427
      debug("Lastid is: $id");
428
      my $scopeFile = $cfgdir . "/" . $FORM::cfg . "/" . $scope . ".lastid";
429
      open(LASTID, ">$scopeFile") or
430
          die "Failed to open lastid file for writing!";
431
      print LASTID $id, "\n";
432
      close(LASTID);
433
  } else {
434
    $errormsg = $metacat->getMessage();
435
    debug("Error in getLastId: $errormsg");
436
  }
437
}
438
439
################################################################################
440
#
441 1929 brooke
# Subroutine for inserting a document to metacat
442
#
443
################################################################################
444
sub insertMetadata {
445
  my $xmldoc = shift;
446
  my $docid = shift;
447
448
  my $notunique = "SUCCESS";
449 1989 jones
  debug("Registry: Starting insert (D1)");
450 1929 brooke
  my $response = $metacat->insert($docid, $xmldoc);
451
  if (! $response) {
452 1989 jones
    debug("Registry: Response gotten (D2)");
453 1929 brooke
    my $errormsg = $metacat->getMessage();
454 1989 jones
    debug("Registry: Error is (D3): ".$errormsg);
455 1929 brooke
    if ($errormsg =~ /is already in use/) {
456
      $notunique = "NOT_UNIQUE";
457
      #print "Accession number already used: $docid\n";
458
    } elsif ($errormsg =~ /<login>/) {
459
      $notunique = "SUCCESS";
460
    } else {
461
      #print "<p>Dumping error on failure...</p>\n";
462
      #print "<p>", $errormsg, "</p>\n";
463
      #die "Failed during insert\n";
464
      #print "<p>Failed during insert</p>\n";
465
      $notunique = $errormsg;
466
    }
467
  }
468 1989 jones
  debug("Registry: Ending insert (D4)");
469 1929 brooke
470
  return $notunique;
471
}
472
473
################################################################################
474
#
475
# Subroutine for generating a new accession number
476
#  Note: this is not threadsafe, assumes only one running process at a time
477
#  Also: need to check metacat for max id # used in this scope already
478
################################################################################
479
sub newAccessionNumber {
480
  my $scope = shift;
481
482
  my $docrev = 1;
483
  my $lastid = 1;
484
485
  my $scopeFile = $cfgdir . "/" . $FORM::cfg . "/" . $scope . ".lastid";
486
  if (-e $scopeFile) {
487
    open(LASTID, "<$scopeFile") or die "Failed to generate accession number!";
488
    $lastid = <LASTID>;
489
    chomp($lastid);
490
    $lastid++;
491
    close(LASTID);
492
  }
493
  open(LASTID, ">$scopeFile") or die "Failed to open lastid file for writing!";
494
  print LASTID $lastid, "\n";
495
  close(LASTID);
496
497
  my $docroot = "$scope.$lastid.";
498
  my $docid = $docroot . $docrev;
499
  return $docid;
500
}
501
502
################################################################################
503
#
504
# Subroutine for generating a new ACL document
505
#
506
################################################################################
507
sub newAccessDocument {
508
  my $aclid = shift;
509
  my $acl = "<?xml version=\"1.0\"?>\n";
510
  $acl .= "<!DOCTYPE acl ";
511
  $acl .= "PUBLIC \"$accesspubid\" \"$accesssysid\">\n";
512
  $acl .= "<acl authSystem=\"ldap://ldap.ecoinformatics.org\" ";
513
  $acl .= "order=\"denyFirst\">\n";
514
  $acl .= "<identifier system=\"knb\">$aclid</identifier>\n";
515
  #$acl .= "<identifier>$aclid</identifier>\n";
516
  $acl .= "<allow><principal>$username</principal>" .
517
          "<permission>all</permission></allow>\n";
518
  #$acl .= "<allow><principal>public</principal>" .
519
  #        "<permission>read</permission></allow>\n";
520
  $acl .= "</acl>\n";
521
522
  return $acl;
523
}
524
525
################################################################################
526
#
527
# Subroutine for inserting identifers to the metadata document passed to us
528
#
529
################################################################################
530
sub insertIdentifiers {
531
  my $docstring = shift;
532
  my $aclid = shift;
533
  my $docid = shift;
534
535
  my $parser = XML::LibXML->new();
536
  my $dom = $parser->parse_string($docstring);
537
538
  my $root = $dom->documentElement;
539
  my $name = $root->getName();
540
541
  my $addedIdentifier = 0;
542
  my $currentElement = $root->getFirstChild();
543
  $name = $currentElement->getName();
544
  while ("$name" !~ "triple" &&
545
         "$name" !~ "temporalCov" &&
546
         "$name" !~ "geographicCov" &&
547
         "$name" !~ "taxonomicCov"
548
        ) {
549
    if ("$name" =~ "identifier" ||
550
        "$name" =~ "title") {
551
      if (! $addedIdentifier) {
552
      my $idelement = $dom->createElement( "identifier" );
553
        $addedIdentifier = 1;
554
        $idelement->setAttribute("system", "knb");
555
        $idelement->appendTextNode($docid);
556
        $root->insertBefore($idelement, $currentElement);
557
      }
558
    }
559
    $currentElement = $currentElement->getNextSibling();
560
    $name = $currentElement->getName();
561
  }
562
  # Link the document to the access doc
563
  my $element = $dom->createElement( "triple" );
564
  $element->appendTextChild( "subject", $aclid);
565
  $element->appendTextChild( "relationship",
566
                             "describes access control rules for");
567
  $element->appendTextChild( "object", $docid);
568
  $root->insertBefore($element, $currentElement);
569
  # Link the access doc to the access doc
570
  $element = $dom->createElement( "triple" );
571
  $element->appendTextChild( "subject", $aclid);
572
  $element->appendTextChild( "relationship",
573
                             "describes access control rules for");
574
  $element->appendTextChild( "object", $aclid);
575
  $root->insertBefore($element, $currentElement);
576
577
  return $dom->toString();
578
}
579
580
################################################################################
581
#
582
# Validate the parameters to make sure that required params are provided
583
#
584
################################################################################
585
sub validateParameters {
586
    my $chkUser = shift;
587
    my @invalidParams;
588
589
    push(@invalidParams, "Provider's first name is missing.")
590
        unless hasContent($FORM::providerGivenName);
591
    push(@invalidParams, "Provider's last name is missing.")
592
        unless hasContent($FORM::providerSurName);
593
    push(@invalidParams, "Name of site is missing.")
594 1974 jones
        unless (hasContent($FORM::site) || $FORM::site =~ /elect/ ||
595
                $FORM::cfg eq "nceas");
596 1929 brooke
    push(@invalidParams, "Data set title is missing.")
597
        unless hasContent($FORM::title);
598
    push(@invalidParams, "Originator's first name is missing.")
599
        unless hasContent($FORM::origNamefirst0);
600
    push(@invalidParams, "Originator's last name is missing.")
601
        unless hasContent($FORM::origNamelast0);
602
    push(@invalidParams, "Abstract is missing.")
603
        unless hasContent($FORM::abstract);
604
    push(@invalidParams, "Beginning year of data set is missing.")
605
        unless hasContent($FORM::beginningYear);
606 1983 jones
    push(@invalidParams, "Geographic description is missing.")
607
        unless (hasContent($FORM::geogdesc));
608 1929 brooke
609
    # If the "use site" coord. box is checked and if the site is in
610
    # the longitude hash ...  && ($siteLatDMS{$FORM::site})
611
612
    if (($FORM::useSiteCoord) && ($siteLatDMS{$FORM::site}) ) {
613
614
        $latDeg1 = $siteLatDMS{$FORM::site}[0];
615
        $latMin1 = $siteLatDMS{$FORM::site}[1];
616
        $latSec1 = $siteLatDMS{$FORM::site}[2];
617
        $hemisphLat1 = $siteLatDMS{$FORM::site}[3];
618
        $longDeg1 = $siteLongDMS{$FORM::site}[0];
619
        $longMin1 = $siteLongDMS{$FORM::site}[1];
620
        $longSec1 = $siteLongDMS{$FORM::site}[2];
621
        $hemisphLong1 = $siteLongDMS{$FORM::site}[3];
622
623
    }  else {
624
625
        $latDeg1 = $FORM::latDeg1;
626
        $latMin1 = $FORM::latMin1;
627
        $latSec1 = $FORM::latSec1;
628
        $hemisphLat1 = $FORM::hemisphLat1;
629
        $longDeg1 = $FORM::longDeg1;
630
        $longMin1 = $FORM::longMin1;
631
        $longSec1 = $FORM::longSec1;
632
        $hemisphLong1 = $FORM::hemisphLong1;
633
    }
634
635
    # Check if latDeg1 and longDeg1 has values if useSiteCoord is used.
636
    # This check is required because some of the sites dont have lat
637
    # and long mentioned in the config file.
638
639
    if ($FORM::useSiteCoord) {
640 1997 jones
    push(@invalidParams, "The Data Registry doesn't have latitude and longitude information for the site that you chose. Please go back and enter the spatial information.")
641 1977 jones
        unless(hasContent($latDeg1) && hasContent($longDeg1));
642 1929 brooke
    }else{
643 1977 jones
    push(@invalidParams, "Latitude degrees are missing.")
644
        unless hasContent($latDeg1);
645
    push(@invalidParams, "Longitude degrees are missing.")
646
        unless hasContent($longDeg1);
647 1929 brooke
    }
648
649 1989 jones
    push(@invalidParams,
650
        "You must provide a method description if you provide a method title.")
651
        if (hasContent($FORM::methodTitle) && !(scalar(@FORM::methodPara) > 0));
652
    push(@invalidParams,
653
        "You must provide a method description if you provide a study extent description.")
654
        if (hasContent($FORM::studyExtentDescription) && !(scalar(@FORM::methodPara) > 0));
655
    push(@invalidParams,
656
        "You must provide both a study extent description and a sampling description, or neither.")
657 1997 jones
        if (
658
                (hasContent($FORM::studyExtentDescription) && !hasContent($FORM::samplingDescription)) ||
659
                (!hasContent($FORM::studyExtentDescription) && hasContent($FORM::samplingDescription))
660
           );
661 1989 jones
662 1929 brooke
    push(@invalidParams, "Contact first name is missing.")
663 1977 jones
    unless (hasContent($FORM::origNamefirstContact) ||
664
        $FORM::useOrigAddress);
665 1929 brooke
    push(@invalidParams, "Contact last name is missing.")
666 1977 jones
    unless (hasContent($FORM::origNamelastContact) ||
667
        $FORM::useOrigAddress);
668 1929 brooke
    push(@invalidParams, "Data medium is missing.")
669 1977 jones
    unless (hasContent($FORM::dataMedium) || $FORM::dataMedium =~ /elect/);
670 1929 brooke
671
    if($chkUser){
672 1977 jones
    my $errorUserPass = validateUserPass();
673
    if($errorUserPass ne ""){
674
        push(@invalidParams, $errorUserPass);
675 1929 brooke
    }
676 1977 jones
    }
677 1929 brooke
678
    return \@invalidParams;
679
}
680
681
################################################################################
682
#
683
# Validate the parameters username and password.
684
#
685
################################################################################
686
sub validateUserPass {
687
    my $userDN = $FORM::username;
688
    my $userOrg = $FORM::organization;
689
    my $userPass = $FORM::password;
690
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
691
692
    my $errorMessage = "";
693
    my $ldap = Net::LDAP->new($ldapUrl) or die "$@";
694
    my $bindresult = $ldap->bind( version => 3, dn => $dname,
695
                                  password => $userPass );
696
    if ($bindresult->code) {
697
        $errorMessage = "Failed to log into metacat. Please check the username, organization and password entered";
698
        return $errorMessage;
699
    }
700
    return $errorMessage;
701
}
702
703
704
################################################################################
705
#
706
# utility function to determine if a paramter is defined and not an empty string
707
#
708
################################################################################
709
sub hasContent {
710
    my $param = shift;
711
712
    my $paramHasContent;
713
    if (!defined($param) || $param eq '') {
714
        $paramHasContent = 0;
715
    } else {
716
        $paramHasContent = 1;
717
    }
718
    return $paramHasContent;
719
}
720
721
722
################################################################################
723
#
724
# Create the XML document from the HTML form input
725
# returns the XML document as a string
726
#
727
################################################################################
728
sub createXMLDocument {
729
730
    my $orig  = "";
731
    my $role  = "associatedParty";
732
    my $creat = "";
733
    my $metaP = "";
734
    my $apart = "";
735
    my $cont  = "";
736
    my $publ  = "";
737
    my $dso   = "";
738
    my $gmt = gmtime($now);
739
    my $doc =  "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
740
741
   $doc .= "<eml:eml\n
742
                     \t packageId=\"docid\" system=\"knb\"\n
743
                     \t xmlns:eml=\"eml://ecoinformatics.org/eml-2.0.0\"\n
744
                     \t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n
745
                     \t xmlns:ds=\"eml://ecoinformatics.org/dataset-2.0.0\"\n
746
                     \t xmlns:stmml=\"http://www.xml-cml.org/schema/stmml\"\n
747
                     \t xsi:schemaLocation=\"eml://ecoinformatics.org/eml-2.0.0 eml.xsd\">\n";
748
749
    $doc .= "<!-- Person who filled in the catalog entry form: ";
750
    $doc .= "$FORM::providerGivenName $FORM::providerSurName -->\n";
751
    $doc .= "<!-- Form filled out at $gmt GMT -->\n";
752
    $doc .= "<dataset>\n";
753
754
    if (hasContent($FORM::identifier)) {
755
        $doc .= "<alternateIdentifier system=\"$FORM::site\">";
756
        $doc .= $FORM::identifier . "</alternateIdentifier>\n";
757
    }
758
759
    if (hasContent($FORM::title)) {
760
        $doc .= "<title>$FORM::title</title>\n";
761
    }
762
763
    if (hasContent($FORM::origNamelast0)) {
764 1977 jones
    $role = "creator";
765 1929 brooke
        $orig .= "<individualName>\n";
766
        $orig .= "<givenName>$FORM::origNamefirst0</givenName>\n";
767
        $orig .= "<surName>$FORM::origNamelast0</surName>\n";
768
        $orig .= "</individualName>\n";
769
    }
770
771
    if (hasContent($FORM::origNameOrg)) {
772
        $orig .= "<organizationName>$FORM::origNameOrg</organizationName>\n";
773
    }
774
775
    if (hasContent($FORM::origDelivery) || hasContent($FORM::origCity) ||
776
        (hasContent($FORM::origState   ) &&
777
        ($FORM::origState !~ "Select state here.")) ||
778
        hasContent($FORM::origStateOther) ||
779
        hasContent($FORM::origZIP ) || hasContent($FORM::origCountry)) {
780
        $orig .= "<address>\n";
781
782
        if (hasContent($FORM::origDelivery)) {
783
            $orig .= "<deliveryPoint>$FORM::origDelivery</deliveryPoint>\n";
784
        }
785
        if (hasContent($FORM::origCity)) {
786
            $orig .= "<city>$FORM::origCity</city>\n";
787
        }
788
789 1977 jones
    if (hasContent($FORM::origState) &&
790 1929 brooke
            ($FORM::origState !~ "Select state here.")) {
791
            $orig .= "<administrativeArea>$FORM::origState";
792
            $orig .= "</administrativeArea>\n";
793
        } elsif (hasContent($FORM::origStateOther)) {
794
            $orig .= "<administrativeArea>$FORM::origStateOther";
795
            $orig .= "</administrativeArea>\n";
796
        }
797
        if (hasContent($FORM::origZIP)) {
798
            $orig .= "<postalCode>$FORM::origZIP</postalCode>\n";
799
        }
800
        if (hasContent($FORM::origCountry)) {
801
            $orig .= "<country>$FORM::origCountry</country>\n";
802
        }
803
        $orig .= "</address>\n";
804
    }
805
806
    if (hasContent($FORM::origPhone)) {
807
        $orig .= "<phone>$FORM::origPhone</phone>\n";
808
    }
809
    if (hasContent($FORM::origFAX)) {
810
        $orig .= "<phone phonetype=\"Fax\">$FORM::origFAX</phone>\n";
811
    }
812
    if (hasContent($FORM::origEmail)) {
813
        $orig .= "<electronicMailAddress>$FORM::origEmail";
814
        $orig .= "</electronicMailAddress>\n";
815
    }
816
    $dso = "<$role>\n$orig</$role>\n";
817
818
    $creat .= $dso;
819
820
    if ($FORM::useOrigAddress) {
821
        # Add a contact originator like the original with a different role
822
            $cont .= "<contact>\n";
823 1977 jones
        $cont .= $orig;
824
        $cont .= "</contact>\n";
825 1929 brooke
    } else {
826
        $cont .= "<contact>\n";
827
828
        $cont .= "<individualName>\n";
829
        $cont .= "<givenName>$FORM::origNamefirstContact</givenName>\n";
830
        $cont .= "<surName>$FORM::origNamelastContact</surName>\n";
831
        $cont .= "</individualName>\n";
832
833 1977 jones
    if (hasContent($FORM::origNameOrgContact)) {
834
        $cont .= "<organizationName>$FORM::origNameOrgContact</organizationName>\n";
835
    }
836 1929 brooke
837
        if (hasContent($FORM::origDeliveryContact) ||
838
            hasContent($FORM::origCityContact) ||
839
            (hasContent($FORM::origStateContact) &&
840
            ($FORM::origStateContact !~ "Select state here.")) ||
841
            hasContent($FORM::origStateOtherContact) ||
842
            hasContent($FORM::origZIPContact) ||
843
            hasContent($FORM::origCountryContact)) {
844
            $cont .= "<address>\n";
845
            if (hasContent($FORM::origDeliveryContact)) {
846
                $cont .= "<deliveryPoint>$FORM::origDeliveryContact";
847
                $cont .= "</deliveryPoint>\n";
848
            }
849
            if (hasContent($FORM::origCityContact)) {
850
                $cont .= "<city>$FORM::origCityContact</city>\n";
851
            }
852
            if (hasContent($FORM::origStateContact) &&
853
                ($FORM::origStateContact !~ "Select state here.")) {
854
                $cont .= "<administrativeArea>$FORM::origStateContact";
855
                $cont .= "</administrativeArea>\n";
856
            } elsif (hasContent($FORM::origStateOtherContact)) {
857
                $cont .= "<administrativeArea>$FORM::origStateOtherContact";
858
                $cont .= "</administrativeArea>\n";
859
            }
860
            if (hasContent($FORM::origZIPContact)) {
861
                $cont .= "<postalCode>$FORM::origZIPContact</postalCode>\n";
862
            }
863
            if (hasContent($FORM::origCountryContact)) {
864
                $cont .= "<country>$FORM::origCountryContact</country>\n";
865
            }
866
            $cont .= "</address>\n";
867
        }
868
        if (hasContent($FORM::origPhoneContact)) {
869
            $cont .= "<phone>$FORM::origPhoneContact</phone>\n";
870
        }
871 1977 jones
    if (hasContent($FORM::origFAXContact)) {
872
        $cont .= "<phone phonetype=\"Fax\">$FORM::origFAXContact</phone>\n";
873
    }
874 1929 brooke
        if (hasContent($FORM::origEmailContact)) {
875
            $cont .= "<electronicMailAddress>$FORM::origEmailContact";
876
            $cont .= "</electronicMailAddress>\n";
877
        }
878 1977 jones
    $cont .= "</contact>\n";
879 1929 brooke
    }
880
881
    $metaP .= "<metadataProvider>\n";
882
    $metaP .= "<individualName>\n";
883
    $metaP .= "<givenName>$FORM::providerGivenName</givenName>\n";
884
    $metaP .= "<surName>$FORM::providerSurName</surName>\n";
885
    $metaP .= "</individualName>\n";
886
    $metaP .= "</metadataProvider>\n";
887
888
    # Additional originators
889
    foreach my $tmp (param()) {
890
        if ($tmp =~ /origNamelast/){
891
            my $tmp1 = $tmp;
892
            $tmp1 =~ s/origNamelast//; # get the index of the parameter 0 to 10
893
            if ( $tmp1 eq '1'
894
                 || $tmp1 eq '2'
895
                 || $tmp1 eq '3'
896
                 || $tmp1 eq '4'
897
                 || $tmp1 eq '5'
898
                 || $tmp1 eq '6'
899
                 || $tmp1 eq '7'
900
                 || $tmp1 eq '8'
901
                 || $tmp1 eq '9'
902
                 || $tmp1 eq '10'
903
                 ) {
904
905
                # do not generate XML for empty originator fields
906
                if (hasContent(param("origNamefirst" . $tmp1))) {
907
908 1977 jones
            my $add = "";
909
            $add .= "<individualName>\n";
910
            $add .= "<givenName>";
911
            $add .= param("origNamefirst" . $tmp1);
912
            $add .= "</givenName>\n";
913
            $add .= "<surName>";
914
            $add .= param("origNamelast" . $tmp1);
915
            $add .= "</surName>\n";
916
            $add .= "</individualName>\n";
917
918
            if(param("origRole" . $tmp1) eq "Originator"){
919
            $creat .= "<creator>\n";
920
            $creat .= $add;
921
            $creat .= "</creator>\n";
922 1929 brooke
            }
923 1977 jones
            elsif(param("origRole" . $tmp1) eq "Metadata Provider"){
924
            $metaP .= "<metadataProvider>\n";
925
            $metaP .= $add;
926
            $metaP .= "</metadataProvider>\n";
927
            }
928
            elsif((param("origRole" . $tmp1) eq "Publisher")  && ($publ eq "")){
929
            $publ .= "<publisher>\n";
930
            $publ .= $add;
931
            $publ .= "</publisher>\n";
932
            }
933
            else{
934
            $apart .= "<associatedParty>\n";
935
            $apart .= $add;
936
            $apart .= "<role>";
937
            $apart .= param("origRole" . $tmp1);
938
            $apart .= "</role>\n";
939
            $apart .= "</associatedParty>\n";
940
            }
941 1929 brooke
        }
942 1977 jones
            }
943
        }
944 1929 brooke
    }
945
946
    $creat .= "<creator>\n";
947
    $creat .= "<organizationName>$FORM::site</organizationName>\n";
948
    $creat .= "</creator>\n";
949
950
    $creat .= "<creator>\n";
951
    $creat .= "<organizationName>$organization</organizationName>\n";
952
    $creat .= "</creator>\n";
953
954 1977 jones
    if ($FORM::cfg eq 'nceas') {
955
        for (my $i = 0; $i < scalar(@FORM::wg); $i++) {
956
            $creat .= "<creator>\n";
957
            $creat .= "<organizationName>$FORM::wg[$i]</organizationName>\n";
958
            $creat .= "</creator>\n";
959
        }
960
    }
961 1929 brooke
    $doc .= $creat;
962
    $doc .= $metaP;
963
    $doc .= $apart;
964
965
    $doc .= "<abstract>\n";
966
    $doc .= "<para>$FORM::abstract</para>\n";
967
    $doc .= "</abstract>\n";
968
969
    # Keyword information
970
    foreach my $tmp (param()) {
971
        if ($tmp =~ /keyword/) {
972
            my $tmp1 = $tmp;
973
            $tmp1 =~ s/keyword//; # get the index of the parameter 0, ..., 10
974
            if ( $tmp1 =~ /[0-9]/ ){
975
                # don't generate xml for empty keyword fields
976
                # don't generate taxonomic keyword fields, those go in taxonomic coverage
977
                if (hasContent(param($tmp))) {
978
                    $doc .= "<keywordSet>\n";
979
                    $doc .= "<keyword ";
980
                    if (hasContent(param("kwType" . $tmp1)) &&
981
                       (param("kwType" . $tmp1) !~ "none") ) {
982
                         $doc .= "keywordType=\"";
983
                         $doc .= param("kwType" . $tmp1);
984
                         $doc .= "\"";
985
                    }
986
                    $doc .= ">";
987
                    $doc .= param("keyword" . $tmp1);
988
                    $doc .= "</keyword>\n";
989
                    $doc .= "<keywordThesaurus>";
990
                    $doc .= param("kwTh" . $tmp1);
991
                    $doc .= "</keywordThesaurus>\n";
992
                    $doc .= "</keywordSet>\n";
993
                }
994
            }
995
        }
996
    }
997
998
    if (hasContent($FORM::addComments)) {
999
        $doc .= "<additionalInfo>\n";
1000
        $doc .= "<para>$FORM::addComments</para>\n";
1001
        $doc .= "</additionalInfo>\n";
1002
    }
1003
1004
    if (hasContent($FORM::useConstraints) ||
1005
        hasContent($FORM::useConstraintsOther)) {
1006
        $doc .= "<intellectualRights>\n";
1007
        if (hasContent($FORM::useConstraints)) {
1008
            $doc .= "<para>$FORM::useConstraints</para>\n";
1009
        }
1010
        if (hasContent($FORM::useConstraintsOther)) {
1011
            $doc .= "<para>$FORM::useConstraintsOther</para>\n";
1012
        }
1013
        $doc .= "</intellectualRights>\n";
1014
    }
1015
1016
1017
    if (hasContent($FORM::url)) {
1018 1977 jones
    $doc .= "<distribution>\n";
1019 1929 brooke
        $doc .= "<online>\n";
1020 1977 jones
    $doc .= "<url>$FORM::url</url>\n";
1021
    $doc .= "</online>\n";
1022
    $doc .= "</distribution>\n";
1023 1929 brooke
    }
1024
1025
    $doc .= "<distribution>\n";
1026
    $doc .= "<offline>\n";
1027
    $doc .= "<mediumName>" . "$FORM::dataMedium   $FORM::dataMediumOther";
1028
    $doc .= "</mediumName>\n";
1029
    $doc .= "</offline>\n";
1030
    $doc .= "</distribution>\n";
1031
1032
    $doc .= "<coverage>\n";
1033
    $doc .= "<temporalCoverage>\n";
1034
1035
1036
    if (hasContent($FORM::endingYear)) {
1037 1977 jones
    $doc .= "<rangeOfDates>\n";
1038
    if (hasContent($FORM::beginningMonth)) {
1039
        my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
1040
             "JUL","AUG","SEP","OCT","NOV","DEC")
1041
        [$FORM::beginningMonth - 1];
1042
        $doc .= "<beginDate>\n";
1043
        $doc .= "<calendarDate>";
1044
        $doc .= "$FORM::beginningYear-$FORM::beginningMonth-$FORM::beginningDay";
1045
        $doc .= "</calendarDate>\n";
1046
        $doc .= "</beginDate>\n";
1047
    } else {
1048
        $doc .= "<beginDate>\n";
1049
        $doc .= "<calendarDate>";
1050
        $doc .= "$FORM::beginningYear";
1051
        $doc .= "</calendarDate>\n";
1052
        $doc .= "</beginDate>\n";
1053
    }
1054 1929 brooke
1055 1977 jones
    if (hasContent($FORM::endingMonth)) {
1056
        my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
1057
             "JUL","AUG","SEP","OCT","NOV","DEC")
1058
        [$FORM::endingMonth - 1];
1059
        $doc .= "<endDate>\n";
1060
        $doc .= "<calendarDate>";
1061
        $doc .= "$FORM::endingYear-$FORM::endingMonth-$FORM::endingDay";
1062
        $doc .= "</calendarDate>\n";
1063
        $doc .= "</endDate>\n";
1064 1929 brooke
    } else {
1065 1977 jones
        $doc .= "<endDate>\n";
1066
        $doc .= "<calendarDate>";
1067
        $doc .= "$FORM::endingYear";
1068
        $doc .= "</calendarDate>\n";
1069
        $doc .= "</endDate>\n";
1070 1929 brooke
    }
1071 1977 jones
    $doc .= "</rangeOfDates>\n";
1072
    } else {
1073
    $doc .= "<singleDateTime>\n";
1074
    if (hasContent($FORM::beginningMonth)) {
1075
        my $month = ("JAN","FEB","MAR","APR","MAY","JUN",
1076
             "JUL","AUG","SEP","OCT","NOV","DEC")
1077
        [$FORM::beginningMonth - 1];
1078
        $doc .= "<calendarDate>";
1079
        $doc .= "$FORM::beginningYear-$FORM::beginningMonth-$FORM::beginningDay";
1080
        $doc .= "</calendarDate>\n";
1081
    } else {
1082
        $doc .= "<calendarDate>";
1083
        $doc .= "$FORM::beginningYear";
1084
        $doc .= "</calendarDate>\n";
1085
    }
1086
    $doc .= "</singleDateTime>\n";
1087
    }
1088 1929 brooke
1089
    $doc .= "</temporalCoverage>\n";
1090
1091
    $doc .= "<geographicCoverage>\n";
1092 1983 jones
    $doc .= "<geographicDescription>$FORM::geogdesc</geographicDescription>\n";
1093 1929 brooke
    $doc .= "<boundingCoordinates>\n";
1094
1095
    # if the second latitude is missing, then set the second lat/long pair
1096
    # equal to the first this makes a point appear like a rectangle
1097
    if ($FORM::latDeg2 == 0 && $FORM::latMin2 == 0 && $FORM::latSec2 == 0) {
1098
1099
        $latDeg2 = $latDeg1;
1100
        $latMin2 = $latMin1;
1101
        $latSec2 = $latSec1;
1102
        $hemisphLat2 = $hemisphLat1;
1103
        $longDeg2 = $longDeg1;
1104
        $longMin2 = $longMin1;
1105
        $longSec2 = $longSec1;
1106
        $hemisphLong2 = $hemisphLong1;
1107
    }
1108
    else
1109
    {
1110
        $latDeg2 = $FORM::latDeg2;
1111
        $latMin2 = $FORM::latMin2;
1112
        $latSec2 = $FORM::latSec2;
1113
        $hemisphLat2 = $FORM::hemisphLat2;
1114
        $longDeg2 = $FORM::longDeg2;
1115
        $longMin2 = $FORM::longMin2;
1116
        $longSec2 = $FORM::longSec2;
1117
        $hemisphLong2 = $FORM::hemisphLong2;
1118
    }
1119
1120
1121
    my $hemisph;
1122
    $hemisph = ($hemisphLong1 eq "W") ? -1 : 1;
1123
    $doc .= "<westBoundingCoordinate>";
1124
    $doc .= $hemisph * ($longDeg1 + (60*$longMin1+$longSec1)/3600);
1125
    $doc .= "</westBoundingCoordinate>\n";
1126
1127
    $hemisph = ($hemisphLong2 eq "W") ? -1 : 1;
1128
    $doc .= "<eastBoundingCoordinate>";
1129
    $doc .= $hemisph * ($longDeg2 + (60*$longMin2+$longSec2)/3600);
1130
    $doc .= "</eastBoundingCoordinate>\n";
1131
1132
    $hemisph = ($hemisphLat1 eq "S") ? -1 : 1;
1133
    $doc .= "<northBoundingCoordinate>";
1134
    $doc .= $hemisph * ($latDeg1 + (60*$latMin1+$latSec1)/3600);
1135
    $doc .= "</northBoundingCoordinate>\n";
1136
1137
    $hemisph = ($hemisphLat2 eq "S") ? -1 : 1;
1138
    $doc .= "<southBoundingCoordinate>";
1139
    $doc .= $hemisph * ($latDeg2 + (60*$latMin2+$latSec2)/3600);
1140
    $doc .= "</southBoundingCoordinate>\n";
1141
1142
    $doc .= "</boundingCoordinates>\n";
1143
    $doc .= "</geographicCoverage>\n";
1144
1145 1984 jones
    # Write out the taxonomic coverage fields
1146
    my $foundFirstTaxon = 0;
1147
    foreach my $trn (param()) {
1148
        if ($trn =~ /taxonRankName/) {
1149
            my $taxIndex = $trn;
1150
            $taxIndex =~ s/taxonRankName//; # get the index of the parameter 0, ..., 10
1151
            my $trv = "taxonRankValue".$taxIndex;
1152
            if ( $taxIndex =~ /[0-9]/ ){
1153
                if (hasContent(param($trn)) && hasContent(param($trv))) {
1154
                    if (! $foundFirstTaxon) {
1155
                        $doc .= "<taxonomicCoverage>\n";
1156
                        $foundFirstTaxon = 1;
1157 1985 jones
                        if (hasContent($FORM::taxaAuth)) {
1158
                            $doc .= "<generalTaxonomicCoverage>".$FORM::taxaAuth."</generalTaxonomicCoverage>\n";
1159
                        }
1160 1984 jones
                    }
1161
                    $doc .= "<taxonomicClassification>\n";
1162
                    $doc .= "  <taxonRankName>".param($trn)."</taxonRankName>\n";
1163
                    $doc .= "  <taxonRankValue>".param($trv)."</taxonRankValue>\n";
1164
                    $doc .= "</taxonomicClassification>\n";
1165
                }
1166
            }
1167
        }
1168
    }
1169
    if ($foundFirstTaxon) {
1170
        $doc .= "</taxonomicCoverage>\n";
1171
    }
1172 1929 brooke
1173
    $doc .= "</coverage>\n";
1174
1175
    $doc .= $cont;
1176
    $doc .= $publ;
1177 1982 jones
1178
    if ((hasContent($FORM::methodTitle)) || scalar(@FORM::methodsPara) > 0) {
1179
        my $methods = "<methods><methodStep><description><section>\n";
1180
        if (hasContent($FORM::methodTitle)) {
1181
            $methods .= "<title>$FORM::methodTitle</title>\n";
1182
        }
1183
        for (my $i = 0; $i < scalar(@FORM::methodPara); $i++) {
1184
            $methods .= "<para>$FORM::methodPara[$i]</para>\n";
1185
        }
1186 1989 jones
        $methods .= "</section></description></methodStep>\n";
1187
        if (hasContent($FORM::studyExtentDescription)) {
1188
            $methods .= "<sampling><studyExtent><description>\n";
1189
            $methods .= "<para>$FORM::studyExtentDescription</para>\n";
1190
            $methods .= "</description></studyExtent>\n";
1191
            $methods .= "<samplingDescription>\n";
1192
            $methods .= "<para>$FORM::samplingDescription</para>\n";
1193
            $methods .= "</samplingDescription>\n";
1194
            $methods .= "</sampling>\n";
1195
        }
1196
        $methods .= "</methods>\n";
1197 1982 jones
        $doc .= $methods;
1198
    }
1199 1929 brooke
1200
    $doc .= "<access authSystem=\"knb\" order=\"denyFirst\">\n";
1201
    $doc .= "<allow>\n";
1202
    $doc .= "<principal>uid=obfsadmin,o=LTER,dc=ecoinformatics,dc=org</principal>\n";
1203
    $doc .= "<permission>all</permission>\n";
1204
    $doc .= "</allow>\n";
1205
    $doc .= "<allow>\n";
1206
    $doc .= "<principal>uid=$FORM::username,o=$FORM::organization,dc=ecoinformatics,dc=org</principal>\n";
1207
    $doc .= "<permission>all</permission>\n";
1208
    $doc .= "</allow>\n";
1209
    $doc .= "<allow>\n";
1210
    $doc .= "<principal>public</principal>\n";
1211
    $doc .= "<permission>read</permission>\n";
1212
    $doc .= "</allow>\n";
1213
    $doc .= "</access>\n";
1214
1215
    $doc .= "</dataset>\n</eml:eml>\n";
1216
1217
    return $doc;
1218
}
1219
1220
1221
################################################################################
1222
#
1223
# send an email message notifying the moderator of a new submission
1224
#
1225
################################################################################
1226
sub sendNotification {
1227
    my $identifier = shift;
1228
    my $mailhost = shift;
1229
    my $sender = shift;
1230
    my $recipient = shift;
1231
1232
    my $smtp = Net::SMTP->new($mailhost);
1233
    $smtp->mail($sender);
1234
    $smtp->to($recipient);
1235
1236
    my $message = <<"    ENDOFMESSAGE";
1237
    To: $recipient
1238
    From: $sender
1239
    Subject: New data submission
1240
1241
    Data was submitted to the data registry.
1242
    The identifying information for the new data set is:
1243
1244
    Identifier: $identifier
1245
    Title: $FORM::title
1246
    Submitter: $FORM::providerGivenName $FORM::providerSurName
1247
1248
    Please review the submmission and grant public read access if appropriate.
1249
    Thanks
1250
1251
    ENDOFMESSAGE
1252
    $message =~ s/^[ \t\r\f]+//gm;
1253
1254
    $smtp->data($message);
1255
    $smtp->quit;
1256
}
1257
1258
1259
################################################################################
1260
#
1261
# read the eml document and send back a form with values filled in.
1262
#
1263
################################################################################
1264
sub modifyData {
1265
1266
    # create metacat instance
1267
    my $metacat;
1268
    my $docid = $FORM::docid;
1269
    my $httpMessage;
1270
    my $doc;
1271
    my $xmldoc;
1272
    my $findType;
1273
    my $parser = XML::LibXML->new();
1274
    my @fileArray;
1275
    my $pushDoc;
1276
    my $alreadyInArray;
1277
    my $node;
1278
    my $response;
1279
    my $element;
1280
    my $tempfile;
1281
1282
    $metacat = Metacat->new();
1283
    if ($metacat) {
1284
        $metacat->set_options( metacatUrl => $metacatUrl );
1285
    } else {
1286
        #die "failed during metacat creation\n";
1287
        push(@errorMessages, "Failed during metacat creation.");
1288
    }
1289
1290
    $httpMessage = $metacat->read($docid);
1291
    $doc = $httpMessage->content();
1292
    $xmldoc = $parser->parse_string($doc);
1293
1294 1977 jones
    #$tempfile = $xslConvDir.$docid;
1295
    #push (@fileArray, $tempfile);
1296 1929 brooke
1297
    if ($xmldoc eq "") {
1298 1977 jones
        $error ="Error in parsing the eml document";
1299
        push(@errorMessages, $error);
1300 1929 brooke
    } else {
1301 1977 jones
        $findType = $xmldoc->findnodes('//dataset/identifier');
1302
        if ($findType->size() > 0) {
1303
            # This is a eml beta6 document
1304
            # Read the documents mentioned in triples also
1305
1306
            $findType = $xmldoc->findnodes('//dataset/triple');
1307
            if ($findType->size() > 0) {
1308
                foreach $node ($findType->get_nodelist) {
1309
                    $pushDoc = findValue($node, 'subject');
1310
1311
                    # If the file is already in @fileArray then do not add it
1312
                    $alreadyInArray = 0;
1313
                    foreach $element (@fileArray) {
1314
                        $tempfile = $tmpdir."/".$pushDoc;
1315
                        if ($element eq $pushDoc) {
1316
                            $alreadyInArray = 1;
1317
                        }
1318
                    }
1319
1320
                    if (!$alreadyInArray) {
1321
                        $tempfile = $tmpdir."/".$pushDoc;
1322
                        $response = "";
1323
                        $response = $metacat->read($pushDoc);
1324
                        if (! $response) {
1325
                            # could not read
1326
                            #push(@errorMessages, $response);
1327
                            push(@errorMessages, $metacat->getMessage());
1328
                            push(@errorMessages, "Failed during reading.\n");
1329
                        } else {
1330
                            my $xdoc = $response->content();
1331
                            #$tempfile = $xslConvDir.$pushDoc;
1332
                            open (TFILE,">$tempfile") ||
1333
                                die ("Cant open xml file... $tempfile\n");
1334
                            print TFILE $xdoc;
1335
                            close(TFILE);
1336
                            push (@fileArray, $tempfile);
1337
                        }
1338
                    }
1339
                }
1340
            }
1341 1929 brooke
1342 1977 jones
            # Read the main document.
1343 1929 brooke
1344 1977 jones
            $tempfile = $tmpdir."/".$docid; #= $xslConvDir.$docid;
1345
            open (TFILE,">$tempfile") || die ("Cant open xml file...\n");
1346
            print TFILE $doc;
1347
            close(TFILE);
1348
1349
            # Transforming beta6 to eml 2
1350
            my $xslt;
1351
            my $triplesheet;
1352
            my $results;
1353
            my $stylesheet;
1354
            my $resultsheet;
1355
1356
            $xslt = XML::LibXSLT->new();
1357
            #$tempfile = $xslConvDir."triple_info.xsl";
1358
            $tempfile = $tmpdir."/"."triple_info.xsl";
1359
1360
            $triplesheet = $xslt->parse_stylesheet_file($tempfile);
1361 1929 brooke
1362 1977 jones
            #$results = $triplesheet->transform($xmldoc, packageDir => "\'$tmpdir/\'", packageName => "\'$docid\'");
1363
            $results = $triplesheet->transform($xmldoc, packageDir => "\'$tmpdir/\'", packageName => "\'$docid\'");
1364 1929 brooke
1365 1977 jones
            #$tempfile = $xslConvDir."emlb6toeml2.xsl";
1366
            $tempfile = $tmpdir."/"."emlb6toeml2.xsl";
1367
            $stylesheet = $xslt->parse_stylesheet_file($tempfile);
1368
            $resultsheet = $stylesheet->transform($results);
1369
1370
            #$tempfile = "/usr/local/apache2/htdocs/xml/test.xml";;
1371
            #open (TFILE,">$tempfile") || die ("Cant open xml file...\n");
1372
            #print TFILE $stylesheet->output_string($resultsheet);
1373
            #close(TFILE);
1374 1929 brooke
1375 1977 jones
            getFormValuesFromEml2($resultsheet);
1376
1377
            # Delete the files written earlier.
1378
            unlink @fileArray;
1379 1929 brooke
1380 1977 jones
        } else {
1381
            getFormValuesFromEml2($xmldoc);
1382
        }
1383 1929 brooke
    }
1384
1385
    if (scalar(@errorMessages)) {
1386 1979 jones
        # if any errors, print them in the response template
1387
        $$templateVars{'status'} = 'failure';
1388
        $$templateVars{'errorMessages'} = \@errorMessages;
1389
        $error = 1;
1390
        $$templateVars{'function'} = "modification";
1391
        $$templateVars{'section'} = "Modification Status";
1392
        $template->process( $responseTemplate, $templateVars);
1393 1929 brooke
    } else {
1394
        $$templateVars{'form'} = 're_entry';
1395 1977 jones
        $template->process( $entryFormTemplate, $templateVars);
1396 1929 brooke
    }
1397
}
1398
1399 1977 jones
################################################################################
1400
#
1401
# Parse an EML 2.0.0 file and extract the metadata into perl variables for
1402
# processing and returning to the template processor
1403
#
1404
################################################################################
1405 1929 brooke
sub getFormValuesFromEml2 {
1406
1407
    my $doc = shift;
1408
    my $results;
1409
    my $error;
1410
    my $node;
1411
    my $tempResult;
1412
    my $tempNode;
1413
    my $aoCount = 1;
1414
    my $foundDSO;
1415
1416
    # find out the tag <alternateIdentifier>.
1417
    $results = $doc->findnodes('//dataset/alternateIdentifier');
1418 1977 jones
    if ($results->size() > 1) {
1419
        errMoreThanOne("alternateIdentifier");
1420 1929 brooke
    } else {
1421 1977 jones
        foreach $node ($results->get_nodelist) {
1422
            $$templateVars{'identifier'} = findValue($node, '../alternateIdentifier');
1423
        }
1424 1929 brooke
    }
1425
1426
    # find out the tag <title>.
1427
    $results = $doc->findnodes('//dataset/title');
1428 1977 jones
    if ($results->size() > 1) {
1429
        errMoreThanOne("title");
1430
    } elsif ($results->size() < 1) {
1431
        $error ="Following tag not found: title. Please use Morpho to edit this document";
1432
        push(@errorMessages, $error."\n");
1433
        #if ($DEBUG == 1){ print $error;}
1434 1929 brooke
    } else {
1435 1977 jones
        foreach $node ($results->get_nodelist) {
1436
            $$templateVars{'title'} = findValue($node, '../title');
1437
        }
1438 1929 brooke
    }
1439
1440
    # find out the tag <creator>.
1441
    $results = $doc->findnodes('//dataset/creator/individualName');
1442 1979 jones
    debug("Registry: Creators: ".$results->size());
1443 1977 jones
    if ($results->size() > 11) {
1444
        errMoreThanN("creator/individualName");
1445 1929 brooke
    } else {
1446 1977 jones
        foreach $node ($results->get_nodelist) {
1447
            dontOccur($node, "../positionName|../onlineURL|../userId",
1448
              "positionName, onlineURL, userId");
1449
1450
            dontOccur($node, "./saluation", "saluation");
1451
1452 1979 jones
            debug("Registry: Checking a creator in loop 1...");
1453 1977 jones
            $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress|../organizationName');
1454
            if($tempResult->size > 0) {
1455
                if($foundDSO == 0) {
1456
                    $foundDSO = 1;
1457 1929 brooke
1458 1979 jones
                    debug("Registry: Recording a creator in loop 1...");
1459 1977 jones
                    $$templateVars{'origNamefirst0'} = findValue($node, 'givenName');
1460
                    $$templateVars{'origNamelast0'} = findValue($node, 'surName');
1461
1462
                    my $tempResult2 = $node->findnodes('../address');
1463
                    if ($tempResult2->size > 1) {
1464
                        errMoreThanOne("address");
1465
                    } else {
1466
                        foreach my $tempNode2 ($tempResult2->get_nodelist) {
1467
                            $$templateVars{'origDelivery'} = findValue($tempNode2, 'deliveryPoint');
1468
                            $$templateVars{'origCity'} = findValue($tempNode2, 'city');
1469
                            $$templateVars{'origState'} = findValue($tempNode2, 'administrativeArea');
1470
                            $$templateVars{'origZIP'} = findValue($tempNode2, 'postalCode');
1471
                            $$templateVars{'origCountry'} = findValue($tempNode2, 'country');
1472
                        }
1473
                    }
1474
1475
                    my $tempResult3 = $node->findnodes('../phone');
1476
                    if ($tempResult3->size > 2) {
1477
                        errMoreThanN("phone");
1478
                    } else {
1479
                        foreach my $tempNode2 ($tempResult3->get_nodelist) {
1480
                            if ($tempNode2->hasAttributes()) {
1481
                                my @attlist = $tempNode2->attributes();
1482
                                if ($attlist[0]->value eq "Fax") {
1483
                                    $$templateVars{'origFAX'} = $tempNode2->textContent();
1484
                                } else {
1485
                                    $$templateVars{'origPhone'} = $tempNode2->textContent();
1486
                                }
1487
                            } else {
1488
                                $$templateVars{'origPhone'} = $tempNode2->textContent();
1489
                            }
1490
                        }
1491
                    }
1492
                    $$templateVars{'origEmail'} = findValue($node, '../electronicMailAddress');
1493
                    $$templateVars{'origNameOrg'} = findValue($node, '../organizationName');
1494
                } else {
1495
                    errMoreThanN("address, phone and electronicMailAddress");
1496
                }
1497
            }
1498
        }
1499
        foreach $node ($results->get_nodelist) {
1500 1979 jones
            debug("Registry: Checking a creator in loop 2...");
1501
            $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress|../organizationName');
1502 1977 jones
            if ($tempResult->size == 0) {
1503
                if ($foundDSO == 0) {
1504 1979 jones
                    debug("Registry: Recording a creator in loop 2 block A...");
1505 1977 jones
                    $foundDSO = 1;
1506
                    $$templateVars{'origNamefirst0'} = findValue($node, 'givenName');
1507
                    $$templateVars{'origNamelast0'} = findValue($node, 'surName');
1508
                    $$templateVars{'origNameOrg'} = findValue($node, '../organizationName');
1509
                } else {
1510 1979 jones
                    debug("Registry: Recording a creator in loop 2 block B...");
1511 1977 jones
                    $$templateVars{"origNamefirst$aoCount"} =  findValue($node, './givenName');
1512
                    $$templateVars{"origNamelast$aoCount"} =  findValue($node, './surName');
1513
                    $$templateVars{"origRole$aoCount"} = "Originator";
1514
                    $aoCount++;
1515
                }
1516
            }
1517
        }
1518 1929 brooke
    }
1519
1520
    $results = $doc->findnodes('//dataset/creator/organizationName');
1521 1979 jones
    my $wgroups = $doc->findnodes("//dataset/creator/organizationName[contains(text(),'(NCEAS ')]");
1522
    debug("Registry: Number Org: ".$results->size());
1523
    debug("Registry:  Number WG: ".$wgroups->size());
1524
    if ($results->size() - $wgroups->size() > 3) {
1525 1977 jones
        errMoreThanN("creator/organizationName");
1526 1929 brooke
    } else {
1527 1977 jones
        foreach $node ($results->get_nodelist) {
1528
            my $tempValue = findValue($node,'../organizationName');
1529
            $tempResult = $node->findnodes('../individualName');
1530
            if ($tempResult->size == 0 && $tempValue ne $organization) {
1531
                $$templateVars{'site'} = $tempValue;
1532
            }
1533
        }
1534 1979 jones
        if ($FORM::cfg == 'nceas') {
1535
            my @wg;
1536
            foreach $node ($results->get_nodelist) {
1537
                my $tempValue = findValue($node,'../organizationName');
1538
                $wg[scalar(@wg)] = $tempValue;
1539
            }
1540
            my $projects = getProjectList();
1541
            $$templateVars{'projects'} = $projects;
1542
            $$templateVars{'wg'} = \@wg;
1543
        }
1544 1929 brooke
    }
1545
1546
    $results = $doc->findnodes('//dataset/metadataProvider');
1547 1977 jones
    if ($results->size() > 11) {
1548
        errMoreThanN("metadataProvider");
1549 1929 brooke
    } else {
1550 1977 jones
        foreach $node ($results->get_nodelist) {
1551
            dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address",
1552
                "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in metadataProvider");
1553
1554
            $tempResult = $node->findnodes('./individualName');
1555
            if ($tempResult->size > 1) {
1556
                errMoreThanOne("metadataProvider/indvidualName");
1557
            } else {
1558
                foreach $tempNode ($tempResult->get_nodelist) {
1559
                    if ($$templateVars{'providerGivenName'} ne "") {
1560
                        $$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1561
                        $$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1562
                        $$templateVars{"origRole$aoCount"} = "Metadata Provider";
1563
                        $aoCount++;
1564
                    } else {
1565
                        $$templateVars{'providerGivenName'} =  findValue($tempNode, './givenName');
1566
                        $$templateVars{'providerSurName'} =  findValue($tempNode, './surName');
1567
                    }
1568
                }
1569
            }
1570
        }
1571 1929 brooke
    }
1572
1573
    $results = $doc->findnodes('//dataset/associatedParty');
1574 1977 jones
    if ($results->size() > 10) {
1575
        errMoreThanN("associatedParty");
1576 1929 brooke
    } else {
1577 1977 jones
        foreach $node ($results->get_nodelist) {
1578
            dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address",
1579
                "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in associatedParty");
1580
1581
            $tempResult = $node->findnodes('./individualName');
1582
            if ($tempResult->size > 1) {
1583
                errMoreThanOne("associatedParty/indvidualName");
1584
            } else {
1585
                foreach $tempNode ($tempResult->get_nodelist) {
1586
                    $$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1587
                    $$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1588
                    $$templateVars{"origRole$aoCount"} = findValue($tempNode, '../role');
1589
                    $aoCount++;
1590
                }
1591
            }
1592
        }
1593 1929 brooke
    }
1594
1595
    $results = $doc->findnodes('//dataset/publisher');
1596 1977 jones
    if ($results->size() > 10) {
1597
        errMoreThanN("publisher");
1598 1929 brooke
    } else {
1599 1977 jones
        foreach $node ($results->get_nodelist) {
1600
            dontOccur($node, "./organizationName|./positionName|./onlineURL|./userId|./electronicMailAddress|./phone|./address",
1601
                "organizationName, positionName, onlineURL, userId, electronicMailAddress, phone, address in associatedParty");
1602
1603
            $tempResult = $node->findnodes('./individualName');
1604
            if ($tempResult->size > 1) {
1605
                errMoreThanOne("publisher/indvidualName");
1606
            } else {
1607
                foreach $tempNode ($tempResult->get_nodelist) {
1608
                    $$templateVars{"origNamefirst$aoCount"} =  findValue($tempNode, './givenName');
1609
                    $$templateVars{"origNamelast$aoCount"} =  findValue($tempNode, './surName');
1610
                    $$templateVars{"origRole$aoCount"} = "Publisher";
1611
                    $aoCount++;
1612
                }
1613
            }
1614
        }
1615 1929 brooke
    }
1616
1617 1977 jones
    if ($aoCount > 11) {
1618
        errMoreThanN("Additional Originators");
1619 1929 brooke
    }
1620
1621
    dontOccur($doc, "./pubDate", "pubDate");
1622
    dontOccur($doc, "./language", "language");
1623
    dontOccur($doc, "./series", "series");
1624
1625
    $results = $doc->findnodes('//dataset/abstract');
1626 1977 jones
    if ($results->size() > 1) {
1627
        errMoreThanOne("abstract");
1628 1929 brooke
    } else {
1629 1977 jones
        foreach my $node ($results->get_nodelist) {
1630
            dontOccur($node, "./section", "section");
1631
            $$templateVars{'abstract'} = findValueNoChild($node, "para");
1632
        }
1633 1929 brooke
    }
1634
1635
    $results = $doc->findnodes('//dataset/keywordSet');
1636 1977 jones
    if ($results->size() > 10) {
1637
        errMoreThanN("keywordSet");
1638 1929 brooke
    } else {
1639 1977 jones
        my $count = 0;
1640
        foreach $node ($results->get_nodelist) {
1641
            $tempResult = $node->findnodes('./keyword');
1642
            if ($tempResult->size() > 1) {
1643
                errMoreThanOne("keyword");
1644
            } else {
1645
                foreach $tempNode ($tempResult->get_nodelist) {
1646
                    $$templateVars{"keyword$count"} = $tempNode->textContent();
1647
                    if ($tempNode->hasAttributes()) {
1648
                        my @attlist = $tempNode->attributes();
1649
                        $$templateVars{"kwType$count"} = $attlist[0]->value;
1650
                    }
1651
                 }
1652
            }
1653
            $$templateVars{"kwTh$count"} = findValue($node, "keywordThesaurus");
1654
            $count++;
1655
        }
1656
1657
        while ($count<11) {
1658
            $$templateVars{"kwType$count"} = "none";
1659
            $$templateVars{"kwTh$count"} = "none";
1660
            $count++;
1661
        }
1662 1929 brooke
    }
1663
1664
    $results = $doc->findnodes('//dataset/additionalInfo');
1665 1977 jones
    if ($results->size() > 1) {
1666
        errMoreThanOne("additionalInfo");
1667 1929 brooke
    } else {
1668 1977 jones
        foreach $node ($results->get_nodelist) {
1669
            dontOccur($node, "./section", "section");
1670
            $$templateVars{'addComments'} = findValueNoChild($node, "para");
1671
        }
1672 1929 brooke
    }
1673
1674
    $$templateVars{'useConstraints'} = "";
1675
    $results = $doc->findnodes('//dataset/intellectualRights');
1676 1977 jones
    if ($results->size() > 1) {
1677
        errMoreThanOne("intellectualRights");
1678 1929 brooke
    } else {
1679 1977 jones
        foreach $node ($results->get_nodelist) {
1680
            dontOccur($node, "./section", "section in intellectualRights");
1681 1929 brooke
1682 1977 jones
            $tempResult = $node->findnodes("para");
1683
            if ($tempResult->size > 2) {
1684
                   errMoreThanN("para");
1685
            } else {
1686
                foreach $tempNode ($tempResult->get_nodelist) {
1687
                    my $childNodes = $tempNode->childNodes;
1688
                    if ($childNodes->size() > 1) {
1689
                        $error ="The tag para in intellectualRights has children which cannot be shown using the form. Please use Morpho to edit this document";
1690
                        push(@errorMessages, $error);
1691
                        #if ($DEBUG == 1){ print $error."\n";}
1692
                    } else {
1693
                        #print $tempNode->nodeName().":".$tempNode->textContent();
1694
                        #print "\n";
1695
                        if ($$templateVars{'useConstraints'} eq "") {
1696
                            $$templateVars{'useConstraints'} = $tempNode->textContent();
1697
                        } else {
1698
                            $$templateVars{'useConstraintsOther'} = $tempNode->textContent();
1699
                        }
1700
                    }
1701
                }
1702
            }
1703
        }
1704 1929 brooke
    }
1705
1706
    $results = $doc->findnodes('//dataset/distribution/online');
1707 1977 jones
    if ($results->size() > 1) {
1708
        errMoreThanOne("distribution/online");
1709 1929 brooke
    } else {
1710 1977 jones
        foreach my $tempNode ($results->get_nodelist){
1711
            $$templateVars{'url'} = findValue($tempNode, "url");
1712
            dontOccur($tempNode, "./connection", "/distribution/online/connection");
1713
            dontOccur($tempNode, "./connectionDefinition", "/distribution/online/connectionDefinition");
1714
        }
1715 1929 brooke
    }
1716
1717
    $results = $doc->findnodes('//dataset/distribution/offline');
1718 1977 jones
    if ($results->size() > 1) {
1719
        errMoreThanOne("distribution/online");
1720 1929 brooke
    } else {
1721 1977 jones
        foreach my $tempNode ($results->get_nodelist) {
1722
            $$templateVars{'dataMedium'} = findValue($tempNode, "mediumName");
1723
            dontOccur($tempNode, "./mediumDensity", "/distribution/offline/mediumDensity");
1724
            dontOccur($tempNode, "./mediumDensityUnits", "/distribution/offline/mediumDensityUnits");
1725
            dontOccur($tempNode, "./mediumVolume", "/distribution/offline/mediumVolume");
1726
            dontOccur($tempNode, "./mediumFormat", "/distribution/offline/mediumFormat");
1727
            dontOccur($tempNode, "./mediumNote", "/distribution/offline/mediumNote");
1728
        }
1729 1929 brooke
    }
1730
1731
    dontOccur($doc, "./inline", "//dataset/distribution/inline");
1732
1733
    $results = $doc->findnodes('//dataset/coverage');
1734 1977 jones
    if ($results->size() > 1) {
1735
        errMoreThanOne("coverage");
1736 1929 brooke
    } else {
1737 1977 jones
        foreach $node ($results->get_nodelist) {
1738 1985 jones
            dontOccur($node, "./temporalCoverage/rangeOfDates/beginDate/time|./temporalCoverage/rangeOfDates/beginDate/alternativeTimeScale|./temporalCoverage/rangeOfDates/endDate/time|./temporalCoverage/rangeOfDates/endDate/alternativeTimeScale|./taxonomicCoverage/taxonomicSystem|./taxonomicCoverage/taxonomicClassification/commonName|./taxonomicCoverage/taxonomicClassification/taxonomicClassification|./geographicCoverage/datasetGPolygon|./geographicCoverage/boundingCoordinates/boundingAltitudes", "temporalCoverage/rangeOfDates/beginDate/time, /temporalCoverage/rangeOfDates/beginDate/alternativeTimeScale, /temporalCoverage/rangeOfDates/endDate/time, /temporalCoverage/rangeOfDates/endDate/alternativeTimeScale, /taxonomicCoverage/taxonomicSystem, /taxonomicCoverage/taxonomicClassification/commonName, /taxonomicCoverage/taxonomicClassification/taxonomicClassification, /geographicCoverage/datasetGPolygon, /geographicCoverage/boundingCoordinates/boundingAltitudes");
1739 1929 brooke
1740 1977 jones
            $tempResult = $node->findnodes('./temporalCoverage');
1741
            if ($tempResult->size > 1) {
1742
                   errMoreThanOne("temporalCoverage");
1743
            } else {
1744
                foreach $tempNode ($tempResult->get_nodelist) {
1745
                    my $x;
1746
                    my $y;
1747
                    my $z;
1748
                    my $tempdate = findValue($tempNode, "rangeOfDates/beginDate/calendarDate");
1749
                    ($x, $y, $z) = split("-", $tempdate);
1750
                    $$templateVars{'beginningYear'} = $x;
1751
                    $$templateVars{'beginningMonth'} = $y;
1752
                    $$templateVars{'beginningDay'} = $z;
1753
1754
                    $tempdate = findValue($tempNode, "rangeOfDates/endDate/calendarDate");
1755
                    ($x, $y, $z) = split("-", $tempdate);
1756
                    $$templateVars{'endingYear'} = $x;
1757
                    $$templateVars{'endingMonth'} = $y;
1758
                    $$templateVars{'endingDay'} = $z;
1759
1760
                    $tempdate = "";
1761
                    $tempdate = findValue($tempNode, "singleDateTime/calendarDate");
1762
                    if($tempdate ne ""){
1763
                        ($x, $y, $z) = split("-", $tempdate);
1764
                        $$templateVars{'beginningYear'} = $x;
1765
                        $$templateVars{'beginningMonth'} = $y;
1766
                        $$templateVars{'beginningDay'} = $z;
1767
                    }
1768
                }
1769
            }
1770 1929 brooke
1771 1977 jones
            $tempResult = $node->findnodes('./geographicCoverage');
1772
            if ($tempResult->size > 1) {
1773
                errMoreThanOne("geographicCoverage");
1774
            } else {
1775
                foreach $tempNode ($tempResult->get_nodelist) {
1776 1983 jones
                    my $geogdesc = findValue($tempNode, "geographicDescription");
1777
                    debug("Registry: geogdesc from xml is: $geogdesc");
1778
                    $$templateVars{'geogdesc'} = $geogdesc;
1779
                    my $coord = findValue($tempNode, "boundingCoordinates/westBoundingCoordinate");
1780 1977 jones
                    if ($coord > 0) {
1781
                        #print "+";
1782
                        $$templateVars{'hemisphLong1'} = "E";
1783
                    } else {
1784
                        #print "-";
1785
                        eval($coord = $coord * -1);
1786
                        $$templateVars{'hemisphLong1'} = "W";
1787
                    }
1788
                    eval($$templateVars{'longDeg1'} = int($coord));
1789
                    eval($coord = ($coord - int($coord))*60);
1790
                    eval($$templateVars{'longMin1'} = int($coord));
1791
                    eval($coord = ($coord - int($coord))*60);
1792
                    eval($$templateVars{'longSec1'} = int($coord));
1793
1794
                    $coord = findValue($tempNode, "boundingCoordinates/southBoundingCoordinate");
1795
                    if ($coord > 0) {
1796
                        #print "+";
1797
                        $$templateVars{'hemisphLat2'} = "N";
1798
                    } else {
1799
                        #print "-";
1800
                        eval($coord = $coord * -1);
1801
                        $$templateVars{'hemisphLat2'} = "S";
1802
                    }
1803
                    eval($$templateVars{'latDeg2'} = int($coord));
1804
                    eval($coord = ($coord - int($coord))*60);
1805
                    eval($$templateVars{'latMin2'} = int($coord));
1806
                    eval($coord = ($coord - int($coord))*60);
1807
                    eval($$templateVars{'latSec2'} = int($coord));
1808
1809
                    $coord = findValue($tempNode, "boundingCoordinates/northBoundingCoordinate");
1810
                    if ($coord > 0) {
1811
                        #print "+";
1812
                        $$templateVars{'hemisphLat1'} = "N";
1813
                    } else {
1814
                        #print "-";
1815
                        eval($coord = $coord * -1);
1816
                        $$templateVars{'hemisphLat1'} = "S";
1817
                    }
1818
                    eval($$templateVars{'latDeg1'} = int($coord));
1819
                    eval($coord = ($coord - int($coord))*60);
1820
                    eval($$templateVars{'latMin1'} = int($coord));
1821
                    eval($coord = ($coord - int($coord))*60);
1822
                    eval($$templateVars{'latSec1'} = int($coord));
1823
1824
                    $coord = findValue($tempNode, "boundingCoordinates/eastBoundingCoordinate");
1825
                    if ($coord > 0) {
1826
                        #print "+";
1827
                        $$templateVars{'hemisphLong2'} = "E";
1828
                    } else {
1829
                        #print "-";
1830
                        eval($coord = $coord * -1);
1831
                        $$templateVars{'hemisphLong2'} = "W";
1832
                    }
1833
                    eval($$templateVars{'longDeg2'} = int($coord));
1834
                    eval($coord = ($coord - int($coord))*60);
1835
                    eval($$templateVars{'longMin2'} = int($coord));
1836
                    eval($coord = ($coord - int($coord))*60);
1837
                    eval($$templateVars{'longSec2'} = int($coord));
1838
                }
1839
            }
1840 1984 jones
1841
            $tempResult = $node->findnodes('./taxonomicCoverage/taxonomicClassification');
1842
            my $taxonIndex = 0;
1843
            foreach $tempNode ($tempResult->get_nodelist) {
1844
                $taxonIndex++;
1845
                my $taxonRankName = findValue($tempNode, "taxonRankName");
1846
                my $taxonRankValue = findValue($tempNode, "taxonRankValue");
1847
                $$templateVars{"taxonRankName".$taxonIndex} = $taxonRankName;
1848
                $$templateVars{"taxonRankValue".$taxonIndex} = $taxonRankValue;
1849
            }
1850
            $$templateVars{'taxaCount'} = $taxonIndex;
1851 1985 jones
            my $taxaAuth = findValue($node, "./taxonomicCoverage/generalTaxonomicCoverage");
1852
            $$templateVars{'taxaAuth'} = $taxaAuth;
1853 1977 jones
        }
1854 1929 brooke
    }
1855
    dontOccur($doc, "./purpose", "purpose");
1856
    dontOccur($doc, "./maintenance", "maintnance");
1857
1858
    $results = $doc->findnodes('//dataset/contact/individualName');
1859 1977 jones
    if ($results->size() > 1) {
1860
        errMoreThanOne("contact/individualName");
1861 1929 brooke
    } else {
1862 1977 jones
        foreach $node ($results->get_nodelist) {
1863
            dontOccur($node, "../positionName|../onlineURL|../userId",
1864
              "positionName, onlineURL, userId in contact tag");
1865
            dontOccur($node, "./saluation", "saluation in contact tag");
1866
1867
            $tempResult = $node->findnodes('../address|../phone|../electronicmailAddress|../organizationName');
1868
            if ($tempResult->size > 0) {
1869
                $$templateVars{'origNamefirstContact'} = findValue($node, 'givenName');
1870
                $$templateVars{'origNamelastContact'} = findValue($node, 'surName');
1871
1872
                my $tempResult2 = $node->findnodes('../address');
1873
                if ($tempResult2->size > 1) {
1874
                    errMoreThanOne("address");
1875
                } else {
1876
                    foreach my $tempNode2 ($tempResult2->get_nodelist) {
1877
                        $$templateVars{'origDeliveryContact'} = findValue($tempNode2, 'deliveryPoint');
1878
                        $$templateVars{'origCityContact'} = findValue($tempNode2, 'city');
1879
                        $$templateVars{'origStateContact'} = findValue($tempNode2, 'administrativeArea');
1880
                        $$templateVars{'origZIPContact'} = findValue($tempNode2, 'postalCode');
1881
                        $$templateVars{'origCountryContact'} = findValue($tempNode2, 'country');
1882
                    }
1883
                }
1884
1885
                my $tempResult3 = $node->findnodes('../phone');
1886
                if ($tempResult3->size > 2) {
1887
                    errMoreThanN("phone");
1888
                } else {
1889
                    foreach my $tempNode2 ($tempResult3->get_nodelist) {
1890
                        if ($tempNode2->hasAttributes()) {
1891
                            my @attlist = $tempNode2->attributes();
1892
                            if ($attlist[0]->value eq "Fax") {
1893
                                $$templateVars{'origFAXContact'} = $tempNode2->textContent();
1894
                            } else {
1895
                                $$templateVars{'origPhoneContact'} = $tempNode2->textContent();
1896
                            }
1897
                        } else {
1898
                            $$templateVars{'origPhoneContact'} = $tempNode2->textContent();
1899
                        }
1900
                    }
1901
                }
1902
                $$templateVars{'origEmailContact'} = findValue($node, '../electronicMailAddress');
1903
                $$templateVars{'origNameOrgContact'} = findValue($node, '../organizationName');
1904
            } else {
1905
                $$templateVars{'origNamefirstContact'} = findValue($node, 'givenName');
1906
                $$templateVars{'origNamelastContact'} = findValue($node, 'surName');
1907
                $$templateVars{'origNameOrgContact'} = findValue($node, '../organizationName');
1908
            }
1909
        }
1910 1929 brooke
    }
1911
1912 1989 jones
    $results = $doc->findnodes(
1913
            '//dataset/methods/methodStep/description/section');
1914 1982 jones
    debug("Registry: Number methods: ".$results->size());
1915
    if ($results->size() > 1) {
1916
        errMoreThanN("methods/methodStep/description/section");
1917
    } else {
1918
1919
        my @methodPara;
1920
        foreach $node ($results->get_nodelist) {
1921
            my @children = $node->childNodes;
1922
            for (my $i = 0; $i < scalar(@children); $i++) {
1923
                debug("Registry: Method child loop ($i)");
1924
                my $child = $children[$i];
1925
                if ($child->nodeName eq 'title') {
1926
                    my $title = $child->textContent();
1927
                    debug("Registry: Method title ($title)");
1928
                    $$templateVars{'methodTitle'} = $title;
1929
                } elsif ($child->nodeName eq 'para') {
1930
                    my $para = $child->textContent();
1931
                    debug("Registry: Method para ($para)");
1932
                    $methodPara[scalar(@methodPara)] = $para;
1933
                }
1934
            }
1935
        }
1936
        if (scalar(@methodPara) > 0) {
1937
            $$templateVars{'methodPara'} = \@methodPara;
1938
        }
1939
    }
1940
1941 1989 jones
    $results = $doc->findnodes(
1942
            '//dataset/methods/sampling/studyExtent/description/para');
1943
    if ($results->size() > 1) {
1944
        errMoreThanN("methods/sampling/studyExtent/description/para");
1945
    } else {
1946
        foreach $node ($results->get_nodelist) {
1947
            my $studyExtentDescription = $node->textContent();
1948
            $$templateVars{'studyExtentDescription'} = $studyExtentDescription;
1949
        }
1950
    }
1951
1952
    $results = $doc->findnodes(
1953
            '//dataset/methods/sampling/samplingDescription/para');
1954
    if ($results->size() > 1) {
1955
        errMoreThanN("methods/sampling/samplingDescription/para");
1956
    } else {
1957
        foreach $node ($results->get_nodelist) {
1958
            my $samplingDescription = $node->textContent();
1959
            $$templateVars{'samplingDescription'} = $samplingDescription;
1960
        }
1961
    }
1962
1963
    dontOccur($doc, "//methodStep/citation", "methodStep/citation");
1964
    dontOccur($doc, "//methodStep/protocol", "methodStep/protocol");
1965
    dontOccur($doc, "//methodStep/instrumentation", "methodStep/instrumentation");
1966
    dontOccur($doc, "//methodStep/software", "methodStep/software");
1967
    dontOccur($doc, "//methodStep/subStep", "methodStep/subStep");
1968
    dontOccur($doc, "//methodStep/dataSource", "methodStep/dataSource");
1969
    dontOccur($doc, "//methods/qualityControl", "methods/qualityControl");
1970
1971
    dontOccur($doc, "//methods/sampling/spatialSamplingUnits", "methods/sampling/spatialSamplingUnits");
1972
    dontOccur($doc, "//methods/sampling/citation", "methods/sampling/citation");
1973 1929 brooke
    dontOccur($doc, "./pubPlace", "pubPlace");
1974
    dontOccur($doc, "./project", "project");
1975
1976
    dontOccur($doc, "./dataTable", "dataTable");
1977
    dontOccur($doc, "./spatialRaster", "spatialRaster");
1978
    dontOccur($doc, "./spatialVector", "spatialVector");
1979
    dontOccur($doc, "./storedProcedure", "storedProcedure");
1980
    dontOccur($doc, "./view", "view");
1981
    dontOccur($doc, "./otherEntity", "otherEntity");
1982
    dontOccur($doc, "./references", "references");
1983
1984
    dontOccur($doc, "//citation", "citation");
1985
    dontOccur($doc, "//software", "software");
1986
    dontOccur($doc, "//protocol", "protocol");
1987
    dontOccur($doc, "//additionalMetadata", "additionalMetadata");
1988
}
1989
1990
################################################################################
1991
#
1992
# Delete the eml file that has been requested for deletion.
1993
#
1994
################################################################################
1995
sub deleteData {
1996
    my $deleteAll = shift;
1997
1998
    # create metacat instance
1999
    my $metacat;
2000
    my $docid = $FORM::docid;
2001
2002
    $metacat = Metacat->new();
2003
    if ($metacat) {
2004
        $metacat->set_options( metacatUrl => $metacatUrl );
2005
    } else {
2006
        #die "failed during metacat creation\n";
2007
        push(@errorMessages, "Failed during metacat creation.");
2008
    }
2009
2010
    # Login to metacat
2011
    my $userDN = $FORM::username;
2012
    my $userOrg = $FORM::organization;
2013
    my $userPass = $FORM::password;
2014
    my $dname = "uid=$userDN,o=$userOrg,dc=ecoinformatics,dc=org";
2015
2016
    my $errorMessage = "";
2017
    my $response = $metacat->login($dname, $userPass);
2018
2019
    if (! $response) {
2020 1977 jones
    # Could not login
2021 1929 brooke
        push(@errorMessages, $metacat->getMessage());
2022
        push(@errorMessages, "Failed during login.\n");
2023
2024
    } else {
2025 1977 jones
    #Able to login - try to delete the file
2026 1929 brooke
2027 1977 jones
    my $parser;
2028
    my @fileArray;
2029
    my $httpMessage;
2030
    my $xmldoc;
2031
    my $doc;
2032
    my $pushDoc;
2033
    my $alreadyInArray;
2034
    my $findType;
2035 1929 brooke
        my $node;
2036 1977 jones
    my $response;
2037
    my $element;
2038 1929 brooke
2039 1977 jones
    push (@fileArray, $docid);
2040
    $parser = XML::LibXML->new();
2041 1929 brooke
2042
        $httpMessage = $metacat->read($docid);
2043 1977 jones
    $doc = $httpMessage->content();
2044
    $xmldoc = $parser->parse_string($doc);
2045 1929 brooke
2046 1977 jones
    if ($xmldoc eq "") {
2047
        $error ="Error in parsing the eml document";
2048
        push(@errorMessages, $error);
2049
    } else {
2050 1929 brooke
2051 1977 jones
        $findType = $xmldoc->findnodes('//dataset/identifier');
2052
        if($findType->size() > 0){
2053
        # This is a eml beta6 document
2054
        # Delete the documents mentioned in triples also
2055
2056
        $findType = $xmldoc->findnodes('//dataset/triple');
2057
        if($findType->size() > 0){
2058
            foreach $node ($findType->get_nodelist){
2059
            $pushDoc = findValue($node, 'subject');
2060
2061
            # If the file is already in the @fileArray then do not add it
2062
            $alreadyInArray = 0;
2063
            foreach $element (@fileArray){
2064
                if($element eq $pushDoc){
2065
                $alreadyInArray = 1;
2066
                }
2067
            }
2068
2069
            if(!$alreadyInArray){
2070
                # If not already in array then delete the file.
2071
                push (@fileArray, $pushDoc);
2072
                $response = $metacat->delete($pushDoc);
2073
2074
                if (! $response) {
2075
                # Could not delete
2076
                #push(@errorMessages, $response);
2077
                push(@errorMessages, $metacat->getMessage());
2078
                push(@errorMessages, "Failed during deleting $pushDoc. Please check if you are authorized to delete this document.\n");
2079
                }
2080
            }
2081
            }
2082
        }
2083
        }
2084 1929 brooke
    }
2085
2086 1977 jones
    # Delete the main document.
2087
    if($deleteAll){
2088
        $response = $metacat->delete($docid);
2089
        if (! $response) {
2090
        # Could not delete
2091
        #push(@errorMessages, $response);
2092
        push(@errorMessages, $metacat->getMessage());
2093
        push(@errorMessages, "Failed during deleting $docid. Please check if you are authorized to delete this document.\n");
2094
        }
2095
    }
2096
    }
2097
2098 1929 brooke
    if (scalar(@errorMessages)) {
2099 1977 jones
    # If any errors, print them in the response template
2100
    $$templateVars{'status'} = 'failure';
2101
    $$templateVars{'errorMessages'} = \@errorMessages;
2102
    $error = 1;
2103 1929 brooke
    }
2104
2105
    # Process the response template
2106
    if($deleteAll){
2107
2108 1977 jones
    $$templateVars{'function'} = "deleted";
2109
    $$templateVars{'section'} = "Deletion Status";
2110
    $template->process( $responseTemplate, $templateVars);
2111 1929 brooke
    }
2112
}
2113
2114
2115
################################################################################
2116
#
2117
# Do data validation and send the data to confirm data template.
2118
#
2119
################################################################################
2120
sub toConfirmData{
2121
    # Check if any invalid parameters
2122
2123
    my $invalidParams;
2124
    if (! $error) {
2125 1977 jones
    $invalidParams = validateParameters(0);
2126
    if (scalar(@$invalidParams)) {
2127
        $$templateVars{'status'} = 'failure';
2128
        $$templateVars{'invalidParams'} = $invalidParams;
2129
        $error = 1;
2130 1929 brooke
    }
2131 1977 jones
    }
2132 1929 brooke
2133
    if (! $error) {
2134 1977 jones
    # If no errors, then print out data in confirm Data template
2135 1929 brooke
2136 1977 jones
    $$templateVars{'providerGivenName'} = $FORM::providerGivenName;
2137
    $$templateVars{'providerSurName'} = $FORM::providerSurName;
2138
    if($FORM::site eq "Select your station here."){
2139
        $$templateVars{'site'} = "";
2140
    }else{
2141
        $$templateVars{'site'} = $FORM::site;
2142
    }
2143
    if($FORM::cfg eq "nceas"){
2144
        $$templateVars{'wg'} = \@FORM::wg;
2145
    }
2146
    $$templateVars{'identifier'} = $FORM::identifier;
2147
    $$templateVars{'title'} = $FORM::title;
2148
    $$templateVars{'origNamefirst0'} = $FORM::origNamefirst0;
2149
    $$templateVars{'origNamelast0'} = $FORM::origNamelast0;
2150
    $$templateVars{'origNameOrg'} = $FORM::origNameOrg;
2151
    # $$templateVars{'origRole0'} = $FORM::origRole0;
2152
    $$templateVars{'origDelivery'} = $FORM::origDelivery;
2153
    $$templateVars{'origCity'} = $FORM::origCity;
2154
    if($FORM::origState eq "Select State Here."){
2155
        $$templateVars{'origState'} = "";
2156
    }else{
2157
        $$templateVars{'origState'} = $FORM::origState;
2158
    }
2159
    $$templateVars{'origStateOther'} = $FORM::origStateOther;
2160
    $$templateVars{'origZIP'} = $FORM::origZIP;
2161
    $$templateVars{'origCountry'} = $FORM::origCountry;
2162
    $$templateVars{'origPhone'} = $FORM::origPhone;
2163
    $$templateVars{'origFAX'} = $FORM::origFAX;
2164
    $$templateVars{'origEmail'} = $FORM::origEmail;
2165
    $$templateVars{'useOrigAddress'} = $FORM::useOrigAddress;
2166
    if($FORM::useOrigAddress eq "on"){
2167
        $$templateVars{'origNamefirstContact'} = $FORM::origNamefirst0;
2168
        $$templateVars{'origNamelastContact'} = $FORM::origNamelast0;
2169
        $$templateVars{'origNameOrgContact'} = $FORM::origNameOrg;
2170
        $$templateVars{'origDeliveryContact'} = $FORM::origDelivery;
2171
        $$templateVars{'origCityContact'} = $FORM::origCity;
2172
        if($FORM::origState eq "Select State Here."){
2173
        $$templateVars{'origStateContact'} = "";
2174
        }else{
2175
        $$templateVars{'origStateContact'} = $FORM::origState;
2176
        }
2177
        $$templateVars{'origStateOtherContact'} = $FORM::origStateOther;
2178
        $$templateVars{'origZIPContact'} = $FORM::origZIP;
2179
        $$templateVars{'origCountryContact'} = $FORM::origCountry;
2180
        $$templateVars{'origPhoneContact'} = $FORM::origPhone;
2181
        $$templateVars{'origFAXContact'} = $FORM::origFAX;
2182
        $$templateVars{'origEmailContact'} = $FORM::origEmail;
2183
    }else{
2184
        $$templateVars{'origNamefirstContact'} = $FORM::origNamefirstContact;
2185
        $$templateVars{'origNamelastContact'} = $FORM::origNamelastContact;
2186
        $$templateVars{'origNameOrgContact'} = $FORM::origNameOrgContact;
2187
        $$templateVars{'origDeliveryContact'} = $FORM::origDeliveryContact;
2188
        $$templateVars{'origCityContact'} = $FORM::origCityContact;
2189
        if($FORM::origStateContact eq "Select State Here."){
2190
        $$templateVars{'origStateContact'} = "";
2191
        }else{
2192
        $$templateVars{'origStateContact'} = $FORM::origStateContact;
2193
        }
2194
        $$templateVars{'origStateOtherContact'} = $FORM::origStateOtherContact;
2195
        $$templateVars{'origZIPContact'} = $FORM::origZIPContact;
2196
        $$templateVars{'origCountryContact'} = $FORM::origCountryContact;
2197
        $$templateVars{'origPhoneContact'} = $FORM::origPhoneContact;
2198
        $$templateVars{'origFAXContact'} = $FORM::origFAXContact;
2199
        $$templateVars{'origEmailContact'} = $FORM::origEmailContact;
2200
    }
2201
    $$templateVars{'origNamefirst1'} = $FORM::origNamefirst1;
2202
    $$templateVars{'origNamelast1'} = $FORM::origNamelast1;
2203
    if($FORM::origNamefirst1 eq "" && $FORM::origNamelast1 eq ""){
2204
        $$templateVars{'origRole1'} = "";
2205
    }else{
2206
        $$templateVars{'origRole1'} = $FORM::origRole1;
2207
    }
2208
    $$templateVars{'origNamefirst2'} = $FORM::origNamefirst2;
2209
    $$templateVars{'origNamelast2'} = $FORM::origNamelast2;
2210
    if($FORM::origNamefirst2 eq "" && $FORM::origNamelast2 eq ""){
2211
        $$templateVars{'origRole2'} = "";
2212
    }else{
2213
        $$templateVars{'origRole2'} = $FORM::origRole2;
2214
    }
2215
    $$templateVars{'origNamefirst3'} = $FORM::origNamefirst3;
2216
    $$templateVars{'origNamelast3'} = $FORM::origNamelast3;
2217
    if($FORM::origNamefirst3 eq "" && $FORM::origNamelast3 eq ""){
2218
        $$templateVars{'origRole3'} = "";
2219
    }else{
2220
        $$templateVars{'origRole3'} = $FORM::origRole3;
2221
    }
2222
    $$templateVars{'origNamefirst4'} = $FORM::origNamefirst4;
2223
    $$templateVars{'origNamelast4'} = $FORM::origNamelast4;
2224
    if($FORM::origNamefirst4 eq "" && $FORM::origNamelast4 eq ""){
2225
        $$templateVars{'origRole4'} = "";
2226
    }else{
2227
        $$templateVars{'origRole4'} = $FORM::origRole4;
2228
    }
2229
    $$templateVars{'origNamefirst5'} = $FORM::origNamefirst5;
2230
    $$templateVars{'origNamelast5'} = $FORM::origNamelast5;
2231
    if($FORM::origNamefirst5 eq "" && $FORM::origNamelast5 eq ""){
2232
        $$templateVars{'origRole5'} = "";
2233
    }else{
2234
        $$templateVars{'origRole5'} = $FORM::origRole5;
2235
    }
2236
    $$templateVars{'origNamefirst6'} = $FORM::origNamefirst6;
2237
    $$templateVars{'origNamelast6'} = $FORM::origNamelast6;
2238
    if($FORM::origNamefirst6 eq "" && $FORM::origNamelast6 eq ""){
2239
        $$templateVars{'origRole6'} = "";
2240
    }else{
2241
        $$templateVars{'origRole6'} = $FORM::origRole6;
2242
    }
2243
    $$templateVars{'origNamefirst7'} = $FORM::origNamefirst7;
2244
    $$templateVars{'origNamelast7'} = $FORM::origNamelast7;
2245
    if($FORM::origNamefirst7 eq "" && $FORM::origNamelast7 eq ""){
2246
        $$templateVars{'origRole7'} = "";
2247
    }else{
2248
        $$templateVars{'origRole7'} = $FORM::origRole7;
2249
    }
2250
    $$templateVars{'origNamefirst8'} = $FORM::origNamefirst8;
2251
    $$templateVars{'origNamelast8'} = $FORM::origNamelast8;
2252
    if($FORM::origNamefirst8 eq "" && $FORM::origNamelast8 eq ""){
2253
        $$templateVars{'origRole8'} = "";
2254
    }else{
2255
        $$templateVars{'origRole8'} = $FORM::origRole8;
2256
    }
2257
    $$templateVars{'origNamefirst9'} = $FORM::origNamefirst9;
2258
    $$templateVars{'origNamelast9'} = $FORM::origNamelast9;
2259
    if($FORM::origNamefirst9 eq "" && $FORM::origNamelast9 eq ""){
2260
        $$templateVars{'origRole9'} = "";
2261
    }else{
2262
        $$templateVars{'origRole9'} = $FORM::origRole9;
2263
    }
2264
    $$templateVars{'origNamefirst10'} = $FORM::origNamefirst10;
2265
    $$templateVars{'origNamelast10'} = $FORM::origNamelast10;
2266
    if($FORM::origNamefirst10 eq "" && $FORM::origNamelast10 eq ""){
2267
        $$templateVars{'origRole10'} = "";
2268
    }else{
2269
        $$templateVars{'origRole10'} = $FORM::origRole10;
2270
    }
2271
    $$templateVars{'abstract'} = $FORM::abstract;
2272
    $$templateVars{'keyword0'} = $FORM::keyword0;
2273
    $$templateVars{'kwType0'} = $FORM::kwType0;
2274
    $$templateVars{'kwTh0'} = $FORM::kwTh0;
2275
    $$templateVars{'kwType1'} = $FORM::kwType1;
2276
    $$templateVars{'keyword1'} = $FORM::keyword1;
2277
    $$templateVars{'kwTh1'} = $FORM::kwTh1;
2278
    $$templateVars{'kwType2'} = $FORM::kwType2;
2279
    $$templateVars{'keyword2'} = $FORM::keyword2;
2280
    $$templateVars{'kwTh2'} = $FORM::kwTh2;
2281
    $$templateVars{'kwType3'} = $FORM::kwType3;
2282
    $$templateVars{'keyword3'} = $FORM::keyword3;
2283
    $$templateVars{'kwTh3'} = $FORM::kwTh3;
2284
    $$templateVars{'kwType4'} = $FORM::kwType4;
2285
    $$templateVars{'keyword4'} = $FORM::keyword4;
2286
    $$templateVars{'kwTh4'} = $FORM::kwTh4;
2287
    $$templateVars{'kwType5'} = $FORM::kwType5;
2288
    $$templateVars{'keyword5'} = $FORM::keyword5;
2289
    $$templateVars{'kwTh5'} = $FORM::kwTh5;
2290
    $$templateVars{'kwType6'} = $FORM::kwType6;
2291
    $$templateVars{'keyword6'} = $FORM::keyword6;
2292
    $$templateVars{'kwTh6'} = $FORM::kwTh6;
2293
    $$templateVars{'kwType7'} = $FORM::kwType7;
2294
    $$templateVars{'keyword7'} = $FORM::keyword7;
2295
    $$templateVars{'kwTh7'} = $FORM::kwTh7;
2296
    $$templateVars{'kwType8'} = $FORM::kwType8;
2297
    $$templateVars{'keyword8'} = $FORM::keyword8;
2298
    $$templateVars{'kwTh8'} = $FORM::kwTh8;
2299
    $$templateVars{'kwType9'} = $FORM::kwType9;
2300
    $$templateVars{'keyword9'} = $FORM::keyword9;
2301
    $$templateVars{'kwTh9'} = $FORM::kwTh9;
2302
    $$templateVars{'addComments'} = $FORM::addComments;
2303
    $$templateVars{'useConstraints'} = $FORM::useConstraints;
2304
    $$templateVars{'useConstraintsOther'} = $FORM::useConstraintsOther;
2305
    $$templateVars{'url'} = $FORM::url;
2306
    if($FORM::dataMedium eq "Select type of medium here."){
2307
        $$templateVars{'dataMedium'} = "";
2308
    }else{
2309
        $$templateVars{'dataMedium'} = $FORM::dataMedium;
2310
    }
2311
    $$templateVars{'dataMediumOther'} = $FORM::dataMediumOther;
2312
    $$templateVars{'beginningYear'} = $FORM::beginningYear;
2313
    $$templateVars{'beginningMonth'} = $FORM::beginningMonth;
2314
    $$templateVars{'beginningDay'} = $FORM::beginningDay;
2315
    $$templateVars{'endingYear'} = $FORM::endingYear;
2316
    $$templateVars{'endingMonth'} = $FORM::endingMonth;
2317
    $$templateVars{'endingDay'} = $FORM::endingDay;
2318 1983 jones
    $$templateVars{'geogdesc'} = $FORM::geogdesc;
2319 1977 jones
    $$templateVars{'useSiteCoord'} = $FORM::useSiteCoord;
2320
    $$templateVars{'latDeg1'} = $FORM::latDeg1;
2321
    $$templateVars{'latMin1'} = $FORM::latMin1;
2322
    $$templateVars{'latSec1'} = $FORM::latSec1;
2323
    $$templateVars{'hemisphLat1'} = $FORM::hemisphLat1;
2324
    $$templateVars{'longDeg1'} = $FORM::longDeg1;
2325
    $$templateVars{'longMin1'} = $FORM::longMin1;
2326
    $$templateVars{'longSec1'} = $FORM::longSec1;
2327
    $$templateVars{'hemisphLong1'} = $FORM::hemisphLong1;
2328
    $$templateVars{'latDeg2'} = $FORM::latDeg2;
2329
    $$templateVars{'latMin2'} = $FORM::latMin2;
2330
    $$templateVars{'latSec2'} = $FORM::latSec2;
2331
    $$templateVars{'hemisphLat2'} = $FORM::hemisphLat2;
2332
    $$templateVars{'longDeg2'} = $FORM::longDeg2;
2333
    $$templateVars{'longMin2'} = $FORM::longMin2;
2334
    $$templateVars{'longSec2'} = $FORM::longSec2;
2335
    $$templateVars{'hemisphLong2'} = $FORM::hemisphLong2;
2336 1984 jones
2337
    $$templateVars{'taxaCount'} = $FORM::taxaCount;
2338
    foreach my $trn (param()) {
2339
        if ($trn =~ /taxonRankName/) {
2340
            my $taxIndex = $trn;
2341
            $taxIndex =~ s/taxonRankName//; # get the index of the parameter 0, ..., 10
2342
            my $trv = "taxonRankValue".$taxIndex;
2343
            if ( $taxIndex =~ /[0-9]/ ){
2344
                if (hasContent(param($trn)) && hasContent(param($trv))) {
2345
                    debug("Registry processing taxon: $trn = ".param($trn)." $trv = ".param($trv));
2346
                    $$templateVars{$trn} = param($trn);
2347
                    $$templateVars{$trv} = param($trv);
2348
                }
2349
            }
2350
        }
2351
    }
2352 1985 jones
    $$templateVars{'taxaAuth'} = $FORM::taxaAuth;
2353 1984 jones
2354 1982 jones
    $$templateVars{'methodTitle'} = $FORM::methodTitle;
2355
    $$templateVars{'methodPara'} = \@FORM::methodPara;
2356 1989 jones
    $$templateVars{'studyExtentDescription'} = $FORM::studyExtentDescription;
2357
    $$templateVars{'samplingDescription'} = $FORM::samplingDescription;
2358 1977 jones
    $$templateVars{'docid'} = $FORM::docid;
2359 1929 brooke
2360 1977 jones
    $$templateVars{'section'} = "Confirm Data";
2361
    $template->process( $confirmDataTemplate, $templateVars);
2362 1929 brooke
2363 1977 jones
    } else{
2364
    # Errors from validation function. print the errors out using the response template
2365
    if (scalar(@errorMessages)) {
2366
        $$templateVars{'status'} = 'failure';
2367
        $$templateVars{'errorMessages'} = \@errorMessages;
2368
        $error = 1;
2369
    }
2370 1929 brooke
        # Create our HTML response and send it back
2371 1977 jones
    $$templateVars{'function'} = "submitted";
2372
    $$templateVars{'section'} = "Submission Status";
2373
    $template->process( $responseTemplate, $templateVars);
2374 1929 brooke
    }
2375
}
2376
2377
2378
################################################################################
2379
#
2380
# From confirm Data template - user wants to make some changes.
2381
#
2382
################################################################################
2383
sub confirmDataToReEntryData{
2384
    my @sortedSites;
2385
    foreach my $site (sort @sitelist) {
2386
        push(@sortedSites, $site);
2387
    }
2388
2389
    $$templateVars{'siteList'} = \@sortedSites;
2390
    $$templateVars{'section'} = "Re-Entry Form";
2391
2392
    $$templateVars{'providerGivenName'} = $FORM::providerGivenName;
2393
    $$templateVars{'providerSurName'} = $FORM::providerSurName;
2394
    $$templateVars{'site'} = $FORM::site;
2395 1982 jones
    if ($FORM::cfg eq "nceas") {
2396 1977 jones
        my $projects = getProjectList();
2397
        $$templateVars{'projects'} = $projects;
2398
        $$templateVars{'wg'} = \@FORM::wg;
2399
    }
2400 1929 brooke
    $$templateVars{'identifier'} = $FORM::identifier;
2401
    $$templateVars{'title'} = $FORM::title;
2402
    $$templateVars{'origNamefirst0'} = $FORM::origNamefirst0;
2403
    $$templateVars{'origNamelast0'} = $FORM::origNamelast0;
2404
    $$templateVars{'origNameOrg'} = $FORM::origNameOrg;
2405
 #   $$templateVars{'origRole0'} = $FORM::origRole0;
2406
    $$templateVars{'origDelivery'} = $FORM::origDelivery;
2407
    $$templateVars{'origCity'} = $FORM::origCity;
2408
    $$templateVars{'origState'} = $FORM::origState;
2409
    $$templateVars{'origStateOther'} = $FORM::origStateOther;
2410
    $$templateVars{'origZIP'} = $FORM::origZIP;
2411
    $$templateVars{'origCountry'} = $FORM::origCountry;
2412
    $$templateVars{'origPhone'} = $FORM::origPhone;
2413
    $$templateVars{'origFAX'} = $FORM::origFAX;
2414
    $$templateVars{'origEmail'} = $FORM::origEmail;
2415 1977 jones
    if ($FORM::useSiteCoord ne "") {
2416
        $$templateVars{'useOrigAddress'} = "CHECKED";
2417 1929 brooke
    }else{
2418 1977 jones
        $$templateVars{'useOrigAddress'} = $FORM::useOrigAddress;
2419 1929 brooke
    }
2420
    $$templateVars{'origNamefirstContact'} = $FORM::origNamefirstContact;
2421
    $$templateVars{'origNamelastContact'} = $FORM::origNamelastContact;
2422
    $$templateVars{'origNameOrgContact'} = $FORM::origNameOrgContact;
2423
    $$templateVars{'origDeliveryContact'} = $FORM::origDeliveryContact;
2424
    $$templateVars{'origCityContact'} = $FORM::origCityContact;
2425
    $$templateVars{'origStateContact'} = $FORM::origStateContact;
2426
    $$templateVars{'origStateOtherContact'} = $FORM::origStateOtherContact;
2427
    $$templateVars{'origZIPContact'} = $FORM::origZIPContact;
2428
    $$templateVars{'origCountryContact'} = $FORM::origCountryContact;
2429
    $$templateVars{'origPhoneContact'} = $FORM::origPhoneContact;
2430
    $$templateVars{'origFAXContact'} = $FORM::origFAXContact;
2431
    $$templateVars{'origEmailContact'} = $FORM::origEmailContact;
2432
    $$templateVars{'origNamefirst1'} = $FORM::origNamefirst1;
2433
    $$templateVars{'origNamelast1'} = $FORM::origNamelast1;
2434
    $$templateVars{'origRole1'} = $FORM::origRole1;
2435
    $$templateVars{'origNamefirst2'} = $FORM::origNamefirst2;
2436
    $$templateVars{'origNamelast2'} = $FORM::origNamelast2;
2437
    $$templateVars{'origRole2'} = $FORM::origRole2;
2438
    $$templateVars{'origNamefirst3'} = $FORM::origNamefirst3;
2439
    $$templateVars{'origNamelast3'} = $FORM::origNamelast3;
2440
    $$templateVars{'origRole3'} = $FORM::origRole3;
2441
    $$templateVars{'origNamefirst4'} = $FORM::origNamefirst4;
2442
    $$templateVars{'origNamelast4'} = $FORM::origNamelast4;
2443
    $$templateVars{'origRole4'} = $FORM::origRole4;
2444
    $$templateVars{'origNamefirst5'} = $FORM::origNamefirst5;
2445
    $$templateVars{'origNamelast5'} = $FORM::origNamelast5;
2446
    $$templateVars{'origRole5'} = $FORM::origRole5;
2447
    $$templateVars{'origNamefirst6'} = $FORM::origNamefirst6;
2448
    $$templateVars{'origNamelast6'} = $FORM::origNamelast6;
2449
    $$templateVars{'origRole6'} = $FORM::origRole6;
2450
    $$templateVars{'origNamefirst7'} = $FORM::origNamefirst7;
2451
    $$templateVars{'origNamelast7'} = $FORM::origNamelast7;
2452
    $$templateVars{'origRole7'} = $FORM::origRole7;
2453
    $$templateVars{'origNamefirst8'} = $FORM::origNamefirst8;
2454
    $$templateVars{'origNamelast8'} = $FORM::origNamelast8;
2455
    $$templateVars{'origRole8'} = $FORM::origRole8;
2456
    $$templateVars{'origNamefirst9'} = $FORM::origNamefirst9;
2457
    $$templateVars{'origNamelast9'} = $FORM::origNamelast9;
2458
    $$templateVars{'origRole9'} = $FORM::origRole9;
2459
    $$templateVars{'origNamefirst10'} = $FORM::origNamefirst10;
2460
    $$templateVars{'origNamelast10'} = $FORM::origNamelast10;
2461
    $$templateVars{'origRole10'} = $FORM::origRole10;
2462
    $$templateVars{'abstract'} = $FORM::abstract;
2463
    $$templateVars{'keyword0'} = $FORM::keyword0;
2464
    $$templateVars{'kwType0'} = $FORM::kwType0;
2465
    $$templateVars{'kwTh0'} = $FORM::kwTh0;
2466
    $$templateVars{'kwType1'} = $FORM::kwType1;
2467
    $$templateVars{'keyword1'} = $FORM::keyword1;
2468
    $$templateVars{'kwTh1'} = $FORM::kwTh1;
2469
    $$templateVars{'kwType2'} = $FORM::kwType2;
2470
    $$templateVars{'keyword2'} = $FORM::keyword2;
2471
    $$templateVars{'kwTh2'} = $FORM::kwTh2;
2472
    $$templateVars{'kwType3'} = $FORM::kwType3;
2473
    $$templateVars{'keyword3'} = $FORM::keyword3;
2474
    $$templateVars{'kwTh3'} = $FORM::kwTh3;
2475
    $$templateVars{'kwType4'} = $FORM::kwType4;
2476
    $$templateVars{'keyword4'} = $FORM::keyword4;
2477
    $$templateVars{'kwTh4'} = $FORM::kwTh4;
2478
    $$templateVars{'kwType5'} = $FORM::kwType5;
2479
    $$templateVars{'keyword5'} = $FORM::keyword5;
2480
    $$templateVars{'kwTh5'} = $FORM::kwTh5;
2481
    $$templateVars{'kwType6'} = $FORM::kwType6;
2482
    $$templateVars{'keyword6'} = $FORM::keyword6;
2483
    $$templateVars{'kwTh6'} = $FORM::kwTh6;
2484
    $$templateVars{'kwType7'} = $FORM::kwType7;
2485
    $$templateVars{'keyword7'} = $FORM::keyword7;
2486
    $$templateVars{'kwTh7'} = $FORM::kwTh7;
2487
    $$templateVars{'kwType8'} = $FORM::kwType8;
2488
    $$templateVars{'keyword8'} = $FORM::keyword8;
2489
    $$templateVars{'kwTh8'} = $FORM::kwTh8;
2490
    $$templateVars{'kwType9'} = $FORM::kwType9;
2491
    $$templateVars{'keyword9'} = $FORM::keyword9;
2492
    $$templateVars{'kwTh9'} = $FORM::kwTh9;
2493
    $$templateVars{'addComments'} = $FORM::addComments;
2494
    $$templateVars{'useConstraints'} = $FORM::useConstraints;
2495
    $$templateVars{'useConstraintsOther'} = $FORM::useConstraintsOther;
2496
    $$templateVars{'url'} = $FORM::url;
2497
    $$templateVars{'dataMedium'} = $FORM::dataMedium;
2498
    $$templateVars{'dataMediumOther'} = $FORM::dataMediumOther;
2499
    $$templateVars{'beginningYear'} = $FORM::beginningYear;
2500
    $$templateVars{'beginningMonth'} = $FORM::beginningMonth;
2501
    $$templateVars{'beginningDay'} = $FORM::beginningDay;
2502
    $$templateVars{'endingYear'} = $FORM::endingYear;
2503
    $$templateVars{'endingMonth'} = $FORM::endingMonth;
2504
    $$templateVars{'endingDay'} = $FORM::endingDay;
2505 1983 jones
    $$templateVars{'geogdesc'} = $FORM::geogdesc;
2506 1929 brooke
    if($FORM::useSiteCoord ne ""){
2507 1977 jones
    $$templateVars{'useSiteCoord'} = "CHECKED";
2508 1929 brooke
    }else{
2509 1977 jones
    $$templateVars{'useSiteCoord'} = "";
2510 1929 brooke
    }
2511
    $$templateVars{'latDeg1'} = $FORM::latDeg1;
2512
    $$templateVars{'latMin1'} = $FORM::latMin1;
2513
    $$templateVars{'latSec1'} = $FORM::latSec1;
2514
    $$templateVars{'hemisphLat1'} = $FORM::hemisphLat1;
2515
    $$templateVars{'longDeg1'} = $FORM::longDeg1;
2516
    $$templateVars{'longMin1'} = $FORM::longMin1;
2517
    $$templateVars{'longSec1'} = $FORM::longSec1;
2518
    $$templateVars{'hemisphLong1'} = $FORM::hemisphLong1;
2519
    $$templateVars{'latDeg2'} = $FORM::latDeg2;
2520
    $$templateVars{'latMin2'} = $FORM::latMin2;
2521
    $$templateVars{'latSec2'} = $FORM::latSec2;
2522
    $$templateVars{'hemisphLat2'} = $FORM::hemisphLat2;
2523
    $$templateVars{'longDeg2'} = $FORM::longDeg2;
2524
    $$templateVars{'longMin2'} = $FORM::longMin2;
2525
    $$templateVars{'longSec2'} = $FORM::longSec2;
2526
    $$templateVars{'hemisphLong2'} = $FORM::hemisphLong2;
2527 1985 jones
    $$templateVars{'taxaCount'} = $FORM::taxaCount;
2528
    foreach my $trn (param()) {
2529
        if ($trn =~ /taxonRankName/) {
2530
            my $taxIndex = $trn;
2531
            $taxIndex =~ s/taxonRankName//; # get the index of the parameter 0, ..., 10
2532
            my $trv = "taxonRankValue".$taxIndex;
2533
            if ( $taxIndex =~ /[0-9]/ ){
2534
                if (hasContent(param($trn)) && hasContent(param($trv))) {
2535
                    debug("Registry processing taxon: $trn = ".param($trn)." $trv = ".param($trv));
2536
                    $$templateVars{$trn} = param($trn);
2537
                    $$templateVars{$trv} = param($trv);
2538
                }
2539
            }
2540
        }
2541 1982 jones
    }
2542 1985 jones
    $$templateVars{'taxaAuth'} = $FORM::taxaAuth;
2543
    $$templateVars{'methodTitle'} = $FORM::methodTitle;
2544
    $$templateVars{'methodPara'} = \@FORM::methodPara;
2545 1989 jones
    $$templateVars{'studyExtentDescription'} = $FORM::studyExtentDescription;
2546
    $$templateVars{'samplingDescription'} = $FORM::samplingDescription;
2547 1929 brooke
    $$templateVars{'docid'} = $FORM::docid;
2548
2549
    $$templateVars{'form'} = 're_entry';
2550
    $template->process( $entryFormTemplate, $templateVars);
2551
}
2552
2553
2554
################################################################################
2555
#
2556
# check if there is multiple occurence of the given tag and find its value.
2557
#
2558
################################################################################
2559
2560
sub findValue {
2561
    my $node = shift;
2562
    my $value = shift;
2563
    my $result;
2564
    my $tempNode;
2565
2566
    $result = $node->findnodes("./$value");
2567 1977 jones
    if ($result->size > 1) {
2568
        errMoreThanOne("$value");
2569 1929 brooke
    } else {
2570 1977 jones
        foreach $tempNode ($result->get_nodelist){
2571
            #print $tempNode->nodeName().":".$tempNode->textContent();
2572
            #print "\n";
2573
            return $tempNode->textContent();
2574
        }
2575 1929 brooke
    }
2576
}
2577
2578
2579
################################################################################
2580
#
2581
# check if given tags has any children. if not return the value
2582
#
2583
################################################################################
2584
sub findValueNoChild {
2585
    my $node = shift;
2586
    my $value = shift;
2587
    my $tempNode;
2588
    my $childNodes;
2589
    my $result;
2590
    my $error;
2591
2592
    $result = $node->findnodes("./$value");
2593 1977 jones
    if($result->size > 1){
2594
       errMoreThanOne("$value");
2595
    } else {
2596
        foreach $tempNode ($result->get_nodelist) {
2597
            $childNodes = $tempNode->childNodes;
2598
            if ($childNodes->size() > 1) {
2599
                $error ="The tag $value has children which cannot be shown using the form. Please use Morpho to edit this document";
2600
                push(@errorMessages, $error);
2601
                #if ($DEBUG == 1){ print $error."\n";}
2602
            } else {
2603
                #print $tempNode->nodeName().":".$tempNode->textContent();
2604
                #print "\n";
2605
                return $tempNode->textContent();
2606
            }
2607
        }
2608
    }
2609 1929 brooke
}
2610
2611
2612
################################################################################
2613
#
2614
# check if given tags are children of given node.
2615
#
2616
################################################################################
2617
sub dontOccur {
2618
    my $node = shift;
2619
    my $value = shift;
2620
    my $errVal = shift;
2621
2622
    my $result = $node->findnodes("$value");
2623
    if($result->size > 0){
2624 1977 jones
        $error ="One of the following tags found: $errVal. Please use Morpho to edit this document";
2625
        push(@errorMessages, $error."\n");
2626
        #if ($DEBUG == 1){ print $error;}
2627 1929 brooke
    }
2628
}
2629
2630
2631
################################################################################
2632
#
2633
# print out error for more than one occurence of a given tag
2634
#
2635
################################################################################
2636
sub errMoreThanOne {
2637
    my $value = shift;
2638
    my $error ="More than one occurence of the tag $value found. Please use Morpho to edit this document";
2639
    push(@errorMessages, $error."\n");
2640
    # if ($DEBUG == 1){ print $error;}
2641
}
2642
2643
2644
################################################################################
2645
#
2646
# print out error for more than given number of occurences of a given tag
2647
#
2648
################################################################################
2649
sub errMoreThanN {
2650
    my $value = shift;
2651
    my $error ="More occurences of the tag $value found than that can be shown in the form. Please use Morpho to edit this document";
2652
    push(@errorMessages, $error);
2653
    #if ($DEBUG == 1){ print $error."\n";}
2654
}
2655
2656
2657
################################################################################
2658
#
2659
# convert coord to degrees, minutes and seconds form.
2660
#
2661
################################################################################
2662
#sub convertCoord {
2663
#    my $wx = shift;
2664
#    print $deg." ".$min." ".$sec;
2665
#    print "\n";
2666
#}
2667
2668
2669 1955 jones
################################################################################
2670
#
2671
# print debugging messages to stderr
2672
#
2673
################################################################################
2674
sub debug {
2675
    my $msg = shift;
2676
2677
    if ($debug) {
2678
        print STDERR "$msg\n";
2679
    }
2680
}
2681 1973 jones
2682
################################################################################
2683
#
2684
# get the list of projects
2685
#
2686
################################################################################
2687
sub getProjectList {
2688
2689 1993 jones
    use NCEAS::AdminDB;
2690
    my $admindb = NCEAS::AdminDB->new();
2691
    $admindb->connect($nceas_db, $nceas_db_user, $nceas_db_password);
2692
    my $projects = $admindb->getProjects();
2693
    #my $projects = getTestProjectList();
2694
    return $projects;
2695
}
2696 1973 jones
2697 1993 jones
################################################################################
2698
#
2699
# get a test list of projects for use only in testing where the NCEAS
2700
# admin db is not available.
2701
#
2702
################################################################################
2703
sub getTestProjectList {
2704 1973 jones
    # This block is for testing only!  Remove for production use
2705
    my @row1;
2706 1974 jones
    $row1[0] = 6000; $row1[1] = 'Andelman'; $row1[2] = 'Sandy'; $row1[3] = 'The very long and windy path to an apparent ecological conclusion: statistics lie';
2707 1973 jones
    my @row2;
2708 1974 jones
    $row2[0] = 7000; $row2[1] = 'Bascompte'; $row2[2] = 'Jordi'; $row2[3] = 'Postdoctoral Fellow';
2709 1980 jones
    my @row3;
2710
    $row3[0] = 7001; $row3[1] = 'Hackett'; $row3[2] = 'Edward'; $row3[3] = 'Sociology rules the world';
2711
    my @row4;
2712
    $row4[0] = 7002; $row4[1] = 'Jones'; $row4[2] = 'Matthew'; $row4[3] = 'Informatics rules the world';
2713
    my @row5;
2714
    $row5[0] = 7003; $row5[1] = 'Schildhauer'; $row5[2] = 'Mark'; $row5[3] = 'Excel rocks my world, assuming a, b, and c';
2715
    my @row6;
2716
    $row6[0] = 7004; $row6[1] = 'Rogers'; $row6[2] = 'Bill'; $row6[3] = 'Graduate Intern';
2717
    my @row7;
2718
    $row7[0] = 7005; $row7[1] = 'Zedfried'; $row7[2] = 'Karl'; $row7[3] = 'A multivariate analysis of thing that go bump in the night';
2719 1973 jones
    my @projects;
2720
    $projects[0] = \@row1;
2721
    $projects[1] = \@row2;
2722 1980 jones
    $projects[2] = \@row3;
2723
    $projects[3] = \@row4;
2724
    $projects[4] = \@row5;
2725
    $projects[5] = \@row6;
2726
    $projects[6] = \@row7;
2727 1973 jones
    return \@projects;
2728
}