All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.audit.AuditLogDB Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.audit;

import com.google.gson.JsonObject;

import java.sql.*;

public class AuditLogDB extends BasicDB {

    public AuditLogDB(String driverClass, String url, String user, String password) {
        super(driverClass, url, user, password);
    }

    public boolean add(String tableName, String requestHash, String requestMsg, String responseHash, String responseMsg) {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "insert into ? values (?, ?, ?, ?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            ps.setString(2, requestHash);
            ps.setString(3, requestMsg);
            ps.setString(4, responseHash);
            ps.setString(5, responseMsg);
            statement.executeUpdate(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean remove(String tableName, String hash, boolean isRequestHash) {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "delete from ? where ? = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            if (isRequestHash)
                ps.setString(2, "requestHash");
            else
                ps.setString(2, "responseHash");
            ps.setString(3, hash);
            statement.executeUpdate(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean modify(String tableName, String hash, String msg, boolean isRequestHash, boolean isRequestMsg) {
        try {
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "update ? set ? = ? where ? = ? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            if (isRequestHash)
                ps.setString(2, "requestHash");
            else
                ps.setString(2, "responseHash");
            ps.setString(3, hash);
            if (isRequestMsg)
                ps.setString(4, "requestMsg");
            else
                ps.setString(4, "responseMsg");
            ps.setString(5, msg);
            statement.executeUpdate(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public JsonObject retrieve(String tableName, String requestHash, boolean isRequestHash) {
        try {
            JsonObject jsonObject = new JsonObject();
            Connection conn = getConn();
            Statement statement = conn.createStatement();
            String sql = "select * from ? where ? = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, formatTableName(tableName));
            if (isRequestHash)
                ps.setString(2, "requestHash");
            else
                ps.setString(2, "responseHash");
            ps.setString(3, requestHash);
            ResultSet resultSet = statement.executeQuery(sql);
            jsonObject.addProperty("requestMsg", resultSet.getString(2));
            jsonObject.addProperty("responseMsg", resultSet.getString(4));
            return jsonObject;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy