Project

General

Profile

1
package eu.dnetlib.data.db;
2

    
3
import java.util.HashMap;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.TreeSet;
9
import java.util.stream.Collectors;
10

    
11
import org.springframework.beans.factory.annotation.Required;
12
import org.springframework.jdbc.core.JdbcTemplate;
13
import org.springframework.jdbc.core.RowMapper;
14

    
15
public class AffiliationsDao {
16

    
17
	private JdbcTemplate jdbcTemplate;
18

    
19
	public List<Affiliation> listAffiliations() {
20
		return jdbcTemplate.query("select * from affiliations", (RowMapper<Affiliation>) (rs, rowNum) -> {
21
			return new Affiliation(rs.getString("pid"), rs.getString("gid"), rs.getInt("year"));
22
		});
23
	}
24

    
25
	public List<Person> listPersonsWithAffiliations() {
26

    
27
		final Map<String, Person> temp = new HashMap<>();
28

    
29
		for (final Map<String, Object> map : jdbcTemplate.queryForList("select * from affiliations_view")) {
30
			final String pid = (String) map.get("pid");
31
			final Integer year = (Integer) map.get("year");
32
			final Group group = new Group((String) map.get("gid"), (String) map.get("gname"), (String) map.get("gtype"));
33

    
34
			temp.putIfAbsent(pid, new Person(pid, (String) map.get("pname")));
35
			temp.get(pid).getAffiliations().putIfAbsent(year, new TreeSet<>((g1, g2) -> {
36
				final int n1 = calculateIntegerForGroup(g1.getType());
37
				final int n2 = calculateIntegerForGroup(g2.getType());
38
				return (n1 == n2) ? g1.getId().compareTo(g2.getId()) : Integer.compare(n1, n2);
39
			}));
40
			temp.get(pid).getAffiliations().get(year).add(group);
41
		}
42

    
43
		return temp.values()
44
				.stream()
45
				.sorted((p1, p2) -> p1.getName().compareTo(p2.getName()))
46
				.collect(Collectors.toList());
47

    
48
	}
49

    
50
	private int calculateIntegerForGroup(final String type) {
51
		if (type.equalsIgnoreCase("Laboratorio")) {
52
			return 0;
53
		} else if (type.equalsIgnoreCase("Servizio")) {
54
			return 10;
55
		} else if (type.equalsIgnoreCase("Centro")) {
56
			return 20;
57
		} else {
58
			return 100;
59
		}
60
	}
61

    
62
	public List<Person> listPersons() {
63
		return jdbcTemplate.query("select * from persons", (RowMapper<Person>) (rs, rowNum) -> {
64
			return new Person(rs.getString("id"), rs.getString("fullname"));
65
		});
66
	}
67

    
68
	public List<Group> listGroups() {
69
		return jdbcTemplate.query("select * from groups", (RowMapper<Group>) (rs, rowNum) -> {
70
			return new Group(rs.getString("id"), rs.getString("name"), rs.getString("type"));
71
		});
72
	}
73

    
74
	public Set<String> getPersonIds() {
75
		return new HashSet<>(jdbcTemplate.query("select id from persons", (RowMapper<String>) (rs, rowNum) -> rs.getString("id")));
76
	}
77

    
78
	public Set<String> getGroupIds() {
79
		return new HashSet<>(jdbcTemplate.query("select id from groups", (RowMapper<String>) (rs, rowNum) -> rs.getString("id")));
80
	}
81

    
82
	public void updatePerson(final String code, final String name) {
83
		jdbcTemplate.update("UPDATE persons SET fullname = ? WHERE id = ?", name, code);
84

    
85
	}
86

    
87
	public void registerPerson(final String code, final String name) {
88
		jdbcTemplate.update("INSERT into persons(id, fullname) VALUES (?, ?)", code, name);
89
	}
90

    
91
	public void updateGroup(final String id, final String name, final String type) {
92
		jdbcTemplate.update("UPDATE groups SET (name, type) = (?, ?) WHERE id = ?", name, type, id);
93
	}
94

    
95
	public void registerGroup(final String id, final String name, final String type) {
96
		jdbcTemplate.update("INSERT into groups(id, name, type) VALUES (?, ?, ?)", id, name, type);
97
	}
98

    
99
	public void registerAffiliation(final String pid, final String gid, final int year) {
100
		jdbcTemplate.update("INSERT into affiliations(pid, gid, year) VALUES (?, ?, ?)", pid, gid, year);
101
	}
102

    
103
	public JdbcTemplate getJdbcTemplate() {
104
		return jdbcTemplate;
105
	}
106

    
107
	@Required
108
	public void setJdbcTemplate(final JdbcTemplate jdbcTemplate) {
109
		this.jdbcTemplate = jdbcTemplate;
110
	}
111

    
112
}
(2-2/4)