1
|
package eu.dnetlib.dli.proto;
|
2
|
|
3
|
import java.util.*;
|
4
|
import java.util.stream.Collectors;
|
5
|
|
6
|
import com.googlecode.protobuf.format.JsonFormat;
|
7
|
import eu.dnetlib.data.proto.*;
|
8
|
import eu.dnetlib.data.proto.dli.DLIObjectProtos;
|
9
|
|
10
|
import static eu.dnetlib.data.proto.dli.DLIObjectProtos.DLIObjectSummary;
|
11
|
import static eu.dnetlib.data.proto.dli.DLIObjectProtos.TypedIdentifier;
|
12
|
|
13
|
/**
|
14
|
* Created by sandro on 3/13/17.
|
15
|
*/
|
16
|
|
17
|
public class DNGFDLISummaryConverter {
|
18
|
|
19
|
private DNGFProtos.DNGF mainDNGFEntity;
|
20
|
|
21
|
private int relatedDatasets = 0;
|
22
|
|
23
|
private int relatedPublications = 0;
|
24
|
|
25
|
private int relatedUnknown = 0;
|
26
|
|
27
|
|
28
|
public void setMainEntity(final DNGFProtos.DNGF mainEntity) {
|
29
|
this.mainDNGFEntity = mainEntity;
|
30
|
}
|
31
|
|
32
|
public void addRelation(final DNGFProtos.DNGF relation) {
|
33
|
final TypeProtos.Type targetType = relation.getRel().getTargetType();
|
34
|
|
35
|
switch (targetType) {
|
36
|
case dataset:
|
37
|
this.relatedDatasets++;
|
38
|
break;
|
39
|
case publication:
|
40
|
this.relatedPublications++;
|
41
|
break;
|
42
|
case unknown:
|
43
|
this.relatedUnknown++;
|
44
|
break;
|
45
|
}
|
46
|
|
47
|
}
|
48
|
|
49
|
public String convertAsJson() {
|
50
|
final DLIObjectSummary convert = convert();
|
51
|
if (convert != null)
|
52
|
return JsonFormat.printToString(convert);
|
53
|
return null;
|
54
|
}
|
55
|
|
56
|
public DLIObjectSummary convert() {
|
57
|
if (!isValid()) {
|
58
|
return null;
|
59
|
}
|
60
|
|
61
|
final DNGFProtos.DNGFEntity entity = mainDNGFEntity.getEntity();
|
62
|
final DLIObjectSummary.Builder summary = DLIObjectSummary.newBuilder();
|
63
|
|
64
|
summary.setId(entity.getId());
|
65
|
final List<FieldTypeProtos.StructuredProperty> identifiers = entity.getExtension(DliProtos.typedIdentifier);
|
66
|
if (identifiers == null || identifiers.isEmpty()) {
|
67
|
this.mainDNGFEntity = null;
|
68
|
return null;
|
69
|
// throw new IllegalStateException(String.format("missing typedIdentifiers on main Entity \n%s", entity));
|
70
|
}
|
71
|
|
72
|
//ADD ALL IDENTIFIERS
|
73
|
identifiers.stream()
|
74
|
.collect(
|
75
|
Collectors.groupingBy(s -> String.format("%s::%s", s.getValue().toLowerCase().trim(), s.getQualifier().getClassid().toLowerCase().trim())))
|
76
|
.values()
|
77
|
.forEach(
|
78
|
v -> summary.addLocalIdentifier(
|
79
|
TypedIdentifier.newBuilder()
|
80
|
.setId(v.get(0).getValue())
|
81
|
.setType(v.get(0).getQualifier().getClassid())
|
82
|
));
|
83
|
|
84
|
|
85
|
// ADD ALL DATASOURCES
|
86
|
final Map<String, DLIObjectProtos.CollectedFromType> datasources = new HashMap<>();
|
87
|
|
88
|
|
89
|
final List<FieldTypeProtos.KeyValue> collectedFromList = entity.getCollectedfromList();
|
90
|
if (collectedFromList != null) {
|
91
|
collectedFromList.forEach(collectedFromValue -> {
|
92
|
try {
|
93
|
datasources.put(collectedFromValue.getKey(),
|
94
|
DLIObjectProtos.CollectedFromType.newBuilder()
|
95
|
.setDatasourceId(collectedFromValue.getKey())
|
96
|
.setDatasourceName(collectedFromValue.getValue())
|
97
|
.setCompletionStatus(DLIObjectProtos.CompletionStatus.valueOf(collectedFromValue.getExtension(DliFieldTypeProtos.completionStatus)))
|
98
|
.build());
|
99
|
} catch (IllegalArgumentException e) {
|
100
|
System.out.println("Error on generate datasource from record " + entity.toString());
|
101
|
throw new RuntimeException(e);
|
102
|
}
|
103
|
}
|
104
|
);
|
105
|
}
|
106
|
final List<FieldTypeProtos.KeyValue> resolvedFromList = entity.getExtension(DliProtos.resolvedfrom);
|
107
|
if (resolvedFromList != null) {
|
108
|
Map<String, FieldTypeProtos.KeyValue> setRFrom = new HashMap<>();
|
109
|
resolvedFromList.forEach(resolvedFromValue -> setRFrom.put(resolvedFromValue.getKey(), resolvedFromValue));
|
110
|
setRFrom.values().forEach(r ->
|
111
|
datasources.put(r.getKey(),
|
112
|
DLIObjectProtos.CollectedFromType.newBuilder()
|
113
|
.setCompletionStatus(DLIObjectProtos.CompletionStatus.complete)
|
114
|
.setDatasourceId(r.getKey())
|
115
|
.setDatasourceName(r.getValue())
|
116
|
.build())
|
117
|
);
|
118
|
}
|
119
|
summary.addAllDatasources(datasources.values());
|
120
|
|
121
|
final Set<String> authorList = DngfDliUtils.parseAuthor(entity);
|
122
|
|
123
|
switch (entity.getType()) {
|
124
|
case dataset:
|
125
|
summary.setTypology(DLIObjectProtos.Typology.dataset);
|
126
|
setMetadataFromDataset(entity.getDataset(), summary, authorList);
|
127
|
break;
|
128
|
case publication:
|
129
|
summary.setTypology(DLIObjectProtos.Typology.publication);
|
130
|
setMetadataFromPublication(entity.getPublication(), summary, authorList);
|
131
|
break;
|
132
|
case unknown:
|
133
|
summary.setTypology(DLIObjectProtos.Typology.unknown);
|
134
|
break;
|
135
|
default:
|
136
|
throw new IllegalStateException(String.format("wrong Type for mainEntity: \n%s", entity));
|
137
|
}
|
138
|
|
139
|
summary.addAllPublisher(DngfDliUtils.parsePublishers(mainDNGFEntity.getEntity()));
|
140
|
summary.setRelatedPublications(relatedPublications);
|
141
|
summary.setRelatedDatasets(relatedDatasets);
|
142
|
summary.setRelatedUnknown(relatedUnknown);
|
143
|
|
144
|
return summary.build();
|
145
|
}
|
146
|
|
147
|
|
148
|
|
149
|
private void setMetadataFromDataset(final DatasetProtos.Dataset dataset, final DLIObjectSummary.Builder metadata, final Set<String> authorSet) {
|
150
|
final List<FieldTypeProtos.StructuredProperty> titleList = dataset.getMetadata().getTitleList();
|
151
|
//ADD Titles
|
152
|
if (titleList != null) {
|
153
|
titleList.forEach(title -> metadata.addTitle(title.getValue()));
|
154
|
}
|
155
|
|
156
|
//ADD AUTHORS
|
157
|
final List<PersonProtos.Person> authorList = dataset.getAuthorList();
|
158
|
if (authorList != null) {
|
159
|
authorList.forEach(person -> authorSet.add(person.getMetadata().getFullname().getValue()));
|
160
|
}
|
161
|
|
162
|
if (dataset.getMetadata().getContributorList() != null)
|
163
|
dataset.getMetadata().getContributorList().forEach(c -> authorSet.add(c.getValue()));
|
164
|
|
165
|
metadata.addAllAuthor(authorSet);
|
166
|
|
167
|
//ADD Abstract
|
168
|
manageDatasetDates(dataset, metadata);
|
169
|
if (dataset.getMetadata().getDescriptionList() != null) {
|
170
|
dataset.getMetadata().getDescriptionList().forEach(desc -> metadata.setAbstract(desc.getValue()));
|
171
|
}
|
172
|
|
173
|
//ADD Subjects
|
174
|
if (dataset.getMetadata().getSubjectList() != null) {
|
175
|
dataset.getMetadata().getSubjectList().forEach(
|
176
|
subject -> metadata.addSubject(
|
177
|
DLIObjectProtos.SchemeValue.newBuilder()
|
178
|
.setValue(subject.getValue())
|
179
|
.setScheme(subject.getQualifier().getClassid())
|
180
|
.build()
|
181
|
)
|
182
|
);
|
183
|
}
|
184
|
|
185
|
|
186
|
}
|
187
|
|
188
|
private void setMetadataFromPublication(final PublicationProtos.Publication publication, final DLIObjectSummary.Builder metadata, final Set<String> authorSet) {
|
189
|
final List<FieldTypeProtos.StructuredProperty> titleList = publication.getMetadata().getTitleList();
|
190
|
//ADD Titles
|
191
|
if (titleList != null) {
|
192
|
titleList.forEach(title -> metadata.addTitle(title.getValue()));
|
193
|
}
|
194
|
//ADD AUTHORS
|
195
|
final List<PersonProtos.Person> authorList = publication.getAuthorList();
|
196
|
if (authorList != null) {
|
197
|
authorList.forEach(person -> authorSet.add(person.getMetadata().getFullname().getValue()));
|
198
|
}
|
199
|
|
200
|
if (publication.getMetadata().getContributorList() != null)
|
201
|
publication.getMetadata().getContributorList().forEach(c -> authorSet.add(c.getValue()));
|
202
|
|
203
|
metadata.addAllAuthor(authorSet);
|
204
|
|
205
|
//ADD Abstract
|
206
|
managePublicationDates(publication, metadata);
|
207
|
if (publication.getMetadata().getDescriptionList() != null) {
|
208
|
publication.getMetadata().getDescriptionList().forEach(desc -> metadata.setAbstract(desc.getValue()));
|
209
|
}
|
210
|
|
211
|
//ADD Subjects
|
212
|
if (publication.getMetadata().getSubjectList() != null) {
|
213
|
publication.getMetadata().getSubjectList().forEach(
|
214
|
subject -> metadata.addSubject(
|
215
|
DLIObjectProtos.SchemeValue.newBuilder()
|
216
|
.setValue(subject.getValue())
|
217
|
.setScheme(subject.getQualifier().getClassid())
|
218
|
.build()
|
219
|
)
|
220
|
);
|
221
|
}
|
222
|
|
223
|
|
224
|
}
|
225
|
|
226
|
private void managePublicationDates(final PublicationProtos.Publication publication, final DLIObjectSummary.Builder metadata) {
|
227
|
if (publication.getMetadata().getDateofacceptance() != null) {
|
228
|
metadata.addDate(publication.getMetadata().getDateofacceptance().getValue());
|
229
|
}
|
230
|
|
231
|
if (publication.getMetadata().getRelevantdateList() != null && !publication.getMetadata().getRelevantdateList().isEmpty()) {
|
232
|
publication.getMetadata().getRelevantdateList().forEach(
|
233
|
date -> metadata.addDate(date.getValue())
|
234
|
);
|
235
|
}
|
236
|
}
|
237
|
|
238
|
private void manageDatasetDates(final DatasetProtos.Dataset dataset, final DLIObjectSummary.Builder metadata) {
|
239
|
if (dataset.getMetadata().getDateofacceptance() != null) {
|
240
|
metadata.addDate(dataset.getMetadata().getDateofacceptance().getValue());
|
241
|
}
|
242
|
|
243
|
if (dataset.getMetadata().getRelevantdateList() != null && !dataset.getMetadata().getRelevantdateList().isEmpty()) {
|
244
|
dataset.getMetadata().getRelevantdateList().forEach(
|
245
|
date -> metadata.addDate(date.getValue())
|
246
|
);
|
247
|
}
|
248
|
|
249
|
}
|
250
|
|
251
|
|
252
|
public boolean isValid() {
|
253
|
return mainDNGFEntity != null && !(this.relatedUnknown == 0 && this.relatedDatasets == 0 && this.relatedPublications == 0);
|
254
|
}
|
255
|
|
256
|
public DNGFProtos.DNGF getMainDNGFEntity() {
|
257
|
return mainDNGFEntity;
|
258
|
}
|
259
|
|
260
|
public void setMainDNGFEntity(DNGFProtos.DNGF mainDNGFEntity) {
|
261
|
this.mainDNGFEntity = mainDNGFEntity;
|
262
|
}
|
263
|
}
|