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
|
logger.info("verificationCode: " + verificationCode);
|
35
|
userVerification.setVerificationCode(verificationCode);
|
36
|
userVerification.setDate(date);
|
37
|
|
38
|
userVerificationDAO.insert(userVerification);
|
39
|
|
40
|
logger.info("Insert user: " + username);
|
41
|
|
42
|
} catch (SQLException e) {
|
43
|
e.printStackTrace();
|
44
|
logger.info("Fail to insert user.", e);
|
45
|
}
|
46
|
|
47
|
}
|
48
|
|
49
|
|
50
|
/**
|
51
|
* Updates the row of verification table with the new date for the given username
|
52
|
*/
|
53
|
public void updateVerificationEntry(String username, String verificationCode, Timestamp date) {
|
54
|
|
55
|
try {
|
56
|
|
57
|
// Get userId to update user
|
58
|
UserVerification userVerificationOld = userVerificationDAO.fetchByUsername(username);
|
59
|
|
60
|
UserVerification userVerification = new UserVerification(username);
|
61
|
logger.info("1 . verificationCode: " + verificationCode);
|
62
|
userVerification.setVerificationCode(verificationCode);
|
63
|
logger.info("2 . verificationCode: " + verificationCode);
|
64
|
userVerification.setDate(date);
|
65
|
userVerification.setId(userVerificationOld.getId());
|
66
|
|
67
|
userVerificationDAO.update(userVerification);
|
68
|
|
69
|
logger.info("3 . verificationCode: " + verificationCode);
|
70
|
logger.info("Update user: " + username + " with verification code " + verificationCode);
|
71
|
|
72
|
} catch (SQLException e) {
|
73
|
e.printStackTrace();
|
74
|
logger.info("Fail to update user.", e);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
|
79
|
/**
|
80
|
* Checks if the verification row exists for the given username
|
81
|
*/
|
82
|
public boolean verificationEntryExists(String username) {
|
83
|
|
84
|
try {
|
85
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
86
|
|
87
|
if (userVerification == null) {
|
88
|
logger.info("There is no user with username: " + username);
|
89
|
return false;
|
90
|
}
|
91
|
else {
|
92
|
logger.info("User: " + username + " was found!");
|
93
|
return true;
|
94
|
}
|
95
|
} catch (SQLException e) {
|
96
|
e.printStackTrace();
|
97
|
logger.info("Fail to search user.", e);
|
98
|
}
|
99
|
|
100
|
return true;
|
101
|
//TODO
|
102
|
//return false;
|
103
|
//throw new UnsupportedOperationException();
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Checks if the verification row exists for the given username
|
108
|
*/
|
109
|
public boolean verificationEntryExists(String username, String verificationCode) {
|
110
|
|
111
|
try {
|
112
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
113
|
|
114
|
if (userVerification == null) {
|
115
|
logger.info("There is no user with username: " + username);
|
116
|
return false;
|
117
|
}
|
118
|
else {
|
119
|
logger.info("User: " + username + " was found!");
|
120
|
|
121
|
if (userVerification.getVerificationCode() == verificationCode) {
|
122
|
logger.info("... and verification code: " + verificationCode + " is correct!");
|
123
|
}
|
124
|
return true;
|
125
|
}
|
126
|
} catch (SQLException e) {
|
127
|
e.printStackTrace();
|
128
|
logger.info("Fail to search user.", e);
|
129
|
}
|
130
|
|
131
|
return true;
|
132
|
}
|
133
|
|
134
|
public DataSourceConnector getDataSourceConnector() {
|
135
|
return dataSourceConnector;
|
136
|
}
|
137
|
|
138
|
public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
|
139
|
this.dataSourceConnector = dataSourceConnector;
|
140
|
}
|
141
|
}
|