1
|
package eu.dnetlib.data.claims.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.entity.Notification;
|
4
|
import eu.dnetlib.data.claims.utils.QueryGenerator;
|
5
|
import eu.dnetlib.data.claims.sql.SQLStoreException;
|
6
|
import eu.dnetlib.data.claims.sql.SqlDAO;
|
7
|
import org.apache.log4j.Logger;
|
8
|
|
9
|
import java.sql.ResultSet;
|
10
|
import java.util.ArrayList;
|
11
|
import java.util.List;
|
12
|
|
13
|
public class FetchNotificationHandler {
|
14
|
private Logger log = Logger.getLogger(this.getClass());
|
15
|
|
16
|
SqlDAO sqlDAO = null;
|
17
|
QueryGenerator queryGenerator = null;
|
18
|
|
19
|
public FetchNotificationHandler() {}
|
20
|
|
21
|
public Notification fetchNotification(String openaireId, String userMail) throws Exception, SQLStoreException {
|
22
|
ArrayList<Object> params = new ArrayList<>();
|
23
|
String query = queryGenerator.generateSelectNotificationQuery(openaireId, userMail, params);
|
24
|
ResultSet rs = sqlDAO.executePreparedQuery(query, params);
|
25
|
|
26
|
Notification notification = fetchNotificationByResultSet(rs);
|
27
|
if(notification == null) {
|
28
|
log.info("No notification for openaireId: "+ openaireId+" and userMail: "+userMail+"\n");
|
29
|
}
|
30
|
|
31
|
return notification;
|
32
|
}
|
33
|
|
34
|
public List<Notification> fetchTrueNotifications() throws Exception, SQLStoreException {
|
35
|
String query = queryGenerator.generateSelectTrueNotificationsQuery();
|
36
|
ResultSet rs = sqlDAO.executePreparedQuery(query);
|
37
|
|
38
|
List<Notification> notifications = fetchNotificationsByResultSet(rs);
|
39
|
|
40
|
return notifications;
|
41
|
}
|
42
|
|
43
|
public Notification fetchNotificationByResultSet(ResultSet rs) throws Exception {
|
44
|
Notification notification = null;
|
45
|
if(rs.next()) {
|
46
|
notification = new Notification();
|
47
|
notification.setOpenaireId(rs.getString("openaire_id"));
|
48
|
notification.setUserMail(rs.getString("user_email"));
|
49
|
notification.setDate(rs.getTimestamp("last_interaction_date"));
|
50
|
notification.setFrequency(rs.getInt("frequency"));
|
51
|
notification.setNotify(rs.getBoolean("notify"));
|
52
|
}
|
53
|
return notification;
|
54
|
}
|
55
|
|
56
|
public List<Notification> fetchNotificationsByResultSet(ResultSet rs) throws Exception {
|
57
|
List<Notification> notifications = null;
|
58
|
while(rs.next()) {
|
59
|
if(notifications == null) {
|
60
|
notifications = new ArrayList<>();
|
61
|
}
|
62
|
Notification notification;
|
63
|
notification = new Notification();
|
64
|
notification.setOpenaireId(rs.getString("openaire_id"));
|
65
|
notification.setUserMail(rs.getString("user_email"));
|
66
|
notification.setDate(rs.getTimestamp("last_interaction_date"));
|
67
|
notification.setFrequency(rs.getInt("frequency"));
|
68
|
notification.setNotify(rs.getBoolean("notify"));
|
69
|
|
70
|
notifications.add(notification);
|
71
|
}
|
72
|
return notifications;
|
73
|
}
|
74
|
|
75
|
public SqlDAO getSqlDAO() {
|
76
|
return sqlDAO;
|
77
|
}
|
78
|
|
79
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
80
|
this.sqlDAO = sqlDAO;
|
81
|
}
|
82
|
|
83
|
public QueryGenerator getQueryGenerator() {
|
84
|
return queryGenerator;
|
85
|
}
|
86
|
|
87
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
88
|
this.queryGenerator = queryGenerator;
|
89
|
}
|
90
|
}
|