Details
diff --git a/api/src/main/java/com/ning/billing/util/api/AuditUserApi.java b/api/src/main/java/com/ning/billing/util/api/AuditUserApi.java
new file mode 100644
index 0000000..1770335
--- /dev/null
+++ b/api/src/main/java/com/ning/billing/util/api/AuditUserApi.java
@@ -0,0 +1,35 @@
+/*
+ * 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.api;
+
+import java.util.List;
+import java.util.UUID;
+
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.dao.ObjectType;
+
+public interface AuditUserApi {
+
+ /**
+ * Get all the audit entries for a given object
+ *
+ * @param objectId the object id
+ * @param objectType the type of object
+ * @return all audit entries for that object
+ */
+ public List<AuditLog> getAuditLogs(final UUID objectId, final ObjectType objectType);
+}
diff --git a/util/src/main/java/com/ning/billing/util/audit/api/DefaultAuditUserApi.java b/util/src/main/java/com/ning/billing/util/audit/api/DefaultAuditUserApi.java
new file mode 100644
index 0000000..6af61ea
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/audit/api/DefaultAuditUserApi.java
@@ -0,0 +1,60 @@
+/*
+ * 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.audit.api;
+
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+
+import com.ning.billing.util.api.AuditUserApi;
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.audit.dao.AuditDao;
+import com.ning.billing.util.dao.ObjectType;
+import com.ning.billing.util.dao.TableName;
+
+import com.google.common.collect.ImmutableList;
+
+public class DefaultAuditUserApi implements AuditUserApi {
+
+ private final AuditDao auditDao;
+
+ @Inject
+ public DefaultAuditUserApi(final AuditDao auditDao) {
+ this.auditDao = auditDao;
+ }
+
+ @Override
+ public List<AuditLog> getAuditLogs(final UUID objectId, final ObjectType objectType) {
+ final TableName tableName = getTableNameFromObjectType(objectType);
+ if (tableName == null) {
+ return ImmutableList.<AuditLog>of();
+ }
+
+ return auditDao.getAuditLogsForRecordId(tableName, objectId);
+ }
+
+ private TableName getTableNameFromObjectType(final ObjectType objectType) {
+ for (final TableName tableName : TableName.values()) {
+ if (objectType.equals(tableName.getObjectType())) {
+ return tableName;
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/util/src/main/java/com/ning/billing/util/audit/dao/AuditDao.java b/util/src/main/java/com/ning/billing/util/audit/dao/AuditDao.java
new file mode 100644
index 0000000..224b78b
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/audit/dao/AuditDao.java
@@ -0,0 +1,28 @@
+/*
+ * 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.audit.dao;
+
+import java.util.List;
+import java.util.UUID;
+
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.dao.TableName;
+
+public interface AuditDao {
+
+ public List<AuditLog> getAuditLogsForRecordId(final TableName tableName, final UUID objectId);
+}
diff --git a/util/src/main/java/com/ning/billing/util/audit/dao/DefaultAuditDao.java b/util/src/main/java/com/ning/billing/util/audit/dao/DefaultAuditDao.java
new file mode 100644
index 0000000..7f38dee
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/audit/dao/DefaultAuditDao.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.audit.dao;
+
+import java.util.List;
+import java.util.UUID;
+
+import javax.inject.Inject;
+
+import org.skife.jdbi.v2.IDBI;
+
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.dao.AuditSqlDao;
+import com.ning.billing.util.dao.TableName;
+
+import com.google.common.collect.ImmutableList;
+
+public class DefaultAuditDao implements AuditDao {
+
+ private final AuditSqlDao auditSqlDao;
+
+ @Inject
+ public DefaultAuditDao(final IDBI dbi) {
+ this.auditSqlDao = dbi.onDemand(AuditSqlDao.class);
+ }
+
+ @Override
+ public List<AuditLog> getAuditLogsForRecordId(final TableName tableName, final UUID objectId) {
+ final Long recordId = auditSqlDao.getRecordIdForTable(tableName, objectId.toString());
+ if (recordId == null) {
+ return ImmutableList.<AuditLog>of();
+ } else {
+ return auditSqlDao.getAuditLogsForRecordId(tableName, recordId);
+ }
+ }
+}
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 3090dae..2e5d353 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,6 +22,7 @@ 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.Define;
import org.skife.jdbi.v2.sqlobject.customizers.RegisterMapper;
import org.skife.jdbi.v2.sqlobject.stringtemplate.ExternalizedSqlViaStringTemplate3;
@@ -49,7 +50,7 @@ public interface AuditSqlDao {
public Long getRecordId(@Bind("id") final String id);
@SqlQuery
- public Long getRecordIdForTable(@TableNameBinder final TableName tableName,
+ public Long getRecordIdForTable(@Define("tableName") final TableName tableName,
@Bind("id") final String id);
@SqlQuery
diff --git a/util/src/main/java/com/ning/billing/util/dao/TableName.java b/util/src/main/java/com/ning/billing/util/dao/TableName.java
index a5038d5..4dedcd0 100644
--- a/util/src/main/java/com/ning/billing/util/dao/TableName.java
+++ b/util/src/main/java/com/ning/billing/util/dao/TableName.java
@@ -16,31 +16,40 @@
package com.ning.billing.util.dao;
+import javax.annotation.Nullable;
+
public enum TableName {
- ACCOUNT("accounts"),
- ACCOUNT_HISTORY("account_history"),
- ACCOUNT_EMAIL_HISTORY("account_email_history"),
- BUNDLES("bundles"),
- CUSTOM_FIELD_HISTORY("custom_field_history"),
- INVOICE_ITEMS("invoice_items"),
- INVOICE_PAYMENTS("invoice_payments"),
- INVOICES("invoices"),
- PAYMENT_ATTEMPTS("payment_attempts"),
- PAYMENT_HISTORY("payment_history"),
- PAYMENTS("payments"),
- PAYMENT_METHODS("payment_methods"),
- SUBSCRIPTIONS("subscriptions"),
- SUBSCRIPTION_EVENTS("subscription_events"),
- REFUNDS("refunds"),
- TAG_HISTORY("tag_history");
+ ACCOUNT("accounts", ObjectType.ACCOUNT),
+ ACCOUNT_HISTORY("account_history", null),
+ ACCOUNT_EMAIL_HISTORY("account_email_history", ObjectType.ACCOUNT_EMAIL),
+ BUNDLES("bundles", ObjectType.BUNDLE),
+ CUSTOM_FIELD_HISTORY("custom_field_history", null),
+ INVOICE_ITEMS("invoice_items", ObjectType.INVOICE_ITEM),
+ INVOICE_PAYMENTS("invoice_payments", ObjectType.INVOICE_PAYMENT),
+ INVOICES("invoices", ObjectType.INVOICE),
+ PAYMENT_ATTEMPTS("payment_attempts", null),
+ PAYMENT_HISTORY("payment_history", null),
+ PAYMENTS("payments", ObjectType.PAYMENT),
+ PAYMENT_METHODS("payment_methods", ObjectType.PAYMENT_METHOD),
+ SUBSCRIPTIONS("subscriptions", ObjectType.SUBSCRIPTION),
+ SUBSCRIPTION_EVENTS("subscription_events", null),
+ REFUNDS("refunds", ObjectType.REFUND),
+ TAG_DEFINITIONS("tag_definitions", ObjectType.TAG_DEFINITION),
+ TAG_HISTORY("tag_history", null);
private final String tableName;
+ private final ObjectType objectType;
- TableName(final String tableName) {
+ TableName(final String tableName, @Nullable final ObjectType objectType) {
this.tableName = tableName;
+ this.objectType = objectType;
}
public String getTableName() {
return tableName;
}
+
+ public ObjectType getObjectType() {
+ return objectType;
+ }
}
diff --git a/util/src/main/java/com/ning/billing/util/dao/TableNameBinder.java b/util/src/main/java/com/ning/billing/util/dao/TableNameBinder.java
index 545186c..9cacf92 100644
--- a/util/src/main/java/com/ning/billing/util/dao/TableNameBinder.java
+++ b/util/src/main/java/com/ning/billing/util/dao/TableNameBinder.java
@@ -31,7 +31,9 @@ import org.skife.jdbi.v2.sqlobject.BindingAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface TableNameBinder {
+
public static class TableNameBinderFactory implements BinderFactory {
+
public Binder build(final Annotation annotation) {
return new Binder<TableNameBinder, TableName>() {
public void bind(final SQLStatement q, final TableNameBinder bind, final TableName tableName) {
diff --git a/util/src/main/java/com/ning/billing/util/glue/AuditModule.java b/util/src/main/java/com/ning/billing/util/glue/AuditModule.java
new file mode 100644
index 0000000..0a1e8fb
--- /dev/null
+++ b/util/src/main/java/com/ning/billing/util/glue/AuditModule.java
@@ -0,0 +1,34 @@
+/*
+ * 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.glue;
+
+import com.ning.billing.util.audit.dao.AuditDao;
+import com.ning.billing.util.audit.dao.DefaultAuditDao;
+
+import com.google.inject.AbstractModule;
+
+public class AuditModule extends AbstractModule {
+
+ protected void installDaos() {
+ bind(AuditDao.class).to(DefaultAuditDao.class).asEagerSingleton();
+ }
+
+ @Override
+ protected void configure() {
+ installDaos();
+ }
+}
diff --git a/util/src/test/java/com/ning/billing/util/audit/dao/TestDefaultAuditDao.java b/util/src/test/java/com/ning/billing/util/audit/dao/TestDefaultAuditDao.java
new file mode 100644
index 0000000..8ce34d0
--- /dev/null
+++ b/util/src/test/java/com/ning/billing/util/audit/dao/TestDefaultAuditDao.java
@@ -0,0 +1,105 @@
+/*
+ * 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.audit.dao;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.UUID;
+
+import org.skife.jdbi.v2.Handle;
+import org.skife.jdbi.v2.IDBI;
+import org.testng.Assert;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+import com.ning.billing.util.ChangeType;
+import com.ning.billing.util.UtilTestSuiteWithEmbeddedDB;
+import com.ning.billing.util.audit.AuditLog;
+import com.ning.billing.util.bus.Bus;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.CallOrigin;
+import com.ning.billing.util.callcontext.DefaultCallContextFactory;
+import com.ning.billing.util.callcontext.UserType;
+import com.ning.billing.util.clock.Clock;
+import com.ning.billing.util.dao.ObjectType;
+import com.ning.billing.util.dao.TableName;
+import com.ning.billing.util.glue.AuditModule;
+import com.ning.billing.util.tag.MockTagStoreModuleSql;
+import com.ning.billing.util.tag.TagDefinition;
+import com.ning.billing.util.tag.dao.AuditedTagDao;
+import com.ning.billing.util.tag.dao.TagDefinitionDao;
+
+import com.google.inject.Inject;
+
+@Guice(modules = {MockTagStoreModuleSql.class, AuditModule.class})
+public class TestDefaultAuditDao extends UtilTestSuiteWithEmbeddedDB {
+
+ @Inject
+ private TagDefinitionDao tagDefinitionDao;
+
+ @Inject
+ private AuditedTagDao tagDao;
+
+ @Inject
+ private AuditDao auditDao;
+
+ @Inject
+ private Clock clock;
+
+ @Inject
+ private Bus bus;
+
+ @Inject
+ private IDBI dbi;
+
+ private CallContext context;
+
+ @BeforeClass(groups = "slow")
+ public void setup() throws IOException {
+ context = new DefaultCallContextFactory(clock).createCallContext("Audit DAO test", CallOrigin.TEST, UserType.TEST, UUID.randomUUID());
+ bus.start();
+ }
+
+ @AfterClass(groups = "slow")
+ public void tearDown() {
+ bus.stop();
+ }
+
+ @Test(groups = "slow")
+ public void testRetrieveAudits() throws Exception {
+ final TagDefinition defYo = tagDefinitionDao.create("yo", "defintion yo", context);
+
+ // Create a tag
+ tagDao.insertTag(UUID.randomUUID(), ObjectType.ACCOUNT, defYo.getId(), context);
+
+ // Verify we get an audit entry for the tag_history table
+ final Handle handle = dbi.open();
+ final String tagHistoryString = (String) handle.select("select id from tag_history limit 1").get(0).get("id");
+ handle.close();
+
+ final List<AuditLog> auditLogs = auditDao.getAuditLogsForRecordId(TableName.TAG_HISTORY, UUID.fromString(tagHistoryString));
+ Assert.assertEquals(auditLogs.size(), 1);
+ Assert.assertEquals(auditLogs.get(0).getUserToken(), context.getUserToken().toString());
+ Assert.assertEquals(auditLogs.get(0).getChangeType(), ChangeType.INSERT);
+ Assert.assertNull(auditLogs.get(0).getComment());
+ Assert.assertNull(auditLogs.get(0).getReasonCode());
+ Assert.assertEquals(auditLogs.get(0).getUserName(), context.getUserName());
+ Assert.assertNotNull(auditLogs.get(0).getCreatedDate());
+ }
+}