>From f71f2f6bf3b5980122989d1059cee9d4516bdfe0 Mon Sep 17 00:00:00 2001
From: Christina Fu <cfu@redhat.com>
Date: Sat, 30 Aug 2014 15:28:57 -0700
Subject: [PATCH] ticket #882 unique certID for certificate records

---
 base/common/src/org/dogtagpki/tps/main/Util.java      | 19 +++++++++++++++++++
 .../dogtagpki/server/tps/dbs/ActivityDatabase.java    | 19 +++++--------------
 .../server/tps/processor/EnrolledCertsInfo.java       |  4 ++--
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/base/common/src/org/dogtagpki/tps/main/Util.java b/base/common/src/org/dogtagpki/tps/main/Util.java
index c39b43577bef22362688a94e7f77703cfc3ad49e..c07a92bb784eaa2875308244a6ecf2e1b64bda97 100644
--- a/base/common/src/org/dogtagpki/tps/main/Util.java
+++ b/base/common/src/org/dogtagpki/tps/main/Util.java
@@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.security.spec.AlgorithmParameterSpec;
+import java.util.Calendar;
 
 import netscape.security.x509.AuthorityKeyIdentifierExtension;
 import netscape.security.x509.KeyIdentifier;
@@ -332,4 +333,22 @@ public class Util {
         return (CMS.BtoA(kid.getIdentifier()).trim());
     }
 
+    public static String generateTimeBaseUniqueString() {
+        Calendar c = Calendar.getInstance();
+        int year = c.get(Calendar.YEAR);
+        int month = c.get(Calendar.MONTH) + 1;
+        int day = c.get(Calendar.DAY_OF_MONTH);
+        int hour = c.get(Calendar.HOUR_OF_DAY);
+        int minute = c.get(Calendar.MINUTE);
+        int second = c.get(Calendar.SECOND);
+        int microSecond = c.get(Calendar.MILLISECOND) * 1000;
+
+        String uniqueString = String.format(
+                "%04d%02d%02d%02d%02d%02d%06d",
+                year, month, day,
+                hour, minute, second, microSecond);
+
+        return uniqueString;
+    }
+
 }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
index 6c5211c55ca6903ba6294245760f9f0f4f7d94c7..25f93cb5867c7db9687f86e4a252e9a6abd7b186 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/ActivityDatabase.java
@@ -22,6 +22,7 @@ import java.util.Calendar;
 import java.util.Date;
 
 import org.apache.commons.lang.StringUtils;
+import org.dogtagpki.tps.main.Util;
 
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.dbs.IDBSubsystem;
@@ -51,22 +52,12 @@ public class ActivityDatabase extends LDAPDatabase<ActivityRecord> {
     public ActivityRecord log(
             String ip, String tokenID, String operation, String result,
             String message, String userID, String tokenType) throws Exception {
-
         Calendar c = Calendar.getInstance();
-        int year = c.get(Calendar.YEAR);
-        int month = c.get(Calendar.MONTH) + 1;
-        int day = c.get(Calendar.DAY_OF_MONTH);
-        int hour = c.get(Calendar.HOUR_OF_DAY);
-        int minute = c.get(Calendar.MINUTE);
-        int second = c.get(Calendar.SECOND);
-        int microSecond = c.get(Calendar.MILLISECOND) * 1000;
+
+        String uniqueString = Util.generateTimeBaseUniqueString();
         long threadID = Thread.currentThread().getId();
-
-        String id = String.format(
-                "%04d%02d%02d%02d%02d%02d%06d.%x",
-                year, month, day,
-                hour, minute, second, microSecond,
-                threadID);
+        String threadIDS = String.format("%x", threadID);
+        String id = uniqueString + "." + threadIDS;
 
         ActivityRecord activityRecord = new ActivityRecord();
         activityRecord.setId(id);
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/EnrolledCertsInfo.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/EnrolledCertsInfo.java
index 16716ab07799b9afd96ed3c14fb226840842ff8b..297b191afcc084c1297efd5042562882795c0190 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/EnrolledCertsInfo.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/processor/EnrolledCertsInfo.java
@@ -167,8 +167,8 @@ public class EnrolledCertsInfo {
             String serialNumber = "0x" + hexSerial;
             certRecord.setSerialNumber(serialNumber);
 
-            //id - TODO - calculate the real id later
-            String id = serialNumber+":"+uid;
+            String uniqueString = Util.generateTimeBaseUniqueString();
+            String id = hexSerial + "." + uniqueString;
 
             certRecord.setId(id);
             CMS.debug("EnrolledCertsInfo.toTPSCertRecords: converting cert:"+ certRecord.getId());
-- 
1.8.4.2

