Added count methods for Organization, Groups and Profiles refs #20629

This commit is contained in:
Luca Frosini 2021-02-03 21:55:32 +01:00
parent 4e479f0db0
commit 2d32d782e9
11 changed files with 149 additions and 16 deletions

View File

@ -98,4 +98,11 @@ public class CKANGroup extends CKAN {
}
return groups;
}
public int count() {
list(100000, 0);
ArrayNode arrayNode = (ArrayNode) result;
return arrayNode.size();
}
}

View File

@ -1,12 +1,12 @@
package org.gcube.gcat.persistence.ckan;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.gcat.utils.ContextUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@ -76,4 +76,11 @@ public class CKANOrganization extends CKAN {
ScopeBean scopeBean = new ScopeBean(context);
return scopeBean.name().toLowerCase();
}
public int count() {
list(100000, 0);
ArrayNode arrayNode = (ArrayNode) result;
return arrayNode.size();
}
}

View File

@ -48,6 +48,14 @@ public class ISProfile {
return mapper;
}
public int count() {
try {
return (new MetadataUtility()).getProfilesNames().size();
}catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
public ArrayNode list() {
ArrayNode arrayNode = mapper.createArrayNode();

View File

@ -5,6 +5,7 @@ import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
import org.gcube.gcat.api.GCatConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,4 +28,14 @@ public class BaseREST {
String.format("%s/%s", uriInfo.getAbsolutePath().toString(), id));
}
protected String createCountJson(int count) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{\"");
stringBuilder.append(GCatConstants.COUNT_KEY);
stringBuilder.append("\":");
stringBuilder.append(count);
stringBuilder.append("}");
return stringBuilder.toString();
}
}

View File

@ -11,6 +11,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH;
@ -30,8 +31,31 @@ public class Group extends REST<CKANGroup> implements org.gcube.gcat.api.interfa
super(GROUPS, GROUP_ID_PARAMETER, CKANGroup.class);
}
/*
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
*/
@Override
public int count() throws WebServiceException {
CKANGroup ckan = getInstance();
return ckan.count();
}
@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) {
if(countOnly) {
int count = count();
return createCountJson(count);
}else {
return list(limit, offset);
}
}
/*
* 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) {

View File

@ -56,20 +56,12 @@ public class Item extends REST<CKANPackage> implements org.gcube.gcat.api.interf
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(GCatConstants.COUNT_KEY);
stringBuilder.append("\":");
stringBuilder.append(count);
stringBuilder.append("}");
ret = stringBuilder.toString();
return createCountJson(count);
}else {
ret = list(limit, offset);
return list(limit, offset);
}
return ret;
}
/*

View File

@ -11,6 +11,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.annotation.PATCH;
@ -31,8 +32,31 @@ public class Organization extends REST<CKANOrganization>
super(ORGANIZATIONS, ORGANIZATION_ID_PARAMETER, CKANOrganization.class);
}
/*
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
*/
@Override
public int count() throws WebServiceException {
CKANOrganization ckan = getInstance();
return ckan.count();
}
@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) {
if(countOnly) {
int count = count();
return createCountJson(count);
}else {
return list(limit, offset);
}
}
/*
* 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) {

View File

@ -13,6 +13,7 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@ -23,6 +24,7 @@ import javax.ws.rs.core.UriInfo;
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
import org.gcube.gcat.ResourceInitializer;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.profile.ISProfile;
import org.xml.sax.SAXException;
@ -53,10 +55,46 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P
@Context
private UriInfo uriInfo;
/*
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
*/
@Override
public int count() {
setCalledMethod("GET /" + PROFILES);
try {
ISProfile isProfile = new ISProfile();
return isProfile.count();
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String list() {
public String listOrCount(@QueryParam(GCatConstants.COUNT_ONLY_PARAMETER) @DefaultValue("false") Boolean countOnly) {
setCalledMethod("GET /" + PROFILES);
try {
ISProfile isProfile = new ISProfile();
if(countOnly) {
return createCountJson(isProfile.count());
}else{
ArrayNode arrayNode = isProfile.list();
return isProfile.getMapper().writeValueAsString(arrayNode);
}
} catch(WebApplicationException e) {
throw e;
} catch(Exception e) {
throw new InternalServerErrorException(e);
}
}
/*
* Not used as REST method, implemented to respect {@link org.gcube.gcat.api.interfaces.Item} interface
*/
@Override
public String list() {
try {
ISProfile isProfile = new ISProfile();
ArrayNode arrayNode = isProfile.list();
@ -161,5 +199,5 @@ public class Profile extends BaseREST implements org.gcube.gcat.api.interfaces.P
public String update(String name, String xml) {
return createOrUpdate(name, xml).getEntity().toString();
}
}

View File

@ -14,10 +14,17 @@ public class CKANGroupTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(CKANGroupTest.class);
@Test
public void count() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
int count = ckanGroup.count();
logger.debug("The groups are {}", count);
}
@Test
public void list() throws Exception {
CKANGroup ckanGroup = new CKANGroup();
String ret = ckanGroup.list(10, 0);
String ret = ckanGroup.list(10000, 0);
logger.debug("{}", ret);
}

View File

@ -21,7 +21,15 @@ public class CKANOrganizationTest extends ContextTest {
*/
@Test
public void listOrganization() throws Exception {
public void countOrganizations() throws Exception {
ContextTest.setContextByName("/gcube");
CKANOrganization ckanOrganization = new CKANOrganization();
int count = ckanOrganization.count();
logger.debug("The organizations are {}", count);
}
@Test
public void listOrganizations() throws Exception {
ContextTest.setContextByName("/gcube");
CKANOrganization ckanOrganization = new CKANOrganization();
String ret = ckanOrganization.list(1000, 0);

View File

@ -18,6 +18,13 @@ public class ProfileTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ProfileTest.class);
@Test
public void count() throws Exception {
ISProfile profile = new ISProfile();
int count = profile.count();
logger.debug("We have {} types", count);
}
@Test
public void list() throws Exception {
ISProfile profile = new ISProfile();