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.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Value;
13

    
14
import com.google.gson.Gson;
15
import com.google.gson.reflect.TypeToken;
16

    
17
import eu.dnetlib.data.mdstore.plugins.objects.Dataset;
18
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
19
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliEntity;
20
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliIdentifier;
21
import eu.dnetlib.data.mdstore.plugins.objects.dli.DliRelation;
22

    
23
public class EnrichDatasetsPlugin extends GenericDoiMdstorePlugin {
24

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

    
27
	private static final Gson gson = new Gson();
28

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

    
32
	// @Autowired
33
	// private OpenAIREAuthenticationProvider openAIREAuthenticationProvider;
34

    
35
	private Counter enrichDatasetsCounter = new Counter();
36

    
37
	@Override
38
	protected URI prepareURI(final String doi) throws URISyntaxException {
39
		return new URI(String.format(baseUrl, doi));
40
	}
41

    
42
	@Override
43
	protected void reconfigure(final Map<String, String> params) {
44
		enrichDatasetsCounter.reset();
45
		// setAccessToken(openAIREAuthenticationProvider.obtainAccessToken());
46
	}
47

    
48
	@Override
49
	protected void resetConfiguration() {
50
		log.info("***** Openaire Enrichment - datasets  : " + enrichDatasetsCounter);
51
		enrichDatasetsCounter.reset();
52
	}
53

    
54
	@Override
55
	protected boolean updateDocument(final MdRecord doc, final String response) {
56

    
57
		doc.getDatasets().clear();
58

    
59
		final List<Dataset> datasets = new ArrayList<>();
60

    
61
		final List<DliRelation> rels = gson.fromJson(response, new TypeToken<List<DliRelation>>() {}.getType());
62

    
63
		for (final DliRelation rel : rels) {
64
			datasets.addAll(findRelatedDatasets(rel.getSource()));
65
			datasets.addAll(findRelatedDatasets(rel.getTarget()));
66
		}
67

    
68
		if (datasets.isEmpty()) {
69
			return false;
70
		} else {
71
			doc.getDatasets().addAll(datasets);
72
			enrichDatasetsCounter.incrementAfter(doc.getDatasets().size());
73
			return true;
74
		}
75
	}
76

    
77
	private List<Dataset> findRelatedDatasets(final DliEntity entity) {
78
		final List<Dataset> res = new ArrayList<>();
79
		if (entity.getObjectType().equalsIgnoreCase("dataset")) {
80
			final String title = entity.getTitle();
81
			for (final DliIdentifier id : entity.getIdentifiers()) {
82
				if (id.getSchema().equalsIgnoreCase("doi") && StringUtils.isNoneBlank(id.getIdentifier()) && StringUtils.isNotBlank(title)) {
83
					res.add(new Dataset(id.getIdentifier(), title));
84
				}
85
			}
86
		}
87
		return res;
88
	}
89

    
90
}
(4-4/11)