Project

General

Profile

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
import org.springframework.beans.factory.annotation.Autowired;
9

    
10
import java.sql.ResultSet;
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.Calendar;
14
import java.util.Date;
15

    
16
public class NotificationHandler {
17
    SqlDAO sqlDAO = null;
18
    QueryGenerator queryGenerator = null;
19

    
20
    private static final Logger logger = Logger.getLogger(ClaimHandler.class);
21

    
22
    @Autowired
23
    private String targetHour;
24
    @Autowired
25
    private String targetMinute;
26
    @Autowired
27
    private String targetSecond;
28
    @Autowired
29
    private String defaultFrequencyInHours;
30

    
31
    public String buildAndInsertNotification(String openaireId, String userMail, int frequency, boolean notify) throws Exception, SQLStoreException {
32
        String id = null;
33

    
34
        logger.info("Trying to create a notification {openaireId:"+openaireId+", userMail:"+userMail+", frequency:"+frequency+", notify:"+notify+"}");
35
        Notification notification = buildNotification(openaireId, userMail, frequency, notify);
36

    
37
        id = saveNotification(notification);
38

    
39
        return id;
40
    }
41

    
42
    private Notification buildNotification(String openaireId, String userMail, int frequency, boolean notify) {
43
        Notification notification = new Notification();
44
        notification.setOpenaireId(openaireId);
45
        notification.setUserMail(userMail);
46

    
47
        Date dateNow = new Date();
48

    
49
        Calendar calendar = Calendar.getInstance();
50
        calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(targetHour));
51
        calendar.set(Calendar.MINUTE,Integer.parseInt(targetMinute));
52
        calendar.set(Calendar.SECOND,Integer.parseInt(targetSecond));
53
        Date dateTodayAtTargetHour = calendar.getTime();
54

    
55
        if(dateNow.after(dateTodayAtTargetHour)) {
56
            //calendar.add(Calendar.SECOND,1);
57
            notification.setDate(calendar.getTime());
58
        } else {
59
            calendar.add(Calendar.HOUR_OF_DAY, -Integer.parseInt(defaultFrequencyInHours));
60
            //calendar.add(Calendar.SECOND, 1);
61
            notification.setDate(calendar.getTime());
62
        }
63
        
64
        notification.setFrequency(frequency);
65
        notification.setNotify(notify);
66

    
67
        return notification;
68
    }
69

    
70
    private String saveNotification(Notification notification) throws SQLStoreException, SQLException {
71
        logger.info("Saving notification...");
72
        String id = null;
73
        ArrayList<Object> params = new ArrayList<>();
74
        String query = generateSaveQueryForNotification(notification, params);
75
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
76
        if(rs.next()) {
77
            id = rs.getString(1);
78
        }
79
        rs.close();
80
        return id;
81
    }
82

    
83
    private String  generateSaveQueryForNotification(Notification notification, ArrayList<Object> params){
84
        String query= null;
85
        query = queryGenerator.generateInsertNotificationQuery(notification.getDate(), notification.getOpenaireId(), notification.getUserMail(), notification.getFrequency(), notification.isNotify(), params);
86
        return query;
87
    }
88

    
89
    public boolean updateNotificationPreferences(String openaireId, String userMail, int frequency, boolean notify) throws SQLStoreException, SQLException, Exception {
90
        logger.info("Updating notification...");
91
        ArrayList<Object> params = new ArrayList<>();
92
        //Date date = new Date();
93
        String query = queryGenerator.generateUpdateNotificationPreferences(openaireId, userMail, frequency, notify, params);
94
        //ResultSet rs = sqlDAO.executeUpdateQuery(query, params);
95
        return sqlDAO.executeUpdateQuery(query, params);
96
    }
97

    
98
    public boolean updateNotificationLastInteractionDate(String openaireId, String userMail, Date date) throws SQLStoreException, SQLException, Exception {
99
        logger.info("Updating notification...");
100
        ArrayList<Object> params = new ArrayList<>();
101
        String query = queryGenerator.generateUpdateNotificationLastInteractionDate(openaireId, userMail, date, params);
102
        return sqlDAO.executeUpdateQuery(query, params);
103
    }
104

    
105
    public SqlDAO getSqlDAO() {
106
        return sqlDAO;
107
    }
108
    public void setSqlDAO(SqlDAO sqlDAO) {
109
        this.sqlDAO = sqlDAO;
110
    }
111

    
112
    public QueryGenerator getQueryGenerator() {
113
        return queryGenerator;
114
    }
115
    public void setQueryGenerator(QueryGenerator queryGenerator) {
116
        this.queryGenerator = queryGenerator;
117
    }
118
}
(10-10/12)