From e1a53c3da8e6ad71b383a25425b280f91210f4cf Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 19 Dec 2017 10:27:29 +0000 Subject: [PATCH] Refs #10248: Create Resource Registry Schema Client Task-Url: https://support.d4science.org/issues/10248 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry-schema-client@160674 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 5 ++ .../schema/ResourceRegistrySchemaClient.java | 6 +-- .../ResourceRegistrySchemaClientImpl.java | 48 ++++++++++++++----- .../ResourceRegistrySchemaClientTest.java | 25 ++++++++++ 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 2b7e3c3..c86da35 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,11 @@ 1.0.13 test + + org.gcube.information-system + gcube-resources + test + diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java index 6837e65..190d6a5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java @@ -13,14 +13,14 @@ import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; */ public interface ResourceRegistrySchemaClient { - public TypeDefinition create(Class clazz) + public TypeDefinition create(Class clz) throws SchemaException, ResourceRegistryException; - public String create(String typeDefinitition) + public String create(String baseType, String typeDefinitition) throws SchemaException, ResourceRegistryException; - public List read(Class clazz, Boolean polymorphic) + public List read(Class clz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; public String read(String type, Boolean polymorphic) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java index 7ac369a..37e5dd2 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java @@ -6,13 +6,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.gcube.informationsystem.model.AccessType; import org.gcube.informationsystem.model.ISManageable; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; -import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.SchemaPath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; @@ -46,12 +46,36 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC return httpCall; } + /* + protected AccessType getType(Class clazz) { + if(ConsistsOf.class.isAssignableFrom(clazz)) { + return AccessType.CONSISTS_OF; + } else if(IsRelatedTo.class.isAssignableFrom(clazz)) { + return AccessType.IS_RELATED_TO; + } else if(Relation.class.isAssignableFrom(clazz)) { + return AccessType.RELATION; + } else if(Resource.class.isAssignableFrom(clazz)) { + return AccessType.RESOURCE; + } else if(Facet.class.isAssignableFrom(clazz)) { + return AccessType.FACET; + } else if(Embedded.class.isAssignableFrom(clazz)) { + return AccessType.EMBEDDED; + } else if(Entity.class.isAssignableFrom(clazz)) { + return AccessType.ENTITY; + } else { + throw new RuntimeException("Type " + clazz.getSimpleName() + " is not an IS base type.") ; + } + } + */ + + @Override - public TypeDefinition create(Class clazz) + public TypeDefinition create(Class clz) throws SchemaException, ResourceRegistryException { try { - String typeDefinition = TypeBinder.serializeType(clazz); - String res = create(typeDefinition); + String typeDefinition = TypeBinder.serializeType(clz); + String type = AccessType.getAccessType(clz).getName(); + String res = create(type, typeDefinition); return TypeBinder.deserializeTypeDefinition(res); } catch(ResourceRegistryException e) { throw e; @@ -61,12 +85,14 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } @Override - public String create(String typeDefinitition) throws ContextAlreadyPresentException, ResourceRegistryException { + public String create(String baseType, String typeDefinitition) throws ContextAlreadyPresentException, ResourceRegistryException { try { logger.trace("Going to create: {}", typeDefinitition); StringWriter stringWriter = new StringWriter(); stringWriter.append(PATH_SEPARATOR); stringWriter.append(SchemaPath.SCHEMA_PATH_PART); + stringWriter.append(PATH_SEPARATOR); + stringWriter.append(baseType); HTTPCall httpCall = getHTTPCall(); String c = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.PUT, typeDefinitition); @@ -81,11 +107,12 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } } + @Override - public List read(Class clazz, Boolean polymorphic) + public List read(Class clz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { try { - String type = Utility.getType(clazz); + String type = Utility.getType(clz); String res = read(type, polymorphic); return TypeBinder.deserializeTypeDefinitions(res); } catch(ResourceRegistryException e) { @@ -104,15 +131,12 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC stringWriter.append(PATH_SEPARATOR); stringWriter.append(SchemaPath.SCHEMA_PATH_PART); stringWriter.append(PATH_SEPARATOR); - stringWriter.append(AccessPath.SCHEMA_PATH_PART); - stringWriter.append(PATH_SEPARATOR); stringWriter.append(type); Map parameters = new HashMap<>(); - if(polymorphic == null) { - polymorphic = false; + if(polymorphic != null) { + parameters.put(SchemaPath.POLYMORPHIC_PARAM, polymorphic.toString()); } - parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); HTTPCall httpCall = getHTTPCall(); String json = httpCall.call(String.class, stringWriter.toString(), HTTPMETHOD.GET, parameters); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientTest.java index 758e00f..1bf57ce 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientTest.java @@ -1,5 +1,10 @@ package org.gcube.informationsystem.resourceregistry.schema; +import java.util.List; + +import org.gcube.informationsystem.model.entity.Facet; +import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; +import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -7,5 +12,25 @@ public class ResourceRegistrySchemaClientTest extends ScopedTest { private static Logger logger = LoggerFactory.getLogger(ResourceRegistrySchemaClientTest.class); + interface Aux extends Facet { + + } + + @Test + public void testCreate() throws Exception { + ResourceRegistrySchemaClient resourceRegistrySchemaClient = ResourceRegistrySchemaClientFactory.create(); + TypeDefinition td = resourceRegistrySchemaClient.create(Aux.class); + logger.debug("{}", td); + } + + @Test + public void testRead() throws Exception { + ResourceRegistrySchemaClient resourceRegistrySchemaClient = ResourceRegistrySchemaClientFactory.create(); + List types = resourceRegistrySchemaClient.read(Facet.class, true); + for(TypeDefinition td : types) { + logger.debug("{}", td); + } + } + }