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.ArrayList;
6
import java.util.List;
7
import java.util.Map;
8

    
9
import org.apache.commons.lang3.StringUtils;
10
import org.springframework.beans.factory.annotation.Value;
11

    
12
import com.google.gson.Gson;
13
import com.google.gson.reflect.TypeToken;
14

    
15
import eu.dnetlib.data.mdstore.plugins.objects.Dataset;
16
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
17
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier;
18
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation;
19

    
20
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
21

    
22
	private static final Gson gson = new Gson();
23

    
24
	@Value("${plugin.enrich.dataset.dli.url}")
25
	private String baseUrl;
26

    
27
	@Override
28
	protected URI prepareURI(final String doi) throws URISyntaxException {
29
		return new URI(String.format(baseUrl, doi));
30
	}
31

    
32
	@Override
33
	protected void reconfigure(final Map<String, String> params) {}
34

    
35
	@Override
36
	protected boolean updateDocument(final MdRecord doc, final String response) {
37

    
38
		doc.getDatasets().clear();
39

    
40
		final List<Dataset> datasets = new ArrayList<>();
41

    
42
		final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
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.add(new Dataset(id.getIdentifier(), title));
49
				}
50
			}
51
		}
52

    
53
		if (datasets.isEmpty()) {
54
			return false;
55
		} else {
56
			doc.getDatasets().addAll(datasets);
57
			return true;
58
		}
59
	}
60

    
61
}
(3-3/9)