Revision 35161
Added by Antonis Lempesis over 9 years ago
modules/uoa-goldoa-service/trunk/src/main/java/eu/dnetlib/goldoa/service/PersonManagerImpl.java | ||
---|---|---|
6 | 6 |
import eu.dnetlib.goldoa.domain.PersonRole; |
7 | 7 |
import eu.dnetlib.goldoa.domain.Project; |
8 | 8 |
import eu.dnetlib.goldoa.domain.Role; |
9 |
import org.springframework.dao.EmptyResultDataAccessException; |
|
9 | 10 |
import org.springframework.jdbc.core.BatchPreparedStatementSetter; |
10 | 11 |
import org.springframework.jdbc.core.JdbcTemplate; |
11 | 12 |
import org.springframework.jdbc.core.PreparedStatementCreator; |
... | ... | |
45 | 46 |
"left join affiliation a on a.person = p.email\n" + |
46 | 47 |
"left join organisation o on o.id = a.organization\n" + |
47 | 48 |
"left join project pro on pro.coordinator=p.email\n" + |
49 |
"where email = ?\n" + |
|
48 | 50 |
"group by p.email, firstname, lastname, initials, password, telephone, active, orcidid\n"; |
49 | 51 |
|
50 | 52 |
private static final String INSERT_PERSON = "insert into person (email, firstname, lastname, initials, password, telephone, orcidid) values (?, ?, ?, ?, md5(?), ?, ?);"; |
... | ... | |
60 | 62 |
private static final String LOGIN = "select 1 from person where email = ? and password = md5(?)"; |
61 | 63 |
|
62 | 64 |
@Override |
63 |
public Person getByEmail(final String email) { |
|
64 |
return new JdbcTemplate(dataSource).queryForObject(GET_BY_EMAIL, new Object[]{email}, new int[]{Types.VARCHAR}, new RowMapper<Person>() { |
|
65 |
@Override |
|
66 |
public Person mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
67 |
Person person = new Person(); |
|
65 |
public Person getByEmail(final String email) throws PersonManagerException { |
|
66 |
try { |
|
67 |
return new JdbcTemplate(dataSource).queryForObject(GET_BY_EMAIL, new Object[]{email}, new int[]{Types.VARCHAR}, new RowMapper<Person>() { |
|
68 |
@Override |
|
69 |
public Person mapRow(ResultSet rs, int rowNum) throws SQLException { |
|
70 |
Person person = new Person(); |
|
68 | 71 |
|
69 |
person.setEmail(rs.getString("email")); |
|
70 |
person.setName(rs.getString("firstname")); |
|
71 |
person.setLastname(rs.getString("lastname")); |
|
72 |
person.setInitials(rs.getString("initials")); |
|
73 |
person.setTelephone(rs.getString("telephone")); |
|
74 |
person.setActive(rs.getBoolean("active")); |
|
75 |
person.setOrcidId(rs.getString("orcidid")); |
|
76 |
person.setPassword(rs.getString("password")); |
|
72 |
person.setEmail(rs.getString("email"));
|
|
73 |
person.setName(rs.getString("firstname"));
|
|
74 |
person.setLastname(rs.getString("lastname"));
|
|
75 |
person.setInitials(rs.getString("initials"));
|
|
76 |
person.setTelephone(rs.getString("telephone"));
|
|
77 |
person.setActive(rs.getBoolean("active"));
|
|
78 |
person.setOrcidId(rs.getString("orcidid"));
|
|
79 |
person.setPassword(rs.getString("password"));
|
|
77 | 80 |
|
78 |
person.setCoordinatedProjects(new ArrayList<String>()); |
|
79 |
for (String projectId:(String[]) rs.getArray("projects").getArray())
|
|
80 |
person.getCoordinatedProjects().add(projectId); |
|
81 |
person.setCoordinatedProjects(new ArrayList<String>());
|
|
82 |
for (String projectId : (String[]) rs.getArray("projects").getArray())
|
|
83 |
person.getCoordinatedProjects().add(projectId);
|
|
81 | 84 |
|
82 |
person.setAffiliations(new ArrayList<Affiliation>()); |
|
83 |
for (String affString:(String[]) rs.getArray("affiliations").getArray()) {
|
|
84 |
String[] parts = affString.split("||"); |
|
85 |
String org = parts[0]; |
|
86 |
String dep = parts[1]; |
|
87 |
Date start = parts[2] == "0"?null:new Date(Long.parseLong(parts[2]));
|
|
88 |
Date end = parts[3] == "0"?null:new Date(Long.parseLong(parts[3]));
|
|
85 |
person.setAffiliations(new ArrayList<Affiliation>());
|
|
86 |
for (String affString : (String[]) rs.getArray("affiliations").getArray()) {
|
|
87 |
String[] parts = affString.split("||");
|
|
88 |
String org = parts[0];
|
|
89 |
String dep = parts[1];
|
|
90 |
Date start = parts[2] == "0" ? null : new Date(Long.parseLong(parts[2]));
|
|
91 |
Date end = parts[3] == "0" ? null : new Date(Long.parseLong(parts[3]));
|
|
89 | 92 |
|
90 |
person.getAffiliations().add(new Affiliation(email, org, dep, start, end)); |
|
91 |
} |
|
93 |
person.getAffiliations().add(new Affiliation(email, org, dep, start, end));
|
|
94 |
}
|
|
92 | 95 |
|
93 |
person.setRoles(new ArrayList<PersonRole>()); |
|
94 |
for (String rString:(String[]) rs.getArray("affiliations").getArray()) {
|
|
95 |
String[] parts = rString.split("||"); |
|
96 |
String role = parts[0]; |
|
97 |
boolean approved = Boolean.parseBoolean(parts[1]); |
|
96 |
person.setRoles(new ArrayList<PersonRole>());
|
|
97 |
for (String rString : (String[]) rs.getArray("affiliations").getArray()) {
|
|
98 |
String[] parts = rString.split("||");
|
|
99 |
String role = parts[0];
|
|
100 |
boolean approved = Boolean.parseBoolean(parts[1]);
|
|
98 | 101 |
|
99 |
person.getRoles().add(new PersonRole(email, getRole(role).getId(), approved)); |
|
102 |
person.getRoles().add(new PersonRole(email, getRole(role).getId(), approved)); |
|
103 |
} |
|
104 |
|
|
105 |
return person; |
|
100 | 106 |
} |
101 |
|
|
102 |
return person;
|
|
103 |
}
|
|
104 |
});
|
|
107 |
}); |
|
108 |
} catch (EmptyResultDataAccessException e) {
|
|
109 |
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
|
110 |
} |
|
105 | 111 |
} |
106 | 112 |
|
107 | 113 |
@Override |
108 | 114 |
public void register(final Person person) throws PersonManagerException { |
109 |
if (getByEmail(person.getEmail()) != null) |
|
115 |
try { |
|
116 |
getByEmail(person.getEmail()); |
|
110 | 117 |
throw new PersonManagerException(PersonManagerException.ErrorCause.ALREADY_EXISTS); |
118 |
} catch (PersonManagerException e) { |
|
111 | 119 |
|
120 |
} |
|
121 |
|
|
112 | 122 |
new JdbcTemplate(dataSource).update(INSERT_PERSON, new PreparedStatementSetter() { |
113 | 123 |
@Override |
114 | 124 |
|
Also available in: Unified diff
fixed queryForObject calls