Author: channa
Date: Mon Jul 14 01:49:12 2008
New Revision: 19222
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=19222

Log:
Added pagination support in the mashup server using caching, pending support 
from the registry. Query results are cached and retrieved from the cache when 
navigating across pages; cache is cleared when a new query is submitted. 
MASHUP-734.


Modified:
   
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/RegistryUtils.java
   trunk/mashup/java/modules/www/search.jsp

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/RegistryUtils.java
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/RegistryUtils.java?rev=19222&r1=19221&r2=19222&view=diff
==============================================================================
--- 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/RegistryUtils.java
  (original)
+++ 
trunk/mashup/java/modules/core/src/org/wso2/mashup/webapp/utils/RegistryUtils.java
  Mon Jul 14 01:49:12 2008
@@ -61,6 +61,7 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Arrays;
+import java.util.Enumeration;
 
 public class RegistryUtils {
 
@@ -409,12 +410,8 @@
             LogEntry[] logs =
                     userRegistry.getLogs(params[0], filterValue, params[2], 
fromDate, toDate, true);
 
-            // Operation can only be confirmed as the initial 'add' if all 
logs are processed.
-            if (logs.length < (maxResults + 1)) {
-                putText = "added";
-            } else {
-                putText = "added/updated";
-            }
+            // Operation cannot be confirmed as 'add' unless ALL logs are 
processed.
+            putText = "added/updated";
 
             int shownResults = 0;
             // iterate until we have the minimum number of non-anon logs
@@ -593,12 +590,30 @@
                     results.getResultCount() : firstElement + pageLength;
             resultPage.setResults(results.getResults().subList(firstElement, 
lastPageElement));
             resultPage.setResultCount(results.getResultCount());
+            resultPage.setQueryName(results.getQueryName());
+            resultPage.setResultsType(results.getResultsType());
             return resultPage;
         } else {
             return results;
         }
     }
 
+    /**
+     * Clears query results cached in session.
+     * @param request Servlet request object.
+     */
+    public static void clearQueryCache(HttpServletRequest request) {
+        HttpSession session = request.getSession();
+        Enumeration attributes = session.getAttributeNames();
+
+        while (attributes.hasMoreElements()) {
+            String attribName =  (String) attributes.nextElement();
+            if (attribName.startsWith(MashupConstants.QUERY_CACHE_KEY)) {
+                session.removeAttribute(attribName);
+            }
+        }
+    }
+
     public static String getRequestBody(HttpServletRequest request) {
         String body = "";
 

Modified: trunk/mashup/java/modules/www/search.jsp
URL: 
http://wso2.org/svn/browse/wso2/trunk/mashup/java/modules/www/search.jsp?rev=19222&r1=19221&r2=19222&view=diff
==============================================================================
--- trunk/mashup/java/modules/www/search.jsp    (original)
+++ trunk/mashup/java/modules/www/search.jsp    Mon Jul 14 01:49:12 2008
@@ -117,6 +117,8 @@
     String queryPath = "";
     String[] queryParams = new String[] { };
     QueryResults results = null;
+    int currentPage = 0;
+    int numPages = 0;
 
     if (query != null) {
         if (query.equalsIgnoreCase("mashups") && searchScope.equals("") && 
searchFor.equals("")) {
@@ -250,14 +252,37 @@
             // queryParams defaults to new String[] { };
         }
 
-        results = RegistryUtils.doQuery(userRegistry, queryTitle, queryPath, 
queryParams,
-                                        MashupConstants.MAX_RESULTS_COUNT);
+        // Implement pagination if the response is an HTML page, not for feeds.
+        if (isFeed) {
+            results = RegistryUtils.doQuery(userRegistry, queryTitle, 
queryPath, queryParams,
+                                            MashupConstants.MAX_RESULTS_COUNT);
+        } else {
+            String requestedPage = request.getParameter("pageNumber") != null ?
+                    request.getParameter("pageNumber") : "1";
+
+            // Clear the cache for a fresh query, to prevent stale results.
+            if (request.getParameter("pagenavi") == null) {
+                RegistryUtils.clearQueryCache(request);
+            }
+
+            int pageLength = MashupConstants.DEFAULT_PAGE_SIZE;
+            int pageNumber = Integer.parseInt(requestedPage);
+            int firstElement = ((pageNumber - 1) * pageLength);
+            results = RegistryUtils.doCachedQuery(userRegistry, queryTitle, 
queryPath, queryParams,
+                                                  
MashupConstants.MAX_RESULTS_COUNT, request,
+                                                  firstElement, pageLength);
+            int numElements = results.getResultCount();
+            numPages = numElements % pageLength == 0 ? numElements / 
pageLength :
+                    (numElements / pageLength) + 1;
+            currentPage = pageNumber > 0 & pageNumber <= numPages ? pageNumber 
: 1;
+        }
 
         if (feelingLucky && results.getResultCount() == 1) {
             Iterator iter = results.getResults().iterator();
             if (results.getResultsType().equals("resource") && iter.hasNext()) 
{
                 QueryResult result = (QueryResult) iter.next();
-                response.sendRedirect("mashup.jsp?author=" + 
result.getAuthor() + "&mashup=" + result.getName());
+                response.sendRedirect("mashup.jsp?author=" + 
result.getAuthor() + "&mashup=" +
+                        result.getName());
             }
         }
     }
@@ -498,6 +523,11 @@
             $(autoSuggestField + "-nameList").innerHTML = "";
             $(autoSuggestField + "-nameList").style.display = 'none';
         }
+
+        function moveToPage(pageNum) {
+            $("pageNumber").value = pageNum;
+            document.paginator.submit();
+        }
     </script>
 </head>
 
@@ -1050,7 +1080,44 @@
 
     nextId++;
 } %>
-
+<% if (query != null) {%>
+      <tr>
+          <td colspan="5" align="right">
+              <form name="paginator" id="paginator" method='get' 
action="search.jsp">
+                  <input type="hidden" name="pagenavi" value="true"/>
+                  <input type="hidden" name="query" value="<%= query %>"/>
+                  <input type="hidden" name="scope" value="<%= searchScope 
%>"/>
+                  <% if (searchFor != null) { %>
+                  <input type="hidden" name="for" value="<%= searchFor %>"/>
+                  <% } if (onMashup != null) { %>
+                  <input type="hidden" name="onmashup" value="<%= onMashup 
%>"/>
+                  <% } if (paramTags != null) { %>
+                  <input type="hidden" name="tags" value="<%= paramTags %>"/>
+                  <% } if (paramTitles != null) { %>
+                  <input type="hidden" name="titles" value="<%= paramTitles 
%>"/>
+                  <% } if (paramDescriptions != null) { %>
+                  <input type="hidden" name="descriptions" value="<%= 
paramDescriptions %>"/>
+                  <% } if (paramPeriod != null) { %>
+                  <input type="hidden" name="period" value="<%= paramPeriod 
%>"/>
+                  <% } if (paramActivity != null) { %>
+                  <input type="hidden" name="activity" value="<%= 
paramActivity %>"/>
+                  <% } %>
+              <% if (currentPage != 1) { %>
+              <a href="javascript:moveToPage(1)"><< First</a>&nbsp;|
+              <a href="javascript:moveToPage(<%= currentPage - 1 %>)">< 
Previous</a>
+              &nbsp;
+              <% }
+              if (currentPage < numPages) { %>
+              &nbsp;
+              <a href="javascript:moveToPage(<%= currentPage + 1 %>)">Next 
></a>&nbsp;|
+              <a href="javascript:moveToPage(<%= numPages %>)">Last >></a>
+              <% } %>
+              &nbsp; Page <input type="text" id="pageNumber" name="pageNumber" 
style="width:40px;" value="<%= currentPage %>"/> of <%= numPages %>
+              &nbsp; <input type="submit" value="Go"/>
+              </form>
+          </td>
+      </tr>
+<% } %>
 </tbody>
 </table>
 </div>

_______________________________________________
Mashup-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to