1
|
package eu.dnetlib.goldoa.service;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Affiliation;
|
4
|
import eu.dnetlib.goldoa.domain.OrganizationManagerException;
|
5
|
import eu.dnetlib.goldoa.domain.Person;
|
6
|
import eu.dnetlib.goldoa.domain.PersonManagerException;
|
7
|
import eu.dnetlib.goldoa.domain.PersonRole;
|
8
|
import eu.dnetlib.goldoa.domain.Project;
|
9
|
import eu.dnetlib.goldoa.domain.Role;
|
10
|
import eu.dnetlib.goldoa.service.dao.OrganizationDAO;
|
11
|
import eu.dnetlib.goldoa.service.dao.PersonDAO;
|
12
|
import eu.dnetlib.goldoa.service.dao.ProjectDAO;
|
13
|
import eu.dnetlib.goldoa.service.utils.EmailUtils;
|
14
|
import org.apache.commons.codec.digest.DigestUtils;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.dao.EmptyResultDataAccessException;
|
17
|
import org.springframework.transaction.annotation.Transactional;
|
18
|
|
19
|
import javax.mail.MessagingException;
|
20
|
import java.math.BigInteger;
|
21
|
import java.security.SecureRandom;
|
22
|
import java.util.ArrayList;
|
23
|
import java.util.List;
|
24
|
import java.util.UUID;
|
25
|
import java.util.concurrent.ExecutorService;
|
26
|
|
27
|
/**
|
28
|
* Created by antleb on 3/4/15.
|
29
|
*/
|
30
|
@Transactional
|
31
|
public class PersonManagerImpl implements PersonManager {
|
32
|
|
33
|
@Autowired
|
34
|
private PersonDAO personDAO;
|
35
|
@Autowired
|
36
|
private ProjectDAO projectDAO;
|
37
|
@Autowired
|
38
|
private OrganizationDAO organizationDAO;
|
39
|
@Autowired
|
40
|
private PublisherManager publisherManager;
|
41
|
@Autowired
|
42
|
private OrganizationManager organizationManager;
|
43
|
@Autowired
|
44
|
private EmailUtils emailUtils;
|
45
|
@Autowired
|
46
|
ExecutorService executorService;
|
47
|
|
48
|
private int tokenTTL = 30;
|
49
|
|
50
|
public static int generateId(Affiliation affiliation) {
|
51
|
StringBuilder sb = new StringBuilder();
|
52
|
|
53
|
if (affiliation.getPerson() != null)
|
54
|
sb.append(affiliation.getPerson().getId());
|
55
|
if (affiliation.getOrganization() != null)
|
56
|
sb.append(affiliation.getOrganization().getId());
|
57
|
|
58
|
return DigestUtils.md5Hex(sb.toString()).hashCode();
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public Person getById(String personId) throws PersonManagerException {
|
63
|
try {
|
64
|
Person person = personDAO.getPersonById(personId);
|
65
|
|
66
|
loadPersonRelations(person);
|
67
|
|
68
|
return person;
|
69
|
} catch (EmptyResultDataAccessException e) {
|
70
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public Person getByEmail(final String email) throws PersonManagerException {
|
76
|
try {
|
77
|
Person person = personDAO.getPersonByEmail(email);
|
78
|
|
79
|
loadPersonRelations(person);
|
80
|
|
81
|
return person;
|
82
|
} catch (EmptyResultDataAccessException e) {
|
83
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public Person savePerson(final Person person) throws PersonManagerException {
|
89
|
final List<PersonRole> previousRoles;
|
90
|
|
91
|
if (person.getId() == null) {
|
92
|
person.setId(generateId(person));
|
93
|
person.setSource("portal");
|
94
|
|
95
|
previousRoles = new ArrayList<PersonRole>();
|
96
|
} else {
|
97
|
previousRoles = personDAO.getPersonById(person.getId()).getRoles();
|
98
|
}
|
99
|
|
100
|
personDAO.savePerson(person);
|
101
|
deletePersonRelations(person);
|
102
|
savePersonRelations(person);
|
103
|
|
104
|
loadPersonRelations(person);
|
105
|
|
106
|
executorService.submit(new Runnable() {
|
107
|
@Override
|
108
|
public void run() {
|
109
|
try {
|
110
|
List<Role> roles = new ArrayList<Role>();
|
111
|
|
112
|
for (PersonRole pRole : person.getRoles()) {
|
113
|
if (!previousRoles.contains(pRole) && !pRole.isApproved())
|
114
|
roles.add(pRole.getRole());
|
115
|
}
|
116
|
|
117
|
if (roles.size() > 0) {
|
118
|
List<Person> moderators = getModerators();
|
119
|
|
120
|
emailUtils.sendUserRoleRequestedEmail(person, roles);
|
121
|
|
122
|
for (Person moderator : moderators)
|
123
|
emailUtils.sendModeratorRoleRequestedEmail(moderator, person, roles);
|
124
|
}
|
125
|
} catch (MessagingException e) {
|
126
|
e.printStackTrace();
|
127
|
}
|
128
|
}
|
129
|
});
|
130
|
|
131
|
return person;
|
132
|
}
|
133
|
|
134
|
@Override
|
135
|
public void register(final Person person) throws PersonManagerException {
|
136
|
final String token = UUID.randomUUID().toString();
|
137
|
|
138
|
try {
|
139
|
personDAO.getPersonByEmail(person.getEmail());
|
140
|
throw new PersonManagerException(PersonManagerException.ErrorCause.ALREADY_EXISTS);
|
141
|
} catch (EmptyResultDataAccessException e) {
|
142
|
} catch (PersonManagerException e) {
|
143
|
throw e;
|
144
|
} catch (RuntimeException e) {
|
145
|
e.printStackTrace();
|
146
|
throw e;
|
147
|
}
|
148
|
|
149
|
this.savePerson(person);
|
150
|
personDAO.saveToken(person.getId(), token, tokenTTL);
|
151
|
|
152
|
executorService.submit(new Runnable() {
|
153
|
@Override
|
154
|
public void run() {
|
155
|
try {
|
156
|
emailUtils.sendActivationEmail(person, token);
|
157
|
} catch (MessagingException e) {
|
158
|
e.printStackTrace();
|
159
|
}
|
160
|
}
|
161
|
});
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
public Person login(String email, String password) throws PersonManagerException {
|
166
|
|
167
|
if (personDAO.verifyLogin(email, password)) {
|
168
|
Person person = getByEmail(email);
|
169
|
|
170
|
if (person.isActive())
|
171
|
return person;
|
172
|
else
|
173
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_ACTIVATED);
|
174
|
} else {
|
175
|
throw new PersonManagerException(PersonManagerException.ErrorCause.WRONG_EMAIL_OR_PASSWORD);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
@Override
|
180
|
public Person activate(final String email, final String token) throws PersonManagerException {
|
181
|
|
182
|
if (!personDAO.verifyToken(email, token))
|
183
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
184
|
else {
|
185
|
Person person = personDAO.getPersonByEmail(email);
|
186
|
|
187
|
personDAO.activateUser(person.getId());
|
188
|
personDAO.deleteToken(person.getId(), token);
|
189
|
}
|
190
|
|
191
|
return getByEmail(email);
|
192
|
}
|
193
|
|
194
|
@Override
|
195
|
public void resetPassword(final String email) throws PersonManagerException {
|
196
|
SecureRandom random = new SecureRandom();
|
197
|
final String newPassword = new BigInteger(50, random).toString(32);
|
198
|
|
199
|
if (!personDAO.updatePassword(newPassword, email)) {
|
200
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
201
|
}
|
202
|
|
203
|
executorService.submit(new Runnable() {
|
204
|
@Override
|
205
|
public void run() {
|
206
|
Person person = personDAO.getPersonByEmail(email);
|
207
|
|
208
|
try {
|
209
|
emailUtils.sendResetPasswordEmail(person, newPassword);
|
210
|
} catch (MessagingException e) {
|
211
|
e.printStackTrace();
|
212
|
}
|
213
|
}
|
214
|
});
|
215
|
}
|
216
|
|
217
|
@Override
|
218
|
public List<Role> getRoles() {
|
219
|
return personDAO.getRoles();
|
220
|
}
|
221
|
|
222
|
@Override
|
223
|
public List<Person> getModerators() {
|
224
|
return personDAO.getModerators();
|
225
|
}
|
226
|
|
227
|
@Override
|
228
|
public List<Person> getAccountingOfficers() {
|
229
|
return personDAO.getAccountingOfficers();
|
230
|
}
|
231
|
|
232
|
@Override
|
233
|
public void activateUser(String userId) {
|
234
|
personDAO.activateUser(userId);
|
235
|
}
|
236
|
|
237
|
@Override
|
238
|
public void activateUsers(List<String> userIds) {
|
239
|
for (String userId : userIds) {
|
240
|
personDAO.activateUser(userId);
|
241
|
}
|
242
|
}
|
243
|
|
244
|
@Override
|
245
|
public void deactivateUser(String userId) {
|
246
|
personDAO.deactivateUser(userId);
|
247
|
}
|
248
|
|
249
|
@Override
|
250
|
public void deactivateUsers(List<String> userIds) {
|
251
|
for (String userId : userIds) {
|
252
|
personDAO.deactivateUser(userId);
|
253
|
}
|
254
|
}
|
255
|
|
256
|
@Override
|
257
|
public void acceptUserRole(final String userId, final String roleId) {
|
258
|
personDAO.acceptUserRole(userId, roleId);
|
259
|
|
260
|
executorService.submit(new Runnable() {
|
261
|
@Override
|
262
|
public void run() {
|
263
|
Person person = personDAO.getPersonById(userId);
|
264
|
Role role = personDAO.getRole(roleId);
|
265
|
List<Person> moderators = getModerators();
|
266
|
|
267
|
try {
|
268
|
emailUtils.sendUserRoleAcceptedEmail(person, role);
|
269
|
|
270
|
for (Person moderator : moderators)
|
271
|
emailUtils.sendModeratorRoleAcceptedEmail(moderator, person, role);
|
272
|
} catch (MessagingException e) {
|
273
|
e.printStackTrace();
|
274
|
}
|
275
|
}
|
276
|
});
|
277
|
}
|
278
|
|
279
|
@Override
|
280
|
public void rejectRole(final String userId, final String roleId) {
|
281
|
personDAO.rejectUserRole(userId, roleId);
|
282
|
|
283
|
executorService.submit(new Runnable() {
|
284
|
@Override
|
285
|
public void run() {
|
286
|
Person person = personDAO.getPersonById(userId);
|
287
|
Role role = personDAO.getRole(roleId);
|
288
|
List<Person> moderators = getModerators();
|
289
|
|
290
|
try {
|
291
|
emailUtils.sendUserRoleRejectedEmail(person, role);
|
292
|
|
293
|
for (Person moderator : moderators)
|
294
|
emailUtils.sendModeratorRoleRejectedEmail(moderator, person, role);
|
295
|
} catch (MessagingException e) {
|
296
|
e.printStackTrace();
|
297
|
}
|
298
|
}
|
299
|
});
|
300
|
}
|
301
|
|
302
|
@Override
|
303
|
public List<Person> getUsers() {
|
304
|
List<Person> persons = personDAO.getUsers();
|
305
|
|
306
|
for (Person person : persons) {
|
307
|
loadPersonRelations(person);
|
308
|
}
|
309
|
|
310
|
return persons;
|
311
|
}
|
312
|
|
313
|
@Override
|
314
|
public String generateId(Person person) {
|
315
|
StringBuilder sb = new StringBuilder();
|
316
|
|
317
|
sb.append(person.getName()).append(person.getLastname()).append(person.getEmail());
|
318
|
return "portal::" + DigestUtils.md5Hex(sb.toString());
|
319
|
}
|
320
|
|
321
|
private void deletePersonRelations(final Person person) {
|
322
|
personDAO.deleteAffiliations(person.getId());
|
323
|
personDAO.deletePersonRoles(person.getId());
|
324
|
personDAO.removeProjectCoordinators(person.getId());
|
325
|
}
|
326
|
|
327
|
private void savePersonRelations(final Person person) {
|
328
|
List<String> projectIds = new ArrayList<String>();
|
329
|
|
330
|
if (person.getCoordinatedProjects() != null)
|
331
|
for (Project project : person.getCoordinatedProjects())
|
332
|
projectIds.add(project.getId());
|
333
|
|
334
|
if (person.getPublisher() != null && person.getPublisher().getId() == null)
|
335
|
publisherManager.savePublisher(person.getPublisher());
|
336
|
|
337
|
for (Affiliation affiliation : person.getAffiliations()) {
|
338
|
if (affiliation.getOrganization() != null && affiliation.getOrganization().getId() == null)
|
339
|
try {
|
340
|
organizationManager.saveOrganization(affiliation.getOrganization());
|
341
|
} catch (OrganizationManagerException e) {
|
342
|
e.printStackTrace();
|
343
|
}
|
344
|
}
|
345
|
|
346
|
personDAO.saveProjectCoordinators(person.getId(), projectIds);
|
347
|
personDAO.savePersonRoles(person.getId(), person.getRoles());
|
348
|
personDAO.saveAffiliations(person, person.getAffiliations());
|
349
|
|
350
|
|
351
|
}
|
352
|
|
353
|
private void loadPersonRelations(Person person) {
|
354
|
|
355
|
if (person.getPublisher() != null && person.getPublisher().getId() != null) {
|
356
|
person.setPublisher(publisherManager.getPublisher(person.getPublisher().getId()));
|
357
|
} else {
|
358
|
person.setPublisher(null);
|
359
|
}
|
360
|
|
361
|
person.setCoordinatedProjects(projectDAO.getProjectsCoordinatedBy(person.getId()));
|
362
|
|
363
|
if (person.getAffiliations() != null) {
|
364
|
for (Affiliation affiliation : person.getAffiliations()) {
|
365
|
if (affiliation.getOrganization() != null)
|
366
|
affiliation.setOrganization(organizationDAO.getOrganization(affiliation.getOrganization().getId()));
|
367
|
}
|
368
|
}
|
369
|
|
370
|
if (person.getRoles() != null)
|
371
|
for (int i = 0; i < person.getRoles().size(); i++) {
|
372
|
person.getRoles().get(i).setRole(personDAO.getRole(person.getRoles().get(i).getRole().getId()));
|
373
|
}
|
374
|
}
|
375
|
|
376
|
public int getTokenTTL() {
|
377
|
return tokenTTL;
|
378
|
}
|
379
|
|
380
|
public void setTokenTTL(int tokenTTL) {
|
381
|
this.tokenTTL = tokenTTL;
|
382
|
}
|
383
|
}
|