refs #7355 - Check Source-Target Relation compatibility

Added creation of instances in admin context

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@146252 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-03-21 16:54:06 +00:00
parent 6cdc2af21f
commit ecff48a91a
2 changed files with 156 additions and 2 deletions

View File

@ -0,0 +1,124 @@
package org.gcube.informationsystem.resourceregistry.schema;
import java.util.Iterator;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
public class SchemaContextManagement implements SchemaManagement {
private static Logger logger = LoggerFactory.getLogger(SchemaContextManagement.class);
public static final String SCHEMA = "__SCHEMA";
protected Vertex getVertex(OrientGraph orientGraph, String vertexType) throws Exception{
Iterable<Vertex> iterable = orientGraph.getVerticesOfClass(vertexType, false);
Iterator<Vertex> iterator = iterable.iterator();
Vertex vertex = null;
if(iterator.hasNext()){
vertex = iterator.next();
} else {
String error = String.format("%s is not a registered type", vertexType);
logger.trace(error);
throw new Exception(error);
}
if(iterator.hasNext()){
String error = String.format("More than one instance of %s found in Management Context. This MUST not happen. Please contact system administrator.", vertexType);
logger.error(error);
throw new Exception(error);
}
return vertex;
}
@Override
public String create(String json, AccessType baseType) throws SchemaException {
OrientGraphFactory orientGraphFactory = SecurityContextMapper
.getSecurityContextFactory(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID,
PermissionMode.WRITER);
OrientGraph orientGraph = null;
try {
orientGraph = orientGraphFactory.getTx();
ObjectMapper mapper = new ObjectMapper();
TypeDefinition typeDefinition = mapper.readValue(json, TypeDefinition.class);
if (Entity.class.isAssignableFrom(baseType.getTypeClass())) {
OrientVertex orientVertex = orientGraph.addVertex("class:" + typeDefinition.getName());
orientVertex.setProperty(SCHEMA, json);
orientVertex.save();
} else if (Relation.class.isAssignableFrom(baseType.getTypeClass())) {
String sourceClass = typeDefinition.getSourceType();
Vertex source = getVertex(orientGraph, sourceClass);
String targetClass = typeDefinition.getTargetType();
Vertex target = getVertex(orientGraph, targetClass);
OrientEdge orientEdge = orientGraph.addEdge(null, source, target, typeDefinition.getName());
orientEdge.setProperty(SCHEMA, json);
orientEdge.save();
} else if (Embedded.class.isAssignableFrom(baseType.getTypeClass())) {
ODocument doc = new ODocument(typeDefinition.getName());
doc.field(SCHEMA, json);
doc.save();
}
orientGraph.commit();
return json;
}catch (Exception e) {
if (orientGraph != null) {
orientGraph.rollback();
}
throw new SchemaException(e);
} finally {
if (orientGraph != null) {
orientGraph.shutdown();
}
}
}
@Override
public String read(String type, boolean includeSubtypes) throws SchemaNotFoundException, SchemaException {
// TODO Auto-generated method stub
return null;
}
@Override
public String update(String type, AccessType accessType, String json)
throws SchemaNotFoundException, SchemaException {
throw new UnsupportedOperationException("Not Yet implemented");
}
@Override
public String delete(String type, AccessType accessType) throws SchemaNotFoundException {
throw new UnsupportedOperationException("Not Yet implemented");
}
}

View File

@ -162,7 +162,7 @@ public class SchemaManagementImpl implements SchemaManagement {
return oSuperclasses;
}
protected String registerTypeSchema(String jsonSchema, AccessType baseType)
throws SchemaException {
@ -192,6 +192,15 @@ public class SchemaManagementImpl implements SchemaManagement {
} else if (Relation.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = orientGraphNoTx.createEdgeType(typeDefinition
.getName());
/*
* This information are persisted in Management Context
*
* String outBaseType = typeDefinition.getOutBaseType();
* String inBaseType = typeDefinition.getInBaseType();
*
*/
} else if (Embedded.class.isAssignableFrom(baseType.getTypeClass())) {
oClass = oSchema.createClass(typeDefinition.getName());
} else {
@ -210,6 +219,7 @@ public class SchemaManagementImpl implements SchemaManagement {
}
try {
// oClass.setAbstract(false); // Used to allow to persist Schema in Context Management
oClass.setAbstract(typeDefinition.isAbstract());
} catch (Exception e) {
logger.error(
@ -240,6 +250,8 @@ public class SchemaManagementImpl implements SchemaManagement {
* so that on Type declaration they are forced to false
* ovp.setMandatory(property.isMandatory());
* ovp.setNotNull(property.isNotnull());
*
* This information are persisted in Management Context
*/
op.setMandatory(false);
op.setNotNull(false);
@ -276,8 +288,15 @@ public class SchemaManagementImpl implements SchemaManagement {
toBeSerializedOClass = getOClass(oSchema,
typeDefinition.getName());
}
/*
SchemaContextManagement managementUtility = new SchemaContextManagement();
String ret = managementUtility.create(jsonSchema, baseType);
*/
// TODO Remove when the previous has been implemented
String ret = getTypeDefinitionAsString(toBeSerializedOClass);
logger.info("{} type registered successfully: {}",
baseType.getName(), jsonSchema);
return ret;
@ -307,12 +326,23 @@ public class SchemaManagementImpl implements SchemaManagement {
OClass baseOClass = getTypeSchema(oSchema, type, null);
List<TypeDefinition> typeDefinitions = new ArrayList<>();
typeDefinitions.add(getTypeDefinition(baseOClass));
if(includeSubtypes){
Collection<OClass> subClasses = baseOClass.getAllSubclasses();
for (OClass oClass : subClasses) {
typeDefinitions.add(getTypeDefinition(oClass));
}
}
/*
Collection<OClass> oClasses = oSchema.getClasses();
for (OClass oClass : oClasses) {
if (oClass.isSubClassOf(baseOClass)) {
typeDefinitions.add(getTypeDefinition(oClass));
}
}
*/
return TypeBinder.serializeTypeDefinitions(typeDefinitions);
} catch (SchemaException e) {