Improving offset limit management to properly support paginated result

This commit is contained in:
luca.frosini 2023-10-31 15:24:29 +01:00
parent 1eb1cf6773
commit c611a839eb
4 changed files with 24 additions and 10 deletions

View File

@ -481,14 +481,18 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
List<Context> contexts = contextCache.getContexts(); List<Context> contexts = contextCache.getContexts();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get(); ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
int limit = requestInfo.getLimit(); Integer limit = requestInfo.getLimit();
if(forceLimit!=null) { if(forceLimit!=null) {
limit = forceLimit; limit = forceLimit;
}else if(limit == null) {
limit = -1;
} }
int offset = requestInfo.getOffset(); Integer offset = requestInfo.getOffset();
if(forceOffset!=null) { if(forceOffset!=null) {
offset = forceOffset; offset = forceOffset;
}else if(offset == null) {
offset = 0;
} }
int position = -1; int position = -1;

View File

@ -24,6 +24,11 @@ public class ServerRequestInfo extends BaseRequestInfo implements RequestInfo {
this.uriInfo = null; this.uriInfo = null;
} }
public ServerRequestInfo(int offset, int limit) {
super(offset, limit);
this.uriInfo = null;
}
public UriInfo getUriInfo() { public UriInfo getUriInfo() {
return uriInfo; return uriInfo;
} }

View File

@ -35,15 +35,20 @@ public class BaseRest {
setAccountingMethod(accountingMethod.toString()); setAccountingMethod(accountingMethod.toString());
} }
private ServerRequestInfo initRequestInfo(ServerRequestInfo requestInfo) {
protected ServerRequestInfo initRequestInfo() {
ServerRequestInfo requestInfo = new ServerRequestInfo();
requestInfo.setUriInfo(uriInfo); requestInfo.setUriInfo(uriInfo);
RequestUtility.getRequestInfo().set(requestInfo); RequestUtility.getRequestInfo().set(requestInfo);
return requestInfo; return requestInfo;
} }
protected ServerRequestInfo initRequestInfo(int offset, int limit) {
ServerRequestInfo requestInfo = new ServerRequestInfo(offset, limit);
return initRequestInfo(requestInfo);
}
protected ServerRequestInfo initRequestInfo() {
ServerRequestInfo requestInfo = new ServerRequestInfo();
return initRequestInfo(requestInfo);
}
} }

View File

@ -18,6 +18,7 @@ import org.gcube.informationsystem.contexts.reference.entities.Context;
import org.gcube.informationsystem.resourceregistry.ResourceInitializer; import org.gcube.informationsystem.resourceregistry.ResourceInitializer;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.request.BaseRequestInfo;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement;
@ -46,10 +47,9 @@ public class ContextManager extends BaseRest {
logger.info("Requested to read all {}s", Context.NAME); logger.info("Requested to read all {}s", Context.NAME);
setAccountingMethod(Method.LIST, Context.NAME); setAccountingMethod(Method.LIST, Context.NAME);
ServerRequestInfo serverRequestInfo = initRequestInfo(); ServerRequestInfo serverRequestInfo = initRequestInfo(BaseRequestInfo.DEFAULT_OFFSET, BaseRequestInfo.UNBOUNDED_LIMIT);
serverRequestInfo.setAllMeta(true); serverRequestInfo.setAllMeta(true);
serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER); serverRequestInfo.checkBooleanQueryParameter(ContextPath.INCLUDE_META_QUERY_PARAMETER);
serverRequestInfo.checkLimitOffset(0, -1);
ContextManagement contextManagement = new ContextManagement(); ContextManagement contextManagement = new ContextManagement();
return contextManagement.all(false); return contextManagement.all(false);