1
|
package eu.dnetlib.dli.proto;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.Map;
|
5
|
import java.util.stream.Collectors;
|
6
|
|
7
|
import com.google.common.collect.Maps;
|
8
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFEntity;
|
9
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFRel;
|
10
|
import eu.dnetlib.data.proto.DatasetProtos.Dataset;
|
11
|
import eu.dnetlib.data.proto.DliFieldTypeProtos;
|
12
|
import eu.dnetlib.data.proto.DliProtos;
|
13
|
import eu.dnetlib.data.proto.FieldTypeProtos.KeyValue;
|
14
|
import eu.dnetlib.data.proto.PublicationProtos.Publication;
|
15
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
16
|
|
17
|
import static eu.dnetlib.data.proto.dli.ScholixObjectProtos.*;
|
18
|
|
19
|
/**
|
20
|
* Created by sandro on 2/27/17.
|
21
|
*/
|
22
|
public class DngfToScholixConverter {
|
23
|
|
24
|
public static Scholix.Builder withSource(final DNGFEntity input) {
|
25
|
return Scholix.newBuilder().setSource(asScholixResource(input));
|
26
|
}
|
27
|
|
28
|
public static Scholix.Builder withTarget(final DNGFEntity input, final DNGFRel rel) {
|
29
|
final Scholix.Builder scholix = Scholix.newBuilder()
|
30
|
.setTarget(asScholixResource(input))
|
31
|
.setRelationship(ScholixRelationship.newBuilder().setName(rel.getRelType().getClassid()).setSchema("datacite"))
|
32
|
.addAllLinkprovider(rel.getCollectedfromList()
|
33
|
.stream()
|
34
|
.map(cf ->
|
35
|
ScholixEntityId.newBuilder()
|
36
|
.setName(cf.getValue())
|
37
|
.addIdentifiers(
|
38
|
ScholixIdentifier.newBuilder()
|
39
|
.setSchema("dnetIdentifier")
|
40
|
.setIdentifier(cf.getKey()))
|
41
|
.build())
|
42
|
.collect(Collectors.toList()));
|
43
|
return scholix;
|
44
|
}
|
45
|
|
46
|
private static ScholixResource asScholixResource(DNGFEntity input) {
|
47
|
|
48
|
final ScholixResource.Builder builder = ScholixResource.newBuilder();
|
49
|
|
50
|
//Set Identifier
|
51
|
builder.addAllIdentifier(input.getExtension(DliProtos.typedIdentifier)
|
52
|
.stream()
|
53
|
.map(id -> ScholixIdentifier.newBuilder()
|
54
|
.setIdentifier(id.getValue())
|
55
|
.setSchema(id.getQualifier().getClassid())
|
56
|
.build())
|
57
|
.collect(Collectors.toMap(
|
58
|
i -> i.getIdentifier(),
|
59
|
i -> i,
|
60
|
(v1, v2) -> v1)).values());
|
61
|
|
62
|
builder.setDnetIdentifier(input.getId());
|
63
|
builder.addAllCreator(DngfDliUtils.parseAuthor(input)
|
64
|
.stream()
|
65
|
.map(a -> ScholixEntityId.newBuilder().setName(a).build())
|
66
|
.collect(Collectors.toList()));
|
67
|
|
68
|
switch (input.getType()) {
|
69
|
case publication:
|
70
|
builder.setObjectType(Type.publication.name());
|
71
|
final Publication.Metadata pm = input.getPublication().getMetadata();
|
72
|
if (pm.getTitleCount() > 0) {
|
73
|
builder.setTitle(pm.getTitleList().stream()
|
74
|
.map(t -> t.getValue())
|
75
|
.findFirst().get());
|
76
|
}
|
77
|
builder.setPublicationDate(pm.getDateofacceptance().getValue());
|
78
|
builder.setPublisher(ScholixEntityId.newBuilder().setName(pm.getPublisher().getValue()));
|
79
|
|
80
|
break;
|
81
|
case dataset:
|
82
|
builder.setObjectType(Type.dataset.name());
|
83
|
final Dataset.Metadata dm = input.getDataset().getMetadata();
|
84
|
if (dm.getTitleCount() > 0) {
|
85
|
builder.setTitle(dm.getTitleList().stream()
|
86
|
.map(t -> t.getValue())
|
87
|
.findFirst().get());
|
88
|
}
|
89
|
builder.setPublicationDate(dm.getDateofacceptance().getValue());
|
90
|
builder.setPublisher(ScholixEntityId.newBuilder().setName(dm.getPublisher().getValue()));
|
91
|
break;
|
92
|
case unknown:
|
93
|
builder.setObjectType(Type.unknown.name());
|
94
|
break;
|
95
|
case datasource:
|
96
|
case organization:
|
97
|
case person:
|
98
|
case project:
|
99
|
throw new IllegalArgumentException("cannot transform type: " + input.getType());
|
100
|
}
|
101
|
|
102
|
final Map<String, ScholixCollectedFrom> datasources = Maps.newHashMap();
|
103
|
if (input.getCollectedfromCount() > 0) {
|
104
|
input.getCollectedfromList().forEach(cf -> datasources.put(
|
105
|
cf.getKey(),
|
106
|
getScholixCollectedFrom(cf, "collected", cf.getExtension(DliFieldTypeProtos.completionStatus))));
|
107
|
}
|
108
|
final List<KeyValue> resolvedFrom = input.getExtension(DliProtos.resolvedfrom);
|
109
|
if (resolvedFrom != null) {
|
110
|
resolvedFrom.forEach(rf -> datasources.put(
|
111
|
rf.getKey(),
|
112
|
getScholixCollectedFrom(rf, "resolved", "complete")));
|
113
|
}
|
114
|
|
115
|
return builder.build();
|
116
|
}
|
117
|
|
118
|
private static ScholixCollectedFrom getScholixCollectedFrom(final KeyValue rf, final String provisionMode, final String completionStatus) {
|
119
|
return ScholixCollectedFrom.newBuilder()
|
120
|
.setProvider(ScholixEntityId.newBuilder()
|
121
|
.setName(rf.getValue())
|
122
|
.addIdentifiers(ScholixIdentifier.newBuilder()
|
123
|
.setIdentifier(rf.getKey())
|
124
|
.setSchema("dnetIdentifier")))
|
125
|
.setProvisionMode(provisionMode)
|
126
|
.setCompletionStatus(completionStatus)
|
127
|
.build();
|
128
|
}
|
129
|
|
130
|
}
|