Added support to get only the number of total items refs #20627

This commit is contained in:
Luca Frosini 2021-02-03 16:28:17 +01:00
parent 989df62fe2
commit 09ae3573f8
4 changed files with 82 additions and 5 deletions

View File

@ -100,6 +100,8 @@ public class CKANPackage extends CKAN {
// The 'results' array is included in the 'result' object for package_search // The 'results' array is included in the 'result' object for package_search
private static final String RESULTS_KEY = "results"; 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 PRIVATE_KEY = "private";
protected static final String SEARCHABLE_KEY = "searchable"; protected static final String SEARCHABLE_KEY = "searchable";
protected static final String CAPACITY_KEY = "capacity"; protected static final String CAPACITY_KEY = "capacity";
@ -326,6 +328,29 @@ public class CKANPackage extends CKAN {
} }
} }
public int count() {
Map<String,String> parameters = new HashMap<>();
if(uriInfo != null) {
MultivaluedMap<String,String> 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 @Override
public String list(int limit, int offset) { public String list(int limit, int offset) {
Map<String,String> parameters = new HashMap<>(); Map<String,String> parameters = new HashMap<>();
@ -347,6 +372,11 @@ public class CKANPackage extends CKAN {
parameters = checkListParameters(queryParameters, parameters); parameters = checkListParameters(queryParameters, parameters);
} }
if(!parameters.containsKey(GCatConstants.Q_KEY)) {
String filter = getFilterForOrganizations();
parameters.put(GCatConstants.Q_KEY, filter);
}
return list(parameters); return list(parameters);
} }

View File

@ -33,6 +33,15 @@ public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interf
super(ITEMS, ITEM_ID_PARAMETER, CKANPackage.class); 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 * 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<CKANPackage> implements org.gcube.gcat.api.interf
@GET @GET
@Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) @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 @Override
public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit, public String list(@QueryParam(GCatConstants.LIMIT_PARAMETER) @DefaultValue("10") int limit,
@QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) { @QueryParam(GCatConstants.OFFSET_PARAMETER) @DefaultValue("0") int offset) {

View File

@ -48,11 +48,20 @@ public class ContextTest {
DEFAULT_TEST_SCOPE_NAME = "/gcube/devsec/devVRE"; 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); AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext(); return authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
} }
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception { public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
@ -68,7 +77,7 @@ public class ContextTest {
String qualifier = authorizationEntry.getQualifier(); String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier); Caller caller = new Caller(clientInfo, qualifier);
AuthorizationProvider.instance.set(caller); AuthorizationProvider.instance.set(caller);
ScopeProvider.instance.set(getCurrentScope(token)); ScopeProvider.instance.set(getContextFullName(token));
} }
@BeforeClass @BeforeClass

View File

@ -45,6 +45,13 @@ public class CKANPackageTest extends ContextTest {
private static final String LICENSE_VALUE = "CC-BY-SA-4.0"; private static final String LICENSE_VALUE = "CC-BY-SA-4.0";
private static final String EXTRAS_TYPE_VALUE_VALUE = "TestEmptyProfile"; 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 @Test
public void list() throws Exception { public void list() throws Exception {
CKANPackage ckanPackage = new CKANPackage(); CKANPackage ckanPackage = new CKANPackage();