Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.apifdxweb.dao.impl.audit;
 
import com.megatim.apifdxweb.model.audit.AuditActionsParticipant;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
 
/**
 *
 * @author ASUS
 */
public class AuditActionsParticicipantsQuery {
 
    private final Connection connection;
    private final AuditActionsParticipant auditAction;
 
    public AuditActionsParticicipantsQuery(Connection connection, AuditActionsParticipant auditAction) {
        this.connection = connection;
        this.auditAction = auditAction;
    }
 
    public void insert() throws SQLException {
        String getNextIdQuery = "SELECT nextval('SEQ_AUDIT_ACTIONS_PARTICIPANT')";
 
        try ( PreparedStatement statement = connection.prepareStatement(insertString());  PreparedStatement getIdStatement = connection.prepareStatement(getNextIdQuery)) {
 
            ResultSet resultSet = getIdStatement.executeQuery();
            long generatedId = 0;
 
            if (resultSet.next()) {
                generatedId = resultSet.getLong(1);
            }
 
            statement.setTimestamp(1, Timestamp.valueOf(auditAction.getDateAction()));
            statement.setInt(2, auditAction.getLastIndexRead());
            statement.setString(3, auditAction.getParticipantDemandeur());
            statement.setString(4, auditAction.getTypeFichierConsulte());
            statement.setLong(5, generatedId);
 
            statement.executeUpdate();
        }
    }
 
    public void update() throws SQLException {
        try ( PreparedStatement statement = connection.prepareStatement(updateString())) {
            statement.setTimestamp(1, Timestamp.valueOf(auditAction.getDateAction()));
            statement.setInt(2, auditAction.getLastIndexRead());
            statement.setLong(3, auditAction.getId());
 
            statement.executeUpdate();
        }
    }
 
    private String insertString() {
        return "INSERT INTO audit_actions_participant(date_action, last_index_read, participant_demandeur, type_fichier_consulte, id)"
                + " VALUES (?, ?, ?, ?, ?);";
    }
 
    private String updateString() {
        return "UPDATE audit_actions_participant SET date_action  = ?, last_index_read = ?  WHERE id = ?";
    }
}