-
Notifications
You must be signed in to change notification settings - Fork 14
/
ZenodoRecord.R
1726 lines (1581 loc) · 74 KB
/
ZenodoRecord.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' ZenodoRecord
#' @docType class
#' @export
#' @keywords zenodo record
#' @return Object of \code{\link{R6Class}} for modelling an ZenodoRecord
#' @format \code{\link{R6Class}} object.
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
#'
ZenodoRecord <- R6Class("ZenodoRecord",
inherit = zen4RLogger,
private = list(
allowed_additional_title_types = c("alternative-title", "subtitle", "translated-title", "other"),
allowed_additional_description_types = c("abstract", "methods", "series-information", "table-of-contents", "technical-info", "other"),
allowed_role_types = c("contactperson", "datacollector", "datacurator", "datamanager",
"distributor", "editor", "funder", "hostinginstitution", "producer",
"projectleader", "projectmanager", "projectmember", "registrationagency",
"registrationauthority", "relatedperson", "researcher", "researchgroup",
"rightsholder", "supervisor", "sponsor", "workpackageleader",
"other"),
allowed_date_types = c("accepted", "available", "collected", "copyrighted", "created", "issued",
"other", "submitted", "updated", "valid", "withdrawn"),
allowed_identifier_schemes = c("ark", "arxiv", "bibcode", "doi", "ean13", "eissn", "handle", "igsn", "isbn",
"issn", "istc", "lissn", "lsid", "pubmed id", "purl", "upc", "url", "urn", "w3id", "other"),
allowed_relation_types = c("iscitedby", "cites", "issupplementto", "issupplementedby",
"iscontinuedby", "continues", "isdescribedby", "describes", "hasmetadata",
"ismetadatafor", "isnewversionof", "ispreviousversionof", "ispartof",
"haspart", "isreferencedby", "references", "isdocumentedby",
"documents", "iscompiledby", "compiles", "isvariantformof", "isoriginalformof",
"isidenticalto", "isalternateidentifier", "isreviewedby", "reviews",
"isderivedfrom", "issourceof", "requires", "isrequiredby", "isobsoletedby",
"obsoletes"),
export_formats = c("BibTeX","CSL","DataCite","DublinCore","DCAT","JSON","JSON-LD","GeoJSON","MARCXML"),
getExportFormatExtension = function(format){
switch(format,
"BibTeX" = "bib",
"CSL" = "json",
"DataCite" ="xml",
"DublinCore" = "xml",
"DCAT" = "rdf",
"JSON" = "json",
"JSON-LD" = "json",
"GeoJSON" = "json",
"MARCXML" = "xml"
)
},
fromList = function(obj){
self$created = obj$created
self$updated = obj$updated
self$revision_id = obj$revision_id
self$status = obj$status
self$is_draft = obj$is_draft
self$is_published = obj$is_published
self$versions = obj$versions
#invenio model
self$access = obj$access
self$links = obj$links
self$files = lapply(obj$files$entries, function(file){
list(
filename = if(!is.null(file$filename)) file$filename else file$key,
filesize = if(!is.null(file$filesize)) file$filesize else file$size,
checksum = if(!startsWith(file$checksum, "md5:")) file$checksum else unlist(strsplit(file$checksum, "md5:"))[2],
download = if(!is.null(file$links$download)) file$links$download else file.path(self$links$self, sprintf("files/%s/content", file$key))
)
})
self$id = obj$id
self$metadata = obj$metadata
#special metadata treatments
resource_type = self$metadata$resource_type
if(!is.null(resource_type)){
resource_type_id = resource_type$id
self$metadata$resource_type = list(id = resource_type_id)
}
self$pids = obj$pids
self$parent = obj$parent
#zen4R specific fields
if(!is.null(obj$stats)) self$stats = data.frame(obj$stats)
}
),
public = list(
#' @field created record creation date
created = NULL,
#' @field updated record update date
updated = NULL,
#' @field revision_id revision id
revision_id = NULL,
#' @field is_draft is draft
is_draft = NULL,
#' @field is_published is published
is_published = NULL,
#' @field status record status
status = NULL,
#' @field versions versions
versions = NULL,
#' @field access access policies
access = list(
record = "public",
files = "public"
),
#' @field files list of files associated to the record
files = list(),
#' @field id record id
id = NULL,
#' @field links list of links associated to the record
links = list(),
#' @field metadata metadata elements associated to the record
metadata = list(),
#' @field parent parent record
parent = NULL,
#' @field pids pids
pids = list(),
#zen4R specific fields
#' @field stats stats
stats = NULL,
#' @description method is used to instantiate a \code{\link{ZenodoRecord}}
#' @param obj an optional list object to create the record
#' @param logger a logger to print log messages. It can be either NULL, "INFO" (with minimum logs),
#' or "DEBUG" (for complete curl http calls logs)
initialize = function(obj = NULL, logger = "INFO"){
super$initialize(logger = logger)
if(!is.null(obj)) private$fromList(obj)
},
#zen4R specific methods
#---------------------------------------------------------------------------
#' @description Get record statistics
#' @return statistics as \code{data.frame}
getStats = function(){
return(self$stats)
},
#Invenio RDM API new methods
#---------------------------------------------------------------------------
#ID methods
#---------------------------------------------------------------------------
#'@description Get the record Id
#'@return the Id, object of class \code{character}
getId = function(){
return(self$id)
},
#'@description Get the parent record Id
#'@return the parent Id, object of class \code{character}
getParentId = function(){
return(self$parent$id)
},
#'@description Get the concept record Id
#'@return the concept Id, object of class \code{character}
getConceptId = function(){
self$getParentId()
},
#DOI methods
#---------------------------------------------------------------------------
#' @description Set the DOI. This method can be used if a DOI has been already assigned outside Zenodo.
#' @param doi DOI to set for the record
#' @param provider DOI provider
#' @param client DOI client
setDOI = function(doi, provider = NULL, client = NULL){
self$pids = list(doi = list(identifier = doi, provider = provider, client = client))
},
#'@description Get the record DOI.
#'@return the DOI, object of class \code{character}
getDOI = function(){
return(self$pids$doi$identifier)
},
#' @description Get the concept (generic) DOI. The concept DOI is a generic DOI
#' common to all versions of a Zenodo record.
#' @return the concept DOI, object of class \code{character}
getConceptDOI = function(){
conceptdoi = NULL
doi <- self$pids$doi$identifier
if(!is.null(doi)) if(regexpr("zenodo", doi)>0){
doi_parts <- unlist(strsplit(doi, "zenodo."))
conceptdoi <- paste0(doi_parts[1], "zenodo.", self$getConceptId())
}
return(conceptdoi)
},
#Access methods
#---------------------------------------------------------------------------
#'@description Set the access policy for record, among values "public" (default) or "restricted"
#'In Zenodo, in principle, the access policy 'restricted' is not available for records.
#'@param access access policy ('public' or 'restricted')
setAccessPolicyRecord = function(access = c("public","restricted")){
self$access$record = access
},
#'@description Set the access policy for files, among values "public" (default) or "restricted"
#'@param access access policy ('public' or 'restricted')
setAccessPolicyFiles = function(access = c("public","restricted")){
self$access$files = access
},
#'@description Set access policy embargo options
#'@param active whether embargo is active or not. Default is \code{FALSE}
#'@param until embargo date, object of class \code{Date}. Default is \code{NULL}. Must be provided if embargo is active
#'@param reason embargo reason, object of class \code{character}. Default is an empty string
setAccessPolicyEmbargo = function(active = FALSE, until = NULL, reason = ""){
if(!is.null(until)) if(!is(until, "Date")) stop("Argument 'until' should be of class 'Date'")
self$access$embargo = list(active = active, until = until, reason = reason)
},
#Resource type methods
#---------------------------------------------------------------------------
#' @description Set the resource type (mandatory).
#' @param resourceType record resource type
setResourceType = function(resourceType){
zenodo = ZenodoManager$new()
zen_resourcetype <- zenodo$getResourceTypeById(resourceType)
if(is.null(zen_resourcetype)){
errorMsg <- sprintf("Resource type with id '%s' doesn't exist in Zenodo", resourceType)
self$ERROR(errorMsg)
stop(errorMsg)
}
self$metadata$resource_type <- list(id = zen_resourcetype$id)
},
#' @description Set the upload type (mandatory). Deprecated since zen4R 1.0
#' @param uploadType record upload type among the following values: 'publication', 'poster',
#' 'presentation', 'dataset', 'image', 'video', 'software', 'lesson', 'physicalobject', 'other'
setUploadType = function(uploadType){
warnMsg = "Method 'setUploadType' is deprecated, please use 'setResourceType'"
self$WARN(warnMsg)
},
#' @description Set the publication type (mandatory if upload type is 'publication'). Deprecated since zen4R 1.0
#' @param publicationType record publication type among the following values: 'annotationcollection', 'book',
#' 'section', 'conferencepaper', 'datamanagementplan', 'article', 'patent', 'preprint', 'deliverable', 'milestone',
#' 'proposal', 'report', 'softwaredocumentation', 'taxonomictreatment', 'technicalnote', 'thesis', 'workingpaper',
#' 'other'
setPublicationType = function(publicationType){
warnMsg = "Method 'setPublicationType' is deprecated, please use 'setResourceType'"
self$WARN(warnMsg)
},
#' @description Set the image type (mandatory if image type is 'image'). Deprecated since zen4R 1.0
#' @param imageType record publication type among the following values: 'figure','plot',
#' 'drawing','diagram','photo', or 'other'
setImageType = function(imageType){
warnMsg = "Method 'setImageType' is deprecated, please use 'setImageType'"
self$WARN(warnMsg)
},
#Publication-related methods
#---------------------------------------------------------------------------
#' @description Set the publisher
#' @param publisher publisher object of class \code{character}
setPublisher = function(publisher){
self$metadata$publisher = publisher
},
#' @description Set the publication date. For more information on the accepted format,
#' please check https://inveniordm.docs.cern.ch/reference/metadata/#publication-date-1
#' @param publicationDate object of class \code{character}
setPublicationDate = function(publicationDate){
self$metadata$publication_date <- publicationDate
},
#' @description Add date
#' @param date date
#' @param type type of date, among following values: 'accepted', 'available', 'collected', 'copyrighted',
#' 'created', 'issued', 'other', 'submitted', 'updated', 'valid', 'withdrawn'
#' @param description free text, specific information about the date
addDate = function(date, type, description = NULL){
if(!type %in% private$allowed_date_types){
errMsg = sprintf("Date type should be among following possible values: %s",
paste0(private$allowed_date_types, collapse=", "))
self$ERROR(errMsg)
stop(errMsg)
}
if(is.null(self$metadata$dates)) self$metadata$dates = list()
self$metadata$dates[[length(self$metadata$dates)+1]] = list(
date = date,
type = list(id = type),
description = description
)
},
#' @description Remove a date
#' @param date the date to remove
#' @param type the date type of the date to be removed
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeDate = function(date, type){
removed <- FALSE
if(!is.null(self$metadata$dates)){
for(i in 1:length(self$metadata$dates)){
dat <- self$metadata$dates[[i]]
if(dat$date == date & dat$type$id == type){
self$metadata$dates[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
#Description methods
#---------------------------------------------------------------------------
#' @description Set the record title.
#' @param title object of class \code{character}
setTitle = function(title){
self$metadata$title <- title
},
#' @description Add additional record title
#' @param title title free text
#' @param type type of title, among following values: alternative-title, subtitle, translated-title, other
#' @param lang language id
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addAdditionalTitle = function(title, type, lang = "eng"){
added = FALSE
if(!(type %in% private$allowed_additional_title_types)){
errMsg = sprintf("Additional title type '%s' incorrect. Possible values are: %s",
type, paste0(private$allowed_additional_title_types, collapse=","))
self$ERROR(errMsg)
stop(errMsg)
}
if(is.null(self$metadata$additional_titles)) self$metadata$additional_titles = list()
ids_df <- data.frame(
title = character(0),
type = character(0),
lang = character(0),
stringsAsFactors = FALSE
)
if(length(self$metadata$additional_titles)>0){
ids_df <- do.call("rbind", lapply(self$metadata$additional_titles, function(x){
data.frame(
title = x$title,
type = x$type$id,
lang = x$lang$id,
stringsAsFactors = FALSE
)
}))
}
if(nrow(ids_df[ids_df$title == title &
ids_df$type == type &
ids_df$lang == lang,])==0){
new_title = list(
title = title,
type = list(id = type),
lang = list(id = lang)
)
self$metadata$additional_titles[[length(self$metadata$additional_titles)+1]] <- new_title
added = TRUE
}
return(added)
},
#' @description Removes additional record title.
#' @param title title free text
#' @param type type of title, among following values: abstract, methods,
#' series-information, table-of-contents, technical-info, other
#' @param lang language id
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeAdditionalTitle = function(title, type, lang = "eng"){
removed <- FALSE
if(!is.null(self$metadata$additional_titles)){
for(i in 1:length(self$metadata$additional_titles)){
desc <- self$metadata$additional_titles[[i]]
if(desc$title == title &&
desc$type$id == type &&
desc$lang$id == lang){
self$metadata$additional_titles[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
#' @description Set the record description
#' @param description object of class \code{character}
setDescription = function(description){
self$metadata$description <- description
},
#' @description Add additional record description
#' @param description description free text
#' @param type type of description, among following values: abstract, methods,
#' series-information, table-of-contents, technical-info, other
#' @param lang language id
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addAdditionalDescription = function(description, type, lang = "eng"){
added = FALSE
if(!(type %in% private$allowed_additional_description_types)){
errMsg = sprintf("Additional description type '%s' incorrect. Possible values are: %s",
type, paste0(private$allowed_additional_description_types, collapse=","))
self$ERROR(errMsg)
stop(errMsg)
}
if(is.null(self$metadata$additional_descriptions)) self$metadata$additional_descriptions = list()
ids_df <- data.frame(
description = character(0),
type = character(0),
lang = character(0),
stringsAsFactors = FALSE
)
if(length(self$metadata$additional_descriptions)>0){
ids_df <- do.call("rbind", lapply(self$metadata$additional_descriptions, function(x){
data.frame(
description = x$description,
type = x$type$id,
lang = x$lang$id,
stringsAsFactors = FALSE
)
}))
}
if(nrow(ids_df[ids_df$description == description &
ids_df$type == type &
ids_df$lang == lang,])==0){
new_desc = list(
description = description,
type = list(id = type),
lang = list(id = lang)
)
self$metadata$additional_descriptions[[length(self$metadata$additional_descriptions)+1]] <- new_desc
added = TRUE
}
return(added)
},
#' @description Removes additional record description
#' @param description description free text
#' @param type type of description, among following values: abstract, methods,
#' series-information, table-of-contents, technical-info, other
#' @param lang language id
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeAdditionalDescription = function(description, type, lang = "eng"){
removed <- FALSE
if(!is.null(self$metadata$additional_descriptions)){
for(i in 1:length(self$metadata$additional_descriptions)){
desc <- self$metadata$additional_descriptions[[i]]
if(desc$description == description &&
desc$type$id == type &&
desc$lang$id == lang){
self$metadata$additional_descriptions[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
# PERSON OR ORG
#---------------------------------------------------------------------------
#' @description Add a person or organization for the record. For persons, the approach is to use the \code{firstname} and
#' \code{lastname} arguments, that by default will be concatenated for Zenodo as \code{lastname, firstname}.
#' For organizations, use the \code{name} argument.
#' @param firstname person first name
#' @param lastname person last name
#' @param name organization name
#' @param orcid person or organization ORCID (optional)
#' @param gnd person or organization GND (optional)
#' @param isni person or organization ISNI (optional)
#' @param ror person or organization ROR (optional)
#' @param role role, values among: contactperson, datacollector, datacurator, datamanager, distributor, editor, funder,
#' hostinginstitution, producer, projectleader, projectmanager, projectmember, registrationagency, registrationauthority,
#' relatedperson, researcher, researchgroup, rightsholder, supervisor, sponsor, workpackageleader, other
#' @param affiliations person or organization affiliations (optional)
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available affiliations. Default is \code{FALSE}
#' @param type type of person or org (creators/contributors)
#' @return \code{TRUE} if added, \code{FALSE} otherwise
#'
#' @note Internal method. Prefer using \code{addCreator} or \code{addContributor}
#'
addPersonOrOrg = function(firstname = NULL, lastname = NULL, name = paste(lastname, firstname, sep = ", "),
orcid = NULL, gnd = NULL, isni = NULL, ror = NULL, role = NULL, affiliations = NULL, sandbox = FALSE,
type){
personOrOrg <- list(
person_or_org = list(
type = if(!is.null(firstname)&&!is.null(lastname)) "personal" else "organizational",
given_name = firstname,
family_name = lastname,
name = name,
identifiers = list()
),
role = NULL,
affiliations = list()
)
#identifiers
if(!is.null(orcid)) personOrOrg$person_or_org$identifiers[[length(personOrOrg$person_or_org$identifiers)+1]] = list(scheme = "orcid", identifier = orcid)
if(!is.null(gnd)) personOrOrg$person_or_org$identifiers[[length(personOrOrg$person_or_org$identifiers)+1]] = list(scheme = "gnd", identifier = gnd)
if(!is.null(isni)) personOrOrg$person_or_org$identifiers[[length(personOrOrg$person_or_org$identifiers)+1]] = list(scheme = "isni", identifier = isni)
if(!is.null(ror)) personOrOrg$person_or_org$identifiers[[length(personOrOrg$person_or_org$identifiers)+1]] = list(scheme = "ror", identifier = ror)
#role
if(!is.null(role)){
if(!(role %in% private$allowed_role_types)){
stop(sprintf("The role type should be one value among values [%s]",
paste(private$allowed_role_types, collapse=",")))
}
personOrOrg$role = list(id = role)
}
#affiliations
if(!is.null(affiliations)) if(personOrOrg$person_or_org$type == "personal"){
zen = ZenodoManager$new(sandbox = sandbox)
affs = lapply(affiliations, function(affiliation){
zen_affiliation <- if(grepl(" ",affiliation)) zen$getAffiliationByName(affiliation) else zen$getAffiliationById(affiliation)
if(is.null(zen_affiliation)){
warnMsg <- sprintf("Affiliation with id or name '%s' doesn't exist in Zenodo", affiliation)
self$WARN(warnMsg)
}
aff = list(name = affiliation)
if(!is.null(zen_affiliation)) aff = list(id = zen_affiliation$id)
return(aff)
})
affs = affs[!sapply(affs,is.null)]
personOrOrg$affiliations = affs
}
if(is.null(self$metadata[[type]])) self$metadata[[type]] <- list()
self$metadata[[type]][[length(self$metadata[[type]])+1]] <- personOrOrg
},
#' @description Removes a person or organization by a property. The \code{by} parameter should be the name
#' of the person or organization property ('name', 'affiliation','orcid','gnd','isni','ror').
#' @param by property used as criterion to remove the person or organization
#' @param property property value used to remove the person or organization
#' @param type type of person or org (creators / contributors)
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
#'
#' @note Internal method. Prefer using \code{removeCreator} or \code{removeContributor}
#'
removePersonOrOrg = function(by, property, type){
removed <- FALSE
for(i in 1:length(self$metadata[[type]])){
personOrOrg <- self$metadata[[type]][[i]]
personOrOrg_map = personOrOrg$person_or_org[names(personOrOrg$person_or_org)!="identifiers"]
if(!is.null(personOrOrg$person_or_org$identifiers)) for(identifier in personOrOrg$person_or_org$identifiers){
personOrOrg_map = c(personOrOrg_map, scheme = identifier$identifier)
names(personOrOrg_map)[names(personOrOrg_map)=="scheme"] = identifier$scheme
}
if(personOrOrg_map[[by]]==property){
self$metadata[[type]][[i]] <- NULL
removed <- TRUE
}
}
return(removed)
},
# CREATORS
#---------------------------------------------------------------------------
#' @description Add a creator for the record. For persons, the approach is to use the \code{firstname} and
#' \code{lastname} arguments, that by default will be concatenated for Zenodo as \code{lastname, firstname}.
#' For organizations, use the \code{name} argument.
#' @param firstname person first name
#' @param lastname person last name
#' @param name organization name
#' @param orcid creator ORCID (optional)
#' @param gnd creator GND (optional)
#' @param isni creator ISNI (optional)
#' @param ror creator ROR (optional)
#' @param role role, values among: contactperson, datacollector, datacurator, datamanager, distributor, editor, funder,
#' hostinginstitution, producer, projectleader, projectmanager, projectmember, registrationagency, registrationauthority,
#' relatedperson, researcher, researchgroup, rightsholder, supervisor, sponsor, workpackageleader, other
#' @param affiliations creator affiliations (optional)
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available affiliations. Default is \code{FALSE}
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addCreator = function(firstname = NULL, lastname = NULL, name = paste(lastname, firstname, sep = ", "),
orcid = NULL, gnd = NULL, isni = NULL, ror = NULL, role = NULL, affiliations = NULL, sandbox = FALSE){
self$addPersonOrOrg(firstname = firstname, lastname = lastname, name = name,
orcid = orcid, gnd = gnd, isni = isni, ror = ror, role = role, affiliations = affiliations, sandbox = sandbox,
type = "creators")
},
#' @description Removes a creator by name.
#' @param name creator name
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByName = function(name){
return(self$removePersonOrOrg(by = "name", name, type = "creators"))
},
#' @description Removes a creator by affiliation.
#' @param affiliation creator affiliation
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByAffiliation = function(affiliation){
return(self$removePersonOrOrg(by = "affiliation", affiliation, type = "creators"))
},
#' @description Removes a creator by ORCID.
#' @param orcid creator ORCID
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByORCID = function(orcid){
return(self$removePersonOrOrg(by = "orcid", orcid, type = "creators"))
},
#' @description Removes a creator by GND.
#' @param gnd creator GND
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByGND = function(gnd){
return(self$removePersonOrOrg(by = "gnd", gnd, type = "creators"))
},
#' @description Removes a creator by ISNI.
#' @param isni creator ISNI
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByISNI = function(isni){
return(self$removePersonOrOrg(by = "isni", isni, type = "creators"))
},
#' @description Removes a creator by ROR.
#' @param ror creator ROR
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCreatorByROR = function(ror){
return(self$removePersonOrOrg(by = "ror", ror, type = "creators"))
},
# CONTRIBUTORS
#---------------------------------------------------------------------------
#' @description Add a contributor for the record. For persons, the approach is to use the \code{firstname} and
#' \code{lastname} arguments, that by default will be concatenated for Zenodo as \code{lastname, firstname}.
#' For organizations, use the \code{name} argument.
#' @param firstname person first name
#' @param lastname person last name
#' @param name organization name
#' @param orcid contributor ORCID (optional)
#' @param gnd contributor GND (optional)
#' @param isni contributor ISNI (optional)
#' @param ror contributor ROR (optional)
#' @param role role, values among: contactperson, datacollector, datacurator, datamanager, distributor, editor, funder,
#' hostinginstitution, producer, projectleader, projectmanager, projectmember, registrationagency, registrationauthority,
#' relatedperson, researcher, researchgroup, rightsholder, supervisor, sponsor, workpackageleader, other
#' @param affiliations contributor affiliations (optional)
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available affiliations. Default is \code{FALSE}
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addContributor = function(firstname = NULL, lastname = NULL, name = paste(lastname, firstname, sep = ", "),
orcid = NULL, gnd = NULL, isni = NULL, ror = NULL, role = NULL, affiliations = NULL, sandbox = FALSE){
self$addPersonOrOrg(firstname = firstname, lastname = lastname, name = name,
orcid = orcid, gnd = gnd, isni = isni, ror = ror, role = role, affiliations = affiliations, sandbox = sandbox,
type = "contributors")
},
#' @description Removes a contributor by name.
#' @param name contributor name
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByName = function(name){
return(self$removePersonOrOrg(by = "name", name, type = "contributors"))
},
#' @description Removes a contributor by affiliation.
#' @param affiliation contributor affiliation
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByAffiliation = function(affiliation){
return(self$removePersonOrOrg(by = "affiliation", affiliation, type = "contributors"))
},
#' @description Removes a contributor by ORCID.
#' @param orcid contributor ORCID
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByORCID = function(orcid){
return(self$removePersonOrOrg(by = "orcid", orcid, type = "contributors"))
},
#' @description Removes a contributor by GND.
#' @param gnd contributor GND
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByGND = function(gnd){
return(self$removePersonOrOrg(by = "gnd", gnd, type = "contributors"))
},
#' @description Removes a contributor by ISNI.
#' @param isni contributor ISNI
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByISNI = function(isni){
return(self$removePersonOrOrg(by = "isni", isni, type = "contributors"))
},
#' @description Removes a contributor by ROR.
#' @param ror contributor ROR
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeContributorByROR = function(ror){
return(self$removePersonOrOrg(by = "ror", ror, type = "contributors"))
},
#'@description Add right/license. Please see https://inveniordm.docs.cern.ch/reference/metadata/#rights-licenses-0-n
#'@param id license id
#'@param title license title
#'@param description a multi-lingual list
#'@param link license link
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available licenses. Default is \code{FALSE}
addRight = function(id = NULL, title = NULL, description = NULL, link = NULL,
sandbox = FALSE){
added = FALSE
if(is.null(self$metadata$rights)) self$metadata$rights = list()
zen <- ZenodoManager$new(sandbox = sandbox)
if(!is.null(id)){
zen_license <- zen$getLicenseById(id)
if(is.null(zen_license)){
errorMsg <- sprintf("License with id '%s' doesn't exist in Zenodo", id)
self$ERROR(errorMsg)
stop(errorMsg)
}
right = list(id = zen_license$id)
self$metadata$rights[[length(self$metadata$rights)+1]] = right
added = TRUE
}else{
if(is.null(title)){
errMsg <- "At least an 'id' or 'title' must be provided"
self$ERROR(errorMsg)
stop(errorMsg)
}else{
right = list(title = title)
if(!is.null(description)) right$description = description
if(!is.null(link)) right$link = link
self$metadata$rights[[length(self$metadata$rights)+1]] = right
added = TRUE
}
}
return(added)
},
#' @description Set license. The license should be set with the Zenodo id of the license. If not
#' recognized by Zenodo, the function will return an error. The list of licenses can
#' fetched with the \code{ZenodoManager} and the function \code{$getLicenses()}.
#' @param licenseId a license Id
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available licenses. Default is \code{FALSE}
#' @return \code{TRUE} if set, \code{FALSE} otherwise
setLicense = function(licenseId, sandbox = FALSE){
self$addRight(id = licenseId, sandbox = sandbox)
},
#' @description Set record version.
#' @param version the record version to set
setVersion = function(version){
self$metadata$version <- version
},
#' @description Adds a language.
#' @param language ISO 639-2 or 639-3 code
addLanguage = function(language){
zenodo = ZenodoManager$new()
zen_language = zenodo$getLanguageById(language)
if(is.null(zen_language)){
errorMsg <- sprintf("Language with id '%s' doesn't exist in Zenodo", language)
self$ERROR(errorMsg)
stop(errorMsg)
}
self$metadata$languages[[length(self$metadata$languages)+1]] <- list(id = language)
},
#' @description Set the language
#' @param language ISO 639-2 or 639-3 code
setLanguage = function(language){
warnMsg = "Method 'setLanguage' is deprecated, please use 'addLanguage'"
self$WARN(warnMsg)
self$addLanguage(language)
},
#' @description Adds a related identifier with a given scheme and relation type.
#' @param identifier identifier
#' @param scheme scheme among following values: ark, arxiv, bibcode, doi, ean13, eissn, handle, igsn, isbn, issn, istc, lissn,
#' lsid, pubmed id, purl, upc, url, urn, w3id
#' @param relation_type relation type among following values: iscitedby, cites, issupplementto, issupplementedby, iscontinuedby,
#' continues, isdescribedby, describes, hasmetadata, ismetadatafor, isnewversionof, ispreviousversionof, ispartof, haspart,
#' isreferencedby, references, isdocumentedby, documents, iscompiledby, compiles, isvariantformof, isoriginalformof, isidenticalto,
#' isalternateidentifier, isreviewedby, reviews, isderivedfrom, issourceof, requires, isrequiredby, isobsoletedby, obsoletes
#' @param resource_type optional resource type
#'@return \code{TRUE} if added, \code{FALSE} otherwise
addRelatedIdentifier = function(identifier, scheme, relation_type, resource_type = NULL){
if(!(scheme %in% private$allowed_identifier_schemes)){
stop(sprintf("Identifier scheme '%s' incorrect. Use a value among the following [%s]",
scheme, paste0(private$allowed_identifier_schemes, collapse=",")))
}
if(!(relation_type %in% private$allowed_relation_types)){
stop(sprintf("Relation type '%s' incorrect. Use a value among the following [%s]",
relation_type, paste(private$allowed_relation_types, collapse=",")))
}
added <- FALSE
if(is.null(self$metadata$related_identifiers)) self$metadata$related_identifiers <- list()
ids_df <- data.frame(
identifier = character(0),
scheme = character(0),
relation_type = character(0),
resource_type = character(0),
stringsAsFactors = FALSE
)
if(length(self$metadata$related_identifiers)>0){
ids_df <- do.call("rbind", lapply(self$metadata$related_identifiers, function(x){
data.frame(
identifier = x$identifier,
scheme = x$scheme,
relation_type = x$relation_type$id,
resource_type = if(!is.null(x$resource_type$id)) x$resource_type$id else NA,
stringsAsFactors = FALSE
)
}))
}
if(nrow(ids_df[ids_df$relation == relation_type & ids_df$identifier == identifier,])==0){
new_rel <- list(
identifier = identifier,
scheme = scheme,
relation_type = list(id = relation_type)
)
if(!is.null(resource_type)) {
zenodo = ZenodoManager$new()
res = ZENODO$getResourceTypeById(resource_type)
if(!is.null(res)){
new_rel$resource_type = list(id = resource_type)
}
}
self$metadata$related_identifiers[[length(self$metadata$related_identifiers)+1]] <- new_rel
added <- TRUE
}
return(added)
},
#' @description Removes a related identifier with a given scheme/relation_type
#' @param identifier identifier
#' @param scheme scheme among following values: ark, arxiv, bibcode, doi, ean13, eissn, handle, igsn, isbn, issn, istc, lissn,
#' lsid, pubmed id, purl, upc, url, urn, w3id
#' @param relation_type relation type among following values: iscitedby, cites, issupplementto, issupplementedby, iscontinuedby,
#' continues, isdescribedby, describes, hasmetadata, ismetadatafor, isnewversionof, ispreviousversionof, ispartof, haspart,
#' isreferencedby, references, isdocumentedby, documents, iscompiledby, compiles, isvariantformof, isoriginalformof, isidenticalto,
#' isalternateidentifier, isreviewedby, reviews, isderivedfrom, issourceof, requires, isrequiredby, isobsoletedby, obsoletes
#'@return \code{TRUE} if removed, \code{FALSE} otherwise
removeRelatedIdentifier = function(identifier, scheme, relation_type){
if(!(scheme %in% private$allowed_identifier_schemes)){
stop(sprintf("Identifier scheme '%s%' incorrect. Use a value among the following [%s]",
scheme, paste0(private$allowed_identifier_schemes, collapse=",")))
}
if(!(relation_type %in% private$allowed_relation_types)){
stop(sprintf("Relation '%s' incorrect. Use a value among the following [%s]",
relation_type, paste(private$allowed_relation_types, collapse=",")))
}
removed <- FALSE
if(!is.null(self$metadata$related_identifiers)){
for(i in 1:length(self$metadata$related_identifiers)){
related_id <- self$metadata$related_identifiers[[i]]
if(related_id$identifier == identifier &
related_id$scheme == scheme &
related_id$relation_type$id == relation_type){
self$metadata$related_identifiers[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
#' @description Set references
#' @param references a vector or list of references to set for the record
setReferences = function(references){
if(is.null(self$metadata$references)) self$metadata$references <- list()
for(reference in references){
self$addReference(reference)
}
},
#' @description Add a reference
#' @param reference the reference to add
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addReference = function(reference){
added <- FALSE
if(is.null(self$metadata$references)) self$metadata$references <- list()
if(!(reference %in% sapply(self$metadata$references, function(x){x$reference}))){
self$metadata$references[[length(self$metadata$references)+1]] <- list(reference = reference)
added <- TRUE
}
return(added)
},
#' @description Remove a reference
#' @param reference the reference to remove
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeReference = function(reference){
removed <- FALSE
if(!is.null(self$metadata$references)){
for(i in 1:length(self$metadata$references)){
ref <- self$metadata$references[[i]]
if(ref$reference == reference){
self$metadata$references[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
#' @description Set subjects
#' @param subjects a vector or list of subjects to set for the record
setSubjects = function(subjects){
if(is.null(self$metadata$subjects)) self$metadata$subjects <- list()
for(subject in subjects){
self$addSubject(subject)
}
},
#' @description Set keywords
#' @param keywords a vector or list of keywords to set for the record
setKeywords = function(keywords){
warnMsg = "Method 'setKeywords' is deprecated, please use 'setSubjects'"
self$WARN(warnMsg)
self$setSubjects(keywords)
},
#' @description Add a subject
#' @param subject the subject to add
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addSubject = function(subject){
added <- FALSE
if(is.null(self$metadata$subjects)) self$metadata$subjects <- list()
if(!(subject %in% self$metadata$subjects)){
self$metadata$subjects[[length(self$metadata$subjects)+1]] <- list(subject = subject)
added <- TRUE
}
return(added)
},
#' @description Add a keyword
#' @param keyword the keyword to add
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addKeyword = function(keyword){
warnMsg = "Method 'addKeyword' is deprecated, please use 'addSubject'"
self$WARN(warnMsg)
self$addSubject(keyword)
},
#' @description Remove a subject
#' @param subject the subject to remove
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeSubject = function(subject){
removed <- FALSE
if(!is.null(self$metadata$subjects)){
for(i in 1:length(self$metadata$subjects)){
sbj <- self$metadata$subjects[[i]]
if(sbj$subject == subject){
self$metadata$subjects[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},
#' @description Remove a keyword
#' @param keyword the keyword to remove
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeKeyword = function(keyword){
warnMsg = "Method 'removeKeyword' is deprecated, please use 'removeSubject'"
self$WARN(warnMsg)
self$removeSubject(keyword)
},
#' @description Set notes. HTML is not allowed
#' @param notes object of class \code{character}
setNotes = function(notes){
self$metadata$notes <- notes
},
#' @description Adds funding. Used internally, prefer using \code{addGrant} instead.
#' @param funder funder id or name
#' @param grant grant id or title
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available grants. Default is \code{FALSE}
addFunding = function(funder = NULL, grant = NULL, sandbox = FALSE){
zen <- ZenodoManager$new(sandbox = sandbox)
if(is.null(self$metadata$funding)) self$metadata$funding <- list()
#funder
funder_item = NULL
if(!is.null(funder)){
funder_id = NULL
zen_funder = zen$getFunderById(URLencode(funder))
if(is.null(zen_funder)){
warnMsg <- sprintf("Funder with id '%s' doesn't exist in Zenodo", funder)
self$WARN(warnMsg)
funders = zen$getFundersByName(name = funder)
if(!is.null(funders)) if(any(funders$name == funder)){
zen_funder = funders[funders$name == funder,][1,]
funder_id = zen_funder$id
}