[Yanel-dev] User management resource: pagination

Cedric Staub cedric.staub at wyona.com
Tue Aug 31 11:17:20 CEST 2010


Hello there

I wrote a patch that extends the user-mgmt resource included in Yanel to
add pagination. This is useful if you have a project which includes lots
of users, and you don't want them to be displayed all at the same time.

Please tell me what you think. I also plan to build a search feature
into the user management page later on.

Cheers,
Cedric
-------------- next part --------------
Index: src/resources/user-mgmt/htdocs/list-users.jelly
===================================================================
--- src/resources/user-mgmt/htdocs/list-users.jelly	(revision 52644)
+++ src/resources/user-mgmt/htdocs/list-users.jelly	(working copy)
@@ -3,6 +3,11 @@
   <html xmlns="http://www.w3.org/1999/xhtml">
     <body>
       <h1>Users</h1>
+      <p>
+        Showing users ${resource.getLowerBound()} through ${resource.getUpperBound()}
+        out of a total of ${resource.getTotalUsers()}.
+      </p>
+      <!-- Table of users -->
       <table border="1">
         <tr>
           <th>ID</th>
@@ -16,13 +21,23 @@
             <td>${user.getName()}</td>
             <td>${user.getEmail()}</td>
             <td><a href="update-user-admin.html?userID=${user.getID()}">Update/Edit</a></td>
-<!-- Edit user whereas one needs to enter old password and also cannot change group settings
-            <td><a href="update-user.html?userID=${user.getID()}">Edit</a></td>
--->
             <td><a href="delete-user.html?userID=${user.getID()}">Delete</a></td>
           </tr>
         </j:forEach>
       </table>
+      <!-- Pagination -->
+      <p>
+        <j:if test="${resource.getCurrentPage() &gt; 1}">
+          <a href="?page=${resource.getCurrentPage()-1}">? Prev</a>
+          &#160;
+        </j:if>
+        Page ${resource.getCurrentPage()}/${resource.getTotalPages()}
+        <j:if test="${resource.getHasNext()}">
+          &#160;
+          <a href="?page=${resource.getCurrentPage()+1}">Next ?</a>
+        </j:if>
+      </p>
+      <!-- Links -->
       <a href="create-user.html">Create new user</a>
     </body>
   </html>
Index: src/resources/user-mgmt/src/java/org/wyona/yanel/impl/resources/ListUsersResource.java
===================================================================
--- src/resources/user-mgmt/src/java/org/wyona/yanel/impl/resources/ListUsersResource.java	(revision 52644)
+++ src/resources/user-mgmt/src/java/org/wyona/yanel/impl/resources/ListUsersResource.java	(working copy)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006 Wyona
+ * Copyright 2010 Wyona
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -22,19 +22,159 @@
 import org.wyona.yanel.impl.resources.usecase.UsecaseException;
 import org.wyona.yanel.impl.resources.usecase.UsecaseResource;
 
+import java.lang.System;
+import java.lang.Integer;
+import java.lang.Boolean;
 
 /**
- *
+ * Resource to list all users.
  */
 public class ListUsersResource extends UsecaseResource {
+    // Contants
+    private static final int DEFAULT_ITEMS_PER_PAGE = 100;
 
-    public User[] getUsers() throws UsecaseException {
+    // Variables
+    private int totalPages = 1;
+    private int currentPage = 1;
+    private int itemsPerPage = 1;
+    private int lowerBound = 1;
+    private int upperBound = 1;
+    private int totalUsers = 0;
+    private boolean hasNext = false;
+    private User[] users = null;
+    private boolean initialized = false;
+
+    /**
+     * Initialize all variables.
+     * This function intializes various private variables. You don't need
+     * to call this function, it will be called automatically the first time
+     * you access a variable and the object finds that it is not initialized.
+     */
+    private void initVars() throws UsecaseException {
         UserManager userManager = getRealm().getIdentityManager().getUserManager();
+
+        // Pagination
+        // Current page
         try {
-            return userManager.getUsers(true);
+            String p = getParameterAsString("page");
+            currentPage = Integer.parseInt(p);
+            if(currentPage < 1) currentPage = 1;
+        } catch(Exception e) {
+            currentPage = 1;
+        }
+
+        // Items per page
+        int items;
+        try {
+            String i = getResourceConfigProperty("items-per-page");
+            itemsPerPage = Integer.parseInt(i);
+            if(itemsPerPage < 1) itemsPerPage = 1;
+        } catch(Exception e) {
+            itemsPerPage = DEFAULT_ITEMS_PER_PAGE;
+        }
+
+        // Result
+        try {
+            User[] all = userManager.getUsers(true);
+
+            int srcPos = (currentPage-1)*itemsPerPage;
+            int length = itemsPerPage;
+            totalPages = all.length/itemsPerPage+1;
+
+            // Source position can't outside
+            // of the array of all users
+            if(srcPos > all.length) {
+                currentPage = 1;
+                srcPos = 0;
+            }
+
+            // Make sure to not copy more 
+            // items than are left in the 
+            // array (after source position).
+            hasNext = false;
+            if((srcPos + length) > all.length) {
+                length = all.length - srcPos;
+            } else if((srcPos + length) < all.length) {
+                hasNext = true;
+            }
+
+            lowerBound = srcPos+1;
+            upperBound = srcPos+length;
+            totalUsers = all.length;
+
+            users = new User[length];
+            System.arraycopy(all, srcPos, users, 0, length);
+
+            initialized = true;
         } catch (AccessManagementException e) {
+            initialized = false;
             throw new UsecaseException(e.toString(), e);
         }
     }
 
+    /**
+     * Get users on current page.
+     */
+    public User[] getUsers() throws UsecaseException {
+        if(!initialized) initVars();
+        return users;
+    }
+
+    /**
+     * Get the current page being displayed.
+     */
+    public String getCurrentPage() throws UsecaseException {
+        if(!initialized) initVars();
+        return Integer.toString(currentPage);
+    }
+
+    /**
+     * Get the total of available pages.
+     */
+    public String getTotalPages() throws UsecaseException {
+        if(!initialized) initVars();
+        return Integer.toString(totalPages);
+    }
+
+    /**
+     * Get the lower bound being displayed.
+     * For example, if we're on page 2 and there are
+     * 10 users being displayed per page, this value
+     * will be 11 - because the first user on the page
+     * is user number 11 in the array.
+     */
+    public String getLowerBound() throws UsecaseException {
+        if(!initialized) initVars();
+        return Integer.toString(lowerBound);
+    }
+
+    /**
+     * Get the upper bound being displayed.
+     * For example, if we're on page 2 and there are
+     * 10 users being displayed per page, this value
+     * will be 20 - because the last user on the page
+     * is user number 20 in the array.
+     */
+    public String getUpperBound() throws UsecaseException {
+        if(!initialized) initVars();
+        return Integer.toString(upperBound);
+    }
+
+    /**
+     * Get the total amount of existing users.
+     */
+    public String getTotalUsers() throws UsecaseException {
+        if(!initialized) initVars();
+        return Integer.toString(totalUsers);
+    }
+
+    /**
+     * Check if there is another page.
+     * Return true if there is another page beyond the 
+     * current one, false if there is not.
+     */
+    public String getHasNext() throws UsecaseException {
+        if(!initialized) initVars();
+        return Boolean.toString(hasNext);
+    }
 }


More information about the Yanel-development mailing list