Project

General

Profile

« Previous | Next » 

Revision 49368

datasets

View differences:

EnrichDatasetsPlugin.java
2 2

  
3 3
import java.net.URI;
4 4
import java.net.URISyntaxException;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
5 8

  
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
6 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;
7 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

  
8 24
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
9 25

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

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

  
10 31
	@Override
11 32
	protected URI prepareURI(final String doi) throws URISyntaxException {
12
		return new URI("https://api-dliservice-prototype-dli.d4science.org/v1/linksFromPid?pid=" + doi);
33
		return new URI(String.format(baseUrl, doi));
13 34
	}
14 35

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

  
18
		System.out.println("**********");
19
		System.out.println(response);
20
		System.out.println("**********");
39
		final Gson gson = new Gson();
40
		final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
21 41

  
22
		return response.trim().length() > 10;
42
		final Map<String, String> datasets = new HashMap<>();
23 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

  
24 67
	}
25 68
}

Also available in: Unified diff