resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/instances/type/SchemaContextManagementOld....

123 lines
4.4 KiB
Java

package org.gcube.informationsystem.resourceregistry.instances.type;
import java.util.Iterator;
import org.gcube.informationsystem.base.reference.AccessType;
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.instances.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.types.reference.TypeDefinition;
import org.gcube.informationsystem.types.reference.relations.RelationTypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.record.impl.ODocument;
public class SchemaContextManagementOld implements SchemaManagement {
private static Logger logger = LoggerFactory.getLogger(SchemaContextManagementOld.class);
public static final String SCHEMA = "__SCHEMA";
protected OVertex getVertex(ODatabaseDocument oDatabaseDocument, String vertexType) throws Exception {
Iterable<ODocument> iterable = oDatabaseDocument.browseClass(vertexType, false);
Iterator<ODocument> iterator = iterable.iterator();
OVertex vertex = null;
if(iterator.hasNext()) {
vertex = (OVertex) 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 {
ODatabaseDocument orientGraph = null;
try {
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
orientGraph = adminSecurityContext.getDatabaseDocument(PermissionMode.WRITER);
TypeDefinition typeDefinition = TypeBinder.deserializeTypeDefinition(json);
if(BaseEntity.class.isAssignableFrom(baseType.getTypeClass())) {
OVertex oVertex = orientGraph.newVertex(typeDefinition.getName());
oVertex.setProperty(SCHEMA, json);
oVertex.save();
} else if(BaseRelation.class.isAssignableFrom(baseType.getTypeClass())) {
@SuppressWarnings("rawtypes")
String sourceClass = ((RelationTypeDefinition) typeDefinition).getSourceType();
OVertex source = getVertex(orientGraph, sourceClass);
@SuppressWarnings("rawtypes")
String targetClass = ((RelationTypeDefinition) typeDefinition).getTargetType();
OVertex target = getVertex(orientGraph, targetClass);
OEdge oEdge = orientGraph.newEdge(source, target, typeDefinition.getName());
oEdge.setProperty(SCHEMA, json);
oEdge.save();
} else if(BaseProperty.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.close();
}
}
}
@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");
}
}