From fa0e18b580bc4322e4ab63d162cc6977aac16aa8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 11 Nov 2020 14:18:12 +0100 Subject: [PATCH] Added getInstanceContexts() --- .../client/ResourceRegistryClient.java | 11 ++++- .../client/ResourceRegistryClientImpl.java | 47 +++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java index 2eb23f9..ab54080 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClient.java @@ -2,6 +2,7 @@ package org.gcube.informationsystem.resourceregistry.client; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import org.gcube.informationsystem.base.reference.Element; @@ -27,13 +28,13 @@ public interface ResourceRegistryClient { public boolean exists(Class clazz, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - public boolean exists(String typeName, UUID uuid) + public boolean exists(String type, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; public IE getInstance(Class clazz, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; - public String getInstance(String typeName, UUID uuid) + public String getInstance(String type, UUID uuid) throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; public List getInstances(Class clazz, Boolean polymorphic) @@ -118,4 +119,10 @@ public interface ResourceRegistryClient { public List getAllContext() throws ResourceRegistryException; + public Set getInstanceContexts(Class clazz, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; + + public Set getInstanceContexts(String type, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException; + } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java index 54f5156..60fda21 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -5,6 +5,7 @@ import java.net.HttpURLConnection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import org.gcube.common.gxhttp.reference.GXConnection; @@ -20,6 +21,7 @@ import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -183,15 +185,15 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { @Override public List getSchema(Class clazz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { - String typeName = Utility.getTypeName(clazz); + String type = Utility.getTypeName(clazz); try { - logger.info("Going to get {} schema", typeName); + logger.info("Going to get {} schema", type); GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); gxHTTPStringRequest.path(AccessPath.TYPES_PATH_PART); - gxHTTPStringRequest.path(typeName); + gxHTTPStringRequest.path(type); Map parameters = new HashMap<>(); parameters.put(AccessPath.POLYMORPHIC_PARAM, polymorphic.toString()); @@ -200,7 +202,7 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); String json = HTTPUtility.getResponse(String.class, httpURLConnection); - logger.debug("Got schema for {} is {}", typeName, json); + logger.debug("Got schema for {} is {}", type, json); return TypeMapper.deserializeTypeDefinitions(json); } catch(ResourceRegistryException e) { // logger.trace("Error while getting {} schema for {}", polymorphic ? AccessPath.POLYMORPHIC_PARAM + " " : "", @@ -601,5 +603,42 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient { Direction direction, boolean polymorphic) throws ResourceRegistryException { return getRelated(entityType, relationType, referenceEntityType, referenceEntity, direction, polymorphic, null); } + + @Override + public Set getInstanceContexts(Class clazz, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + String typeName = Utility.getTypeName(clazz); + return getInstanceContexts(typeName, uuid); + } + + @Override + public Set getInstanceContexts(String type, UUID uuid) + throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { + try { + logger.trace("Going to get contexts of {} with UUID {}", type, uuid); + GXHTTPStringRequest gxHTTPStringRequest = GXHTTPStringRequest.newRequest(address); + gxHTTPStringRequest.from(ResourceRegistryClient.class.getSimpleName()); + gxHTTPStringRequest.header("Accept", GXConnection.APPLICATION_JSON_CHARSET_UTF_8); + gxHTTPStringRequest.path(AccessPath.ACCESS_PATH_PART); + gxHTTPStringRequest.path(AccessPath.INSTANCES_PATH_PART); + gxHTTPStringRequest.path(type); + gxHTTPStringRequest.path(uuid.toString()); + gxHTTPStringRequest.path(AccessPath.CONTEXTS_PATH_PART); + + HttpURLConnection httpURLConnection = gxHTTPStringRequest.get(); + String jsonArray = HTTPUtility.getResponse(String.class, httpURLConnection); + + logger.info("Contexts of {} with UUID {} are {}", type, uuid, jsonArray); + + Set contexts = ContextUtility.getContextUUIDSet(jsonArray); + return contexts; + } catch(ResourceRegistryException e) { + // logger.trace("Error while getting {} with UUID {}", type, uuid, e); + throw e; + } catch(Exception e) { + // logger.trace("Error while getting {} with UUID {}", type, uuid, e); + throw new RuntimeException(e); + } + } }