1
|
package eu.dnetlib.data.mdstore.plugins;
|
2
|
|
3
|
import java.net.URI;
|
4
|
import java.net.URISyntaxException;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import org.apache.commons.lang3.StringUtils;
|
10
|
import org.apache.commons.logging.Log;
|
11
|
import org.apache.commons.logging.LogFactory;
|
12
|
import org.springframework.beans.factory.annotation.Value;
|
13
|
|
14
|
import com.google.gson.Gson;
|
15
|
import com.google.gson.reflect.TypeToken;
|
16
|
|
17
|
import eu.dnetlib.data.mdstore.plugins.objects.Dataset;
|
18
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
19
|
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliEntity;
|
20
|
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier;
|
21
|
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation;
|
22
|
|
23
|
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(EnrichDatasetsPlugin.class);
|
26
|
|
27
|
private static final Gson gson = new Gson();
|
28
|
|
29
|
@Value("${plugin.enrich.dataset.dli.url}")
|
30
|
private String baseUrl;
|
31
|
|
32
|
private Counter enrichDatasetsCounter = new Counter();
|
33
|
|
34
|
@Override
|
35
|
protected URI prepareURI(final String doi) throws URISyntaxException {
|
36
|
return new URI(String.format(baseUrl, doi));
|
37
|
}
|
38
|
|
39
|
@Override
|
40
|
protected void reconfigure(final Map<String, String> params) {
|
41
|
enrichDatasetsCounter.reset();
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
protected void resetConfiguration() {
|
46
|
log.info("***** Openaire Enrichment - datasets : " + enrichDatasetsCounter);
|
47
|
enrichDatasetsCounter.reset();
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
protected boolean updateDocument(final MdRecord doc, final String response) {
|
52
|
|
53
|
doc.getDatasets().clear();
|
54
|
|
55
|
final List<Dataset> datasets = new ArrayList<>();
|
56
|
|
57
|
final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
|
58
|
|
59
|
for (final DliRelation rel : rels) {
|
60
|
datasets.addAll(findRelatedDatasets(rel.getSource()));
|
61
|
datasets.addAll(findRelatedDatasets(rel.getTarget()));
|
62
|
}
|
63
|
|
64
|
if (datasets.isEmpty()) {
|
65
|
return false;
|
66
|
} else {
|
67
|
doc.getDatasets().addAll(datasets);
|
68
|
enrichDatasetsCounter.incrementAfter(doc.getDatasets().size());
|
69
|
return true;
|
70
|
}
|
71
|
}
|
72
|
|
73
|
private List<Dataset> findRelatedDatasets(final DliEntity entity) {
|
74
|
final List<Dataset> res = new ArrayList<>();
|
75
|
if (entity.getObjectType().equalsIgnoreCase("dataset")) {
|
76
|
final String title = entity.getTitle();
|
77
|
for (final DliIdentifier id : entity.getIdentifiers()) {
|
78
|
if (id.getSchema().equalsIgnoreCase("doi") && StringUtils.isNoneBlank(id.getIdentifier()) && StringUtils.isNotBlank(title)) {
|
79
|
res.add(new Dataset(id.getIdentifier(), title));
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
return res;
|
84
|
}
|
85
|
|
86
|
}
|