Project

General

Profile

1
#!/usr/bin/perl
2
#
3
#  '$RCSfile: transfer_emldocs_from_metacat1_to_metacat2.pl,v $'
4
#  Copyright: 2000 Regents of the University of California 
5
#
6
#   '$Author: sgarg $'
7
#     '$Date: 2004/04/09 23:12:46 $'
8
# '$Revision: 1.1 $' 
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
# This is a CGI application for inserting metadata documents into
26
# the Metacat database.  It utilizes the Metacat.pm module for most work.
27
# You can specify two metacats and a list of documents. This script will
28
# copy documents from one metacat to another.
29

    
30

    
31
use Metacat;
32

    
33
my $metacat1;
34
my $docid;
35
my $rev;
36
my $error = 0;
37
my $xmldoc;
38
my $xa;
39
my $response;
40

    
41
my $listname = "kruger_docids";
42

    
43
my $metacat1_url = "http://dataknp.sanparks.org/sanparks/metacat";
44
my $username = "uid=judithk,o=SANParks,dc=ecoinformatics,dc=org";
45
my $password = "xxxxxx";
46
#my $metacat1_url = "http://fred.msi.ucsb.edu/knb/metacat";
47
#my $username = "uid=leinfelder,o=NCEAS,dc=ecoinformatics,dc=org";
48
#my $password = "xxxxxx";
49

    
50
#used for inserting xml in the appropriate location
51
my $updatedcount = 0;
52
my $insertionindex;
53
#my $insertionmarker = "</abstract>";
54
my $insertionmarker = "</keywordSet>";
55
my $xmlsnippet = "";
56
$xmlsnippet .= "<keywordSet>";
57
#$xmlsnippet .= "<keywordThesaurus>location</keywordThesaurus>";
58
$xmlsnippet .= "<keyword>SANParks, South Africa</keyword>";
59
$xmlsnippet .= "<keyword>Kruger National Park, South Africa</keyword>";
60
$xmlsnippet .= "</keywordSet>";
61

    
62
$metacat1 = Metacat->new();
63

    
64
if ($metacat1) {
65
    $metacat1->set_options( metacatUrl => $metacat1_url);
66
} else {
67
    #die "failed during metacat creation\n";
68
    print "Failed during metacat1 creation.";
69
    $error = 1;
70
}
71

    
72
# Login to metacat
73
print "Connecting to metacat1..........\n";
74
my $response1 = $metacat1->login($username, $password);
75
if (! $response1) {
76
    print $metacat1->getMessage();
77
    print "Failed during login: metacat1.\n";
78
    $error = 2;
79
} else {
80
    print "Connected to metacat1\n";
81
}
82

    
83
if($error == 0){
84
    open(file,$listname) || die ("Couldn't open the file");
85
    while(<file>) {
86
	chomp();
87
	$docid = $_;
88

    
89
	print "Original docid: $docid\n";
90

    
91
	#get the document
92
	$xmldoc = $metacat1->read($docid);
93
	$xa = $xmldoc->content;
94

    
95
	#construct the next docid revision
96
	#three parts to the docid: scope.id.rev
97
	@docidparts = split(/\./, $docid);
98
	$rev = @docidparts[2];
99
	$rev = $rev + 1;
100
	print "next rev: $rev\n";
101
	@docidparts[2] = $rev;
102
	$docid = join('.', @docidparts);
103
	
104
	print "Revised docid: $docid\n";
105

    
106
	#check for the inserted data (maybe we did it already)
107
	$insertionindex = index($xa, $xmlsnippet);
108
	if ($insertionindex > 0) {
109
		print "Already added the xml snippet to this doc\n";
110
		next;
111
	}
112

    
113
	#insert the snippet
114
	$insertionindex = index($xa, $insertionmarker);
115
	if ($insertionindex > 0) {
116
		#we want it after the end of the marker
117
		$insertionindex += length($insertionmarker);
118

    
119
		#construct the new xml with inserted elements
120
		$xa = substr($xa, 0, $insertionindex) . $xmlsnippet . substr($xa, $insertionindex);
121

    
122
		print "\n----------\n";
123
		#print $xa;
124
		#print "\n----------\n";
125
		
126
		#send the updated document
127
		$response = $metacat1->update($docid, $xa);
128
		print $metacat1->getMessage();
129

    
130
		$updatedcount++;
131
	}
132
    }
133
print "Updated $updatedcount documents\n";
134
} else {
135
    print $error;
136
}
(12-12/12)