1
|
package eu.dnetlib.openaire.user.utils;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.UserVerification;
|
4
|
import eu.dnetlib.openaire.user.dao.UserVerificationDAO;
|
5
|
import eu.dnetlib.openaire.user.store.DataSourceConnector;
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.stereotype.Component;
|
9
|
|
10
|
import java.sql.SQLException;
|
11
|
import java.sql.Timestamp;
|
12
|
|
13
|
/**
|
14
|
* Created by kiatrop on 2/10/2017.
|
15
|
*/
|
16
|
@Component(value = "verificationActions")
|
17
|
public class VerificationActions {
|
18
|
|
19
|
@Autowired
|
20
|
private DataSourceConnector dataSourceConnector;
|
21
|
|
22
|
private Logger logger = Logger.getLogger(VerificationActions.class);
|
23
|
|
24
|
@Autowired
|
25
|
private UserVerificationDAO userVerificationDAO;
|
26
|
|
27
|
/**
|
28
|
* Adds new row in the verification table
|
29
|
*/
|
30
|
public void addVerificationEntry(String username, String verificationCode, Timestamp date) {
|
31
|
|
32
|
try {
|
33
|
UserVerification userVerification = new UserVerification(username);
|
34
|
userVerification.setVerificationCode(verificationCode);
|
35
|
userVerification.setDate(date);
|
36
|
|
37
|
userVerificationDAO.insert(userVerification);
|
38
|
|
39
|
logger.info("Insert user: " + username);
|
40
|
|
41
|
} catch (SQLException e) {
|
42
|
e.printStackTrace();
|
43
|
logger.info("Fail to insert user.", e);
|
44
|
}
|
45
|
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* Updates the row of verification table with the new date for the given username
|
51
|
*/
|
52
|
public void updateVerificationEntry(String username, String verificationCode, Timestamp date) {
|
53
|
|
54
|
try {
|
55
|
UserVerification userVerification = new UserVerification(username);
|
56
|
userVerification.setVerificationCode(verificationCode);
|
57
|
userVerification.setDate(date);
|
58
|
|
59
|
userVerificationDAO.update(userVerification);
|
60
|
|
61
|
logger.info("Update user: " + username + " with verification code " + verificationCode);
|
62
|
|
63
|
} catch (SQLException e) {
|
64
|
e.printStackTrace();
|
65
|
logger.info("Fail to update user.", e);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
|
70
|
/**
|
71
|
* Checks if the verification row exists for the given username
|
72
|
*/
|
73
|
public boolean verificationEntryExists(String username) {
|
74
|
|
75
|
try {
|
76
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
77
|
|
78
|
if (userVerification == null) {
|
79
|
logger.info("There is no user with username: " + username);
|
80
|
return false;
|
81
|
}
|
82
|
else {
|
83
|
logger.info("User: " + username + " was found!");
|
84
|
return true;
|
85
|
}
|
86
|
} catch (SQLException e) {
|
87
|
e.printStackTrace();
|
88
|
logger.info("Fail to search user.", e);
|
89
|
}
|
90
|
|
91
|
return true;
|
92
|
//TODO
|
93
|
//return false;
|
94
|
//throw new UnsupportedOperationException();
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Checks if the verification row exists for the given username
|
99
|
*/
|
100
|
public boolean verificationEntryExists(String username, String verificationCode) {
|
101
|
//TODO
|
102
|
//return false;
|
103
|
throw new UnsupportedOperationException();
|
104
|
}
|
105
|
|
106
|
public DataSourceConnector getDataSourceConnector() {
|
107
|
return dataSourceConnector;
|
108
|
}
|
109
|
|
110
|
public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
|
111
|
this.dataSourceConnector = dataSourceConnector;
|
112
|
}
|
113
|
}
|