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.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

    
10
import java.sql.ResultSet;
11
import java.util.ArrayList;
12
import java.util.List;
13

    
14
public class FetchNotificationHandler {
15
    private Logger log = LogManager.getLogger(this.getClass());
16

    
17
    SqlDAO sqlDAO = null;
18
    QueryGenerator queryGenerator = null;
19

    
20
    public FetchNotificationHandler() {}
21

    
22
    public Notification fetchNotification(String openaireId, String userMail) throws Exception, SQLStoreException {
23
        ArrayList<Object> params = new ArrayList<>();
24
        String query = queryGenerator.generateSelectNotificationQuery(openaireId, userMail, params);
25
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);
26

    
27
        Notification notification = fetchNotificationByResultSet(rs);
28
        if(notification == null) {
29
            log.info("No notification for openaireId: "+ openaireId+" and userMail: "+userMail+"\n");
30
        }
31

    
32
        return notification;
33
    }
34

    
35
    public List<Notification> fetchTrueNotifications() throws Exception, SQLStoreException {
36
        String query = queryGenerator.generateSelectTrueNotificationsQuery();
37
        ResultSet rs = sqlDAO.executePreparedQuery(query);
38

    
39
        List<Notification> notifications = fetchNotificationsByResultSet(rs);
40

    
41
        return notifications;
42
    }
43

    
44
    public Notification fetchNotificationByResultSet(ResultSet rs) throws Exception {
45
        Notification notification = null;
46
        if(rs.next()) {
47
            notification = new Notification();
48
            notification.setOpenaireId(rs.getString("openaire_id"));
49
            notification.setUserMail(rs.getString("user_email"));
50
            notification.setDate(rs.getTimestamp("last_interaction_date"));
51
            notification.setFrequency(rs.getInt("frequency"));
52
            notification.setNotify(rs.getBoolean("notify"));
53
        }
54
        return notification;
55
    }
56

    
57
    public List<Notification> fetchNotificationsByResultSet(ResultSet rs) throws Exception {
58
        List<Notification> notifications = null;
59
        while(rs.next()) {
60
            if(notifications == null) {
61
                notifications = new ArrayList<>();
62
            }
63
            Notification notification;
64
            notification = new Notification();
65
            notification.setOpenaireId(rs.getString("openaire_id"));
66
            notification.setUserMail(rs.getString("user_email"));
67
            notification.setDate(rs.getTimestamp("last_interaction_date"));
68
            notification.setFrequency(rs.getInt("frequency"));
69
            notification.setNotify(rs.getBoolean("notify"));
70

    
71
            notifications.add(notification);
72
        }
73
        return notifications;
74
    }
75

    
76
    public SqlDAO getSqlDAO() {
77
        return sqlDAO;
78
    }
79

    
80
    public void setSqlDAO(SqlDAO sqlDAO) {
81
        this.sqlDAO = sqlDAO;
82
    }
83

    
84
    public QueryGenerator getQueryGenerator() {
85
        return queryGenerator;
86
    }
87

    
88
    public void setQueryGenerator(QueryGenerator queryGenerator) {
89
        this.queryGenerator = queryGenerator;
90
    }
91
}
(7-7/13)