Skip to content

Commit 7ef2c86

Browse files
committed
fixed issues
1 parent eed4d71 commit 7ef2c86

File tree

2 files changed

+53
-23
lines changed

2 files changed

+53
-23
lines changed

src/main/java/org/audit4j/handler/db/AuditLogDaoImpl.java

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.sql.PreparedStatement;
2323
import java.sql.SQLException;
24+
import java.util.Date;
25+
import java.util.UUID;
2426

2527
import org.audit4j.core.dto.AuditEvent;
2628
import org.audit4j.core.dto.Field;
@@ -30,8 +32,13 @@
3032
*
3133
* @author <a href="mailto:[email protected]">Janith Bandara</a>
3234
*/
33-
class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
35+
final class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
3436

37+
public static boolean initialized = false;
38+
39+
public static AuditLogDao auditDao;
40+
41+
private AuditLogDaoImpl(){}
3542
/*
3643
* (non-Javadoc)
3744
*
@@ -41,32 +48,44 @@ class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
4148
*/
4249
@Override
4350
public boolean writeEvent(AuditEvent event) throws SQLException {
44-
51+
String uuid;
52+
String timestamp;
4553
StringBuffer elements = new StringBuffer();
54+
55+
if (event.getUuid() == null) {
56+
uuid=String.valueOf(UUID.randomUUID().getMostSignificantBits());
57+
} else {
58+
uuid = event.getUuid().toString();
59+
}
60+
61+
if (event.getTimestamp() == null) {
62+
timestamp = new Date().toString();
63+
} else {
64+
timestamp = event.getTimestamp().toString();
65+
}
66+
4667

4768
for (Field element : event.getFields()) {
4869
elements.append(element.getName() + " " + element.getType() + ":" + element.getValue() + ", ");
4970
}
5071
StringBuffer query = new StringBuffer();
51-
query.append("insert into audit(auditId, uuid, timestamp, actor, origin, action, elements) ").append(
52-
"values (?, ?, ?, ?, ?, ?, ?)");
72+
query.append("insert into audit(uuid, timestamp, actor, origin, action, elements) ").append(
73+
"values (?, ?, ?, ?, ?, ?)");
5374

5475
PreparedStatement statement = getConnection().prepareStatement(query.toString());
55-
statement.setInt(1, event.getAuditId());
56-
statement.setString(2, event.getUuid().toString());
57-
statement.setString(3, event.getTimestamp().toString());
58-
statement.setString(4, event.getActor());
59-
statement.setString(5, event.getOrigin());
60-
statement.setString(6, event.getAction());
61-
statement.setString(7, elements.toString());
76+
statement.setString(1, uuid);
77+
statement.setString(2, timestamp);
78+
statement.setString(3, event.getActor());
79+
statement.setString(4, event.getOrigin());
80+
statement.setString(5, event.getAction());
81+
statement.setString(6, elements.toString());
6282
return statement.execute();
6383
}
6484

6585
@Override
6686
public boolean createAuditTableIFNotExist() throws SQLException {
6787
StringBuffer query = new StringBuffer("create table if not exists audit (");
68-
query.append("auditId INT NOT NULL,");
69-
query.append("uuid char(60) NOT NULL,");
88+
query.append("uuid varchar(200) NOT NULL,");
7089
query.append("timestamp varchar(100) NOT NULL,");
7190
query.append("actor varchar(200) NOT NULL,");
7291
query.append("origin varchar(200),");
@@ -77,5 +96,16 @@ public boolean createAuditTableIFNotExist() throws SQLException {
7796
getConnection().prepareStatement(query.toString()).execute();
7897
return true;
7998
}
80-
99+
100+
public static AuditLogDao getInstance() {
101+
if (!initialized) {
102+
synchronized (AuditLogDaoImpl.class) {
103+
if (!initialized) {
104+
auditDao = new AuditLogDaoImpl();
105+
initialized = true;
106+
}
107+
}
108+
}
109+
return auditDao;
110+
}
81111
}

src/main/java/org/audit4j/handler/db/DatabaseAuditHandler.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
/**
2929
* The Class GeneralDatabaseAuditHandler.
30-
*
30+
*
31+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
3132
*/
3233
public class DatabaseAuditHandler extends Handler {
3334

@@ -79,7 +80,7 @@ public void init() throws InitializationException {
7980
factory.setConnectionType(ConnectionFactory.POOLED_CONNECTION);
8081
factory.init();
8182

82-
AuditLogDao dao = new AuditLogDaoImpl();
83+
AuditLogDao dao = AuditLogDaoImpl.getInstance();
8384
try {
8485
dao.createAuditTableIFNotExist();
8586
} catch (SQLException e) {
@@ -94,7 +95,7 @@ public void init() throws InitializationException {
9495
*/
9596
@Override
9697
public void handle() throws HandlerException {
97-
AuditLogDao dao = new AuditLogDaoImpl();
98+
AuditLogDao dao = AuditLogDaoImpl.getInstance();
9899
try {
99100
dao.writeEvent(getAuditEvent());
100101
} catch (SQLException e) {
@@ -104,20 +105,19 @@ public void handle() throws HandlerException {
104105

105106
/**
106107
* Gets the embedded.
107-
*
108+
*
108109
* @return the embedded
109110
*/
110-
String getEmbedded() {
111+
public String getEmbedded() {
111112
return embedded;
112113
}
113114

114115
/**
115116
* Sets the embedded.
116-
*
117-
* @param embedded
118-
* the new embedded
117+
*
118+
* @param embedded the new embedded
119119
*/
120-
void setEmbedded(String embedded) {
120+
public void setEmbedded(String embedded) {
121121
this.embedded = embedded;
122122
}
123123

0 commit comments

Comments
 (0)