1
|
package eu.dnetlib.data.claims.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.entity.Notification;
|
4
|
import eu.dnetlib.data.claims.sql.SQLStoreException;
|
5
|
import eu.dnetlib.data.claims.sql.SqlDAO;
|
6
|
import eu.dnetlib.data.claims.utils.QueryGenerator;
|
7
|
import org.apache.logging.log4j.LogManager;
|
8
|
import org.apache.logging.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Calendar;
|
15
|
import java.util.Date;
|
16
|
|
17
|
public class NotificationHandler {
|
18
|
SqlDAO sqlDAO = null;
|
19
|
QueryGenerator queryGenerator = null;
|
20
|
|
21
|
private static final Logger logger = LogManager.getLogger(NotificationHandler.class);
|
22
|
|
23
|
@Autowired
|
24
|
private String targetHour;
|
25
|
@Autowired
|
26
|
private String targetMinute;
|
27
|
@Autowired
|
28
|
private String targetSecond;
|
29
|
@Autowired
|
30
|
private String defaultFrequencyInHours;
|
31
|
|
32
|
public String buildAndInsertNotification(String openaireId, String userMail, int frequency, boolean notify) throws Exception, SQLStoreException {
|
33
|
String id = null;
|
34
|
|
35
|
logger.info("Trying to create a notification {openaireId:"+openaireId+", userMail:"+userMail+", frequency:"+frequency+", notify:"+notify+"}");
|
36
|
Notification notification = buildNotification(openaireId, userMail, frequency, notify);
|
37
|
|
38
|
id = saveNotification(notification);
|
39
|
|
40
|
return id;
|
41
|
}
|
42
|
|
43
|
private Notification buildNotification(String openaireId, String userMail, int frequency, boolean notify) {
|
44
|
Notification notification = new Notification();
|
45
|
notification.setOpenaireId(openaireId);
|
46
|
notification.setUserMail(userMail);
|
47
|
|
48
|
Date dateNow = new Date();
|
49
|
|
50
|
Calendar calendar = Calendar.getInstance();
|
51
|
calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(targetHour));
|
52
|
calendar.set(Calendar.MINUTE,Integer.parseInt(targetMinute));
|
53
|
calendar.set(Calendar.SECOND,Integer.parseInt(targetSecond));
|
54
|
Date dateTodayAtTargetHour = calendar.getTime();
|
55
|
|
56
|
if(dateNow.after(dateTodayAtTargetHour)) {
|
57
|
//calendar.add(Calendar.SECOND,1);
|
58
|
notification.setDate(calendar.getTime());
|
59
|
} else {
|
60
|
calendar.add(Calendar.HOUR_OF_DAY, -Integer.parseInt(defaultFrequencyInHours));
|
61
|
//calendar.add(Calendar.SECOND, 1);
|
62
|
notification.setDate(calendar.getTime());
|
63
|
}
|
64
|
|
65
|
notification.setFrequency(frequency);
|
66
|
notification.setNotify(notify);
|
67
|
|
68
|
return notification;
|
69
|
}
|
70
|
|
71
|
private String saveNotification(Notification notification) throws SQLStoreException, SQLException {
|
72
|
logger.info("Saving notification...");
|
73
|
String id = null;
|
74
|
ArrayList<Object> params = new ArrayList<>();
|
75
|
String query = generateSaveQueryForNotification(notification, params);
|
76
|
ResultSet rs = sqlDAO.executePreparedQuery(query, params);
|
77
|
if(rs.next()) {
|
78
|
id = rs.getString(1);
|
79
|
}
|
80
|
rs.close();
|
81
|
return id;
|
82
|
}
|
83
|
|
84
|
private String generateSaveQueryForNotification(Notification notification, ArrayList<Object> params){
|
85
|
String query= null;
|
86
|
query = queryGenerator.generateInsertNotificationQuery(notification.getDate(), notification.getOpenaireId(), notification.getUserMail(), notification.getFrequency(), notification.isNotify(), params);
|
87
|
return query;
|
88
|
}
|
89
|
|
90
|
public boolean updateNotificationPreferences(String openaireId, String userMail, int frequency, boolean notify) throws SQLStoreException, SQLException, Exception {
|
91
|
logger.info("Updating notification...");
|
92
|
ArrayList<Object> params = new ArrayList<>();
|
93
|
//Date date = new Date();
|
94
|
String query = queryGenerator.generateUpdateNotificationPreferences(openaireId, userMail, frequency, notify, params);
|
95
|
//ResultSet rs = sqlDAO.executeUpdateQuery(query, params);
|
96
|
return sqlDAO.executeUpdateQuery(query, params);
|
97
|
}
|
98
|
|
99
|
public boolean updateNotificationLastInteractionDate(String openaireId, String userMail, Date date) throws SQLStoreException, SQLException, Exception {
|
100
|
logger.info("Updating notification...");
|
101
|
ArrayList<Object> params = new ArrayList<>();
|
102
|
String query = queryGenerator.generateUpdateNotificationLastInteractionDate(openaireId, userMail, date, params);
|
103
|
return sqlDAO.executeUpdateQuery(query, params);
|
104
|
}
|
105
|
|
106
|
public SqlDAO getSqlDAO() {
|
107
|
return sqlDAO;
|
108
|
}
|
109
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
110
|
this.sqlDAO = sqlDAO;
|
111
|
}
|
112
|
|
113
|
public QueryGenerator getQueryGenerator() {
|
114
|
return queryGenerator;
|
115
|
}
|
116
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
117
|
this.queryGenerator = queryGenerator;
|
118
|
}
|
119
|
}
|