Fixing Type definition

This commit is contained in:
Luca Frosini 2019-11-08 18:14:45 +01:00
parent 6bc15e3164
commit 6354f99103
12 changed files with 205 additions and 144 deletions

View File

@ -6,22 +6,37 @@ import java.net.URL;
import java.security.Key; import java.security.Key;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.gcube.common.encryption.SymmetricKey; import org.gcube.common.encryption.SymmetricKey;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.ISConstants; import org.gcube.informationsystem.base.reference.ISConstants;
import org.gcube.informationsystem.base.reference.ISManageable; 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.properties.Header;
import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.resourceregistry.instances.base.ERManagement;
import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.type.entities.EntityTypeDefinitionManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.properties.PropertyTypeDefinitionManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.relations.RelationTypeDefinitionManagement;
import org.gcube.informationsystem.resourceregistry.security.AdminSecurityContext; import org.gcube.informationsystem.resourceregistry.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.security.ContextSecurityContext; import org.gcube.informationsystem.resourceregistry.security.ContextSecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SchemaSecurityContext; import org.gcube.informationsystem.resourceregistry.security.SchemaSecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode; 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.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.gcube.informationsystem.utils.discovery.ISMDiscovery; import org.gcube.informationsystem.utils.discovery.ISMDiscovery;
import org.gcube.informationsystem.utils.discovery.RegistrationProvider; import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
import org.gcube.informationsystem.utils.discovery.SchemaAction; import org.gcube.informationsystem.utils.discovery.SchemaAction;
@ -43,10 +58,12 @@ import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.security.ORole; import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.OSecurity; import com.orientechnologies.orient.core.metadata.security.OSecurity;
import com.orientechnologies.orient.core.metadata.security.OUser; import com.orientechnologies.orient.core.metadata.security.OUser;
import com.orientechnologies.orient.core.record.OElement;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
@SuppressWarnings("unchecked")
public class DatabaseEnvironment { public class DatabaseEnvironment {
private static Logger logger = LoggerFactory.getLogger(DatabaseEnvironment.class); private static Logger logger = LoggerFactory.getLogger(DatabaseEnvironment.class);
@ -208,6 +225,39 @@ public class DatabaseEnvironment {
schemaSecurityContext.create(); schemaSecurityContext.create();
SchemaAction schemaAction = new SchemaActionImpl();
schemaAction.managePropertyClass(BaseProperty.class);
schemaAction.managePropertyClass(Header.class);
schemaAction.managePropertyClass(PropertyDefinition.class);
schemaAction.managePropertyClass(PropertyTypeDefinition.class);
schemaAction.manageEntityClass(BaseEntity.class);
schemaAction.manageEntityClass(EntityTypeDefinition.class);
schemaAction.manageRelationClass(BaseRelation.class);
schemaAction.manageRelationClass(RelationTypeDefinition.class);
Set<Class<? extends ISManageable>> definitionToBeCreated = new HashSet<>();
definitionToBeCreated.add(BaseProperty.class);
definitionToBeCreated.add(Header.class);
definitionToBeCreated.add(BaseEntity.class);
definitionToBeCreated.add(BaseRelation.class);
for(Class<? extends ISManageable> clz : definitionToBeCreated) {
ERManagement<? extends OElement> erManagement = null;
if(BaseEntity.class.isAssignableFrom(clz)) {
erManagement = new EntityTypeDefinitionManagement();
} else if(BaseRelation.class.isAssignableFrom(clz)) {
erManagement = new RelationTypeDefinitionManagement();
} else if(BaseProperty.class.isAssignableFrom(clz)) {
erManagement = new PropertyTypeDefinitionManagement();
}
erManagement.setJson(TypeBinder.serializeType(clz));
erManagement.create();
}
List<Package> packages = new ArrayList<Package>(); List<Package> packages = new ArrayList<Package>();
Class<TypeDefinition> tdClz = TypeDefinition.class; Class<TypeDefinition> tdClz = TypeDefinition.class;
@ -225,7 +275,7 @@ public class DatabaseEnvironment {
packages.addAll(registrationProvider.getPackagesToRegister()); packages.addAll(registrationProvider.getPackagesToRegister());
} }
SchemaAction schemaAction = new SchemaActionImpl();
ISMDiscovery.manageISM(schemaAction, packages); ISMDiscovery.manageISM(schemaAction, packages);
} }

View File

@ -4,15 +4,11 @@ import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.entities.BaseEntity; import org.gcube.informationsystem.base.reference.entities.BaseEntity;
import org.gcube.informationsystem.base.reference.properties.BaseProperty; import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.relations.BaseRelation; import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo; import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl; import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl;
import org.gcube.informationsystem.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.utils.discovery.SchemaAction; import org.gcube.informationsystem.utils.discovery.SchemaAction;
@ -26,17 +22,12 @@ public class SchemaActionImpl implements SchemaAction {
private static Logger logger = LoggerFactory.getLogger(SchemaActionImpl.class); private static Logger logger = LoggerFactory.getLogger(SchemaActionImpl.class);
protected SchemaManagement schemaManagement;
public SchemaActionImpl() {
this.schemaManagement = new SchemaManagementImpl();
}
@Override @Override
public <R extends BaseRelation<? extends BaseEntity,? extends BaseEntity>> void manageRelationClass(Class<R> r) public <R extends BaseRelation<? extends BaseEntity,? extends BaseEntity>> void manageRelationClass(Class<R> r)
throws Exception { throws Exception {
try { try {
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(r)); SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
schemaManagement.setTypeName(TypeBinder.getType(r));
String json = TypeBinder.serializeType(r); String json = TypeBinder.serializeType(r);
logger.trace(json); logger.trace(json);
if(ConsistsOf.class.isAssignableFrom(r)) { if(ConsistsOf.class.isAssignableFrom(r)) {
@ -49,8 +40,7 @@ public class SchemaActionImpl implements SchemaAction {
} catch(SchemaAlreadyPresentException sape) { } catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(r)); logger.warn("{} already exists. It will be ignored", TypeBinder.getType(r));
} catch(Exception ex) { } catch(Exception ex) {
logger.error("Error creating schema for {} type {} : {}", Relation.NAME, r.getSimpleName(), logger.error("Error creating schema for {} {}: {}", BaseRelation.NAME, r.getSimpleName(), ex.getMessage());
ex.getMessage());
throw ex; throw ex;
} }
} }
@ -58,7 +48,8 @@ public class SchemaActionImpl implements SchemaAction {
@Override @Override
public <E extends BaseEntity> void manageEntityClass(Class<E> e) throws Exception { public <E extends BaseEntity> void manageEntityClass(Class<E> e) throws Exception {
try { try {
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(e)); SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
schemaManagement.setTypeName(TypeBinder.getType(e));
String json = TypeBinder.serializeType(e); String json = TypeBinder.serializeType(e);
logger.trace(json); logger.trace(json);
if(Facet.class.isAssignableFrom(e)) { if(Facet.class.isAssignableFrom(e)) {
@ -71,23 +62,23 @@ public class SchemaActionImpl implements SchemaAction {
} catch(SchemaAlreadyPresentException sape) { } catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e)); logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e));
} catch(Exception ex) { } catch(Exception ex) {
logger.error("Error creating schema for {} type {} : {}", Entity.NAME, e.getSimpleName(), ex.getMessage()); logger.error("Error creating schema for {} {}: {}", BaseEntity.NAME, e.getSimpleName(), ex.getMessage());
throw ex; throw ex;
} }
} }
@Override @Override
public <P extends BaseProperty> void managePropertyClass(Class<P> e) throws Exception { public <P extends BaseProperty> void managePropertyClass(Class<P> p) throws Exception {
try { try {
((SchemaManagementImpl) schemaManagement).setTypeName(TypeBinder.getType(e)); SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
String json = TypeBinder.serializeType(e); schemaManagement.setTypeName(TypeBinder.getType(p));
String json = TypeBinder.serializeType(p);
logger.trace(json); logger.trace(json);
schemaManagement.create(json, AccessType.BASE_PROPERTY); schemaManagement.create(json, AccessType.BASE_PROPERTY);
} catch(SchemaAlreadyPresentException sape) { } catch(SchemaAlreadyPresentException sape) {
logger.warn("{} already exists. It will be ignored", TypeBinder.getType(e)); logger.warn("{} already exists. It will be ignored", TypeBinder.getType(p));
} catch(Exception ex) { } catch(Exception ex) {
logger.error("Error creating schema for {} type {} : {}", Property.NAME, e.getSimpleName(), logger.error("Error creating schema for {} {}: {}", BaseProperty.NAME, p.getSimpleName(), ex.getMessage());
ex.getMessage());
throw ex; throw ex;
} }

View File

@ -31,7 +31,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.Schema
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.properties.BasePropertyManagement; import org.gcube.informationsystem.resourceregistry.instances.base.properties.BasePropertyManagement;
import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient; import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient;
@ -177,7 +176,7 @@ public abstract class ERManagement<El extends OElement> {
if(element != null) { if(element != null) {
oClass = getOClass(element); oClass = getOClass(element);
} else { } else {
oClass = SchemaManagementImpl.getTypeSchema(elementType, accessType); oClass = ERManagementUtility.getTypeSchema(elementType, accessType);
} }
} }
return oClass; return oClass;

View File

@ -12,20 +12,26 @@ import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
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.instances.model.entities.EntityManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl; import org.gcube.informationsystem.resourceregistry.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.metadata.OMetadata;
import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.record.OEdge; import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OElement; import com.orientechnologies.orient.core.record.OElement;
import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.record.OVertex;
@ -39,7 +45,7 @@ public class ERManagementUtility {
public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { public static AccessType getBaseAccessType(String type) throws ResourceRegistryException {
OClass oClass = SchemaManagementImpl.getTypeSchema(type, null); OClass oClass = ERManagementUtility.getTypeSchema(type, null);
if(oClass.isSubClassOf(Resource.NAME)) { if(oClass.isSubClassOf(Resource.NAME)) {
return AccessType.RESOURCE; return AccessType.RESOURCE;
@ -61,7 +67,7 @@ public class ERManagementUtility {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ERManagement getERManagement(String type) throws ResourceRegistryException { public static ERManagement getERManagement(String type) throws ResourceRegistryException {
OClass oClass = SchemaManagementImpl.getTypeSchema(type, null); OClass oClass = ERManagementUtility.getTypeSchema(type, null);
ERManagement erManagement = null; ERManagement erManagement = null;
if(oClass.isSubClassOf(Resource.NAME)) { if(oClass.isSubClassOf(Resource.NAME)) {
@ -199,4 +205,85 @@ public class ERManagementUtility {
return relationManagement; return relationManagement;
} }
public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type, AccessType accessType)
throws SchemaException, SchemaNotFoundException {
OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema();
return getTypeSchema(oSchema, type, accessType);
}
public static OClass getTypeSchema(OSchema oSchema, String type, AccessType accessType)
throws SchemaException, SchemaNotFoundException {
try {
OClass oClass = oSchema.getClass(type);
if(oClass == null) {
throw new SchemaNotFoundException(type + " was not registered");
}
if(accessType != null && type.compareTo(accessType.getName()) != 0) {
if(!oClass.isSubClassOf(accessType.getName())) {
throw new SchemaException(type + " is not a " + accessType.getName());
}
}
return oClass;
} catch(SchemaNotFoundException snfe) {
throw snfe;
} catch(Exception e) {
throw new SchemaException(e.getMessage());
}
}
public static OClass getTypeSchema(String type, AccessType accessType)
throws SchemaException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
return getTypeSchema(oDatabaseDocument, type, accessType);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
/*
try {
ExecutorService es = Executors.newSingleThreadExecutor();
Future<OClass> result = es.submit(new Callable<OClass>() {
public OClass call() throws Exception {
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
oDatabaseDocument.activateOnCurrentThread();
return getTypeSchema(oDatabaseDocument, type, accessType);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
}
}
});
return result.get();
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
*/
}
} }

View File

@ -50,9 +50,9 @@ public abstract class BaseEntityManagement<E extends BaseEntity> extends ERManag
} }
protected BaseEntityManagement(AccessType accessType, SecurityContext workingContext, ODatabaseDocument orientGraph) { protected BaseEntityManagement(AccessType accessType, SecurityContext workingContext, ODatabaseDocument oDatabaseDocument) {
this(accessType); this(accessType);
this.oDatabaseDocument = orientGraph; this.oDatabaseDocument = oDatabaseDocument;
setWorkingContext(workingContext); setWorkingContext(workingContext);
} }

View File

@ -14,7 +14,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.ERManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ERManagement;
import org.gcube.informationsystem.resourceregistry.instances.type.SchemaManagementImpl; import org.gcube.informationsystem.resourceregistry.instances.base.ERManagementUtility;
import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient; import org.gcube.informationsystem.resourceregistry.utils.EncryptedOrient;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -56,7 +56,7 @@ public class BasePropertyManagement {
OClass oClass = null; OClass oClass = null;
try { try {
oClass = SchemaManagementImpl.getTypeSchema(type, AccessType.BASE_PROPERTY); oClass = ERManagementUtility.getTypeSchema(type, AccessType.BASE_PROPERTY);
} catch(SchemaNotFoundException e) { } catch(SchemaNotFoundException e) {
throw e; throw e;
} }
@ -125,7 +125,7 @@ public class BasePropertyManagement {
return jsonNode; return jsonNode;
} }
OClass oClass = SchemaManagementImpl.getTypeSchema(type, AccessType.BASE_PROPERTY); OClass oClass = ERManagementUtility.getTypeSchema(type, AccessType.BASE_PROPERTY);
/* /*
* In case it is an Encrypted type the value is encrypted with the DB Key * In case it is an Encrypted type the value is encrypted with the DB Key

View File

@ -22,7 +22,6 @@ import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Property; import org.gcube.informationsystem.model.reference.properties.Property;
import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaCreationException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
@ -68,7 +67,7 @@ public class SchemaManagementImpl implements SchemaManagement {
protected String typeName; protected String typeName;
protected static OClass getOClass(OSchema oSchema, String type) throws SchemaException { protected OClass getOClass(OSchema oSchema, String type) throws SchemaException {
return oSchema.getClass(type); return oSchema.getClass(type);
} }
@ -76,87 +75,6 @@ public class SchemaManagementImpl implements SchemaManagement {
this.typeName = typeName; this.typeName = typeName;
} }
public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type, AccessType accessType)
throws SchemaException, SchemaNotFoundException {
OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema();
return getTypeSchema(oSchema, type, accessType);
}
public static OClass getTypeSchema(OSchema oSchema, String type, AccessType accessType)
throws SchemaException, SchemaNotFoundException {
try {
OClass oClass = oSchema.getClass(type);
if(oClass == null) {
throw new SchemaNotFoundException(type + " was not registered");
}
if(accessType != null && type.compareTo(accessType.getName()) != 0) {
if(!oClass.isSubClassOf(accessType.getName())) {
throw new SchemaException(type + " is not a " + accessType.getName());
}
}
return oClass;
} catch(SchemaNotFoundException snfe) {
throw snfe;
} catch(Exception e) {
throw new SchemaException(e.getMessage());
}
}
public static OClass getTypeSchema(String type, AccessType accessType)
throws SchemaException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
return getTypeSchema(oDatabaseDocument, type, accessType);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
/*
try {
ExecutorService es = Executors.newSingleThreadExecutor();
Future<OClass> result = es.submit(new Callable<OClass>() {
public OClass call() throws Exception {
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
oDatabaseDocument.activateOnCurrentThread();
return getTypeSchema(oDatabaseDocument, type, accessType);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
}
}
});
return result.get();
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
*/
}
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static TypeDefinition getOClassTypeDefinition(OClass oClass) throws SchemaException { private static TypeDefinition getOClassTypeDefinition(OClass oClass) throws SchemaException {
try { try {
@ -189,7 +107,7 @@ public class SchemaManagementImpl implements SchemaManagement {
} }
} }
private static TypeDefinition getTypeDefinition(OClass oClass) throws SchemaException { private TypeDefinition getTypeDefinition(OClass oClass) throws SchemaException {
try { try {
ERManagement<? extends OElement> erManagement = null; ERManagement<? extends OElement> erManagement = null;
@ -211,7 +129,7 @@ public class SchemaManagementImpl implements SchemaManagement {
} }
} }
protected static String getTypeDefinitionAsString(OClass oClass) throws SchemaException { protected String getTypeDefinitionAsString(OClass oClass) throws SchemaException {
try { try {
TypeDefinition typeDefinition = getTypeDefinition(oClass); TypeDefinition typeDefinition = getTypeDefinition(oClass);
return TypeBinder.serializeTypeDefinition(typeDefinition); return TypeBinder.serializeTypeDefinition(typeDefinition);
@ -256,8 +174,8 @@ public class SchemaManagementImpl implements SchemaManagement {
} }
private static Set<String> baseTypes; private static Set<String> baseTypes;
private static Set<String> typeDefinitionTypes; public static Set<String> typeDefinitionTypes;
private static Set<String> skipTypeDefinitionCreation; public static Set<String> skipTypeDefinitionCreation;
static { static {
baseTypes = new HashSet<String>(); baseTypes = new HashSet<String>();
@ -272,10 +190,8 @@ public class SchemaManagementImpl implements SchemaManagement {
skipTypeDefinitionCreation = new HashSet<String>(); skipTypeDefinitionCreation = new HashSet<String>();
skipTypeDefinitionCreation.addAll(baseTypes); skipTypeDefinitionCreation.addAll(baseTypes);
skipTypeDefinitionCreation.addAll(typeDefinitionTypes);
skipTypeDefinitionCreation.add(Header.NAME); skipTypeDefinitionCreation.add(Header.NAME);
skipTypeDefinitionCreation.add(PropertyDefinition.NAME); skipTypeDefinitionCreation.add(PropertyDefinition.NAME);
} }
protected void registerTypeSchema(TypeDefinition typeDefinition, AccessType baseType) protected void registerTypeSchema(TypeDefinition typeDefinition, AccessType baseType)
@ -406,7 +322,6 @@ public class SchemaManagementImpl implements SchemaManagement {
oDatabaseDocument.commit(); oDatabaseDocument.commit();
logger.info("{} {} registered successfully", baseType.getName(), typeDefinition.getName()); logger.info("{} {} registered successfully", baseType.getName(), typeDefinition.getName());
} catch(Exception e) { } catch(Exception e) {
oSchema.dropClass(typeDefinition.getName()); oSchema.dropClass(typeDefinition.getName());
throw e; throw e;
@ -433,7 +348,7 @@ public class SchemaManagementImpl implements SchemaManagement {
OMetadata oMetadata = oDatabaseDocument.getMetadata(); OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();
OClass baseOClass = getTypeSchema(oSchema, type, null); OClass baseOClass = oSchema.getClass(type);
List<TypeDefinition> typeDefinitions = new ArrayList<>(); List<TypeDefinition> typeDefinitions = new ArrayList<>();
typeDefinitions.add(getTypeDefinition(baseOClass)); typeDefinitions.add(getTypeDefinition(baseOClass));

View File

@ -33,19 +33,16 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
protected String name; protected String name;
private void init() {
this.elementType = EntityTypeDefinition.NAME;
}
public EntityTypeDefinitionManagement() { public EntityTypeDefinitionManagement() {
super(AccessType.ENTITY_TYPE_DEFINITION); super(AccessType.ENTITY_TYPE_DEFINITION);
init(); this.elementType = EntityTypeDefinition.NAME;
} }
public EntityTypeDefinitionManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { public EntityTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
this(); this();
this.oDatabaseDocument = oDatabaseDocument; this.oDatabaseDocument = oDatabaseDocument;
getWorkingContext(); setWorkingContext(securityContext);
} }
@Override @Override

View File

@ -33,19 +33,15 @@ public class PropertyTypeDefinitionManagement extends ERManagement<OElement> {
protected String name; protected String name;
private void init() { public PropertyTypeDefinitionManagement() {
super(AccessType.PROPERTY_TYPE_DEFINITION);
this.elementType = PropertyTypeDefinition.NAME; this.elementType = PropertyTypeDefinition.NAME;
} }
public PropertyTypeDefinitionManagement() { public PropertyTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
super(AccessType.PROPERTY_TYPE_DEFINITION);
init();
}
public PropertyTypeDefinitionManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
this(); this();
this.oDatabaseDocument = oDatabaseDocument; this.oDatabaseDocument = oDatabaseDocument;
getWorkingContext(); setWorkingContext(securityContext);
} }
@Override @Override

View File

@ -35,12 +35,13 @@ public class RelationTypeDefinitionManagement
public RelationTypeDefinitionManagement() { public RelationTypeDefinitionManagement() {
super(AccessType.RELATION_TYPE_DEFINITION, EntityTypeDefinition.class, EntityTypeDefinition.class); super(AccessType.RELATION_TYPE_DEFINITION, EntityTypeDefinition.class, EntityTypeDefinition.class);
this.elementType = RelationTypeDefinition.NAME;
} }
public RelationTypeDefinitionManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { public RelationTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
this(); this();
this.oDatabaseDocument = oDatabaseDocument; this.oDatabaseDocument = oDatabaseDocument;
getWorkingContext(); setWorkingContext(securityContext);
} }
@Override @Override
@ -220,12 +221,12 @@ public class RelationTypeDefinitionManagement
@Override @Override
protected EntityTypeDefinitionManagement newSourceEntityManagement() throws ResourceRegistryException { protected EntityTypeDefinitionManagement newSourceEntityManagement() throws ResourceRegistryException {
return new EntityTypeDefinitionManagement(oDatabaseDocument); return new EntityTypeDefinitionManagement(getWorkingContext(), oDatabaseDocument);
} }
@Override @Override
protected EntityTypeDefinitionManagement newTargetEntityManagement() throws ResourceRegistryException { protected EntityTypeDefinitionManagement newTargetEntityManagement() throws ResourceRegistryException {
return new EntityTypeDefinitionManagement(oDatabaseDocument); return new EntityTypeDefinitionManagement(getWorkingContext(), oDatabaseDocument);
} }
} }

View File

@ -144,5 +144,5 @@ public class Utility {
Header header = HeaderUtility.getHeader(element); Header header = HeaderUtility.getHeader(element);
return header.getUUID(); return header.getUUID();
} }
} }

View File

@ -1,7 +1,7 @@
/** /**
* *
*/ */
package org.gcube.informationsystem.resourceregistry.schema; package org.gcube.informationsystem.resourceregistry.type;
import java.util.List; import java.util.List;
@ -10,6 +10,7 @@ import org.gcube.informationsystem.base.reference.ISManageable;
import org.gcube.informationsystem.base.reference.properties.BaseProperty; import org.gcube.informationsystem.base.reference.properties.BaseProperty;
import org.gcube.informationsystem.base.reference.properties.Header; import org.gcube.informationsystem.base.reference.properties.Header;
import org.gcube.informationsystem.base.reference.relations.BaseRelation; import org.gcube.informationsystem.base.reference.relations.BaseRelation;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.entities.Facet; import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource; import org.gcube.informationsystem.model.reference.entities.Resource;
@ -167,6 +168,18 @@ public class SchemaManagementImplTest {
} }
@Test
public void createContextType() throws Exception {
EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(Context.class);
SchemaManagement schemaManagement = new SchemaManagementImpl();
((SchemaManagementImpl) schemaManagement).setTypeName(Context.NAME);
String ret = schemaManagement.create(ISMapper.marshal(entityTypeDefinition), AccessType.BASE_ENTITY);
logger.debug(ret);
}
@Test @Test
public void createFacetType() throws Exception { public void createFacetType() throws Exception {
EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(AccessPointFacet.class); EntityTypeDefinition entityTypeDefinition = new EntityTypeDefinitionImpl(AccessPointFacet.class);
@ -199,6 +212,18 @@ public class SchemaManagementImplTest {
} }
@Test
public void createRelationTypeDefinitionType() throws Exception {
RelationTypeDefinition relationTypeDefinition = new RelationTypeDefinitionImpl((Class<? extends BaseRelation<?,?>>) RelationTypeDefinition.class);
SchemaManagement schemaManagement = new SchemaManagementImpl();
((SchemaManagementImpl) schemaManagement).setTypeName(RelationTypeDefinition.NAME);
String ret = schemaManagement.create(ISMapper.marshal(relationTypeDefinition), AccessType.BASE_RELATION);
logger.debug(ret);
}
@Test @Test
public void createIsRelatedToType() throws Exception { public void createIsRelatedToType() throws Exception {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")