Revision 51020
Added by Michele Artini about 6 years ago
EnrichDatasetsPlugin.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.net.URI; |
4 | 4 |
import java.net.URISyntaxException; |
5 |
import java.util.HashMap;
|
|
5 |
import java.util.ArrayList;
|
|
6 | 6 |
import java.util.List; |
7 |
import java.util.Map; |
|
8 | 7 |
|
9 | 8 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.apache.commons.logging.Log; |
|
11 |
import org.apache.commons.logging.LogFactory; |
|
12 |
import org.dom4j.Document; |
|
13 |
import org.dom4j.Element; |
|
14 |
import org.dom4j.Namespace; |
|
15 |
import org.dom4j.QName; |
|
16 | 9 |
import org.springframework.beans.factory.annotation.Value; |
17 | 10 |
|
18 | 11 |
import com.google.gson.Gson; |
19 | 12 |
import com.google.gson.reflect.TypeToken; |
20 | 13 |
|
14 |
import eu.dnetlib.data.mdstore.plugins.objects.Dataset; |
|
15 |
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord; |
|
21 | 16 |
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier; |
22 | 17 |
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation; |
23 | 18 |
|
24 | 19 |
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin { |
25 | 20 |
|
26 |
private static final Log log = LogFactory.getLog(EnrichDatasetsPlugin.class);
|
|
21 |
private static final Gson gson = new Gson();
|
|
27 | 22 |
|
28 | 23 |
@Value("${plugin.enrich.dataset.dli.url}") |
29 | 24 |
private String baseUrl; |
... | ... | |
34 | 29 |
} |
35 | 30 |
|
36 | 31 |
@Override |
37 |
protected boolean updateDocument(final Document doc, final String response) {
|
|
32 |
protected void reconfigure() {}
|
|
38 | 33 |
|
39 |
final Gson gson = new Gson(); |
|
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 |
|
|
40 | 41 |
final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType()); |
41 | 42 |
|
42 |
final Map<String, String> datasets = new HashMap<>(); |
|
43 |
|
|
44 | 43 |
for (final DliRelation rel : rels) { |
45 | 44 |
final String title = rel.getTarget().getTitle(); |
46 | 45 |
for (final DliIdentifier id : rel.getTarget().getIdentifiers()) { |
47 | 46 |
if (id.getSchema().equalsIgnoreCase("doi") && StringUtils.isNoneBlank(id.getIdentifier()) && StringUtils.isNotBlank(title)) { |
48 |
datasets.put(id.getIdentifier(), title);
|
|
47 |
datasets.add(new Dataset(id.getIdentifier(), title));
|
|
49 | 48 |
} |
50 | 49 |
} |
51 | 50 |
} |
52 | 51 |
|
53 |
if (datasets.isEmpty()) { return false; } |
|
52 |
if (datasets.isEmpty()) { |
|
53 |
return false; |
|
54 |
} else { |
|
55 |
doc.getDatasets().addAll(datasets); |
|
56 |
return true; |
|
57 |
} |
|
58 |
} |
|
54 | 59 |
|
55 |
final Element node = (Element) doc.selectSingleNode("//*[local-name() = 'datasets']"); |
|
56 |
|
|
57 |
datasets.entrySet().forEach(e -> { |
|
58 |
final Element ds = node.addElement(new QName("dataset", new Namespace("isti", "http://www.isti.cnr.it/"))); |
|
59 |
ds.addAttribute("doi", e.getKey()); |
|
60 |
ds.addAttribute("url", "https://dx.doi.org/" + e.getKey()); |
|
61 |
ds.setText(e.getValue()); |
|
62 |
log.info("Found dataset: " + e.getKey() + " -> " + e.getValue()); |
|
63 |
}); |
|
64 |
|
|
65 |
return true; |
|
66 |
|
|
67 |
} |
|
68 | 60 |
} |
Also available in: Unified diff
refactoring using jaxb