Project

General

Profile

1
package eu.dnetlib.data.mdstore.plugins;
2

    
3
import java.net.URI;
4
import java.net.URISyntaxException;
5
import java.util.HashMap;
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.dom4j.Document;
13
import org.dom4j.Element;
14
import org.dom4j.Namespace;
15
import org.dom4j.QName;
16
import org.springframework.beans.factory.annotation.Value;
17

    
18
import com.google.gson.Gson;
19
import com.google.gson.reflect.TypeToken;
20

    
21
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier;
22
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation;
23

    
24
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
25

    
26
	private static final Log log = LogFactory.getLog(EnrichDatasetsPlugin.class);
27

    
28
	@Value("${plugin.enrich.dataset.dli.url}")
29
	private String baseUrl;
30

    
31
	@Override
32
	protected URI prepareURI(final String doi) throws URISyntaxException {
33
		return new URI(String.format(baseUrl, doi));
34
	}
35

    
36
	@Override
37
	protected boolean updateDocument(final Document doc, final String response) {
38

    
39
		final Gson gson = new Gson();
40
		final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
41

    
42
		final Map<String, String> datasets = new HashMap<>();
43

    
44
		for (final DliRelation rel : rels) {
45
			final String title = rel.getTarget().getTitle();
46
			for (final DliIdentifier id : rel.getTarget().getIdentifiers()) {
47
				if (id.getSchema().equalsIgnoreCase("doi") && StringUtils.isNoneBlank(id.getIdentifier()) && StringUtils.isNotBlank(title)) {
48
					datasets.put(id.getIdentifier(), title);
49
				}
50
			}
51
		}
52

    
53
		if (datasets.isEmpty()) { return false; }
54

    
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
}
(2-2/7)