From 550fa3bb14a3832aaf66099714eb5ef6e321c746 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 16 Dec 2013 02:24:44 -0500
Subject: [PATCH] Added dialog for adding TPS tokens.

A new dialog box has been added for adding TPS token. A separate
token ID attribute has been added to the REST service as required
by Backbone.

Ticket #654
---
 .../com/netscape/certsrv/tps/token/TokenData.java  | 52 ++++++++++++++++++++++
 .../netscape/cmstools/tps/token/TokenAddCLI.java   |  2 +-
 base/tps-tomcat/shared/webapps/tps/js/token.js     |  7 ++-
 base/tps-tomcat/shared/webapps/tps/tokens.html     | 25 ++++++++---
 .../dogtagpki/server/tps/token/TokenService.java   |  8 ++--
 5 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/tps/token/TokenData.java b/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
index cbd8275b2091153468909bdff6c62afa95a6d6da..3c6b2d7baff23940cc2cbdb235f241fbe5313ae9 100644
--- a/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
+++ b/base/common/src/com/netscape/certsrv/tps/token/TokenData.java
@@ -51,6 +51,7 @@ public class TokenData {
     }
 
     String id;
+    String tokenID;
     String userID;
     String status;
     String reason;
@@ -70,6 +71,15 @@ public class TokenData {
         this.id = id;
     }
 
+    @XmlElement(name="TokenID")
+    public String getTokenID() {
+        return tokenID;
+    }
+
+    public void setTokenID(String tokenID) {
+        this.tokenID = tokenID;
+    }
+
     @XmlElement(name="UserID")
     public String getUserID() {
         return userID;
@@ -146,8 +156,15 @@ public class TokenData {
     public int hashCode() {
         final int prime = 31;
         int result = 1;
+        result = prime * result + ((appletID == null) ? 0 : appletID.hashCode());
+        result = prime * result + ((createTimestamp == null) ? 0 : createTimestamp.hashCode());
         result = prime * result + ((id == null) ? 0 : id.hashCode());
+        result = prime * result + ((keyInfo == null) ? 0 : keyInfo.hashCode());
         result = prime * result + ((link == null) ? 0 : link.hashCode());
+        result = prime * result + ((modifyTimestamp == null) ? 0 : modifyTimestamp.hashCode());
+        result = prime * result + ((reason == null) ? 0 : reason.hashCode());
+        result = prime * result + ((status == null) ? 0 : status.hashCode());
+        result = prime * result + ((tokenID == null) ? 0 : tokenID.hashCode());
         result = prime * result + ((userID == null) ? 0 : userID.hashCode());
         return result;
     }
@@ -161,16 +178,51 @@ public class TokenData {
         if (getClass() != obj.getClass())
             return false;
         TokenData other = (TokenData) obj;
+        if (appletID == null) {
+            if (other.appletID != null)
+                return false;
+        } else if (!appletID.equals(other.appletID))
+            return false;
+        if (createTimestamp == null) {
+            if (other.createTimestamp != null)
+                return false;
+        } else if (!createTimestamp.equals(other.createTimestamp))
+            return false;
         if (id == null) {
             if (other.id != null)
                 return false;
         } else if (!id.equals(other.id))
             return false;
+        if (keyInfo == null) {
+            if (other.keyInfo != null)
+                return false;
+        } else if (!keyInfo.equals(other.keyInfo))
+            return false;
         if (link == null) {
             if (other.link != null)
                 return false;
         } else if (!link.equals(other.link))
             return false;
+        if (modifyTimestamp == null) {
+            if (other.modifyTimestamp != null)
+                return false;
+        } else if (!modifyTimestamp.equals(other.modifyTimestamp))
+            return false;
+        if (reason == null) {
+            if (other.reason != null)
+                return false;
+        } else if (!reason.equals(other.reason))
+            return false;
+        if (status == null) {
+            if (other.status != null)
+                return false;
+        } else if (!status.equals(other.status))
+            return false;
+        if (tokenID == null) {
+            if (other.tokenID != null)
+                return false;
+        } else if (!tokenID.equals(other.tokenID))
+            return false;
         if (userID == null) {
             if (other.userID != null)
                 return false;
diff --git a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
index 0d495ddc766773dae3221f2045248dfa80f2637a..bb9d452a575573d4201f623e1fc88a584585794d 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/token/TokenAddCLI.java
@@ -69,7 +69,7 @@ public class TokenAddCLI extends CLI {
         String tokenID = cmdArgs[0];
 
         TokenData tokenData = new TokenData();
-        tokenData.setID(tokenID);
+        tokenData.setTokenID(tokenID);
         tokenData.setUserID(cmd.getOptionValue("user"));
 
         tokenData = tokenCLI.tokenClient.addToken(tokenData);
diff --git a/base/tps-tomcat/shared/webapps/tps/js/token.js b/base/tps-tomcat/shared/webapps/tps/js/token.js
index 8131f4b76a943c6adc751654c423e3a286929580..4017b6859ceb3f2bc3cb4cf542181d3955be53a1 100644
--- a/base/tps-tomcat/shared/webapps/tps/js/token.js
+++ b/base/tps-tomcat/shared/webapps/tps/js/token.js
@@ -22,8 +22,10 @@
 var TokenModel = Model.extend({
     urlRoot: "/tps/rest/tokens",
     parseResponse: function(response) {
+        if (!response || !response.Token) return {};
         return {
             id: response.Token["@id"],
+            tokenID: response.Token.TokenID,
             userID: response.Token.UserID,
             status: response.Token.Status,
             reason: response.Token.Reason,
@@ -36,7 +38,8 @@ var TokenModel = Model.extend({
     createRequest: function(attributes) {
         return {
             Token: {
-                "@id": attributes.id,
+                "@id": this.id,
+                TokenID: attributes.tokenID,
                 UserID: attributes.userID,
                 Status: attributes.status,
                 Reason: attributes.reason,
@@ -50,6 +53,7 @@ var TokenModel = Model.extend({
 });
 
 var TokenCollection = Collection.extend({
+    model: TokenModel,
     urlRoot: "/tps/rest/tokens",
     getEntries: function(response) {
         return response.Tokens.Token;
@@ -60,6 +64,7 @@ var TokenCollection = Collection.extend({
     parseEntry: function(entry) {
         return new TokenModel({
             id: entry["@id"],
+            tokenID: entry.TokenID,
             userID: entry.UserID,
             status: entry.Status,
             reason: entry.Reason,
diff --git a/base/tps-tomcat/shared/webapps/tps/tokens.html b/base/tps-tomcat/shared/webapps/tps/tokens.html
index 695ddf1df39254b5419849c44b4d310209d2adf5..6a0502c9e43f5df771106ecbbb7643458b0d25e7 100644
--- a/base/tps-tomcat/shared/webapps/tps/tokens.html
+++ b/base/tps-tomcat/shared/webapps/tps/tokens.html
@@ -25,16 +25,26 @@
     <script src="/tps/js/token.js"></script>
     <script>
 $(function() {
+    var addDialog = new Dialog({
+        el: $("#token-dialog"),
+        title: "Add Token",
+        readonly: ["status", "reason", "appletID", "keyInfo",
+            "createTimestamp", "modifyTimestamp"],
+        actions: ["cancel", "add"]
+    });
+
     var editDialog = new Dialog({
         el: $("#token-dialog"),
         title: "Edit Token",
-        readonly: ["id", "status", "reason", "appletID", "keyInfo",
-            "createTimestamp", "modifyTimestamp"]
+        readonly: ["tokenID", "status", "reason", "appletID", "keyInfo",
+            "createTimestamp", "modifyTimestamp"],
+        actions: ["cancel", "save"]
     });
 
     new TableView({
         el: $("table[name='tokens']"),
         collection: new TokenCollection({ size: 3 }),
+        addDialog: addDialog,
         editDialog: editDialog
     });
 });
@@ -49,7 +59,10 @@ $(function() {
     <tr>
          <th class="rcue-table-actions" colspan="8">
              <span><input type="text" placeholder="Search..."></span>
-             <span><button>Add</button><button>Remove</button></span>
+             <span>
+                 <button name="add">Add</button>
+                 <button name="remove">Remove</button>
+             </span>
          </th>
     </tr>
     <tr>
@@ -65,7 +78,7 @@ $(function() {
 </thead>
 <tbody>
     <tr>
-        <td name="id">&nbsp;</td>
+        <td name="tokenID">&nbsp;</td>
         <td name="userID">&nbsp;</td>
         <td name="status">&nbsp;</td>
         <td name="reason">&nbsp;</td>
@@ -94,7 +107,7 @@ $(function() {
             <a class="rcue-button-close" href="#"></a>
         </header>
         <fieldset>
-            <label>Token ID</label><input name="id" type="text"><br>
+            <label>Token ID</label><input name="tokenID" type="text"><br>
             <label>User ID</label><input name="userID" type="text"><br>
             <label>Status</label><input name="status" type="text"><br>
             <label>Reason</label><input name="reason" type="text"><br>
@@ -104,7 +117,9 @@ $(function() {
             <label>Modified</label><input name="modifyTimestamp" type="text"><br>
         </fieldset>
         <footer>
+            <button name="add" class="primary">Add</button>
             <button name="save" class="primary">Save</button>
+            <button name="remove" class="primary">Remove</button>
             <button name="cancel">Cancel</button>
         </footer>
     </div>
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
index 4183ff6e247660fbe89356b5da63742de1f3defc..27291cf7f4e30605834e1c15d444f03f793e1794 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/token/TokenService.java
@@ -69,6 +69,7 @@ public class TokenService extends PKIService implements TokenResource {
 
         TokenData tokenData = new TokenData();
         tokenData.setID(tokenRecord.getId());
+        tokenData.setTokenID(tokenRecord.getId());
         tokenData.setUserID(tokenRecord.getUserID());
         tokenData.setStatus(tokenRecord.getStatus());
         tokenData.setReason(tokenRecord.getReason());
@@ -177,14 +178,15 @@ public class TokenService extends PKIService implements TokenResource {
 
         if (tokenData == null) throw new BadRequestException("Token data is null.");
 
-        CMS.debug("TokenService.addToken(\"" + tokenData.getID() + "\")");
+        String tokenID = tokenData.getTokenID();
+        CMS.debug("TokenService.addToken(\"" + tokenID + "\")");
 
         try {
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             TokenDatabase database = subsystem.getTokenDatabase();
 
-            database.addRecord(tokenData.getID(), createTokenRecord(tokenData));
-            tokenData = createTokenData(database.getRecord(tokenData.getID()));
+            database.addRecord(tokenID, createTokenRecord(tokenData));
+            tokenData = createTokenData(database.getRecord(tokenID));
 
             return Response
                     .created(tokenData.getLink().getHref())
-- 
1.8.4.2

