1
|
package eu.dnetlib.msro.workflows.nodes;
|
2
|
|
3
|
import java.util.Calendar;
|
4
|
import java.util.Map;
|
5
|
import java.util.Set;
|
6
|
import java.util.stream.Collectors;
|
7
|
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
|
13
|
import eu.dnetlib.clients.pimpa.PimpaStubFactory;
|
14
|
import eu.dnetlib.data.db.AffiliationsDao;
|
15
|
import eu.dnetlib.data.utils.IstiConstants;
|
16
|
import eu.dnetlib.data.utils.XsltFunctions;
|
17
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
18
|
import eu.dnetlib.msro.workflows.procs.Env;
|
19
|
|
20
|
public class PimpaUpdateJobNode extends SimpleJobNode {
|
21
|
|
22
|
@Autowired
|
23
|
private PimpaStubFactory pimpa;
|
24
|
|
25
|
@Autowired
|
26
|
private AffiliationsDao dao;
|
27
|
|
28
|
private boolean overridePersons = false;
|
29
|
private boolean overrideGroups = false;
|
30
|
private boolean capitalizeFullnames = false;
|
31
|
|
32
|
private static final Log log = LogFactory.getLog(PimpaUpdateJobNode.class);
|
33
|
|
34
|
@Override
|
35
|
protected String execute(final Env env) throws Exception {
|
36
|
log.info("Reimport records from PIMPA - START");
|
37
|
|
38
|
final Map<String, Boolean> oldPids = dao.getPersonIds().stream().collect(Collectors.toMap(s -> s, s -> true));
|
39
|
final Map<String, Boolean> oldGids = dao.getGroupIds().stream().collect(Collectors.toMap(s -> s, s -> true));
|
40
|
|
41
|
final Set<String> oldAffiliations = dao.listAffiliations().stream()
|
42
|
.map(a -> String.format("%s#%s#%s", a.getPid(), a.getGid(), a.getYear()))
|
43
|
.collect(Collectors.toSet());
|
44
|
|
45
|
pimpa.getStub()
|
46
|
.getDataRange(IstiConstants.PIMPA_START_YEAR, Calendar.getInstance().get(Calendar.YEAR))
|
47
|
.getYears()
|
48
|
.forEach(y -> {
|
49
|
y.getPersons().getList().forEach(p -> {
|
50
|
if (StringUtils.isNotBlank(p.getCode())) {
|
51
|
final String pid = "matricola:" + p.getCode().trim();
|
52
|
|
53
|
if (!oldPids.containsKey(pid)) {
|
54
|
dao.registerPerson(pid, String.format("%s %s", p.getName().trim(), p.getSurname().trim()));
|
55
|
oldPids.put(pid, false);
|
56
|
} else if (overridePersons && oldPids.get(pid)) {
|
57
|
log.info("Overriding person " + pid);
|
58
|
dao.updatePerson(pid, String.format("%s %s", p.getName().trim(), p.getSurname().trim()));
|
59
|
oldPids.put(pid, false);
|
60
|
}
|
61
|
p.getLabs().getList().forEach(lab -> {
|
62
|
if (StringUtils.isNotBlank(lab.getCode())) {
|
63
|
if (!oldGids.containsKey(lab.getCode())) {
|
64
|
dao.registerGroup(lab.getCode(), lab.getDescription(), lab.getType());
|
65
|
oldGids.put(lab.getCode(), false);
|
66
|
} else if (overrideGroups && oldGids.get(lab.getCode())) {
|
67
|
log.info("Overriding group " + lab.getCode());
|
68
|
dao.updateGroup(lab.getCode(), lab.getDescription(), lab.getType());
|
69
|
oldGids.put(lab.getCode(), false);
|
70
|
}
|
71
|
|
72
|
if (!oldAffiliations.contains(String.format("%s#%s#%s", pid, lab.getCode(), y.getYear()))) {
|
73
|
dao.registerAffiliation(pid, lab.getCode(), y.getYear());
|
74
|
}
|
75
|
}
|
76
|
});
|
77
|
}
|
78
|
});
|
79
|
});
|
80
|
|
81
|
if (capitalizeFullnames) {
|
82
|
log.info("Capitalizing person fullnames");
|
83
|
dao.listPersons().forEach(p -> {
|
84
|
dao.updatePerson(p.getId(), XsltFunctions.capitalize(p.getName()));
|
85
|
});;
|
86
|
}
|
87
|
|
88
|
log.info("Reimport records from PIMPA - END");
|
89
|
|
90
|
return Arc.DEFAULT_ARC;
|
91
|
}
|
92
|
|
93
|
public boolean isOverridePersons() {
|
94
|
return overridePersons;
|
95
|
}
|
96
|
|
97
|
public void setOverridePersons(final boolean overridePersons) {
|
98
|
this.overridePersons = overridePersons;
|
99
|
}
|
100
|
|
101
|
public boolean isOverrideGroups() {
|
102
|
return overrideGroups;
|
103
|
}
|
104
|
|
105
|
public void setOverrideGroups(final boolean overrideGroups) {
|
106
|
this.overrideGroups = overrideGroups;
|
107
|
}
|
108
|
|
109
|
public boolean isCapitalizeFullnames() {
|
110
|
return capitalizeFullnames;
|
111
|
}
|
112
|
|
113
|
public void setCapitalizeFullnames(final boolean capitalizeFullnames) {
|
114
|
this.capitalizeFullnames = capitalizeFullnames;
|
115
|
}
|
116
|
}
|