org.bdware.doip.audit.AuditRuleDB Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.audit;
import com.google.gson.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class AuditRuleDB extends BasicDB {
public AuditRuleDB(String driverClass, String url, String user, String password) {
super(driverClass, url, user, password);
}
/**
* Add some audit rule for a repo and save it in a table.
* @param tableName The table name saving audit rule for repo.
* @param repoId A unique identifier for repo.
* @param auditRule An audit rule list surpported by a repo.
* @return either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
*/
public int add(String tableName, String repoId, AuditRule auditRule) {
try {
Connection conn = getConn();
String sql = "insert into " + formatTableName(tableName) + " (repoId, type, displayname, description) values (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, repoId);
ps.setInt(2, auditRule.type);
ps.setString(3, auditRule.displayname);
ps.setString(4, auditRule.description);
int count = ps.executeUpdate();
ps.close();
conn.close();
return count;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* Delete the audit rules for the repo.
* @param tableName The table name saving audit rule for repo.
* @param repoId A unique identifier for repo.
* @return either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
*/
public int remove(String tableName, String repoId) {
try {
Connection conn = getConn();
String sql = "delete from " + formatTableName(tableName) + " where repoId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, repoId);
int count = ps.executeUpdate();
ps.close();
conn.close();
return count;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* Update the audit rules for the repo.
* @param tableName The table name saving audit rule for repo.
* @param repoId A unique identifier for repo.
* @param auditRule The new audit rule list supported by the repo.
* @return either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing.
*/
public int modify(String tableName, String repoId, AuditRule auditRule) {
try {
Connection conn = getConn();
String sql = "update " + formatTableName(tableName) + " set type = ?, displayname = ?, description = ? where repoId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, auditRule.type);
ps.setString(2, auditRule.displayname);
ps.setString(3, auditRule.description);
ps.setString(4, repoId);
int count = ps.executeUpdate();
ps.close();
conn.close();
return count;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* Retrieve the audit rules supported by the repo.
* @param tableName The table name saving audit rule for repo.
* @param repoId A unique identifier for repo.
* @return either (1) the audit rule list supported by the repo or (2) null if some error occurred when querying the table.
*/
public List retrieve(String tableName, String repoId) {
try {
Connection conn = getConn();
String sql = "select * from " + formatTableName(tableName) + " where repoId = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, repoId);
ResultSet resultSet = ps.executeQuery();
ps.close();
conn.close();
List auditRuleList = new ArrayList<>();
while (resultSet.next()) {
String auditRuleStr = resultSet.getString("auditRule");
JsonArray auditRuleArray = JsonParser.parseString(auditRuleStr).getAsJsonArray();
for (JsonElement auditRule: auditRuleArray) {
auditRuleList.add(new Gson().fromJson(auditRule, AuditRule.class));
}
}
return auditRuleList;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Retrieve the audit rules for all repos.
* @param tableName The table name saving audit rule for repo.
* @return either (1) the audit rules for different repos or (2) null if some error occurred when querying the table.
*/
public Map> retrieveAll(String tableName) {
try {
Connection conn = getConn();
String sql = "select * from " + formatTableName(tableName);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
ps.close();
conn.close();
Map> ret = null;
while (resultSet.next()) {
String repoId = resultSet.getString("repoId");
String auditRuleStr = resultSet.getString("auditRule");
JsonArray auditRuleArray = JsonParser.parseString(auditRuleStr).getAsJsonArray();
List auditRuleList = new ArrayList<>();
for (JsonElement auditRule: auditRuleArray) {
auditRuleList.add(new Gson().fromJson(auditRule, AuditRule.class));
}
ret.put(repoId, auditRuleList);
}
return ret;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}