From 1d998b8cfa39094b6814ce8807db1517bc387d1b Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 12 Sep 2013 16:14:41 -0400
Subject: [PATCH] Connected TPS activity database to LDAP.

The ActivityDatabase has been reimplemented using LDAPDatabase. The
ActivityRecord has been modified to specify the object class and
attribute mappings.

Ticket #652
---
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java |  5 ++-
 .../server/tps/logging/ActivityDatabase.java       | 49 ++++++++--------------
 .../server/tps/logging/ActivityRecord.java         | 23 ++++++++--
 .../server/tps/logging/ActivityService.java        |  6 +--
 4 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
index 853e970c1acd51056857d409ed2378bb5873660a..937ac6de0ad59523a5ccfa03f83cae58dbe2e394 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -52,7 +52,7 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
     public ISubsystem owner;
     public IConfigStore config;
 
-    public ActivityDatabase activityDatabase = new ActivityDatabase();
+    public ActivityDatabase activityDatabase;
     public AuthenticatorDatabase authenticatorDatabase = new AuthenticatorDatabase();
     public ConnectionDatabase connectionDatabase = new ConnectionDatabase();
     public TPSCertDatabase certDatabase = new TPSCertDatabase();
@@ -76,6 +76,9 @@ public class TPSSubsystem implements IAuthority, ISubsystem {
         IDBSubsystem dbSubsystem = DBSubsystem.getInstance();
         IConfigStore cs = CMS.getConfigStore();
 
+        String activityDatabaseDN = cs.getString("tokendb.activityBaseDN");
+        activityDatabase = new ActivityDatabase(dbSubsystem, activityDatabaseDN);
+
         String tokenDatabaseDN = cs.getString("tokendb.baseDN");
         tokenDatabase = new TokenDatabase(dbSubsystem, tokenDatabaseDN);
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityDatabase.java
index 0b74e5fe4b89efbf4e6ff81159f16156c69b27de..aecfcad2a6ff1f9cdb230f7b0c77ce6459d2d2ec 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityDatabase.java
@@ -20,7 +20,9 @@ package org.dogtagpki.server.tps.logging;
 
 import java.util.Date;
 
-import com.netscape.cmscore.dbs.Database;
+import com.netscape.certsrv.base.EBaseException;
+import com.netscape.certsrv.dbs.IDBSubsystem;
+import com.netscape.cmscore.dbs.LDAPDatabase;
 
 /**
  * This class implements in-memory activity database. In the future this
@@ -28,43 +30,26 @@ import com.netscape.cmscore.dbs.Database;
  *
  * @author Endi S. Dewata
  */
-public class ActivityDatabase extends Database<ActivityRecord> {
+public class ActivityDatabase extends LDAPDatabase<ActivityRecord> {
 
-    public ActivityDatabase() {
-        super("Activity");
-
-        // add sample records
-        try {
-            ActivityRecord record1 = new ActivityRecord();
-            record1.setID("activity1");
-            record1.setTokenID("token1");
-            record1.setUserID("user1");
-            record1.setIp("192.168.1.1");
-            record1.setOperation("enroll");
-            record1.setResult("success");
-            addRecord(record1);
-
-            ActivityRecord record2 = new ActivityRecord();
-            record2.setID("activity2");
-            record2.setTokenID("token2");
-            record2.setUserID("user2");
-            record2.setIp("192.168.1.2");
-            record2.setOperation("enroll");
-            record2.setResult("failed");
-            addRecord(record2);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+    public ActivityDatabase(IDBSubsystem dbSubsystem, String baseDN) throws EBaseException {
+        super("Activity", dbSubsystem, baseDN, ActivityRecord.class);
     }
 
-    public void addRecord(ActivityRecord activityRecord) throws Exception {
+    @Override
+    public void addRecord(String id, ActivityRecord activityRecord) throws Exception {
         activityRecord.setDate(new Date());
 
-        addRecord(activityRecord.getID(), activityRecord);
+        super.addRecord(id, activityRecord);
     }
 
-    public void updateRecord(ActivityRecord activityRecord) throws Exception {
-        updateRecord(activityRecord.getID(), activityRecord);
+    @Override
+    public String createDN(String id) {
+        return "cn=" + id + "," + baseDN;
+    }
+
+    @Override
+    public String createFilter(String filter) {
+        return "(id=*)";
     }
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityRecord.java
index 387a85f00afffd4f4f2105169d243293535566cf..4794ecb8bc579e601f35ce97e77af2a3ec0ed686 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityRecord.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityRecord.java
@@ -20,10 +20,17 @@ package org.dogtagpki.server.tps.logging;
 
 import java.util.Date;
 
+import com.netscape.cmscore.dbs.DBAttribute;
+import com.netscape.cmscore.dbs.DBObjectClasses;
+import com.netscape.cmscore.dbs.DBRecord;
+
 /**
  * @author Endi S. Dewata
  */
-public class ActivityRecord {
+@DBObjectClasses({ "top", "tokenActivity" })
+public class ActivityRecord extends DBRecord {
+
+    private static final long serialVersionUID = 1L;
 
     String id;
     String tokenID;
@@ -36,14 +43,16 @@ public class ActivityRecord {
     String type;
     Date date;
 
-    public String getID() {
+    @DBAttribute("cn")
+    public String getId() {
         return id;
     }
 
-    public void setID(String id) {
+    public void setId(String id) {
         this.id = id;
     }
 
+    @DBAttribute("tokenID")
     public String getTokenID() {
         return tokenID;
     }
@@ -52,6 +61,7 @@ public class ActivityRecord {
         this.tokenID = tokenID;
     }
 
+    @DBAttribute("tokenUserID")
     public String getUserID() {
         return userID;
     }
@@ -60,6 +70,7 @@ public class ActivityRecord {
         this.userID = userID;
     }
 
+    @DBAttribute("tokenIP")
     public String getIp() {
         return ip;
     }
@@ -68,6 +79,7 @@ public class ActivityRecord {
         this.ip = ip;
     }
 
+    @DBAttribute("tokenOp")
     public String getOperation() {
         return operation;
     }
@@ -76,6 +88,7 @@ public class ActivityRecord {
         this.operation = operation;
     }
 
+    @DBAttribute("tokenResult")
     public String getResult() {
         return result;
     }
@@ -84,6 +97,7 @@ public class ActivityRecord {
         this.result = result;
     }
 
+    @DBAttribute("tokenMsg")
     public String getMessage() {
         return message;
     }
@@ -92,6 +106,7 @@ public class ActivityRecord {
         this.message = message;
     }
 
+    @DBAttribute("extensions")
     public String getExtensions() {
         return extensions;
     }
@@ -100,6 +115,7 @@ public class ActivityRecord {
         this.extensions = extensions;
     }
 
+    @DBAttribute("tokenType")
     public String getType() {
         return type;
     }
@@ -108,6 +124,7 @@ public class ActivityRecord {
         this.type = type;
     }
 
+    @DBAttribute("dateOfCreate")
     public Date getDate() {
         return date;
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityService.java
index fdbda48160b5c78149d7dda7a5fd48f6f05f88a8..61e7367e6817fca0f31ac6aee8b976c7ac851bf4 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/logging/ActivityService.java
@@ -47,12 +47,12 @@ public class ActivityService extends PKIService implements ActivityResource {
     public ActivityData createActivityData(ActivityRecord activityRecord) {
 
         ActivityData activityData = new ActivityData();
-        activityData.setID(activityRecord.getID());
+        activityData.setID(activityRecord.getId());
         activityData.setTokenID(activityRecord.getTokenID());
         activityData.setUserID(activityRecord.getUserID());
         activityData.setDate(activityRecord.getDate());
 
-        String activityID = activityRecord.getID();
+        String activityID = activityRecord.getId();
         try {
             activityID = URLEncoder.encode(activityID, "UTF-8");
         } catch (UnsupportedEncodingException e) {
@@ -69,7 +69,7 @@ public class ActivityService extends PKIService implements ActivityResource {
     public ActivityRecord createActivityRecord(ActivityData activityData) {
 
         ActivityRecord activityRecord = new ActivityRecord();
-        activityRecord.setID(activityData.getID());
+        activityRecord.setId(activityData.getID());
         activityRecord.setTokenID(activityData.getTokenID());
         activityRecord.setUserID(activityData.getUserID());
         activityRecord.setDate(activityData.getDate());
-- 
1.8.1.4

