Fixing resource-registry due to model redesign

This commit is contained in:
Luca Frosini 2019-10-30 09:57:36 +01:00
parent 10e5feb9d9
commit 495cba3145
8 changed files with 144 additions and 41 deletions

View File

@ -4,19 +4,26 @@ import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.UUID;
import org.gcube.common.encryption.SymmetricKey;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.ISConstants;
import org.gcube.informationsystem.base.reference.ISManageable;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.context.security.ContextSecurityContext;
import org.gcube.informationsystem.resourceregistry.context.security.SchemaSecurityContext;
import org.gcube.informationsystem.resourceregistry.context.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.utils.discovery.ISMDiscovery;
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
import org.gcube.informationsystem.utils.discovery.SchemaAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -202,8 +209,27 @@ public class DatabaseEnvironment {
schemaSecurityContext.create();
List<Package> packages = new ArrayList<Package>();
@SuppressWarnings("rawtypes")
Class<TypeDefinition> tdClz = TypeDefinition.class;
packages.add(tdClz.getPackage());
AccessType[] accessTypes = AccessType.values();
for(AccessType accessType : accessTypes) {
Class<ISManageable> clz = accessType.getTypeClass();
packages.add(clz.getPackage());
}
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
.load(RegistrationProvider.class);
for(RegistrationProvider registrationProvider : regsitrationProviders) {
packages.addAll(registrationProvider.getPackagesToRegister());
}
SchemaAction schemaAction = new SchemaActionImpl();
ISMDiscovery.manageISM(schemaAction);
ISMDiscovery.manageISM(schemaAction, packages);
}
logger.info("Database Connection has been properly initialized");

View File

@ -44,7 +44,7 @@ public class SchemaActionImpl implements SchemaAction {
} else if(IsRelatedTo.class.isAssignableFrom(r)) {
schemaManagement.create(json, AccessType.IS_RELATED_TO);
} else {
schemaManagement.create(json, AccessType.RELATION);
schemaManagement.create(json, AccessType.BASE_RELATION);
}
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(r));
@ -66,7 +66,7 @@ public class SchemaActionImpl implements SchemaAction {
} else if(Resource.class.isAssignableFrom(e)) {
schemaManagement.create(json, AccessType.RESOURCE);
} else {
schemaManagement.create(json, AccessType.ENTITY);
schemaManagement.create(json, AccessType.BASE_ENTITY);
}
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e));
@ -82,7 +82,7 @@ public class SchemaActionImpl implements SchemaAction {
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(e));
String json = TypeBinder.serializeType(e);
logger.trace(json);
schemaManagement.create(json, AccessType.PROPERTY);
schemaManagement.create(json, AccessType.BASE_PROPERTY);
} catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e));
} catch(Exception ex) {

View File

@ -80,6 +80,7 @@ public class SchemaManager {
AccessType accessType = null;
String firstGotType = null;
try {
@SuppressWarnings("unchecked")
Set<String> superClasses = TypeBinder.deserializeTypeDefinition(json).getSuperClasses();
if(superClasses.size()==0) {
throw new ResourceRegistryException("No superclasses defined");

View File

@ -3,9 +3,9 @@ package org.gcube.informationsystem.resourceregistry.schema;
import java.util.Iterator;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
@ -63,13 +63,13 @@ public class SchemaContextManagement implements SchemaManagement {
orientGraph = adminSecurityContext.getGraph(PermissionMode.WRITER);
@SuppressWarnings("rawtypes")
TypeDefinition<? extends TypeDefinition> typeDefinition = TypeBinder.deserializeTypeDefinition(json);
TypeDefinition typeDefinition = TypeBinder.deserializeTypeDefinition(json);
if(Entity.class.isAssignableFrom(baseType.getTypeClass())) {
if(BaseEntity.class.isAssignableFrom(baseType.getTypeClass())) {
OrientVertex orientVertex = orientGraph.addVertex("class:" + typeDefinition.getName());
orientVertex.setProperty(SCHEMA, json);
orientVertex.save();
} else if(Relation.class.isAssignableFrom(baseType.getTypeClass())) {
} else if(BaseRelation.class.isAssignableFrom(baseType.getTypeClass())) {
@SuppressWarnings("rawtypes")
String sourceClass = ((RelationTypeDefinition) typeDefinition).getSourceType();
Vertex source = getVertex(orientGraph, sourceClass);
@ -82,7 +82,7 @@ public class SchemaContextManagement implements SchemaManagement {
orientEdge.setProperty(SCHEMA, json);
orientEdge.save();
} else if(Property.class.isAssignableFrom(baseType.getTypeClass())) {
} else if(BaseProperty.class.isAssignableFrom(baseType.getTypeClass())) {
ODocument doc = new ODocument(typeDefinition.getName());
doc.field(SCHEMA, json);
doc.save();

View File

@ -5,6 +5,7 @@ package org.gcube.informationsystem.resourceregistry.schema;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
@ -16,6 +17,9 @@ import javax.activation.UnsupportedDataTypeException;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.ISManageable;
import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Property;
@ -28,12 +32,20 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.Schema
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.context.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.types.Type;
import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
import org.gcube.informationsystem.types.reference.properties.PropertyTypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.OMetadata;
@ -127,28 +139,54 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
protected static <ISM extends ISManageable> TypeDefinition<ISM> getTypeDefinition(OClass oClass) throws SchemaException {
@SuppressWarnings("rawtypes")
protected static TypeDefinition getTypeDefinition(OClass oClass) throws SchemaException {
// TODO OrientSerilizatoin is not acceptable anymore since TypeDefiniton introduction
ODocument oDocument = ((OClassImpl) oClass).toStream();
String json = oDocument.toJSON();
try {
return TypeBinder.deserializeTypeDefinition(json);
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree(json);
if(oClass.isSubClassOf(BaseProperty.NAME)) {
node.put(ISManageable.CLASS_PROPERTY, PropertyTypeDefinition.NAME);
} else if(oClass.isSubClassOf(BaseEntity.NAME)) {
node.put(ISManageable.CLASS_PROPERTY, EntityTypeDefinition.NAME);
} else if(oClass.isSubClassOf(BaseRelation.NAME)) {
node.put(ISManageable.CLASS_PROPERTY, RelationTypeDefinition.NAME);
}
ArrayNode arrayNode = (ArrayNode) node.get(TypeDefinition.PROPERTIES_PROPERTY);
Iterator<JsonNode> iterator = arrayNode.iterator();
while(iterator.hasNext()) {
ObjectNode propertyNode = (ObjectNode) iterator.next();
propertyNode.put(ISManageable.CLASS_PROPERTY, PropertyDefinition.NAME);
}
String managedJson = mapper.writeValueAsString(node);
logger.trace("{} -> {}", json, managedJson);
return TypeBinder.deserializeTypeDefinition(managedJson);
} catch(Exception e) {
throw new SchemaException(e);
}
}
protected static <ISM extends ISManageable> String getTypeDefinitionAsString(OClass oClass) throws SchemaException {
@SuppressWarnings({"rawtypes"})
protected static String getTypeDefinitionAsString(OClass oClass) throws SchemaException {
try {
TypeDefinition<ISM> typeDefinition = getTypeDefinition(oClass);
TypeDefinition typeDefinition = getTypeDefinition(oClass);
return TypeBinder.serializeTypeDefinition(typeDefinition);
} catch(Exception e) {
throw new SchemaException(e);
}
}
protected <ISM extends ISManageable> List<OClass> getSuperclassesAndCheckCompliancy(ODatabaseSession oDatabaseSession,
TypeDefinition<ISM> typeDefinition, String baseType) throws SchemaException {
@SuppressWarnings({"unchecked", "rawtypes"})
protected List<OClass> getSuperclassesAndCheckCompliancy(ODatabaseSession oDatabaseSession,
TypeDefinition typeDefinition, String baseType) throws SchemaException {
Set<String> superClasses = typeDefinition.getSuperClasses();
if(baseType != null) {
@ -182,15 +220,35 @@ public class SchemaManagementImpl implements SchemaManagement {
return oSuperclasses;
}
private static List<String> baseTypes;
private static List<String> typeDefinitionTypes;
static {
baseTypes = new ArrayList<String>();
baseTypes.add(BaseProperty.NAME);
baseTypes.add(BaseEntity.NAME);
baseTypes.add(BaseRelation.NAME);
typeDefinitionTypes = new ArrayList<String>();
typeDefinitionTypes.add(PropertyTypeDefinition.NAME);
typeDefinitionTypes.add(EntityTypeDefinition.NAME);
typeDefinitionTypes.add(RelationTypeDefinition.NAME);
}
protected String registerTypeSchema(String jsonSchema, AccessType baseType) throws SchemaException {
ODatabaseSession oDatabaseSession = null;
try {
logger.info("Trying to register {} {}", baseType.getName(), jsonSchema);
@SuppressWarnings("rawtypes")
TypeDefinition<? extends TypeDefinition> typeDefinition = TypeBinder.deserializeTypeDefinition(jsonSchema);
TypeDefinition typeDefinition = null;
try {
typeDefinition = TypeBinder.deserializeTypeDefinition(jsonSchema);
logger.info("Trying to register {} {} : {}", baseType.getName(), typeDefinition.getName(), jsonSchema);
}catch (Exception e) {
logger.error("Error while trying to register {} {}", baseType.getName(), jsonSchema);
throw e;
}
if(typeName.compareTo(typeDefinition.getName())!=0) {
String error = String.format("Provided type name path argument %s does not match with the type name in the definition %S. Please be coherent.", typeName, typeDefinition.getName());
@ -206,9 +264,9 @@ public class SchemaManagementImpl implements SchemaManagement {
OClass oClass = null;
if(Entity.class.isAssignableFrom(baseType.getTypeClass())) {
if(BaseEntity.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = oDatabaseSession.createVertexClass(typeDefinition.getName());
} else if(Relation.class.isAssignableFrom(baseType.getTypeClass())) {
} else if(BaseRelation.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = oDatabaseSession.createEdgeClass(typeDefinition.getName());
/*
@ -217,7 +275,7 @@ public class SchemaManagementImpl implements SchemaManagement {
* typeDefinition.getInBaseType();
*/
} else if(Property.class.isAssignableFrom(baseType.getTypeClass())) {
} else if(BaseProperty.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = oSchema.createClass(typeDefinition.getName());
} else {
String error = String.format("Allowed superclass are %s, %s, %s, or any subclasses of them.",
@ -247,31 +305,41 @@ public class SchemaManagementImpl implements SchemaManagement {
typeDefinition.getName());
}
if(typeDefinition.getName().compareTo(Property.NAME) != 0) {
if(! baseTypes.contains(typeDefinition.getName())) {
List<OClass> oSuperclasses = getSuperclassesAndCheckCompliancy(oDatabaseSession, typeDefinition,
baseType.getName());
oClass.setSuperClasses(oSuperclasses);
}
if(Resource.class.isAssignableFrom(baseType.getTypeClass())) {
@SuppressWarnings("unchecked")
Set<PropertyDefinition> propertyDefinitions = typeDefinition.getProperties();
if(Resource.class.isAssignableFrom(baseType.getTypeClass())) {
if(propertyDefinitions != null && propertyDefinitions.size() > 0) {
throw new SchemaCreationException("A Resource cannot contains any properties.");
}
} else {
for(PropertyDefinition propertyDefinition : typeDefinition.getProperties()) {
for(PropertyDefinition propertyDefinition : propertyDefinitions) {
OType oType = OType.getById(propertyDefinition.getType().byteValue());
/*
* Types update is not allowed,
* hence bug https://github.com/orientechnologies/orientdb/issues/7354 cannot occur
* Excluding the check from types used for type definition
*
*/
if(!typeDefinitionTypes.contains(typeDefinition.getName())) {
switch(oType) {
case EMBEDDEDLIST:
throw new UnsupportedDataTypeException(oType.name()
throw new UnsupportedDataTypeException(Type.OType.PROPERTYLIST
+ " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
case EMBEDDEDSET:
throw new UnsupportedDataTypeException(oType.name()
throw new UnsupportedDataTypeException(Type.OType.PROPERTYSET
+ " support is currently disabled due to OrientDB bug see https://github.com/orientechnologies/orientdb/issues/7354");
default:
break;
}
}
OProperty op = oClass.createProperty(propertyDefinition.getName(), oType);
op.setDescription(propertyDefinition.getDescription());
@ -342,6 +410,7 @@ public class SchemaManagementImpl implements SchemaManagement {
}
}
@SuppressWarnings("unchecked")
protected <ISM extends ISManageable> String getSchema(String type, boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
ODatabaseSession oDatabaseSession = null;
try {

View File

@ -49,15 +49,21 @@ public class ContextTest {
}
// PARENT_DEFAULT_TEST_SCOPE = "/pred4s"
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "preprod";
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "/preprod";
// ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE";
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "devNext";
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
try {
setContextByName(DEFAULT_TEST_SCOPE);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {

View File

@ -34,7 +34,7 @@ public class DiscoveryTest {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
boolean first = true;
for (Class<Resource> resource : resourceDiscovery.getDiscovered()) {
for (Class<? extends Resource> resource : resourceDiscovery.getDiscovered()) {
if(first) {
first = false;
}else {

View File

@ -60,7 +60,8 @@ public class SchemaManagementImplTest {
public void getFacetSchema() throws Exception {
String json = new SchemaManagementImpl().read(ContactFacet.NAME, false);
logger.info(json);
List<TypeDefinition<ContactFacet>> typeDefinitions = TypeBinder.deserializeTypeDefinitions(json);
@SuppressWarnings("rawtypes")
List<TypeDefinition> typeDefinitions = TypeBinder.deserializeTypeDefinitions(json);
logger.info("{}", typeDefinitions);
}