Added support for model knowledge #25922
This commit is contained in:
parent
46a4ecc5a9
commit
c1128dd10b
|
@ -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<Type> {
|
||||
|
||||
protected ResourceRegistrySchemaClientImpl rrsc;
|
||||
|
||||
public RRCCTypesDiscoverer(ResourceRegistrySchemaClientImpl rrsc) {
|
||||
this.rrsc = rrsc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Type> discover(AccessType accessType) {
|
||||
try {
|
||||
return rrsc.getTypeFromServer(accessType.getTypeClass(), true);
|
||||
} catch (ResourceRegistryException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Type, TypeInformation> 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 <E extends Element> boolean exist(Class<E> clz) throws ResourceRegistryException;
|
||||
public <ERElem extends ERElement> boolean exist(Class<ERElem> clz) throws ResourceRegistryException;
|
||||
|
||||
public String read(String typeName, Boolean polymorphic) throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <ERElem extends ERElement> List<Type> read(Class<ERElem> clazz, Boolean polymorphic)
|
||||
throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
|
||||
public String read(String typeName, int level)
|
||||
throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <ERElem extends ERElement> List<Type> read(Class<ERElem> clazz, int level)
|
||||
throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <ERElem extends ERElement> Node<Type> getTypeTreeNode(String typeName)
|
||||
throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
public <ERElem extends ERElement> Node<Type> getTypeTreeNode(Class<ERElem> clazz)
|
||||
throws SchemaNotFoundException, ResourceRegistryException;
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String, String> 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<Type, TypeInformation> 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 <E extends Element> boolean exist(Class<E> clz) throws ResourceRegistryException {
|
||||
public <ERElem extends ERElement> boolean exist(Class<ERElem> 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 <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic)
|
||||
throws SchemaNotFoundException, ResourceRegistryException {
|
||||
return getTypeFromTypesKnowledge(typeName, polymorphic, -1);
|
||||
}
|
||||
|
||||
|
||||
public <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, int level)
|
||||
throws SchemaNotFoundException, ResourceRegistryException {
|
||||
return getTypeFromTypesKnowledge(typeName, true, level);
|
||||
}
|
||||
|
||||
protected List<Type> addChildren(Node<Type> node, List<Type> types, int currentLevel, int maxLevel) {
|
||||
if(maxLevel>=0 && maxLevel <= currentLevel) {
|
||||
return types;
|
||||
}
|
||||
|
||||
Set<Node<Type>> children = node.getChildrenNodes();
|
||||
if(children!=null && children.size()>0) {
|
||||
for(Node<Type> child : children) {
|
||||
types.add(child.getNodeElement());
|
||||
types = addChildren(child, types, ++currentLevel, maxLevel);
|
||||
}
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
public <ERElem extends ERElement> List<Type> getTypeFromTypesKnowledge(String typeName, Boolean polymorphic, int level)
|
||||
throws SchemaNotFoundException, ResourceRegistryException {
|
||||
|
||||
Node<Type> node = getTypeTreeNode(typeName);
|
||||
|
||||
List<Type> 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<Type> 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 <ERElem extends ERElement> List<Type> read(Class<ERElem> 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<Type> types = getTypeFromTypesKnowledge(typeName, level);
|
||||
return TypeMapper.serializeTypeDefinitions(types);
|
||||
} catch(ResourceRegistryException e) {
|
||||
throw e;
|
||||
} catch(Exception e) {
|
||||
throw new ResourceRegistryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <ERElem extends ERElement> List<Type> read(Class<ERElem> 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<Type> getTypeTreeNode(String typeName) throws SchemaNotFoundException, ResourceRegistryException {
|
||||
try {
|
||||
Node<Type> 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 <ERElem extends ERElement> Node<Type> getTypeTreeNode(Class<ERElem> 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 <E extends Element> List<Type> read(Class<E> clz, Boolean polymorphic)
|
||||
public <ERElem extends ERElement> List<Type> getTypeFromServer(Class<ERElem> 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();
|
||||
|
|
Loading…
Reference in New Issue