From 0cc1ead6308fbf9a7abeafcea2bb30c7c5252664 Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Fri, 27 Oct 2023 18:14:16 +0200 Subject: [PATCH] Adding support for types as tree using ModelKnowledge as cache --- .../client/RRCTypesDiscoverer.java | 26 +++++++ .../client/ResourceRegistryClientImpl.java | 72 ++++++++++++++++--- 2 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java b/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java new file mode 100644 index 0000000..90f779e --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/RRCTypesDiscoverer.java @@ -0,0 +1,26 @@ +package org.gcube.informationsystem.resourceregistry.client; + +import java.util.Collection; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.model.knowledge.TypesDiscoverer; +import org.gcube.informationsystem.types.reference.Type; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class RRCTypesDiscoverer implements TypesDiscoverer { + + protected ResourceRegistryClient resourceRegistryClient; + + public RRCTypesDiscoverer(ResourceRegistryClient resourceRegistryClient) { + this.resourceRegistryClient = resourceRegistryClient; + } + + @Override + public Collection discover(AccessType accessType) { + + return null; + } + +} 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 8969585..f435d46 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/client/ResourceRegistryClientImpl.java @@ -43,8 +43,10 @@ import org.gcube.informationsystem.resourceregistry.api.rest.QueryTemplatePath; import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.serialization.ElementMapper; +import org.gcube.informationsystem.tree.Node; import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.knowledge.TypeInformation; +import org.gcube.informationsystem.types.knowledge.TypesKnowledge; import org.gcube.informationsystem.types.reference.Type; import org.gcube.informationsystem.utils.TypeUtility; import org.slf4j.Logger; @@ -66,7 +68,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou protected ContextCache contextCache; - protected ModelKnowledge modelKnowledge; + protected TypesKnowledge typesKnowledge; @Deprecated @Override @@ -148,21 +150,30 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou } public ResourceRegistryClientImpl(String address) { - this(address, true); + this(address, true, true); } - public ResourceRegistryClientImpl(String address, boolean sharedContextCache) { + public ResourceRegistryClientImpl(String address, boolean sharedContextCache, boolean sharedModelKnowledge) { super(); this.address = address; this.headers = new HashMap<>(); if(sharedContextCache) { - contextCache = ContextCache.getInstance(); + this.contextCache = ContextCache.getInstance(); }else { - contextCache = new ContextCache(); + this.contextCache = new ContextCache(); } - contextCache.setContextCacheRenewal(contextCacheRenewal); + this.contextCache.setContextCacheRenewal(contextCacheRenewal); + + if(sharedModelKnowledge) { + this.typesKnowledge = TypesKnowledge.getInstance(); + }else { + this.typesKnowledge = new TypesKnowledge(); + } + typesKnowledge.setTypesDiscoverer(new RRCTypesDiscoverer(this)); } + + /** * It reads all the contexts from server. * The cache used for contexts is bypassed and not updated. @@ -210,6 +221,10 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou return contextCache; } + public ModelKnowledge getModelKnowledge() { + return typesKnowledge.getModelKnowledge(); + } + /** * It reads the context from server. * The cache used for contexts is bypassed and not updated. @@ -318,6 +333,14 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou @Override public boolean existType(String typeName) throws ResourceRegistryException { + try { + return typesKnowledge.getModelKnowledge().getTypeByName(typeName) != null; + }catch (RuntimeException e) { + return false; + } + } + + public boolean existTypeFromServer(String typeName) throws ResourceRegistryException { try { logger.info("Going to get {} schema", typeName); GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest(); @@ -344,7 +367,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou // logger.trace("Error while getting {}schema for {}", polymorphic ? // AccessPath.POLYMORPHIC_PARAM + " " : "", // type, e); - throw new RuntimeException(e); + throw new ResourceRegistryException(e); } } @@ -357,10 +380,43 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new RuntimeException(e); + throw new ResourceRegistryException(e); } } + public List getTypeFromServer(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String json = getTypeFromServer(TypeUtility.getTypeName(clazz), polymorphic); + return TypeMapper.deserializeTypeDefinitions(json); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } + + /* + @Override + public String getType(String typeName, Boolean polymorphic) + */ + public String getTypeFromKnowledge(String typeName, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + try { + Node node = typesKnowledge.getModelKnowledge().getNodeByName(typeName); + if(polymorphic) { + // TODO + }else { + // TODO + } + return null; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } + + + // public String getTypeFromServer(String typeName, Boolean polymorphic) @Override public String getType(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException {