Project

General

Profile

1
#!/usr/bin/perl
2

    
3
# This script is INCOMPLETE -- see TODO markers throughout
4

    
5
# This script loops through a list of metadata documents and associated data
6
# files, and for each one uploads the data file to metacat, puts the accession 
7
# number in the metadata, then inserts the metadata to metacat.
8
# Matt Jones February 14, 2008
9

    
10
use Metacat;
11
use XML::DOM;
12
use strict;
13

    
14
############################################################################
15
#
16
# MAIN program block
17
#
18
############################################################################
19

    
20
# check that the correct number or parameters are passed from the commandline
21
if (($#ARGV +1) != 1) {die "Usage: %./insert_fgdc.pl <metacat_url> \n\n";}
22
# Get the URL to the metacat server from the command line options
23
my ($url) = @ARGV; 
24
my $dn = 'uid=jones,o=NCEAS,dc=ecoinformatics,dc=org';
25
my $password = 'foobar';
26

    
27
# Open a metacat connection and login
28
my $metacat = openMetacatConnection($url);
29
loginToMetacat($metacat, $dn, $password);
30

    
31
# Read in the list of FGDC documents
32
my $dataDir = "/tmp/data";
33
# datalist key = data filename, value = metadata filename
34
my $datalist = getDataList($dataDir); 
35
my $datafilename;
36

    
37
# Loop through the list of datafiles
38
foreach $datafilename (keys %$datalist) {
39
    print("Processing...  $datafilename $$datalist{$datafilename}\n");
40

    
41
    # Upload the data file into Metacat, catching accession # errors
42
    my $dataId = uploadData($metacat, $datafilename);
43

    
44
    # Reference the accession # URL for the data file in the FGDC metadata
45
    my $metadata = createDataReference($dataId, $$datalist{$datafilename});
46

    
47
    # Insert the metadata file into Metacat, catching accession # errors
48
    my $metadataId = insertMetadata($metacat, $metadata );
49

    
50
    # Set access control rules for the metadata and data files
51
    setAccess($dataId);
52
    setAccess($metadataId);
53
}
54

    
55
exit(0);
56

    
57
############################################################################
58
#
59
# SUBROUTINES
60
#
61
############################################################################
62

    
63
#
64
# Create a connection to the metacat server
65
#
66
sub openMetacatConnection {
67
    my $url = shift;
68

    
69
    my $metacat = Metacat->new();
70
    if ($metacat) {
71
        $metacat->set_options( metacatUrl => $url );
72
    } else {
73
        die("Could not open connection to Metacat url: $url\n");
74
    }
75
    return $metacat;
76
}
77

    
78
#
79
# Login to the metacat server
80
#
81
sub loginToMetacat {
82
    my $metacat = shift;
83
    my $dn = shift;
84
    my $password = shift;
85

    
86
    my $response1 = $metacat->login($dn, $password);
87
    if (! $response1) {
88
        print $metacat->getMessage();
89
        print "Failed during login: metacat.\n";
90
    } else {
91
        print "Connected to metacat\n";
92
    }
93
}
94

    
95
#
96
# TODO
97
# Get a hashed list of datafile names and their associated metadata file names
98
#
99
sub getDataList {
100
    my $dataDir = shift;
101

    
102
    my %dataList = ("datafile_a"=>"metadatafile_ma", 
103
                    "datafile_b"=>"metadatafile_mb");
104

    
105
    return \%dataList;
106
}
107

    
108
#
109
# TODO
110
# Upload a data file to metacat, returning the identifier used
111
#
112
sub uploadData {
113
    my $metacat = shift;
114
    my $datafilename = shift;
115

    
116
    # TODO: Create a unique ID
117
    my $identifier;
118

    
119
    # TODO: Read in the data file from disk
120
    # my $data;
121

    
122
    # TODO: Do the metacat insertion as a MIME insertion
123
    #my $response = $metacat->upload($identifier, $data);
124

    
125
    # TODO: Check the insertion succeeded, if not possibly try again with new id
126
	my $message =  $metacat->getMessage();
127

    
128
    return $identifier;
129

    
130
    return $identifier;
131
}
132

    
133
#
134
# TODO
135
# Insert a reference to a data file into the FGDC metadata document
136
#
137
sub createDataReference {
138
    my $metacat = shift;
139
    my $dataId = shift;
140
    my $metadatafilename = shift;
141

    
142
    my $metadata;
143

    
144
    return $metadata;
145
}
146

    
147
#
148
# TODO
149
# Insert the metadata document into the Metacat database
150
#
151
sub insertMetadata {
152
    my $metacat = shift;
153
    my $metadatafilename = shift;
154

    
155
    # TODO: Create a unique ID
156
    my $identifier;
157

    
158
    # TODO: Read in the metadata file from disk
159
    # my $metadata;
160

    
161
    # Do the metacat insertion
162
    # my $response = $metacat->insert($identifier, $metadata);
163

    
164
    # TODO: Check the insertion succeeded, if not possibly try again with new id
165
	my $message =  $metacat->getMessage();
166

    
167
    return $identifier;
168
}
169

    
170
#
171
# TODO
172
# Set access control permissions on the file identified
173
#
174
sub setAccess {
175
    my $metacat = shift;
176
    my $identifier;
177

    
178
}
(6-6/12)