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
|
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.springframework.beans.factory.annotation.Value;
|
10
|
|
11
|
import com.google.gson.Gson;
|
12
|
import com.google.gson.reflect.TypeToken;
|
13
|
|
14
|
import eu.dnetlib.data.mdstore.plugins.objects.Dataset;
|
15
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
16
|
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier;
|
17
|
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation;
|
18
|
|
19
|
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
|
20
|
|
21
|
private static final Gson gson = new Gson();
|
22
|
|
23
|
@Value("${plugin.enrich.dataset.dli.url}")
|
24
|
private String baseUrl;
|
25
|
|
26
|
@Override
|
27
|
protected URI prepareURI(final String doi) throws URISyntaxException {
|
28
|
return new URI(String.format(baseUrl, doi));
|
29
|
}
|
30
|
|
31
|
@Override
|
32
|
protected void reconfigure() {}
|
33
|
|
34
|
@Override
|
35
|
protected boolean updateDocument(final MdRecord doc, final String response) {
|
36
|
|
37
|
doc.getDatasets().clear();
|
38
|
|
39
|
final List<Dataset> datasets = new ArrayList<>();
|
40
|
|
41
|
final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
|
42
|
|
43
|
for (final DliRelation rel : rels) {
|
44
|
final String title = rel.getTarget().getTitle();
|
45
|
for (final DliIdentifier id : rel.getTarget().getIdentifiers()) {
|
46
|
if (id.getSchema().equalsIgnoreCase("doi") && StringUtils.isNoneBlank(id.getIdentifier()) && StringUtils.isNotBlank(title)) {
|
47
|
datasets.add(new Dataset(id.getIdentifier(), title));
|
48
|
}
|
49
|
}
|
50
|
}
|
51
|
|
52
|
if (datasets.isEmpty()) {
|
53
|
return false;
|
54
|
} else {
|
55
|
doc.getDatasets().addAll(datasets);
|
56
|
return true;
|
57
|
}
|
58
|
}
|
59
|
|
60
|
}
|