paginated bean result

This commit is contained in:
Alfredo Oliviero 2024-03-26 17:39:59 +01:00
parent 745e3b7425
commit c4786572ab
3 changed files with 95 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package org.gcube.service.idm.rest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -23,6 +22,7 @@ import org.gcube.service.idm.IdMManager;
import org.gcube.service.idm.controller.KKUserClient;
import org.gcube.service.idm.serializers.ContextSerializator;
import org.gcube.service.rest.ResponseBean;
import org.gcube.service.rest.ResponseBeanPaginated;
import org.gcube.smartgears.annotations.ManagedBy;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
@ -49,9 +49,10 @@ import jakarta.ws.rs.core.MediaType;
// })
public class UsersSocialAPI {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class);
private static final String NOT_USER_TOKEN_CONTEXT_USED = "User's information can only be retrieved through a user token (not qualified)";
private static final List<String> GLOBAL_ROLES_ALLOWED_BY_LOCAL_CALL_METHOD = Arrays.asList("DataMiner-Manager",
"Quota-Manager");
// private static final List<String> GLOBAL_ROLES_ALLOWED_BY_LOCAL_CALL_METHOD =
// Arrays.asList("DataMiner-Manager",
// "Quota-Manager");
@GET
@Path("/get-usernames-by-role")
@ -59,9 +60,14 @@ public class UsersSocialAPI {
public Response getUsernamesByRole(
@QueryParam("role-name") String roleName,
@QueryParam("firstResult") @DefaultValue("0") @Min(value = 0, message = "from cannot be negative") int firstResult,
@QueryParam("maxResults") @DefaultValue("100000") @Min(value = 0, message = "quantity cannot be negative") int maxResults) {
@QueryParam("maxResults") @DefaultValue("10000") @Min(value = 0, message = "quantity cannot be negative") int maxResults) {
Status status = Status.OK;
ResponseBean responseBean = new ResponseBean();
ResponseBean responseBean;
if (firstResult > 0 || maxResults > 0) {
responseBean = new ResponseBeanPaginated(firstResult, maxResults);
} else {
responseBean = new ResponseBean();
}
List<String> usernames = new ArrayList<String>();
try {
List<UserRepresentation> users = KKUserClient.searchUsersByRole(roleName, firstResult, maxResults);
@ -83,10 +89,10 @@ public class UsersSocialAPI {
return Response.serverError().build();
} catch (Exception e) {
logger.error("Unable to retrieve users with the requested role", e);
// responseBean.setMessage(e.getMessage());
responseBean.setMessage(e.getMessage());
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(responseBean).build();
}
return Response.status(status).entity(responseBean).build();
}
@GET
@ -156,7 +162,7 @@ public class UsersSocialAPI {
// return Response.status(status).entity(responseBean).build();
// }
UserManager lr_userManager = null; //UserManagerWSBuilder.getInstance().getUserManager();
UserManager lr_userManager = null; // UserManagerWSBuilder.getInstance().getUserManager();
try {
GCubeUser user = lr_userManager.getUserByUsername(userId);
@ -223,6 +229,7 @@ public class UsersSocialAPI {
responseBean.setResult(userprofiile);
responseBean.setSuccess(true);
} catch (Exception e) {
logger.error("Unable to retrieve user's profile", e);
responseBean.setMessage(e.getMessage());
status = Status.INTERNAL_SERVER_ERROR;

View File

@ -12,16 +12,16 @@ public class ResponseBean implements Serializable {
/**
* The result of the request: true if it succeeded, false otherwise
*/
private boolean success;
protected boolean success;
/**
* An error message if something wrong happened, null/empty otherwise
*/
private String message;
protected String message;
/**
* The result object of the request
*/
private Object result;
protected Object result;
public ResponseBean() {
super();

View File

@ -0,0 +1,76 @@
package org.gcube.service.rest;
import java.io.Serializable;
/**
* Response bean
*
*/
public class ResponseBeanPaginated extends ResponseBean {
private static final long serialVersionUID = -2725238162673879658L;
protected Integer firstResult;
protected Integer maxResults;
public Integer getFirstResult() {
return firstResult;
}
public void setFirstResult(Integer firstResult) {
this.firstResult = firstResult;
}
public Integer getMaxResults() {
return maxResults;
}
public void setMaxResults(Integer maxResults) {
this.maxResults = maxResults;
}
public ResponseBeanPaginated() {
super();
}
// /**
// * @param success
// * @param message
// * @param result
// */
// public ResponseBeanPaginated(boolean success, String message, Object result)
// {
// super(success, message, result);
// }
/**
* @param firstResult
* @param maxResults
*/
public ResponseBeanPaginated(Integer firstResult, Integer maxResults) {
this.firstResult = firstResult;
this.maxResults = maxResults;
}
/**
* @param success
* @param message
* @param result
* @param firstResult
* @param maxResults
*/
public ResponseBeanPaginated(boolean success, String message, Object result, Integer firstResult,
Integer maxResults) {
super(success, message, result);
this.firstResult = firstResult;
this.maxResults = maxResults;
}
@Override
public String toString() {
return "ResponseBean [success=" + success
+ ", message=" + message + ", result=" + result + ", firstResult=" + firstResult + ", maxResults="
+ maxResults + "]";
}
}