Skip to content

Commit

Permalink
#3614 pagination working for api call. example url with 14k+ users ht…
Browse files Browse the repository at this point in the history
…tp://localhost:8080/api/admin/list-users?&numDisplay=25&q=&selectedPage=50
  • Loading branch information
raprasad committed Jun 1, 2017
1 parent 85d7979 commit 4b98b38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ public Response listAuthenticatedUsers() {
@Path("list-users")
@Produces({"application/json"})
public Response filterAuthenticatedUsers(@QueryParam("q") String searchTerm,
@QueryParam("numDisplay") Integer itemsPerPage,
@QueryParam("selectedPage") Integer selectedPage,
@QueryParam("numDisplay") Integer itemsPerPage,
@QueryParam("sortKey") String sortKey
) {
System.out.println("_YE_OLDE_QUERY_COUNTER_");
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/edu/harvard/iq/dataverse/userdata/UserListMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package edu.harvard.iq.dataverse.userdata;

import edu.harvard.iq.dataverse.UserServiceBean;
import edu.harvard.iq.dataverse.mydata.Pager;
import edu.harvard.iq.dataverse.search.SearchConstants;
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder;
import java.util.List;
import javax.json.Json;
Expand All @@ -23,6 +25,9 @@ public class UserListMaker {
public boolean errorFound = false;
public String errorMessage = null;

public static final int ITEMS_PER_PAGE = 25;


/*
* Constructor
*/
Expand All @@ -43,7 +48,7 @@ public JsonObjectBuilder runSearch(String searchTerm, Integer itemsPerPage, Inte

// Initialize itemsPerPage
if ((itemsPerPage == null) || (itemsPerPage < 10)){
itemsPerPage = 25;
itemsPerPage = ITEMS_PER_PAGE;
}

// Initialize selectedPage
Expand All @@ -69,18 +74,28 @@ public JsonObjectBuilder runSearch(String searchTerm, Integer itemsPerPage, Inte
// (2) Do some calculations here regarding the selected page, offset, etc.
// -------------------------------------------------

// e.g Are there enough results to justify the selected page, etc.
int offset = (selectedPage - 1) * itemsPerPage;
if (offset > userCount){
offset = 0;
selectedPage = 1;
}


// -------------------------------------------------
// (3) Retrieve the users
// -------------------------------------------------
JsonArrayBuilder jsonUserListArray = userService.getUserListAsJSON(searchTerm, sortKey, itemsPerPage, 0);


JsonArrayBuilder jsonUserListArray = userService.getUserListAsJSON(searchTerm, sortKey, itemsPerPage, offset);
if (jsonUserListArray==null){
return getNoResultsJSON();
}

Pager pager = new Pager(userCount.intValue(), itemsPerPage, selectedPage);

JsonObjectBuilder jsonOverallData = Json.createObjectBuilder();
jsonOverallData.add("userCount", userCount)
.add("pagination", pager.asJsonObjectBuilder())
.add("users", jsonUserListArray)
.add("selectedPage", 1);

Expand Down

0 comments on commit 4b98b38

Please sign in to comment.