Improving listing REST API

This commit is contained in:
Luca Frosini 2023-05-23 13:59:26 +02:00
parent f0a14878ca
commit 6cf98b706c
4 changed files with 36 additions and 75 deletions

View File

@ -4,6 +4,7 @@ import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
@ -74,10 +75,39 @@ public class BaseRESTAPIs<R extends Record> {
}
public String list(int limit, int offset) {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
R record = getInstance();
return record.list(limit, offset);
Boolean countOnly = false;
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.COUNT_QUERY_PARAMETER)) {
countOnly = Boolean.parseBoolean(queryParameters.get(GCatConstants.COUNT_QUERY_PARAMETER).get(0));
}
Record record = getInstance();
if(queryParameters.containsKey(GCatConstants.Q_KEY)) {
String q = queryParameters.get(GCatConstants.Q_KEY).get(0);
q = String.format("%s AND extras_systemtype:%s", q, record.getType());
queryParameters.add(GCatConstants.Q_KEY, q);
}
String ret = null;
if(countOnly) {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
int size = record.count();
ret = createCountJson(size);
}else {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
return record.list(limit, offset);
}
String accept = httpHeaders.getHeaderString("Accept");
if(accept!=null && accept.contains(GCatConstants.APPLICATION_JSON_API)) {
return resultAsJsonAPI(ret);
}
return ret;
}
public Response create(String json) {
setCalledMethod("POST /" + COLLECTION_PARAMETER);

View File

@ -10,7 +10,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
@ -47,29 +46,7 @@ public class FisheryRESTAPIs extends BaseRESTAPIs<Fishery> {
})
public String list(@QueryParam(GCatConstants.LIMIT_QUERY_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(GCatConstants.OFFSET_QUERY_PARAMETER) @DefaultValue("0") int offset) {
Boolean countOnly = false;
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.COUNT_QUERY_PARAMETER)) {
countOnly = Boolean.parseBoolean(queryParameters.get(GCatConstants.COUNT_QUERY_PARAMETER).get(0));
}
String ret = null;
if(countOnly) {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
Fishery fishery = getInstance();
int size = fishery.count();
ret = createCountJson(size);
}else {
ret = super.list(limit, offset);
}
String accept = httpHeaders.getHeaderString("Accept");
if(accept!=null && accept.contains(GCatConstants.APPLICATION_JSON_API)) {
return resultAsJsonAPI(ret);
}
return ret;
return super.list(limit, offset);
}
/**

View File

@ -10,7 +10,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
@ -47,29 +46,7 @@ public class StockRESTAPIs extends BaseRESTAPIs<Stock> {
})
public String list(@QueryParam(GCatConstants.LIMIT_QUERY_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(GCatConstants.OFFSET_QUERY_PARAMETER) @DefaultValue("0") int offset) {
Boolean countOnly = false;
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.COUNT_QUERY_PARAMETER)) {
countOnly = Boolean.parseBoolean(queryParameters.get(GCatConstants.COUNT_QUERY_PARAMETER).get(0));
}
String ret = null;
if(countOnly) {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
Stock stock = getInstance();
int size = stock.count();
ret = createCountJson(size);
}else {
ret = super.list(limit, offset);
}
String accept = httpHeaders.getHeaderString("Accept");
if(accept!=null && accept.contains(GCatConstants.APPLICATION_JSON_API)) {
return resultAsJsonAPI(ret);
}
return ret;
return super.list(limit, offset);
}
/**

View File

@ -10,7 +10,6 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.gcube.gcat.api.GCatConstants;
@ -44,29 +43,7 @@ public class TraceabilityUnitRESTAPIs extends BaseRESTAPIs<TraceabilityUnit>{
})
public String list(@QueryParam(GCatConstants.LIMIT_QUERY_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(GCatConstants.OFFSET_QUERY_PARAMETER) @DefaultValue("0") int offset) {
Boolean countOnly = false;
MultivaluedMap<String,String> queryParameters = uriInfo.getQueryParameters();
if(queryParameters.containsKey(GCatConstants.COUNT_QUERY_PARAMETER)) {
countOnly = Boolean.parseBoolean(queryParameters.get(GCatConstants.COUNT_QUERY_PARAMETER).get(0));
}
String ret = null;
if(countOnly) {
setCalledMethod("GET /" + COLLECTION_PARAMETER);
TraceabilityUnit traceabilityUnit = getInstance();
int size = traceabilityUnit.count();
ret = createCountJson(size);
}else {
ret = super.list(limit, offset);
}
String accept = httpHeaders.getHeaderString("Accept");
if(accept!=null && accept.contains(GCatConstants.APPLICATION_JSON_API)) {
return resultAsJsonAPI(ret);
}
return ret;
return super.list(limit, offset);
}
/**