diff --git a/CHANGELOG.md b/CHANGELOG.md index 57c8acf..6f01f59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,18 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for gCube Catalogue (gCat) Service -## [v2.0.0-SNAPSHOT] [r5.0.0] - +## [v2.0.0-SNAPSHOT] -- Switched JSON management to gcube-jackson [#19735] - Added support to publish an item organizations not matching the current context [#19365] +## [v1.4.4] + +- Added count method for Item collection [#20627] +- Added count method for Organization, Group and Profile collection [#20629] +- Switched JSON management to gcube-jackson [#19735] + + ## [v1.4.3] [r4.23.0] - 2020-06-19 - Social Post is disabled if not explicitly enabled by the client [#19295] diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java index ae2f50a..9144968 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANGroup.java @@ -98,4 +98,11 @@ public class CKANGroup extends CKAN { } return groups; } + + public int count() { + list(100000, 0); + ArrayNode arrayNode = (ArrayNode) result; + return arrayNode.size(); + } + } diff --git a/src/main/java/org/gcube/gcat/persistence/ckan/CKANOrganization.java b/src/main/java/org/gcube/gcat/persistence/ckan/CKANOrganization.java index 5b7fa58..dd1907f 100644 --- a/src/main/java/org/gcube/gcat/persistence/ckan/CKANOrganization.java +++ b/src/main/java/org/gcube/gcat/persistence/ckan/CKANOrganization.java @@ -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(); + } + } diff --git a/src/main/java/org/gcube/gcat/profile/ISProfile.java b/src/main/java/org/gcube/gcat/profile/ISProfile.java index c3415a6..591544b 100644 --- a/src/main/java/org/gcube/gcat/profile/ISProfile.java +++ b/src/main/java/org/gcube/gcat/profile/ISProfile.java @@ -1,11 +1,7 @@ package org.gcube.gcat.profile; import java.io.StringWriter; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Set; import javax.ws.rs.InternalServerErrorException; @@ -13,11 +9,8 @@ import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; -import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; -import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; import org.gcube.common.resources.gcore.GenericResource; import org.gcube.common.resources.gcore.Resources; -import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery; import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil; import org.gcube.gcat.persistence.ckan.CKANUser; @@ -26,22 +19,6 @@ import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.gcube.gcat.utils.Constants; import org.gcube.informationsystem.publisher.RegistryPublisher; import org.gcube.informationsystem.publisher.RegistryPublisherFactory; -import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; -import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; -import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; -import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl; -import org.gcube.resourcemanagement.model.impl.entities.facets.SchemaFacetImpl; -import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl; -import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl; -import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl; -import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; -import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet; -import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet.IdentificationType; -import org.gcube.resourcemanagement.model.reference.entities.facets.SchemaFacet; -import org.gcube.resourcemanagement.model.reference.entities.facets.SimpleFacet; -import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration; -import org.gcube.resourcemanagement.model.reference.properties.ValueSchema; -import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.impl.QueryBox; @@ -52,24 +29,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; + public class ISProfile { public static int PRETTY_PRINT_INDENT_FACTOR = 4; - public static final String PROFILE = "profile"; - public static final String PROFILE_SCHEMA = "profileSchema"; - - public static final String SCHEMA_URL_ADDRESS = "https://code-repo.d4science.org/gCubeSystem/gcubedatacatalogue-metadata-discovery/raw/branch/master/src/main/resources/org/gcube/datacatalogue/metadatadiscovery/Gdcmetadataprofilev3.xsd"; - public static final URL SCHEMA_URL; - - static { - try { - SCHEMA_URL = new URL(SCHEMA_URL_ADDRESS); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - } - private static Logger logger = LoggerFactory.getLogger(ISProfile.class); protected ObjectMapper mapper; @@ -82,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(); @@ -96,45 +70,6 @@ public class ISProfile { } } - /* - * https://wiki.gcube-system.org/gcube/Facet_Based_Resource_Model#Configuration - */ - protected Configuration instantiateConfiguration(String name, String xml) throws Exception { - Configuration configuration = new ConfigurationImpl(); - - IdentifierFacet identifierFacet = new IdentifierFacetImpl(); - identifierFacet.setValue(name); - identifierFacet.setType(IdentificationType.STRING); - IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl(configuration, identifierFacet); - configuration.addFacet(isIdentifiedBy); - - SimpleFacet profile = new SimpleFacetImpl(); - profile.setAdditionalProperty(PROFILE, xml); - configuration.addFacet(profile); - - SchemaFacet schemaFacet = new SchemaFacetImpl(); - schemaFacet.setName("gCube Item Profile"); - schemaFacet.setDescription("gCube Metadata Profile defines a Metadata schema XML-based for adding custom metadata fields."); - - ValueSchema valueSchema = new ValueSchemaImpl(); - valueSchema.setSchema(SCHEMA_URL.toURI()); - valueSchema.setValue(DataCalogueMetadataFormatReader.getProfileSchemaString()); - schemaFacet.setSchema(valueSchema); - - configuration.addFacet(schemaFacet); - - return configuration; - } - - - protected Configuration getConfiguration(String name) throws ResourceRegistryException { - ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); - Map map = new HashMap<>(); - map.put(IdentifierFacet.VALUE_PROPERTY, name); - List configurations = resourceRegistryClient.getFilteredResources(Configuration.class, IsIdentifiedBy.class, IdentifierFacet.class, false, map); - return configurations.get(0); - } - /* * TODO Check the Queries because the name in the Profile differs from the name in * diff --git a/src/main/java/org/gcube/gcat/profile/ResourceRegistryProfile.java b/src/main/java/org/gcube/gcat/profile/ResourceRegistryProfile.java new file mode 100644 index 0000000..6ae9826 --- /dev/null +++ b/src/main/java/org/gcube/gcat/profile/ResourceRegistryProfile.java @@ -0,0 +1,284 @@ +package org.gcube.gcat.profile; + +import java.io.StringWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.ws.rs.InternalServerErrorException; +import javax.ws.rs.NotAuthorizedException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.WebApplicationException; + +import org.gcube.com.fasterxml.jackson.databind.ObjectMapper; +import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode; +import org.gcube.common.resources.gcore.GenericResource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader; +import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery; +import org.gcube.datacatalogue.metadatadiscovery.reader.QueryForResourceUtil; +import org.gcube.gcat.persistence.ckan.CKANUser; +import org.gcube.gcat.persistence.ckan.CKANUser.Role; +import org.gcube.gcat.persistence.ckan.CKANUserCache; +import org.gcube.gcat.utils.Constants; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.facets.SchemaFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl; +import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl; +import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl; +import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl; +import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet.IdentificationType; +import org.gcube.resourcemanagement.model.reference.entities.facets.SchemaFacet; +import org.gcube.resourcemanagement.model.reference.entities.facets.SimpleFacet; +import org.gcube.resourcemanagement.model.reference.entities.resources.Configuration; +import org.gcube.resourcemanagement.model.reference.properties.ValueSchema; +import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.json.JSONObject; +import org.json.XML; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +public class ResourceRegistryProfile { + + public static int PRETTY_PRINT_INDENT_FACTOR = 4; + + public static final String PROFILE = "profile"; + public static final String PROFILE_SCHEMA = "profileSchema"; + + public static final String SCHEMA_URL_ADDRESS = "https://code-repo.d4science.org/gCubeSystem/gcubedatacatalogue-metadata-discovery/raw/branch/master/src/main/resources/org/gcube/datacatalogue/metadatadiscovery/Gdcmetadataprofilev3.xsd"; + public static final URL SCHEMA_URL; + + static { + try { + SCHEMA_URL = new URL(SCHEMA_URL_ADDRESS); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + + private static Logger logger = LoggerFactory.getLogger(ResourceRegistryProfile.class); + + protected ObjectMapper mapper; + + public ResourceRegistryProfile() { + mapper = new ObjectMapper(); + } + + public ObjectMapper getMapper() { + return mapper; + } + + public ArrayNode list() { + ArrayNode arrayNode = mapper.createArrayNode(); + + try { + Set names = (new MetadataUtility()).getProfilesNames(); + for(String name : names) { + arrayNode.add(name); + } + return arrayNode; + } catch(Exception e) { + throw new InternalServerErrorException(e); + } + } + + /* + * https://wiki.gcube-system.org/gcube/Facet_Based_Resource_Model#Configuration + */ + protected Configuration instantiateConfiguration(String name, String xml) throws Exception { + Configuration configuration = new ConfigurationImpl(); + + IdentifierFacet identifierFacet = new IdentifierFacetImpl(); + identifierFacet.setValue(name); + identifierFacet.setType(IdentificationType.STRING); + IsIdentifiedBy isIdentifiedBy = new IsIdentifiedByImpl(configuration, identifierFacet); + configuration.addFacet(isIdentifiedBy); + + SimpleFacet profile = new SimpleFacetImpl(); + profile.setAdditionalProperty(PROFILE, xml); + configuration.addFacet(profile); + + SchemaFacet schemaFacet = new SchemaFacetImpl(); + schemaFacet.setName("gCube Item Profile"); + schemaFacet.setDescription("gCube Metadata Profile defines a Metadata schema XML-based for adding custom metadata fields."); + + ValueSchema valueSchema = new ValueSchemaImpl(); + valueSchema.setSchema(SCHEMA_URL.toURI()); + valueSchema.setValue(DataCalogueMetadataFormatReader.getProfileSchemaString()); + schemaFacet.setSchema(valueSchema); + + configuration.addFacet(schemaFacet); + + return configuration; + } + + + protected Configuration getConfiguration(String name) throws ResourceRegistryException { + ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + Map map = new HashMap<>(); + map.put(IdentifierFacet.VALUE_PROPERTY, name); + List configurations = resourceRegistryClient.getFilteredResources(Configuration.class, IsIdentifiedBy.class, IdentifierFacet.class, false, map); + return configurations.get(0); + } + + /* + * TODO Check the Queries because the name in the Profile differs from the name in + * + * + */ + protected GenericResource instantiateGenericResource(String name, String xml) throws Exception { + GenericResource genericResource = new GenericResource(); + org.gcube.common.resources.gcore.GenericResource.Profile profile = genericResource.newProfile(); + profile.type(MetadataFormatDiscovery.DATA_CATALOGUE_METADATA_SECONDARY_TYPE); + profile.name(name); + profile.description("Profile create using " + Constants.CATALOGUE_NAME); + // appendXmlFragment(profile, xml); + profile.newBody(xml); + StringWriter stringWriter = new StringWriter(); + Resources.marshal(genericResource, stringWriter); + logger.debug("The generated {} is\n{}", GenericResource.class.getSimpleName(), stringWriter.toString()); + return genericResource; + } + + protected void createGenericResource(String name, String xml) throws Exception { + GenericResource genericResource = instantiateGenericResource(name, xml); + RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); + genericResource = registryPublisher.create(genericResource); + StringWriter stringWriter = new StringWriter(); + Resources.marshal(genericResource, stringWriter); + logger.trace("The {} with ID {} has been created \n{}", GenericResource.class.getSimpleName(), + genericResource.id(), stringWriter.toString()); + } + + protected GenericResource getGenericResource(String name) { + String query = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryTypeAndName(name, + MetadataFormatDiscovery.DATA_CATALOGUE_METADATA_SECONDARY_TYPE); + Query q = new QueryBox(query); + DiscoveryClient client = ICFactory.clientFor(GenericResource.class); + List resources = client.submit(q); + + if(resources == null || resources.size() == 0) { + throw new InternalServerErrorException( + "No Resources with secondaryType '" + MetadataFormatDiscovery.DATA_CATALOGUE_METADATA_SECONDARY_TYPE + + "' and name '" + name + "' exists in the current context"); + } else { + if(resources.size() == 1) { + GenericResource genericResource = resources.get(0); + return genericResource; + } else { + throw new InternalServerErrorException("More than one Resource with secondaryType '" + + MetadataFormatDiscovery.DATA_CATALOGUE_METADATA_SECONDARY_TYPE + "' and name '" + name + + "' exists in the current context"); + } + } + } + + protected void updateGenericResource(String name, String xml) { + + GenericResource genericResource = getGenericResource(name); + logger.info("The {} with ID {} is going to be updated", GenericResource.class.getSimpleName(), + genericResource.id()); + + genericResource.profile().newBody(xml); + RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); + registryPublisher.update(genericResource); + + StringWriter stringWriter = new StringWriter(); + Resources.marshal(genericResource, stringWriter); + logger.trace("The {} with ID {} has been updated to \n{}", GenericResource.class.getSimpleName(), + genericResource.id(), stringWriter.toString()); + + } + + protected void removeGenericResource(String name) { + GenericResource genericResource = getGenericResource(name); + RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); + registryPublisher.remove(genericResource); + } + + public String read(String name, boolean xml) { + try { + String profile = (new MetadataUtility()).getMetadataFormat(name).getMetadataSource(); + if(profile != null) { + if(xml) { + return profile; + } else { + JSONObject xmlJSONObj = XML.toJSONObject(profile); + String jsonString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR); + return jsonString; + } + } else { + throw new NotFoundException("Profile with name " + name + " not found"); + } + } catch(WebApplicationException e) { + throw e; + } catch(Exception e) { + throw new InternalServerErrorException(e.getMessage()); + } + } + + public boolean createOrUpdate(String name, String xml) throws SAXException { + try { + CKANUser ckanUser = CKANUserCache.getCurrrentCKANUser(); + if(ckanUser.getRole().ordinal() 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_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) { diff --git a/src/main/java/org/gcube/gcat/rest/Item.java b/src/main/java/org/gcube/gcat/rest/Item.java index d22c981..6503d4b 100644 --- a/src/main/java/org/gcube/gcat/rest/Item.java +++ b/src/main/java/org/gcube/gcat/rest/Item.java @@ -55,21 +55,13 @@ public class Item extends REST implements org.gcube.gcat.api.interf @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; + @QueryParam(GCatConstants.COUNT_PARAMETER) @DefaultValue("false") Boolean countOnly) { 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; } /* diff --git a/src/main/java/org/gcube/gcat/rest/Organization.java b/src/main/java/org/gcube/gcat/rest/Organization.java index d1acc3c..11b0d53 100644 --- a/src/main/java/org/gcube/gcat/rest/Organization.java +++ b/src/main/java/org/gcube/gcat/rest/Organization.java @@ -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 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_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) { diff --git a/src/main/java/org/gcube/gcat/rest/Profile.java b/src/main/java/org/gcube/gcat/rest/Profile.java index 68ce3c2..4b115a8 100644 --- a/src/main/java/org/gcube/gcat/rest/Profile.java +++ b/src/main/java/org/gcube/gcat/rest/Profile.java @@ -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_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(); } - + } diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANGroupTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANGroupTest.java index 0ce4152..a80a206 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANGroupTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANGroupTest.java @@ -22,10 +22,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); } @@ -82,7 +89,7 @@ public class CKANGroupTest extends ContextTest { if(createdGroup.contains(name)) { return; } - String sysAdminAPI = "522557b4-f709-4935-be81-947e13afa2a7"; // CKANUtility.getSysAdminAPI(); + String sysAdminAPI = ""; // CKANUtility.getSysAdminAPI(); CKANGroup ckanGroupToCreate = new CKANGroup(); ckanGroupToCreate.setApiKey(sysAdminAPI); ckanGroupToCreate.setName(name); @@ -103,7 +110,7 @@ public class CKANGroupTest extends ContextTest { // @Test public void createGRSFGroups() throws ObjectNotFound, Exception { // Setting GRSF ADMIN token of grsf.publisher user to read groups - ContextTest.setContext("5213d3d8-6ea3-4d55-bf1c-da25ed3aba43-843339462"); + ContextTest.setContext(""); String key = CKANUtility.getSysAdminAPI(); List groupNames = listGroup(); Map groups = new HashMap<>(); @@ -118,7 +125,7 @@ public class CKANGroupTest extends ContextTest { Set createdGroup = new HashSet<>(); // Setting GRSF_PRE token of grsf.publisher user to create groups - ContextTest.setContext("17f72d12-5fe0-48a4-90d3-c398c41aedfe-843339462"); + ContextTest.setContext(""); for(String name : groupNames) { create(createdGroup, groups, name); } diff --git a/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java b/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java index cb25939..d76a8bf 100644 --- a/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java +++ b/src/test/java/org/gcube/gcat/persistence/ckan/CKANOrganizationTest.java @@ -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); diff --git a/src/test/java/org/gcube/gcat/profile/ProfileTest.java b/src/test/java/org/gcube/gcat/profile/ProfileTest.java index 24f1064..d3a2314 100644 --- a/src/test/java/org/gcube/gcat/profile/ProfileTest.java +++ b/src/test/java/org/gcube/gcat/profile/ProfileTest.java @@ -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();