Refactoring code

This commit is contained in:
Luca Frosini 2021-02-08 16:26:58 +01:00
parent 3434f373f6
commit c21fa2aa1a
16 changed files with 217 additions and 95 deletions

View File

@ -30,7 +30,6 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement; import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.informationsystem.utils.ElementMapper;
@ -337,7 +336,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
move(newParentContextManagement, false); move(newParentContextManagement, false);
} }
element = (OVertex) ElementManagement.updateProperties(oClass, getElement(), jsonNode, ignoreKeys, element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys,
ignoreStartWithKeys); ignoreStartWithKeys);
ContextCache.getInstance().cleanCache(); ContextCache.getInstance().cleanCache();

View File

@ -41,6 +41,7 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.base.properties.PropertyElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.properties.PropertyElementManagement;
import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient; import org.gcube.informationsystem.resourceregistry.utils.HeaderOrient;
import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility; import org.gcube.informationsystem.resourceregistry.utils.HeaderUtility;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
@ -63,7 +64,7 @@ public abstract class ElementManagement<El extends OElement> {
protected Logger logger = LoggerFactory.getLogger(this.getClass()); protected Logger logger = LoggerFactory.getLogger(this.getClass());
private static Logger staticLogger = LoggerFactory.getLogger(ElementManagement.class); // private static Logger staticLogger = LoggerFactory.getLogger(ElementManagement.class);
public final static String DELETED = "deleted"; public final static String DELETED = "deleted";
@ -185,7 +186,8 @@ public abstract class ElementManagement<El extends OElement> {
if(element != null) { if(element != null) {
oClass = getOClass(element); oClass = getOClass(element);
} else { } else {
oClass = ElementManagementUtility.getTypeSchema(elementType, accessType); oClass = TypesCache.getTypeSchema(elementType);
TypesCache.checkAccessType(oClass, elementType, accessType);
} }
} }
return oClass; return oClass;
@ -535,7 +537,7 @@ public abstract class ElementManagement<El extends OElement> {
} }
} }
public JsonNode read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException { public String read() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal(); ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try { try {
@ -543,7 +545,8 @@ public abstract class ElementManagement<El extends OElement> {
getElement(); getElement();
return serializeAsJson(); jsonNode = serializeAsJson();
return jsonNode.toString();
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to read {} with UUID {}", accessType.getName(), uuid); logger.error("Unable to read {} with UUID {}", accessType.getName(), uuid);
throw e; throw e;
@ -767,7 +770,7 @@ public abstract class ElementManagement<El extends OElement> {
return null; return null;
} }
public static Map<String,Object> getPropertyMap(JsonNode jsonNode, Set<String> ignoreKeys, public Map<String,Object> getPropertyMap(JsonNode jsonNode, Set<String> ignoreKeys,
Set<String> ignoreStartWith) throws JsonProcessingException, IOException { Set<String> ignoreStartWith) throws JsonProcessingException, IOException {
Map<String,Object> map = new HashMap<>(); Map<String,Object> map = new HashMap<>();
@ -805,7 +808,7 @@ public abstract class ElementManagement<El extends OElement> {
map.put(key, object); map.put(key, object);
} }
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
staticLogger.warn("An invalidy property has been provided. It will be ignored."); logger.warn("An invalidy property has been provided. It will be ignored.");
} }
} }
@ -813,7 +816,7 @@ public abstract class ElementManagement<El extends OElement> {
return map; return map;
} }
public static OElement updateProperties(OClass oClass, OElement element, JsonNode jsonNode, Set<String> ignoreKeys, public OElement updateProperties(OClass oClass, OElement element, JsonNode jsonNode, Set<String> ignoreKeys,
Set<String> ignoreStartWithKeys) throws ResourceRegistryException { Set<String> ignoreStartWithKeys) throws ResourceRegistryException {
Set<String> oldKeys = element.getPropertyNames(); Set<String> oldKeys = element.getPropertyNames();
@ -859,7 +862,7 @@ public abstract class ElementManagement<El extends OElement> {
} catch(Exception e) { } catch(Exception e) {
String error = String.format("Error while setting property %s : %s (%s)", key, String error = String.format("Error while setting property %s : %s (%s)", key,
properties.get(key).toString(), e.getMessage()); properties.get(key).toString(), e.getMessage());
staticLogger.error(error); logger.error(error);
throw new ResourceRegistryException(error, e); throw new ResourceRegistryException(error, e);
} }
} }

View File

@ -12,26 +12,20 @@ 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.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
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.types.TypesCache;
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;
@ -45,7 +39,7 @@ public class ElementManagementUtility {
public static AccessType getBaseAccessType(String type) throws ResourceRegistryException { public static AccessType getBaseAccessType(String type) throws ResourceRegistryException {
OClass oClass = ElementManagementUtility.getTypeSchema(type, null); OClass oClass = TypesCache.getTypeSchema(type);
if(oClass.isSubClassOf(Resource.NAME)) { if(oClass.isSubClassOf(Resource.NAME)) {
return AccessType.RESOURCE; return AccessType.RESOURCE;
@ -66,7 +60,7 @@ public class ElementManagementUtility {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ElementManagement getERManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument, String type) throws ResourceRegistryException { public static ElementManagement getERManagement(SecurityContext workingContext, ODatabaseDocument oDatabaseDocument, String type) throws ResourceRegistryException {
OClass oClass = ElementManagementUtility.getTypeSchema(oDatabaseDocument, type, null); OClass oClass = TypesCache.getTypeSchema(oDatabaseDocument, type);
ElementManagement erManagement = null; ElementManagement erManagement = null;
if(oClass.isSubClassOf(Resource.NAME)) { if(oClass.isSubClassOf(Resource.NAME)) {
@ -90,7 +84,7 @@ public class ElementManagementUtility {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ElementManagement getERManagement(String type) throws ResourceRegistryException { public static ElementManagement getERManagement(String type) throws ResourceRegistryException {
OClass oClass = ElementManagementUtility.getTypeSchema(type, null); OClass oClass = TypesCache.getTypeSchema(type);
ElementManagement erManagement = null; ElementManagement erManagement = null;
if(oClass.isSubClassOf(Resource.NAME)) { if(oClass.isSubClassOf(Resource.NAME)) {
@ -228,55 +222,4 @@ public class ElementManagementUtility {
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();
}
}
}
} }

View File

@ -153,7 +153,7 @@ public abstract class EntityElementManagement<E extends EntityElement> extends E
if(accessType == AccessType.RESOURCE) { if(accessType == AccessType.RESOURCE) {
// Facet and relation are created in calling method // Facet and relation are created in calling method
} else { } else {
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
} }
logger.info("Created {} is {}", OVertex.class.getSimpleName(), logger.info("Created {} is {}", OVertex.class.getSimpleName(),

View File

@ -17,7 +17,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.ElementManagement; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
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;
@ -53,7 +53,8 @@ public class PropertyElementManagement {
OClass oClass = null; OClass oClass = null;
try { try {
oClass = ElementManagementUtility.getTypeSchema(type, AccessType.PROPERTY_ELEMENT); oClass = TypesCache.getTypeSchema(type);
TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT);
} catch(SchemaNotFoundException e) { } catch(SchemaNotFoundException e) {
throw e; throw e;
} }
@ -122,8 +123,8 @@ public class PropertyElementManagement {
return jsonNode; return jsonNode;
} }
OClass oClass = ElementManagementUtility.getTypeSchema(type, AccessType.PROPERTY_ELEMENT); OClass oClass = TypesCache.getTypeSchema(type);
TypesCache.checkAccessType(oClass, type, AccessType.PROPERTY_ELEMENT);
/* /*
* 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
* Resource Registry must decrypt the value with the DB Key and Encrypt it with Context key. * Resource Registry must decrypt the value with the DB Key and Encrypt it with Context key.

View File

@ -167,7 +167,7 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
element = oDatabaseDocument.newEdge(source, target, elementType); element = oDatabaseDocument.newEdge(source, target, elementType);
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
return element; return element;
} }
@ -182,7 +182,7 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
logger.debug("Trying to update {} : {}", elementType, jsonNode); logger.debug("Trying to update {} : {}", elementType, jsonNode);
OEdge edge = getElement(); OEdge edge = getElement();
ElementManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys);
if(accessType.compareTo(AccessType.CONSISTS_OF) == 0) { if(accessType.compareTo(AccessType.CONSISTS_OF) == 0) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);

View File

@ -277,7 +277,7 @@ public abstract class EntityManagement<E extends Entity> extends EntityElementMa
if(accessType == AccessType.RESOURCE) { if(accessType == AccessType.RESOURCE) {
// Facet and relation are created in calling method // Facet and relation are created in calling method
} else { } else {
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
} }
logger.info("Created {} is {}", OVertex.class.getSimpleName(), logger.info("Created {} is {}", OVertex.class.getSimpleName(),

View File

@ -12,7 +12,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.record.OVertex;
@ -63,7 +62,7 @@ public class FacetManagement extends EntityManagement<Facet> {
@Override @Override
protected OVertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException { protected OVertex reallyUpdate() throws FacetNotFoundException, ResourceRegistryException {
OVertex facet = getElement(); OVertex facet = getElement();
facet = (OVertex) ElementManagement.updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys); facet = (OVertex) updateProperties(oClass, facet, jsonNode, ignoreKeys, ignoreStartWithKeys);
return facet; return facet;
} }

View File

@ -1,7 +1,9 @@
package org.gcube.informationsystem.resourceregistry.instances.model.entities; package org.gcube.informationsystem.resourceregistry.instances.model.entities;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -28,7 +30,11 @@ import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityCo
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.types.entities.ResourceTypeDefinitionManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.entities.ResourceType;
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument; import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.metadata.schema.OClass; import com.orientechnologies.orient.core.metadata.schema.OClass;
@ -296,10 +302,38 @@ public class ResourceManagement extends EntityManagement<Resource> {
} }
} }
// TODO Create something like SchemaDefinitionNotRespected o ConstaintNotRespected Exception @Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws ResourceRegistryException { public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws ResourceRegistryException {
// TODO check that at least a facet and its consistOf will be added to the context /*
JsonNode jsonNode = read(); * Actually this function only check the Resource constraint, i.e., resource has at least a Facet
* in any context.
*
* We need to load the Resource definition and validate it.
* This task is not very easy because we cannot check just the type but
* me must take in account that a specialisation instance is valid.
*
*/
read();
ResourceTypeDefinitionManagement resourceTypeDefinitionManagement = new ResourceTypeDefinitionManagement();
resourceTypeDefinitionManagement.setName(elementType);
String stringType = resourceTypeDefinitionManagement.read().toString();
ResourceType resourceType = null;
try {
resourceType = (ResourceType) TypeMapper.deserializeTypeDefinition(stringType);
}catch (Exception e) {
throw new ResourceRegistryException(e);
}
// TODO Create something like SchemaDefinitionNotRespected o ConstaintNotRespected Exception
List<LinkedEntity> constraint = new ArrayList<>();
List<LinkedEntity> linkedEntities = resourceType.getFacets();
for(LinkedEntity linkedEntity : linkedEntities) {
if(linkedEntity.getMin()>0 || linkedEntity.getMax()>0) {
constraint.add(linkedEntity);
}
}
boolean found = false; boolean found = false;
ArrayNode consistsOfArrayNode = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY); ArrayNode consistsOfArrayNode = (ArrayNode) jsonNode.get(Resource.CONSISTS_OF_PROPERTY);
@ -307,7 +341,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
String consistsOfUUIDString = consistsOfJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText(); String consistsOfUUIDString = consistsOfJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText();
UUID consistsOfUUID = UUID.fromString(consistsOfUUIDString); UUID consistsOfUUID = UUID.fromString(consistsOfUUIDString);
if(expectedInstances.containsKey(consistsOfUUID)) { if(expectedInstances.containsKey(consistsOfUUID)) {
// we need to check that also the facets are // we need to check that also the facets is present
JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY); JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY);
String facetUUIDString = facetJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText(); String facetUUIDString = facetJsonNode.get(IdentifiableElement.HEADER_PROPERTY).get(Header.UUID_PROPERTY).asText();
UUID facetUUID = UUID.fromString(facetUUIDString); UUID facetUUID = UUID.fromString(facetUUIDString);

View File

@ -346,7 +346,7 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
logger.debug("Trying to update {} : {}", elementType, jsonNode); logger.debug("Trying to update {} : {}", elementType, jsonNode);
OEdge edge = getElement(); OEdge edge = getElement();
ElementManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys);
if(accessType.compareTo(AccessType.CONSISTS_OF) == 0) { if(accessType.compareTo(AccessType.CONSISTS_OF) == 0) {
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);

View File

@ -176,7 +176,7 @@ public class SchemaManagement {
return erManagement; return erManagement;
} }
private static ElementManagement<? extends OElement> getTypeManagement(OClass oClass) { public static ElementManagement<? extends OElement> getTypeManagement(OClass oClass) {
ElementManagement<? extends OElement> erManagement = null; ElementManagement<? extends OElement> erManagement = null;
if(oClass.isSubClassOf(Property.NAME)) { if(oClass.isSubClassOf(Property.NAME)) {
erManagement = new PropertyTypeDefinitionManagement(); erManagement = new PropertyTypeDefinitionManagement();

View File

@ -0,0 +1,143 @@
package org.gcube.informationsystem.resourceregistry.types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.entities.Facet;
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.IsRelatedTo;
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.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode;
import org.gcube.informationsystem.types.reference.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.OSchema;
public class TypesCache {
private static Logger logger = LoggerFactory.getLogger(TypesCache.class);
protected static final Map<String, OClass> oClasses;
protected static final Map<String, Type> types;
protected static final Map<String, List<String>> superTypes;
protected static final Map<String, List<String>> specilisationTypes;
static {
oClasses = new HashMap<>();
types = new HashMap<>();
superTypes = new HashMap<>();
specilisationTypes = new HashMap<>();
}
public static AccessType getBaseAccessType(String type) throws ResourceRegistryException {
OClass oClass = getTypeSchema(type);
if(oClass.isSubClassOf(Resource.NAME)) {
return AccessType.RESOURCE;
} else if(oClass.isSubClassOf(Facet.NAME)) {
return AccessType.FACET;
} else if(oClass.isSubClassOf(ConsistsOf.NAME)) {
return AccessType.CONSISTS_OF;
} else if(oClass.isSubClassOf(IsRelatedTo.NAME)) {
return AccessType.IS_RELATED_TO;
} else if(oClass.isSubClassOf(Property.NAME)) {
return AccessType.PROPERTY;
}
throw new ResourceRegistryException(type + "is not a base type");
}
public static void checkAccessType(OClass oClass, String type, AccessType accessType) throws SchemaException {
if(accessType != null && type.compareTo(accessType.getName()) != 0) {
if(!oClass.isSubClassOf(accessType.getName())) {
throw new SchemaException(type + " is not a " + accessType.getName());
}
}
}
private static OClass getTypeSchema(OSchema oSchema, String type)
throws SchemaException, SchemaNotFoundException {
try {
OClass oClass;
synchronized (oClasses) {
oClass = oClasses.get(type);
if(oClass==null) {
oClass = oSchema.getClass(type);
if(oClass == null) {
throw new SchemaNotFoundException(type + " was not registered");
}
oClasses.put(type, oClass);
}
}
return oClass;
} catch(SchemaNotFoundException snfe) {
throw snfe;
} catch(Exception e) {
throw new SchemaException(e.getMessage());
}
}
public static OClass getTypeSchema(ODatabaseDocument oDatabaseDocument, String type)
throws SchemaException, SchemaNotFoundException {
synchronized (oClasses) {
OClass oClass = oClasses.get(type);
if(oClass!=null) {
return oClass;
}
}
OMetadata oMetadata = oDatabaseDocument.getMetadata();
OSchema oSchema = oMetadata.getSchema();
return getTypeSchema(oSchema, type);
}
public static OClass getTypeSchema(String type)
throws SchemaException, ResourceRegistryException {
synchronized (oClasses) {
OClass oClass = oClasses.get(type);
if(oClass!=null) {
return oClass;
}
}
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
ODatabaseDocument oDatabaseDocument = null;
try {
logger.debug("GettingType {} schema", type);
AdminSecurityContext adminSecurityContext = ContextUtility.getAdminSecurityContext();
oDatabaseDocument = adminSecurityContext.getDatabaseDocument(PermissionMode.READER);
return getTypeSchema(oDatabaseDocument, type);
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
}
}

View File

@ -93,7 +93,7 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException { protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", this.elementType, getName()); logger.debug("Going to update {} for {}", this.elementType, getName());
OVertex entityTypeDefinition = getElement(); OVertex entityTypeDefinition = getElement();
entityTypeDefinition = (OVertex) ElementManagement.updateProperties(oClass, entityTypeDefinition, jsonNode, entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys); ignoreKeys, ignoreStartWithKeys);
return entityTypeDefinition; return entityTypeDefinition;
} }
@ -174,7 +174,7 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
this.element = oDatabaseDocument.newVertex(elementType); this.element = oDatabaseDocument.newVertex(elementType);
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true)); logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true));

View File

@ -91,7 +91,7 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
protected OElement reallyUpdate() throws NotFoundException, ResourceRegistryException { protected OElement reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", PropertyType.NAME, getName()); logger.debug("Going to update {} for {}", PropertyType.NAME, getName());
OElement propertyTypeDefinition = getElement(); OElement propertyTypeDefinition = getElement();
propertyTypeDefinition = (OElement) ElementManagement.updateProperties(oClass, propertyTypeDefinition, jsonNode, propertyTypeDefinition = updateProperties(oClass, propertyTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys); ignoreKeys, ignoreStartWithKeys);
return propertyTypeDefinition; return propertyTypeDefinition;
} }
@ -166,7 +166,7 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
try { try {
this.element = new ODocument(elementType); this.element = new ODocument(elementType);
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
logger.debug("Created {} is {}", PropertyType.NAME, Utility.toJsonString(element, true)); logger.debug("Created {} is {}", PropertyType.NAME, Utility.toJsonString(element, true));

View File

@ -105,7 +105,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
element = oDatabaseDocument.newEdge(source, target, elementType); element = oDatabaseDocument.newEdge(source, target, elementType);
ElementManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
return element; return element;
} }
@ -114,7 +114,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
protected OEdge reallyUpdate() throws NotFoundException, ResourceRegistryException { protected OEdge reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", RelationType.NAME, getName()); logger.debug("Going to update {} for {}", RelationType.NAME, getName());
OEdge relationTypeDefinition = getElement(); OEdge relationTypeDefinition = getElement();
relationTypeDefinition = (OEdge) ElementManagement.updateProperties(oClass, relationTypeDefinition, jsonNode, relationTypeDefinition = (OEdge) updateProperties(oClass, relationTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys); ignoreKeys, ignoreStartWithKeys);
return relationTypeDefinition; return relationTypeDefinition;

View File

@ -443,7 +443,7 @@ public class BasicTest extends ContextTest {
try { try {
ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true);
}catch (ResourceRegistryException e) { }catch (ResourceRegistryException e) {
logger.debug("As expected", e); logger.debug("As expected {}", e.getMessage());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -456,7 +456,7 @@ public class BasicTest extends ContextTest {
ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true); ERManagementUtility.addToContextNoPropagationConstraint(expectedInstances, contextUUID, true);
}catch (ResourceRegistryException e) { }catch (ResourceRegistryException e) {
// as expected // as expected
logger.debug("As expected", e); logger.debug("As expected {}", e.getMessage());
}finally { }finally {
resourceManagement.delete(); resourceManagement.delete();
} }