From 09ae3573f85b7fa4cbc5f97ee960f0300d0a77de Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 3 Feb 2021 16:28:17 +0100 Subject: [PATCH] Added support to get only the number of total items refs #20627 --- .../gcat/persistence/ckan/CKANPackage.java | 30 ++++++++++++++++++ src/main/java/org/gcube/gcat/rest/Item.java | 31 +++++++++++++++++++ src/test/java/org/gcube/gcat/ContextTest.java | 19 +++++++++--- .../persistence/ckan/CKANPackageTest.java | 7 +++++ 4 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java index fbfba87..8f91e24 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANPackage.java @@ -100,6 +100,8 @@ public class CKANPackage extends CKAN { // The 'results' array is included in the 'result' object for package_search private static final String RESULTS_KEY = "results"; + private static final String COUNT_KEY = "count"; + protected static final String PRIVATE_KEY = "private"; protected static final String SEARCHABLE_KEY = "searchable"; protected static final String CAPACITY_KEY = "capacity"; @@ -326,6 +328,29 @@ public class CKANPackage extends CKAN { } } + public int count() { + Map parameters = new HashMap<>(); + if(uriInfo != null) { + MultivaluedMap queryParameters = uriInfo.getQueryParameters(); + parameters = checkListParameters(queryParameters, parameters); + } + + int limit = 1; + parameters.put(ROWS_KEY, String.valueOf(limit)); + int offset = 0; + parameters.put(START_KEY, String.valueOf(offset * limit)); + + if(!parameters.containsKey(GCatConstants.Q_KEY)) { + String filter = getFilterForOrganizations(); + parameters.put(GCatConstants.Q_KEY, filter); + } + + sendGetRequest(LIST, parameters); + + int count = result.get(COUNT_KEY).asInt(); + return count; + } + @Override public String list(int limit, int offset) { Map parameters = new HashMap<>(); @@ -347,6 +372,11 @@ public class CKANPackage extends CKAN { parameters = checkListParameters(queryParameters, parameters); } + if(!parameters.containsKey(GCatConstants.Q_KEY)) { + String filter = getFilterForOrganizations(); + parameters.put(GCatConstants.Q_KEY, filter); + } + return list(parameters); } diff --git a/src/main/java/org/gcube/gcat/rest/Item.java b/src/main/java/org/gcube/gcat/rest/Item.java index eea6314..f6182e5 100644 --- a/src/main/java/org/gcube/gcat/rest/Item.java +++ b/src/main/java/org/gcube/gcat/rest/Item.java @@ -33,6 +33,15 @@ public class Item extends REST implements org.gcube.gcat.api.interf super(ITEMS, ITEM_ID_PARAMETER, CKANPackage.class); } + /* + * Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface + */ + @Override + public int count() throws WebServiceException { + CKANPackage ckan = getInstance(); + return ckan.count(); + } + /* * Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface */ @@ -44,6 +53,28 @@ public class Item extends REST implements org.gcube.gcat.api.interf @GET @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) + public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit, + @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset, + @QueryParam(GCatConstants.COUNT_ONLY_PARAMETER) @DefaultValue("false") Boolean countOnly) { + String ret = null; + if(countOnly) { + int count = count(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("{\""); + stringBuilder.append(Item.COUNT_KEY); + stringBuilder.append("\":"); + stringBuilder.append(count); + stringBuilder.append("}"); + ret = stringBuilder.toString(); + }else { + ret = list(limit, offset); + } + return ret; + } + + /* + * Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface + */ @Override public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit, @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) { diff --git a/src/test/java/org/gcube/gcat/ContextTest.java b/src/test/java/org/gcube/gcat/ContextTest.java index 9d66a0b..b146499 100644 --- a/src/test/java/org/gcube/gcat/ContextTest.java +++ b/src/test/java/org/gcube/gcat/ContextTest.java @@ -48,11 +48,20 @@ public class ContextTest { DEFAULT_TEST_SCOPE_NAME = "/gcube/devsec/devVRE"; } - public static String getCurrentScope(String token) throws ObjectNotFound, Exception { + public static String getCurrentContextFullName() { + String token = SecurityTokenProvider.instance.get(); + AuthorizationEntry authorizationEntry = null; + try { + authorizationEntry = Constants.authorizationService().get(token); + } catch(Exception e) { + return ScopeProvider.instance.get(); + } + return authorizationEntry.getContext(); + } + + public static String getContextFullName(String token) throws ObjectNotFound, Exception { AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); - String context = authorizationEntry.getContext(); - logger.info("Context of token {} is {}", token, context); - return context; + return authorizationEntry.getContext(); } public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { @@ -68,7 +77,7 @@ public class ContextTest { String qualifier = authorizationEntry.getQualifier(); Caller caller = new Caller(clientInfo, qualifier); AuthorizationProvider.instance.set(caller); - ScopeProvider.instance.set(getCurrentScope(token)); + ScopeProvider.instance.set(getContextFullName(token)); } @BeforeClass diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java index 9d8c570..9578ba8 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANPackageTest.java @@ -45,6 +45,13 @@ public class CKANPackageTest extends ContextTest { private static final String LICENSE_VALUE = "CC-BY-SA-4.0"; private static final String EXTRAS_TYPE_VALUE_VALUE = "TestEmptyProfile"; + @Test + public void count() throws Exception { + CKANPackage ckanPackage = new CKANPackage(); + int count = ckanPackage.count(); + logger.debug("Number of items in {} is {}", ContextTest.getCurrentContextFullName(), count); + } + @Test public void list() throws Exception { CKANPackage ckanPackage = new CKANPackage();