Project

General

Profile

1
package eu.dnetlib.data.mdstore.plugins;
2

    
3
import java.util.HashMap;
4
import java.util.Map;
5
import java.util.Set;
6
import java.util.regex.Matcher;
7
import java.util.regex.Pattern;
8
import java.util.stream.Collectors;
9

    
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13

    
14
import com.google.common.collect.Sets;
15

    
16
import eu.dnetlib.data.db.AffiliationsDao;
17
import eu.dnetlib.data.db.Group;
18
import eu.dnetlib.data.db.Person;
19
import eu.dnetlib.data.mdstore.plugins.objects.Affiliation;
20
import eu.dnetlib.data.mdstore.plugins.objects.CnrPerson;
21
import eu.dnetlib.data.mdstore.plugins.objects.MdRecord;
22
import eu.dnetlib.data.utils.IstiConstants;
23
import eu.dnetlib.data.utils.XsltFunctions;
24

    
25
public class EnrichLabsPlugin extends MdRecordPlugin {
26

    
27
	private static final Log log = LogFactory.getLog(EnrichLabsPlugin.class);
28

    
29
	private static final Group UNKNOWN_GROUP = new Group("UNKNOWN", "UNKNOWN", "UNKNOWN");
30

    
31
	@Autowired
32
	private AffiliationsDao dao;
33

    
34
	private Map<String, Person> affiliations = new HashMap<>();
35

    
36
	@Override
37
	protected void reconfigure(final Map<String, String> params) {
38
		affiliations = dao.listPersonsWithAffiliations().stream().collect(Collectors.toMap(Person::getId, a -> a));
39
	}
40

    
41
	@Override
42
	protected void resetConfiguration() {
43
		affiliations.clear();
44
	}
45

    
46
	@Override
47
	protected boolean updateRecord(final String recordId, final MdRecord doc) {
48
		final int year = doc.getDate();
49

    
50
		for (final CnrPerson cp : doc.getCnrPersons()) {
51
			cp.getAffiliations().clear();
52

    
53
			if (year >= IstiConstants.PIMPA_START_YEAR) {
54
				final String infoId = cp.getId();
55
				final Pattern pattern = Pattern.compile("info:cnr-pdr\\/author\\/(.+)\\/(.+)\\/(.+)");
56
				final Matcher matcher = pattern.matcher(infoId);
57
				if (matcher.find()) {
58
					final String code = matcher.group(1);
59
					final String surname = matcher.group(2);
60
					final String name = matcher.group(3);
61

    
62
					final Person p = affiliations.get(code);
63

    
64
					if (p != null) {
65
						final Set<Group> groups = p.getAffiliations().get(year);
66
						if (groups != null) {
67
							cp.setName(p.getFullname());
68
							for (final Group g : groups) {
69
								if (!g.getId().equals(UNKNOWN_GROUP.getId())) {
70
									cp.getAffiliations().add(new Affiliation(code,
71
											g.getType(), g.getId(), g.getName()));
72
								}
73
							}
74
						} else {
75
							log.info("Affiliation not found, infoId: " +
76
									infoId + ", year: " + year);
77
							dao.registerAffiliation(code, UNKNOWN_GROUP.getId(), year);
78
							p.getAffiliations().put(year, Sets.newHashSet(UNKNOWN_GROUP));
79
						}
80

    
81
					} else {
82
						log.info("Person not found, infoId: " + infoId + ", year: " + year);
83
						dao.registerPerson(code, XsltFunctions.capitalize(name), XsltFunctions.capitalize((surname)));
84
						dao.registerAffiliation(code, UNKNOWN_GROUP.getId(), year);
85

    
86
						final Person np = new Person(code, String.format("%s, %s", surname, name));
87
						np.getAffiliations().put(year, Sets.newHashSet(UNKNOWN_GROUP));
88

    
89
						affiliations.put(code, np);
90
					}
91
				} else {
92
					log.warn("Invalid infoId: " + infoId);
93
				}
94
			}
95

    
96
		}
97
		return true;
98
	}
99

    
100
}
(5-5/12)