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() {
38
		affiliations = dao.listPersonsWithAffiliations()
39
				.stream()
40
				.collect(Collectors.toMap(Person::getId, a -> a));
41
	}
42

    
43
	@Override
44
	protected boolean updateRecord(final MdRecord doc) {
45
		final int year = doc.getDate();
46

    
47
		for (final CnrPerson cp : doc.getCnrPersons()) {
48
			cp.getAffiliations().clear();
49

    
50
			if (year >= IstiConstants.PIMPA_START_YEAR) {
51
				final String infoId = cp.getId();
52
				final Pattern pattern = Pattern.compile("info:cnr-pdr\\/author\\/(.+)\\/(.+)\\/(.+)");
53
				final Matcher matcher = pattern.matcher(infoId);
54
				if (matcher.find()) {
55
					final String code = matcher.group(1);
56
					final String fullname = XsltFunctions.capitalize(String.format("%s %s", matcher.group(3), matcher.group(2)));
57

    
58
					final Person p = affiliations.get(code);
59

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

    
79
						final Person np = new Person(code, fullname);
80
						np.getAffiliations().put(year, Sets.newHashSet(UNKNOWN_GROUP));
81

    
82
						affiliations.put(code, np);
83
					}
84
				} else {
85
					log.warn("Invalid infoId: " + infoId);
86
				}
87
			}
88

    
89
		}
90
		return true;
91
	}
92

    
93
}
(3-3/8)