>From 3890eca43f9e1b872b350dccf3df75bc6962970a Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Mon, 6 Aug 2012 13:17:38 -0400
Subject: [PATCH] Ticket 149 - Implementing the search functionality,server
 side.

            Search function call supporting various already present.
            Changes the ds call from searchCertificates to CertificateRepository.findCertRecords().
            Added pagination using start and size options . provided in command line.
---
 .../com/netscape/cms/client/cert/CertFindCLI.java  |   17 +++++++-
 .../netscape/cms/client/cert/CertRestClient.java   |    7 +---
 .../netscape/cms/servlet/cert/CertResource.java    |   16 ++++----
 .../cms/servlet/cert/CertResourceService.java      |   43 +++++++++++++++++---
 .../cms/servlet/cert/model/CertDataInfos.java      |   34 ++++------------
 5 files changed, 71 insertions(+), 46 deletions(-)

diff --git a/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java b/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
index 7e184215aeb836b07404e81b55e2e2dd64caaf95..bc795389db3b32417e8f00dad6d8425d613968c4 100644
--- a/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
+++ b/base/common/src/com/netscape/cms/client/cert/CertFindCLI.java
@@ -103,10 +103,16 @@ public class CertFindCLI extends CLI {
             searchData = new CertSearchData();
             searchData.setSerialNumberRangeInUse(true);
         }
+        String s = cmd.getOptionValue("start");
+        Integer start = s == null ? null : Integer.valueOf(s);
+
+        s = cmd.getOptionValue("size");
+        Integer size = s == null ? null : Integer.valueOf(s);
+
         addSearchAttribute(cmd, searchData);
         CertDataInfos certs = null;
         try {
-            certs = parent.client.findCerts(searchData);
+            certs = parent.client.findCerts(searchData, start, size);
         } catch (CMSException e) {
             System.err.println("Error: Cannot list certificates." + e.getMessage());
             System.exit(-1);
@@ -136,6 +142,15 @@ public class CertFindCLI extends CLI {
 
         Option option = null;
 
+        //pagination options
+        option = new Option(null, "start", true, "Page start");
+        option.setArgName("start");
+        options.addOption(option);
+
+        option = new Option(null, "size", true, "Page size");
+        option.setArgName("size");
+        options.addOption(option);
+
         //help
         options.addOption(null, "help", false, "Show help options");
 
diff --git a/base/common/src/com/netscape/cms/client/cert/CertRestClient.java b/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
index 8410f41660c8aed9453a8613b70968e2d847f9e1..7c8b9f3e42ba0d40d5261eaaf480ea93c3f8f09f 100644
--- a/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
+++ b/base/common/src/com/netscape/cms/client/cert/CertRestClient.java
@@ -54,11 +54,8 @@ public class CertRestClient extends CMSRestClient {
         return certClient.getCert(id);
     }
 
-    public CertDataInfos findCerts(CertSearchData data) {
-        return certClient.searchCerts(
-                data,
-                CertResource.DEFAULT_MAXRESULTS,
-                CertResource.DEFAULT_MAXTIME);
+    public CertDataInfos findCerts(CertSearchData data, Integer start, Integer size) {
+        return certClient.searchCerts(data, start, size);
     }
 
     public CertRequestInfo revokeCert(CertId id, CertRevokeRequest request) {
diff --git a/base/common/src/com/netscape/cms/servlet/cert/CertResource.java b/base/common/src/com/netscape/cms/servlet/cert/CertResource.java
index 48650f05f6cc385154ea6b21ccc00bdeb5244b9c..f3a9d4129af9d6b5ce61c2b413a9dc03225a8e25 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/CertResource.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/CertResource.java
@@ -21,25 +21,25 @@ import com.netscape.cms.servlet.request.model.CertRequestInfo;
 @Path("")
 public interface CertResource {
 
-    public static final int DEFAULT_MAXTIME = 10;
-    public static final int DEFAULT_MAXRESULTS = 100;
+    public static final int DEFAULT_MAXTIME = 0;
+    public static final int DEFAULT_MAXRESULTS = 20;
 
     @GET
     @Path("certs")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public CertDataInfos listCerts(
-                                 @QueryParam("status") String status,
-                                 @DefaultValue(""+DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
-                                 @DefaultValue(""+DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
+            @QueryParam("status") String status,
+            @DefaultValue("" + DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
+            @DefaultValue("" + DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
 
     @POST
     @Path("certs/search")
     @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
     public CertDataInfos searchCerts(
-                                CertSearchData data,
-                                @DefaultValue(""+DEFAULT_MAXRESULTS) @QueryParam("maxResults") int maxResults,
-                                @DefaultValue(""+DEFAULT_MAXTIME) @QueryParam("maxTime") int maxTime);
+            CertSearchData data,
+            @QueryParam("start") Integer start,
+            @QueryParam("size") Integer size);
 
     @GET
     @Path("certs/{id}")
diff --git a/base/common/src/com/netscape/cms/servlet/cert/CertResourceService.java b/base/common/src/com/netscape/cms/servlet/cert/CertResourceService.java
index 395907b5366d5baad567993d5e4d9ec078720ed4..6a3f0d79a96d9957440e2db86618e5dd6882db25 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/CertResourceService.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/CertResourceService.java
@@ -79,6 +79,7 @@ public class CertResourceService extends CMSResourceService implements CertResou
     ICertificateAuthority authority;
     ICertificateRepository repo;
 
+    public final static int DEFAULT_SIZE = 20;
 
     public CertResourceService() {
         authority = (ICertificateAuthority) CMS.getSubsystem("ca");
@@ -330,18 +331,48 @@ public class CertResourceService extends CMSResourceService implements CertResou
     }
 
     @Override
-    public CertDataInfos searchCerts(CertSearchData data, int maxResults, int maxTime) {
+    public CertDataInfos searchCerts(CertSearchData data, Integer start, Integer size) {
         if (data == null) {
             throw new WebApplicationException(Response.Status.BAD_REQUEST);
         }
+        start = start == null ? 0 : start;
+        size = size == null ? DEFAULT_SIZE : size;
         String filter = createSearchFilter(data);
-        CertDataInfos infos;
 
+        CertDataInfos infos = new CertDataInfos();
+
+        Enumeration<ICertRecord> e = null;
         try {
-            infos = getCertList(filter, maxResults, maxTime);
-        } catch (EBaseException e) {
-            e.printStackTrace();
-            throw new CMSException("Error listing certs in CertsResourceService.listCerts!");
+
+            e = repo.findCertRecords(filter);
+
+            int i = 0;
+
+            // skip to the start of the page
+            for (; i < start && e.hasMoreElements(); i++)
+                e.nextElement();
+
+            // return entries up to the page size
+            for (; i < start + size && e.hasMoreElements(); i++) {
+                ICertRecord user = e.nextElement();
+                infos.addCertData(createCertDataInfo(user));
+            }
+
+            // count the total entries
+            for (; e.hasMoreElements(); i++)
+                e.nextElement();
+
+            if (start > 0) {
+                URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", Math.max(start - size, 0)).build();
+                infos.addLink(new Link("prev", uri));
+            }
+
+            if (start + size < i) {
+                URI uri = uriInfo.getRequestUriBuilder().replaceQueryParam("start", start + size).build();
+                infos.addLink(new Link("next", uri));
+            }
+        } catch (EBaseException e1) {
+            throw new CMSException("Error listing certs in CertsResourceService.listCerts!" + e.toString());
         }
 
         return infos;
diff --git a/base/common/src/com/netscape/cms/servlet/cert/model/CertDataInfos.java b/base/common/src/com/netscape/cms/servlet/cert/model/CertDataInfos.java
index 2b1ad42e80e43174f30b612dc3b0c6e523f0e9ea..189c58cbeb9d2de467dc4aacf3fcf4a8e9112571 100644
--- a/base/common/src/com/netscape/cms/servlet/cert/model/CertDataInfos.java
+++ b/base/common/src/com/netscape/cms/servlet/cert/model/CertDataInfos.java
@@ -17,20 +17,20 @@
 //--- END COPYRIGHT BLOCK ---
 package com.netscape.cms.servlet.cert.model;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
 import javax.xml.bind.annotation.XmlElementRef;
 import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlTransient;
 
-import com.netscape.cms.servlet.base.model.Link;
+import org.jboss.resteasy.plugins.providers.atom.Link;
 
 @XmlRootElement(name = "CertDataInfos")
 public class CertDataInfos {
 
-    protected Collection<CertDataInfo> certInfos;
-    protected List<Link> links;
+    protected Collection<CertDataInfo> certInfos = new ArrayList<CertDataInfo>();
+    protected List<Link> links = new ArrayList<Link>();
 
     /**
      * @return the CertInfos
@@ -62,29 +62,11 @@ public class CertDataInfos {
         this.links = links;
     }
 
-    @XmlTransient
-    public String getNext() {
-        if (links == null) {
-            return null;
-        }
-        for (Link link : links) {
-            if ("next".equals(link.getRelationship())) {
-                return link.getHref();
-            }
-        }
-        return null;
+    public void addCertData(CertDataInfo certInfo){
+        this.certInfos.add(certInfo);
     }
 
-    @XmlTransient
-    public String getPrevious() {
-        if (links == null) {
-            return null;
-        }
-        for (Link link : links) {
-            if ("previous".equals(link.getRelationship())) {
-                return link.getHref();
-            }
-        }
-        return null;
+    public void addLink(Link link) {
+        this.links.add(link);
     }
 }
-- 
1.7.10.2

