-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildEml.R
345 lines (317 loc) · 10.4 KB
/
buildEml.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
library("EML")
library("pracma")
library("rlist")
source('OracleConnection.R')
source('PostgresConnection.R')
thomas <- new(
"metadataProvider",
individualName = new ("individualName", givenName="Thomas", surName="Vandenberghe"),
electronicMail = "[email protected]",
organizationName = "Royal Belgian Institute for Natural Sciences",
positionName="Data manager",
userId = set_userId("http://orcid.org/0000-0002-9269-6548", directory =
"http://orcid.org/")
)
serge <- new(
"associatedParty",
individualName = new ("individualName", givenName="Serge", surName="Scory"),
electronicMail = "[email protected]",
organizationName = "Royal Belgian Institute for Natural Sciences",
positionName="Head of BMDC",
role = "contentProvider",
userId = set_userId("http://orcid.org/0000-0003-2692-8651", directory =
"http://orcid.org/")
)
patrick <- new(
"associatedParty",
individualName = new ("individualName", givenName="Patrick", surName="Semal"),
electronicMail = "[email protected]",
organizationName = "Royal Belgian Institute for Natural Sciences",
positionName="Head of Section Scientific Heritage",
role = "contentProvider"
)
contact <- new("contact",
electronicMail = "[email protected]",
organizationName = "Biodiversity and Ecological Data and Information Centre - Royal Belgian Institute for Natural Sciences")
oraresults <- oraresults(
"select
title_en,
null as title_nl,
null as title_fr,
abstract,
acronym,
null as name,
null as citation,
scope,
funding_agency,
institute_name,
institute_dept_abbrev,
boss,
boss_email,
boss_role,
subboss,
subboss_email,
subboss_role,
geographic_coverage,
min_lon,
max_lon,
min_lat,
max_lat,
start_date,
end_date,
pub_date,
class_taxonomic_coverage,
order_taxonomic_coverage,
keywords,
null as nb_spec,
null as ig_num,
null as id
from select_eml order by acronym"
)
pgresults <- pgresults(
"select
TITLE_EN as \"TITLE_EN\",
TITLE_NL as \"TITLE_NL\",
TITLE_FR as \"TITLE_FR\",
ABSTRACT as \"ABSTRACT\",
CODE AS \"ACRONYM\",
NAME as \"NAME\",
CITATION as \"CITATION\",
SCOPE as \"SCOPE\",
NULL AS \"FUNDING_AGENCY\",
INSTITUTE_NAME as \"INSTITUTE_NAME\",
INSTITUTE_DEPT_ABBREV as \"INSTITUTE_DEPT_ABBREV\",
BOSS as \"BOSS\",
BOSS_EMAIL as \"BOSS_EMAIL\",
BOSS_ROLE as \"BOSS_ROLE\",
SUBBOSS as \"SUBBOSS\",
SUBBOSS_EMAIL as \"SUBBOSS_EMAIL\",
SUBBOSS_ROLE as \"SUBBOSS_ROLE\",
GEOGRAPHIC_COVERAGE as \"GEOGRAPHIC_COVERAGE\",
MIN_LON as \"MIN_LON\",
MAX_LON as \"MAX_LON\",
MIN_LAT as \"MIN_LAT\",
MAX_LAT as \"MAX_LAT\",
START_DATE as \"START_DATE\",
END_DATE as \"END_DATE\",
NULL as \"PUB_DATE\",
CLASS_TAXONOMIC_COVERAGE as \"CLASS_TAXONOMIC_COVERAGE\",
ORDER_TAXONOMIC_COVERAGE as \"ORDER_TAXONOMIC_COVERAGE\",
KEYWORDS as \"KEYWORDS\",
NB_SPEC as \"NB_SPEC\",
IG_NUM as \"IG_NUM\",
ID as \"ID\"
from SELECT_EML_MARINE"
)
print ("working")
#data <- rbind(oraresults, pgresults, fill=TRUE)
for (el in pgresults) {
name = el$NAME
title_en = el$TITLE_EN
title_nl = el$TITLE_NL
title_fr = el$TITLE_FR
acronym = el$ACRONYM
citation_text = el$CITATION
scope = el$SCOPE
if (!is.na(acronym)) {
print(paste("processing ", acronym))
}
abstract_text = el$ABSTRACT
institute = el$INSTITUTE_NAME
dept = el$INSTITUTE_DEPT_ABBREV
boss_text = el$BOSS
subboss_text = el$SUBBOSS
boss_email = el$BOSS_EMAIL
subboss_email = el$SUBBOSS_EMAIL
boss_role = el$BOSS_ROLE
subboss_role = el$SUBBOSS_ROLE
start = el$START_DATE
stop = el$END_DATE
pub_date = el$PUB_DATE
w = el$MIN_LON
e = el$MAX_LON
n = el$MAX_LAT
s = el$MIN_LAT
geodesc = el$GEOGRAPHIC_COVERAGE
#as.na(boss)
#as.na(subboss)
if (nchar(el$CLASS_TAXONOMIC_COVERAGE) < nchar(el$ORDER_TAXONOMIC_COVERAGE)) {
taxodesc = el$CLASS_TAXONOMIC_COVERAGE
taxolevel = 'Class'
} else{
taxodesc = el$ORDER_TAXONOMIC_COVERAGE
taxolevel = 'Order'
}
if (!is.null(el$KEYWORDS)) {
keywords <- unlist(strsplit(el$KEYWORDS, ", "))
}
creators=list()
if (string_is_not_null_or_empty(boss_text)) {
boss <- new(
"creator",
individualName = boss_text,
organizationName = paste(institute, ' - ', dept)
)
citation_authors=boss_text;
#list.append(creators, boss)
#creators<-c(creators,boss)
if(!identical(creators[length(creators)],boss)) {creators[[length(creators)+1]] <- boss}
}
if (string_is_not_null_or_empty(subboss_text)) {
subboss <- new(
"creator",
individualName = subboss_text,
organizationName = paste(institute, ' - ', dept)
)
citation_authors=paste(citation_authors, subboss_text, sep = "")
#list.append(creators, subboss)
#creators<-c(creators,subboss)
if(!identical(creators[length(creators)],subboss)) {creators[[length(creators)+1]] <- subboss}
}
#print (subboss_text)
#print(boss_text)
#if(string_is_not_null_or_empty(subboss_text) && string_is_not_null_or_empty(boss_text) && !strcmp(subboss_text, boss_text)){
# creators <- list(subboss, boss)
#
# citation=paste("<citation>",boss,", ",subboss_text," (",substr(pub_date, 0, 4) ,") ",title_en," (",acronym,")","</citation>", sep = "")
#}else if(string_is_not_null_or_empty(boss_text)){
# creators <- list(boss)
# citation=paste("<citation>",boss," (",substr(pub_date, 0, 4) ,") ",title_en," (",acronym,")","</citation>", sep = "")
#}
if (scope == 'collection') {
associatedParty = patrick
}
else if (scope == 'BMDC'){
associatedParty = serge
geodesc='Belgian part of the North Sea (http://marineregions.org/mrgid/26567)'
}
do_stuff()
}
print("Program ended")
do_stuff = function() {
geographicCoverage <-
new (
"geographicCoverage",
geographicDescription = geodesc,
boundingCoordinates = new(
"boundingCoordinates",
westBoundingCoordinate = toString(w),
eastBoundingCoordinate = toString(e),
northBoundingCoordinate = toString(n),
southBoundingCoordinate = toString(s)
)
)
temporalCoverage <-
new ("temporalCoverage",
rangeOfDates = new(
"rangeOfDates",
beginDate = new("beginDate", calendarDate = start),
endDate = new("endDate", calendarDate = stop)
))
taxonomicGroups <- unlist(strsplit(el$ORDER_TAXONOMIC_COVERAGE, ", "))
taxonomicGroups[[length(taxonomicGroups)+1]] <- subboss
taxonomicClassification = new (
"taxonomicClassification",
taxonRankName = taxolevel,
taxonRankValue = taxodesc
)
taxonomicCoverage <-
new (
"taxonomicCoverage",
)
coverage <-
new (
"coverage",
geographicCoverage = geographicCoverage,
temporalCoverage = temporalCoverage,
taxonomicCoverage = taxonomicCoverage
)
keywordSet <-
c(new("keywordSet",
keyword <- keywords))
if (is.na(acronym)) {
title_en <-
new("title",
value = title_en,
lang = "en")
} else{
title_en <-
new("title",
value = paste(title_en, " (", acronym, ")", sep = ""),
lang = "en")
}
title_nl <- new("title", value = title_nl, lang = "nl")
title_fr <- new("title", value = title_fr, lang = "fr")
print(length(creators))
dataset <- new(
"dataset",
title = c(title_en, title_nl, title_fr),
creator = creators,
pubDate = as.character(format(Sys.Date(), '%Y-%m-%d')),
intellectualRights = "<para>This work is licensed under a <ulink url=\"http://creativecommons.org/licenses/by/4.0/legalcode\"><citetitle>Creative Commons Attribution (CC-BY) 4.0 License</citetitle></ulink>.</para>",
abstract = abstract_text, #= new("abstract",abstract_text),
alternateIdentifier = "",
language="eng",
associatedParty = associatedParty,
additionalInfo="",
distribution="",
metadataProvider=thomas,
keywordSet = keywordSet,
coverage = coverage,
contact = contact
)
eml <- new("eml",
packageId = "f0cda3bf-2619-425e-b8be-8deb6bc6094d",
# from uuid::UUIDgenerate(),
system = "uuid",
# type of identifier
dataset = dataset,
additionalMetadata=new ("metadata",create_additional_metadata())#create_additional_metadata()
)
filename1=paste("./",acronym,".xml", sep = "");
filename2=paste("./",acronym,"2.xml", sep = "");
filename3=paste("./",acronym,"3.xml", sep = "");
filename4=paste("./",acronym,"4.xml", sep = "");
if (!file.exists(filename1)) {
write_eml(eml, filename1)
replace_in_file(filename1)
}else if (!file.exists(filename2)) {
write_eml(eml, filename2)
replace_in_file(filename2)
}else if (!file.exists(filename3)) {
write_eml(eml, filename3)
replace_in_file(filename3)
}else{
write_eml(eml, filename4)
replace_in_file(filename4)
}
}
replace_in_file=function(filename){
tx <- readLines(filename)
tx <- gsub(pattern = "<", replace = "<", x = tx)
tx <- gsub(pattern = ">", replace = ">", x = tx)
writeLines(tx, con=filename)
}
string_is_not_null_or_empty=function(string){
return (!is.null(string) && string != '' && string != ' ' && !is.na(string))
}
create_additional_metadata= function() {
wrapper=c("<gbif>","</gbif>")
tm <- as.POSIXlt(Sys.time(), "UTC", "%Y-%m-%dT%H:%M:%S")
#2018-07-09T05:43:57.925+02:00
datestamp=paste("<dateStamp>",strftime(tm , "%Y-%m-%dT%H:%M:%S%z"),"</dateStamp>", sep = "")
result =paste("<gbif>",datestamp,"<hierarchyLevel>dataset</hierarchyLevel>",citation_text,"</gbif>", sep = "")
#<collection>
# <parentCollectionIdentifier>NO ID</parentCollectionIdentifier>
# <collectionIdentifier>MY ID</collectionIdentifier>
# <collectionName>This collection</collectionName>
# </collection>
# <collection>
# <parentCollectionIdentifier>MY ID</parentCollectionIdentifier>
# <collectionIdentifier>ITS ID</collectionIdentifier>
# <collectionName>Subcollection</collectionName>
# </collection>
# <specimenPreservationMethod>glycerin</specimenPreservationMethod>
# <dc:replaces>http://ipt.biodiversity.be/resource?id=test-chelicerata/v1.1.xml</dc:replaces>
return (result)
}