killbill-aplcache

util: add getAuditLogsForRecordId in AuditSqlDao This

7/30/2012 5:41:15 PM

Details

diff --git a/util/src/main/java/com/ning/billing/util/callcontext/CallContextBase.java b/util/src/main/java/com/ning/billing/util/callcontext/CallContextBase.java
index 40d7ef5..2c21d05 100644
--- a/util/src/main/java/com/ning/billing/util/callcontext/CallContextBase.java
+++ b/util/src/main/java/com/ning/billing/util/callcontext/CallContextBase.java
@@ -19,12 +19,13 @@ package com.ning.billing.util.callcontext;
 import java.util.UUID;
 
 public abstract class CallContextBase implements CallContext {
-    private final UUID userToken;
-    private final String userName;
-    private final CallOrigin callOrigin;
-    private final UserType userType;
-    private final String reasonCode;
-    private final String comment;
+
+    protected final UUID userToken;
+    protected final String userName;
+    protected final CallOrigin callOrigin;
+    protected final UserType userType;
+    protected final String reasonCode;
+    protected final String comment;
 
     public CallContextBase(final String userName, final CallOrigin callOrigin, final UserType userType) {
         this(userName, callOrigin, userType, null);
diff --git a/util/src/main/java/com/ning/billing/util/callcontext/DefaultCallContext.java b/util/src/main/java/com/ning/billing/util/callcontext/DefaultCallContext.java
index f32cf2d..e294d30 100644
--- a/util/src/main/java/com/ning/billing/util/callcontext/DefaultCallContext.java
+++ b/util/src/main/java/com/ning/billing/util/callcontext/DefaultCallContext.java
@@ -42,6 +42,12 @@ public class DefaultCallContext extends CallContextBase {
         this(userName, callOrigin, userType, null, clock);
     }
 
+    public DefaultCallContext(final String userName, final DateTime createdDate, final String reasonCode,
+                              final String comment, final UUID userToken) {
+        super(userName, null, null, reasonCode, comment, userToken);
+        this.createdDate = createdDate;
+    }
+
     @Override
     public DateTime getCreatedDate() {
         return createdDate;
@@ -51,4 +57,68 @@ public class DefaultCallContext extends CallContextBase {
     public DateTime getUpdatedDate() {
         return createdDate;
     }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append("CallContextBase");
+        sb.append("{userToken=").append(userToken);
+        sb.append(", userName='").append(userName).append('\'');
+        sb.append(", callOrigin=").append(callOrigin);
+        sb.append(", userType=").append(userType);
+        sb.append(", reasonCode='").append(reasonCode).append('\'');
+        sb.append(", comment='").append(comment).append('\'');
+        sb.append(", createdDate='").append(createdDate).append('\'');
+        sb.append(", updatedDate='").append(createdDate).append('\'');
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final DefaultCallContext that = (DefaultCallContext) o;
+
+        if (callOrigin != that.callOrigin) {
+            return false;
+        }
+        if (comment != null ? !comment.equals(that.comment) : that.comment != null) {
+            return false;
+        }
+        if (reasonCode != null ? !reasonCode.equals(that.reasonCode) : that.reasonCode != null) {
+            return false;
+        }
+        if (userName != null ? !userName.equals(that.userName) : that.userName != null) {
+            return false;
+        }
+        if (userToken != null ? !userToken.equals(that.userToken) : that.userToken != null) {
+            return false;
+        }
+        if (createdDate != null ? createdDate.compareTo(that.createdDate) != 0 : that.createdDate != null) {
+            return false;
+        }
+        if (userType != that.userType) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = userToken != null ? userToken.hashCode() : 0;
+        result = 31 * result + (userName != null ? userName.hashCode() : 0);
+        result = 31 * result + (callOrigin != null ? callOrigin.hashCode() : 0);
+        result = 31 * result + (userType != null ? userType.hashCode() : 0);
+        result = 31 * result + (reasonCode != null ? reasonCode.hashCode() : 0);
+        result = 31 * result + (comment != null ? comment.hashCode() : 0);
+        result = 31 * result + (createdDate != null ? createdDate.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/util/src/main/java/com/ning/billing/util/dao/AuditLogMapper.java b/util/src/main/java/com/ning/billing/util/dao/AuditLogMapper.java
new file mode 100644
index 0000000..89ca281
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/dao/AuditLogMapper.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.dao;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.skife.jdbi.v2.StatementContext;
+import org.skife.jdbi.v2.tweak.ResultSetMapper;
+
+import com.ning.billing.util.ChangeType;
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.audit.DefaultAuditLog;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.DefaultCallContext;
+
+public class AuditLogMapper extends MapperBase implements ResultSetMapper<AuditLog> {
+
+    @Override
+    public AuditLog map(final int index, final ResultSet r, final StatementContext ctx) throws SQLException {
+        final String tableName = r.getString("table_name");
+        final long recordId = r.getLong("record_id");
+        final String changeType = r.getString("change_type");
+        final DateTime changeDate = getDateTime(r, "change_date");
+        final String changedBy = r.getString("changed_by");
+        final String reasonCode = r.getString("reason_code");
+        final String comments = r.getString("comments");
+        final String userToken = r.getString("user_token");
+
+        final EntityAudit entityAudit = new EntityAudit(TableName.valueOf(tableName), recordId, ChangeType.valueOf(changeType));
+        final CallContext callContext = new DefaultCallContext(changedBy, changeDate, reasonCode, comments, UUID.fromString(userToken));
+        return new DefaultAuditLog(entityAudit, callContext);
+    }
+}
diff --git a/util/src/main/java/com/ning/billing/util/dao/AuditSqlDao.java b/util/src/main/java/com/ning/billing/util/dao/AuditSqlDao.java
index 4f9c256..3090dae 100644
--- a/util/src/main/java/com/ning/billing/util/dao/AuditSqlDao.java
+++ b/util/src/main/java/com/ning/billing/util/dao/AuditSqlDao.java
@@ -22,13 +22,17 @@ import org.skife.jdbi.v2.sqlobject.Bind;
 import org.skife.jdbi.v2.sqlobject.SqlBatch;
 import org.skife.jdbi.v2.sqlobject.SqlQuery;
 import org.skife.jdbi.v2.sqlobject.SqlUpdate;
+import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
 import org.skife.jdbi.v2.sqlobject.stringtemplate.ExternalizedSqlViaStringTemplate3;
 
+import com.ning.billing.util.audit.AuditLog;
 import com.ning.billing.util.callcontext.CallContext;
 import com.ning.billing.util.callcontext.CallContextBinder;
 
 @ExternalizedSqlViaStringTemplate3
+@RegisterMapper(AuditLogMapper.class)
 public interface AuditSqlDao {
+
     @SqlUpdate
     public void insertAuditFromTransaction(@AuditBinder final EntityAudit audit,
                                            @CallContextBinder final CallContext context);
@@ -38,9 +42,17 @@ public interface AuditSqlDao {
                                            @CallContextBinder final CallContext context);
 
     @SqlQuery
+    public List<AuditLog> getAuditLogsForRecordId(@TableNameBinder final TableName tableName,
+                                                  @Bind("recordId") final long recordId);
+
+    @SqlQuery
     public Long getRecordId(@Bind("id") final String id);
 
     @SqlQuery
+    public Long getRecordIdForTable(@TableNameBinder final TableName tableName,
+                                    @Bind("id") final String id);
+
+    @SqlQuery
     public Long getHistoryRecordId(@Bind("recordId") final Long recordId);
 
 }
diff --git a/util/src/main/resources/com/ning/billing/util/audit/dao/AuditSqlDao.sql.stg b/util/src/main/resources/com/ning/billing/util/audit/dao/AuditSqlDao.sql.stg
index 9fdbce8..2581d78 100644
--- a/util/src/main/resources/com/ning/billing/util/audit/dao/AuditSqlDao.sql.stg
+++ b/util/src/main/resources/com/ning/billing/util/audit/dao/AuditSqlDao.sql.stg
@@ -14,4 +14,18 @@ fields(prefix) ::= <<
 insertAuditFromTransaction() ::= <<
     INSERT INTO audit_log(<fields()>)
     VALUES(:tableName, :recordId, :changeType, :createdDate, :userName, :reasonCode, :comment, :userToken);
->>
\ No newline at end of file
+>>
+
+getAuditLogsForRecordId() ::= <<
+  SELECT <recordId()>
+  FROM audit_log
+  WHERE record_id = :recordId
+  AND tableName = :tableName;
+>>
+
+getRecordIdForTable ::= <<
+  SELECT record_id
+  FROM :tableName
+  WHERE id = :id
+  LIMIT 1
+>>
diff --git a/util/src/test/java/com/ning/billing/util/callcontext/TestDefaultCallContext.java b/util/src/test/java/com/ning/billing/util/callcontext/TestDefaultCallContext.java
new file mode 100644
index 0000000..c56b5f3
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/callcontext/TestDefaultCallContext.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning licenses this file to you under the Apache License, version 2.0
+ * (the "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at:
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package com.ning.billing.util.callcontext;
+
+import java.util.UUID;
+
+import org.joda.time.DateTime;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.ning.billing.util.UtilTestSuite;
+import com.ning.billing.util.clock.Clock;
+import com.ning.billing.util.clock.ClockMock;
+
+public class TestDefaultCallContext extends UtilTestSuite {
+
+    private final Clock clock = new ClockMock();
+
+    @Test(groups = "fast")
+    public void testGetters() throws Exception {
+        final String userName = UUID.randomUUID().toString();
+        final DateTime createdDate = clock.getUTCNow();
+        final String reasonCode = UUID.randomUUID().toString();
+        final String comment = UUID.randomUUID().toString();
+        final UUID userToken = UUID.randomUUID();
+        final DefaultCallContext callContext = new DefaultCallContext(userName, createdDate, reasonCode, comment, userToken);
+
+        Assert.assertEquals(callContext.getCreatedDate(), createdDate);
+        Assert.assertNull(callContext.getCallOrigin());
+        Assert.assertEquals(callContext.getComment(), comment);
+        Assert.assertEquals(callContext.getReasonCode(), reasonCode);
+        Assert.assertEquals(callContext.getUserName(), userName);
+        Assert.assertEquals(callContext.getUpdatedDate(), createdDate);
+        Assert.assertEquals(callContext.getUserToken(), userToken);
+        Assert.assertNull(callContext.getUserType());
+    }
+
+    @Test(groups = "fast")
+    public void testEquals() throws Exception {
+        final String userName = UUID.randomUUID().toString();
+        final DateTime createdDate = clock.getUTCNow();
+        final String reasonCode = UUID.randomUUID().toString();
+        final String comment = UUID.randomUUID().toString();
+        final UUID userToken = UUID.randomUUID();
+
+        final DefaultCallContext callContext = new DefaultCallContext(userName, createdDate, reasonCode, comment, userToken);
+        Assert.assertEquals(callContext, callContext);
+
+        final DefaultCallContext sameCallContext = new DefaultCallContext(userName, createdDate, reasonCode, comment, userToken);
+        Assert.assertEquals(sameCallContext, callContext);
+
+        final DefaultCallContext otherCallContext = new DefaultCallContext(UUID.randomUUID().toString(), createdDate, reasonCode, comment, userToken);
+        Assert.assertNotEquals(otherCallContext, callContext);
+    }
+}