1 |
44133
|
michele.ar
|
package eu.dnetlib.data.mdstore.plugins;
|
2 |
|
|
|
3 |
48734
|
michele.ar
|
import java.net.URI;
|
4 |
|
|
import java.net.URISyntaxException;
|
5 |
44523
|
michele.ar
|
import java.util.HashMap;
|
6 |
|
|
import java.util.List;
|
7 |
44133
|
michele.ar
|
import java.util.Map;
|
8 |
51020
|
michele.ar
|
import java.util.stream.Collectors;
|
9 |
44133
|
michele.ar
|
|
10 |
44523
|
michele.ar
|
import org.apache.commons.lang3.StringUtils;
|
11 |
44133
|
michele.ar
|
import org.apache.commons.logging.Log;
|
12 |
|
|
import org.apache.commons.logging.LogFactory;
|
13 |
49368
|
michele.ar
|
import org.springframework.beans.factory.annotation.Value;
|
14 |
44133
|
michele.ar
|
|
15 |
51020
|
michele.ar
|
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
|
16 |
48734
|
michele.ar
|
import eu.dnetlib.data.mdstore.plugins.objects.Project;
|
17 |
44133
|
michele.ar
|
|
18 |
51020
|
michele.ar
|
public class EnrichProjectsPlugin extends MdRecordPlugin {
|
19 |
44133
|
michele.ar
|
|
20 |
|
|
private static final Log log = LogFactory.getLog(EnrichProjectsPlugin.class);
|
21 |
|
|
|
22 |
49368
|
michele.ar
|
@Value("${plugin.enrich.projects.openaire.url}")
|
23 |
|
|
private String baseUrl;
|
24 |
|
|
|
25 |
51020
|
michele.ar
|
private Map<String, Project> mapProjects = new HashMap<>();
|
26 |
|
|
|
27 |
44133
|
michele.ar
|
@Override
|
28 |
51852
|
michele.ar
|
protected void reconfigure(final Map<String, String> params) {
|
29 |
51020
|
michele.ar
|
log.info("Cleaning projects cache");
|
30 |
|
|
mapProjects.clear();
|
31 |
|
|
}
|
32 |
44133
|
michele.ar
|
|
33 |
51020
|
michele.ar
|
@Override
|
34 |
|
|
protected boolean updateRecord(final MdRecord record) {
|
35 |
|
|
log.debug("Found " + record.getProjects().size() + " projects");
|
36 |
44523
|
michele.ar
|
|
37 |
51020
|
michele.ar
|
if (record.getProjects().isEmpty()) { return false; }
|
38 |
49250
|
michele.ar
|
|
39 |
51020
|
michele.ar
|
final List<Project> list = record.getProjects()
|
40 |
|
|
.stream()
|
41 |
|
|
.map(Project::getInfoId)
|
42 |
|
|
.filter(StringUtils::isNotBlank)
|
43 |
|
|
.map(Project::newInstance)
|
44 |
|
|
.map(this::findEnrichedProject)
|
45 |
|
|
.collect(Collectors.toList());
|
46 |
49250
|
michele.ar
|
|
47 |
51020
|
michele.ar
|
log.debug("Processed " + list.size() + " projects");
|
48 |
49250
|
michele.ar
|
|
49 |
51020
|
michele.ar
|
if (list.isEmpty()) { return false; }
|
50 |
44133
|
michele.ar
|
|
51 |
51020
|
michele.ar
|
record.getProjects().clear();
|
52 |
|
|
record.getProjects().addAll(list);
|
53 |
44559
|
michele.ar
|
|
54 |
51020
|
michele.ar
|
return true;
|
55 |
44523
|
michele.ar
|
}
|
56 |
|
|
|
57 |
51020
|
michele.ar
|
private Project findEnrichedProject(final Project simpleProject) {
|
58 |
|
|
final String s = simpleProject.getFunder() + "/" + simpleProject.getProgram() + "/" + simpleProject.getCode();
|
59 |
|
|
if (!mapProjects.containsKey(s)) {
|
60 |
|
|
final Project project = resolveProject(s);
|
61 |
|
|
mapProjects.put(s, project != null ? project : simpleProject);
|
62 |
44523
|
michele.ar
|
}
|
63 |
51020
|
michele.ar
|
return mapProjects.get(s);
|
64 |
44523
|
michele.ar
|
}
|
65 |
|
|
|
66 |
48734
|
michele.ar
|
private Project resolveProject(final String s) {
|
67 |
|
|
try {
|
68 |
|
|
final String[] arr = s.split("/");
|
69 |
49368
|
michele.ar
|
final String url = String.format(baseUrl, arr[0], arr[1], arr[2]);
|
70 |
48734
|
michele.ar
|
return Project.newInstance(new URI(url));
|
71 |
|
|
} catch (final URISyntaxException e) {
|
72 |
|
|
log.error("Error resolving project: " + s, e);
|
73 |
|
|
throw new RuntimeException("Error resolving project: " + s, e);
|
74 |
|
|
}
|
75 |
|
|
}
|
76 |
51020
|
michele.ar
|
|
77 |
44133
|
michele.ar
|
}
|