From c1128dd10bcfbb28a8e95ed01289ae272fcbaa6d Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 24 Jan 2024 15:16:37 +0100 Subject: [PATCH] Added support for model knowledge #25922 --- .../schema/RRCCTypesDiscoverer.java | 30 +++ .../schema/ResourceRegistrySchemaClient.java | 24 ++- .../ResourceRegistrySchemaClientImpl.java | 174 ++++++++++++++++-- 3 files changed, 213 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/gcube/informationsystem/resourceregistry/schema/RRCCTypesDiscoverer.java diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/RRCCTypesDiscoverer.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/RRCCTypesDiscoverer.java new file mode 100644 index 0000000..100df2b --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/RRCCTypesDiscoverer.java @@ -0,0 +1,30 @@ +package org.gcube.informationsystem.resourceregistry.schema; + +import java.util.Collection; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.model.knowledge.TypesDiscoverer; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.types.reference.Type; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class RRCCTypesDiscoverer implements TypesDiscoverer { + + protected ResourceRegistrySchemaClientImpl rrsc; + + public RRCCTypesDiscoverer(ResourceRegistrySchemaClientImpl rrsc) { + this.rrsc = rrsc; + } + + @Override + public Collection discover(AccessType accessType) { + try { + return rrsc.getTypeFromServer(accessType.getTypeClass(), true); + } catch (ResourceRegistryException e) { + throw new RuntimeException(e); + } + } + +} 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 ea39ad9..a132ada 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClient.java @@ -3,9 +3,13 @@ package org.gcube.informationsystem.resourceregistry.schema; import java.util.List; import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.model.knowledge.ModelKnowledge; +import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException; +import org.gcube.informationsystem.tree.Node; +import org.gcube.informationsystem.types.knowledge.TypeInformation; import org.gcube.informationsystem.types.reference.Type; /** @@ -13,6 +17,10 @@ import org.gcube.informationsystem.types.reference.Type; */ public interface ResourceRegistrySchemaClient { + public ModelKnowledge getModelKnowledge(); + + public void renewModelKnowledge(); + public void addHeader(String name, String value); public String create(String typeDefinitition) throws SchemaException, ResourceRegistryException; @@ -21,11 +29,23 @@ public interface ResourceRegistrySchemaClient { public boolean exist(String typeName) throws ResourceRegistryException; - public boolean exist(Class clz) throws ResourceRegistryException; + public boolean exist(Class clz) throws ResourceRegistryException; public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException; + + public List read(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException; - public List read(Class clz, Boolean polymorphic) + public String read(String typeName, int level) + throws SchemaNotFoundException, ResourceRegistryException; + + public List read(Class clazz, int level) + throws SchemaNotFoundException, ResourceRegistryException; + + public Node getTypeTreeNode(String typeName) + throws SchemaNotFoundException, ResourceRegistryException; + + public Node getTypeTreeNode(Class clazz) throws SchemaNotFoundException, ResourceRegistryException; } 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 c9cf201..b30582a 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/ResourceRegistrySchemaClientImpl.java @@ -2,14 +2,18 @@ package org.gcube.informationsystem.resourceregistry.schema; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import org.gcube.common.gxhttp.reference.GXConnection; import org.gcube.common.gxhttp.request.GXHTTPStringRequest; import org.gcube.common.http.GXHTTPUtility; import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.model.knowledge.ModelKnowledge; +import org.gcube.informationsystem.model.reference.ERElement; import org.gcube.informationsystem.model.reference.properties.Metadata; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; @@ -20,7 +24,10 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaN import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.TypePath; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; +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; @@ -40,6 +47,8 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC protected Map headers; + protected TypesKnowledge typesKnowledge; + /** * Track if the client must request to include {@link Metadata} */ @@ -91,9 +100,31 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } public ResourceRegistrySchemaClientImpl(String address) { + this(address, true); + + } + + public ResourceRegistrySchemaClientImpl(String address, boolean sharedModelKnowledge) { this.address = address; this.headers = new HashMap<>(); this.includeMeta = false; + + if(sharedModelKnowledge) { + this.typesKnowledge = TypesKnowledge.getInstance(); + }else { + this.typesKnowledge = new TypesKnowledge(); + } + typesKnowledge.setTypesDiscoverer(new RRCCTypesDiscoverer(this)); + } + + @Override + public ModelKnowledge getModelKnowledge() { + return typesKnowledge.getModelKnowledge(); + } + + @Override + public void renewModelKnowledge() { + typesKnowledge.renew(); } @Override @@ -128,6 +159,9 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC HttpURLConnection httpURLConnection = gxHTTPStringRequest.put(typeDefinitition); String c = HTTPUtility.getResponse(String.class, httpURLConnection); + Type t = TypeMapper.deserializeTypeDefinition(c); + typesKnowledge.getModelKnowledge().addType(t); + logger.trace("{} successfully created", c); return c; @@ -139,19 +173,136 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } @Override - public boolean exist(Class clz) throws ResourceRegistryException { + public boolean exist(Class clazz) throws ResourceRegistryException { + return exist(TypeUtility.getTypeName(clazz)); + } + + @Override + public boolean exist(String typeName) throws ResourceRegistryException { try { - String typeName = TypeUtility.getTypeName(clz); - return exist(typeName); + return typesKnowledge.getModelKnowledge().getTypeByName(typeName) != null; + }catch (RuntimeException e) { + return false; + } + } + + public List getTypeFromTypesKnowledge(String typeName, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + return getTypeFromTypesKnowledge(typeName, polymorphic, -1); + } + + + public List getTypeFromTypesKnowledge(String typeName, int level) + throws SchemaNotFoundException, ResourceRegistryException { + return getTypeFromTypesKnowledge(typeName, true, level); + } + + protected List addChildren(Node node, List types, int currentLevel, int maxLevel) { + if(maxLevel>=0 && maxLevel <= currentLevel) { + return types; + } + + Set> children = node.getChildrenNodes(); + if(children!=null && children.size()>0) { + for(Node child : children) { + types.add(child.getNodeElement()); + types = addChildren(child, types, ++currentLevel, maxLevel); + } + } + + return types; + } + + public List getTypeFromTypesKnowledge(String typeName, Boolean polymorphic, int level) + throws SchemaNotFoundException, ResourceRegistryException { + + Node node = getTypeTreeNode(typeName); + + List types = new ArrayList<>(); + types.add(node.getNodeElement()); + + if (polymorphic) { + addChildren(node, types, 0, level); + } + + return types; + } + + @Override + public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { + try { + List types = getTypeFromTypesKnowledge(typeName, polymorphic); + return TypeMapper.serializeTypeDefinitions(types); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new RuntimeException(e); + throw new ResourceRegistryException(e); + } + } + + @Override + public List read(Class clazz, Boolean polymorphic) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String typeName = TypeUtility.getTypeName(clazz); + return getTypeFromTypesKnowledge(typeName, polymorphic); + } catch (Exception e) { + throw new ResourceRegistryException(e); } } @Override - public boolean exist(String typeName) throws ResourceRegistryException { + public String read(String typeName, int level) throws SchemaNotFoundException, ResourceRegistryException { + try { + List types = getTypeFromTypesKnowledge(typeName, level); + return TypeMapper.serializeTypeDefinitions(types); + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } + + @Override + public List read(Class clazz, int level) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String typeName = TypeUtility.getTypeName(clazz); + return getTypeFromTypesKnowledge(typeName, level); + } catch (Exception e) { + throw new ResourceRegistryException(e); + } + } + + @Override + public Node getTypeTreeNode(String typeName) throws SchemaNotFoundException, ResourceRegistryException { + try { + Node node = null; + try { + node = typesKnowledge.getModelKnowledge().getNodeByName(typeName); + } catch (RuntimeException e) { + throw new SchemaNotFoundException(e); + } + return node; + } catch(ResourceRegistryException e) { + throw e; + } catch(Exception e) { + throw new ResourceRegistryException(e); + } + } + + @Override + public Node getTypeTreeNode(Class clazz) + throws SchemaNotFoundException, ResourceRegistryException { + try { + String typeName = TypeUtility.getTypeName(clazz); + return getTypeTreeNode(typeName); + } catch (Exception e) { + throw new ResourceRegistryException(e); + } + } + + public boolean existTypeFromServer(String typeName) throws ResourceRegistryException { try { logger.info("Going to get {} schema", typeName); GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest(); @@ -176,23 +327,20 @@ public class ResourceRegistrySchemaClientImpl implements ResourceRegistrySchemaC } } - - @Override - public List read(Class clz, Boolean polymorphic) + public List getTypeFromServer(Class clz, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException { try { String typeName = TypeUtility.getTypeName(clz); - String res = read(typeName, polymorphic); - return TypeMapper.deserializeTypeDefinitions(res); + String json = getTypeFromServer(typeName, polymorphic); + return TypeMapper.deserializeTypeDefinitions(json); } catch(ResourceRegistryException e) { throw e; } catch(Exception e) { - throw new RuntimeException(e); + throw new ResourceRegistryException(e); } } - @Override - public String read(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException { + public String getTypeFromServer(String typeName, Boolean polymorphic) throws ContextNotFoundException, ResourceRegistryException { try { logger.info("Going to get {} schema", typeName); GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();