Adding support for types as tree using ModelKnowledge as cache

This commit is contained in:
luca.frosini 2023-10-27 18:14:16 +02:00
parent 29b559a71a
commit 0cc1ead630
2 changed files with 90 additions and 8 deletions

View File

@ -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<Type> {
protected ResourceRegistryClient resourceRegistryClient;
public RRCTypesDiscoverer(ResourceRegistryClient resourceRegistryClient) {
this.resourceRegistryClient = resourceRegistryClient;
}
@Override
public Collection<Type> discover(AccessType accessType) {
return null;
}
}

View File

@ -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.TypePath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.types.TypeMapper; import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.knowledge.TypeInformation; 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.types.reference.Type;
import org.gcube.informationsystem.utils.TypeUtility; import org.gcube.informationsystem.utils.TypeUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -66,7 +68,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
protected ContextCache contextCache; protected ContextCache contextCache;
protected ModelKnowledge<Type, TypeInformation> modelKnowledge; protected TypesKnowledge typesKnowledge;
@Deprecated @Deprecated
@Override @Override
@ -148,21 +150,30 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
} }
public ResourceRegistryClientImpl(String address) { 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(); super();
this.address = address; this.address = address;
this.headers = new HashMap<>(); this.headers = new HashMap<>();
if(sharedContextCache) { if(sharedContextCache) {
contextCache = ContextCache.getInstance(); this.contextCache = ContextCache.getInstance();
}else { }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. * It reads all the contexts from server.
* The cache used for contexts is bypassed and not updated. * The cache used for contexts is bypassed and not updated.
@ -210,6 +221,10 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
return contextCache; return contextCache;
} }
public ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
return typesKnowledge.getModelKnowledge();
}
/** /**
* It reads the context from server. * It reads the context from server.
* The cache used for contexts is bypassed and not updated. * The cache used for contexts is bypassed and not updated.
@ -318,6 +333,14 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
@Override @Override
public boolean existType(String typeName) throws ResourceRegistryException { 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 { try {
logger.info("Going to get {} schema", typeName); logger.info("Going to get {} schema", typeName);
GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest(); GXHTTPStringRequest gxHTTPStringRequest = getGXHTTPStringRequest();
@ -344,7 +367,7 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
// logger.trace("Error while getting {}schema for {}", polymorphic ? // logger.trace("Error while getting {}schema for {}", polymorphic ?
// AccessPath.POLYMORPHIC_PARAM + " " : "", // AccessPath.POLYMORPHIC_PARAM + " " : "",
// type, e); // type, e);
throw new RuntimeException(e); throw new ResourceRegistryException(e);
} }
} }
@ -357,10 +380,43 @@ public class ResourceRegistryClientImpl extends BaseRequestInfo implements Resou
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {
throw new RuntimeException(e); throw new ResourceRegistryException(e);
} }
} }
public <ERElem extends ERElement> List<Type> getTypeFromServer(Class<ERElem> 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<Type> 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 @Override
public String getType(String typeName, Boolean polymorphic) public String getType(String typeName, Boolean polymorphic)
throws SchemaNotFoundException, ResourceRegistryException { throws SchemaNotFoundException, ResourceRegistryException {