Reorganized utilities and their usage

This commit is contained in:
Luca Frosini 2023-04-21 15:56:52 +02:00
parent 8f67beb172
commit 40b21420a8
9 changed files with 40 additions and 53 deletions

View File

@ -37,6 +37,7 @@ import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.gcube.informationsystem.utils.UUIDManager;
import org.gcube.informationsystem.utils.UUIDUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -235,7 +236,7 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
checkContext(parentContextManagement);
if (uuid == null) {
uuid = UUIDManager.getInstance().generateValidRandomUUID();
uuid = UUIDManager.getInstance().generateValidUUID();
}
createVertex();
@ -307,7 +308,7 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
}
if (parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) {
UUID parentUUID = org.gcube.informationsystem.utils.Utility.getUUIDFromJsonNode(parentContextJsonNode);
UUID parentUUID = UUIDUtility.getUUID(parentContextJsonNode);
if (actualParentContextManagement != null) {
if (parentUUID.compareTo(actualParentContextManagement.uuid) != 0) {
parentChanged = true;

View File

@ -323,13 +323,21 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
protected void checkJsonNode() throws ResourceRegistryException {
if(uuid == null) {
try {
uuid = org.gcube.informationsystem.utils.Utility.getUUIDFromJsonNode(jsonNode);
uuid = UUIDUtility.getUUID(jsonNode);
} catch(Exception e) {
}
} else {
checkUUIDMatch();
}
if(uuid!=null) {
try {
UUIDManager.getInstance().validateUUID(uuid);
} catch (Exception e) {
throw new ResourceRegistryException(e.getMessage());
}
}
if(this.typeName == null) {
this.typeName = getClassProperty(jsonNode);
getOClass();
@ -351,9 +359,11 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
getOClass();
}
protected void checkUUIDMatch() throws ResourceRegistryException {
if(jsonNode.has(IdentifiableElement.UUID_PROPERTY)) {
UUID resourceUUID = UUID.fromString(jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText());
UUID resourceUUID = UUIDUtility.getUUID(jsonNode);
if(resourceUUID.compareTo(uuid) != 0) {
String error = String.format(
"UUID provided in the instance (%s) differs from UUID (%s) used to identify the %s instance",
@ -436,9 +446,7 @@ public abstract class ElementManagement<El extends OElement, T extends Type> {
UUIDManager uuidManager = UUIDManager.getInstance();
if(uuid == null) {
uuid = uuidManager.generateValidRandomUUID();
}else {
uuid = uuidManager.validateUUID(uuid);
uuid = uuidManager.generateValidUUID();
}
element = reallyCreate();

View File

@ -19,6 +19,7 @@ import org.gcube.informationsystem.resourceregistry.instances.base.entities.Enti
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.gcube.informationsystem.types.reference.relations.RelationType;
import org.gcube.informationsystem.utils.UUIDUtility;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.ODirection;
@ -143,8 +144,7 @@ public abstract class RelationElementManagement<SEM extends EntityElementManagem
throw new ResourceRegistryException("Error while creating relation. No source definition found");
}
UUID sourceUUID = org.gcube.informationsystem.utils.Utility
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
UUID sourceUUID = UUIDUtility.getUUID(jsonNode.get(Relation.SOURCE_PROPERTY));
sourceEntityManagement = newSourceEntityManagement();
sourceEntityManagement.setUUID(sourceUUID);

View File

@ -2,39 +2,16 @@ package org.gcube.informationsystem.resourceregistry.utils;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.utils.UUIDManager;
import com.orientechnologies.orient.core.record.OElement;
public class UUIDUtility {
public static UUID getUUID(JsonNode jsonNode) throws ResourceRegistryException {
if(jsonNode.has(IdentifiableElement.UUID_PROPERTY)) {
String uuidString = jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText();
return getUUID(uuidString);
}
return null;
}
public class UUIDUtility extends org.gcube.informationsystem.utils.UUIDUtility {
public static UUID getUUID(OElement element) throws ResourceRegistryException {
String uuidString = element.getProperty(IdentifiableElement.UUID_PROPERTY);
UUID uuid = getUUID(uuidString);
return uuid;
}
public static UUID getUUID(String uuidString) throws ResourceRegistryException {
UUIDManager uuidManager = UUIDManager.getInstance();
UUID uuid = null;
if(uuidString != null) {
if(!uuidManager.isReservedUUID(uuidString)) {
uuid = UUID.fromString(uuidString);
}else {
throw new ResourceRegistryException(uuidString + " UUID is reserved. All the following UUID are reserved " + uuidManager.getAllReservedUUIDAsStrings().toString());
}
}
UUID uuid = UUID.fromString(uuidString);
return uuid;
}

View File

@ -30,13 +30,13 @@ import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
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.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.TypeUtility;
import org.gcube.resourcemanagement.model.impl.entities.facets.AccessPointFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.EventFacetImpl;
@ -328,7 +328,7 @@ public class ERManagementTest extends ContextTest {
public static <R extends Resource> ResourceManagement getResourceManagement(R r) throws Exception {
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Utility.getTypeName(r));
resourceManagement.setElementType(TypeUtility.getTypeName(r));
resourceManagement.setJson(ElementMapper.marshal(r));
if(r.getUUID()!=null) {
resourceManagement.setUUID(r.getUUID());
@ -338,7 +338,7 @@ public class ERManagementTest extends ContextTest {
public static <R extends Resource> IsRelatedToManagement getIsRelatedToManagement(IsRelatedTo<? extends Resource, ? extends Resource> isRelatedTo) throws Exception {
IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setElementType(Utility.getTypeName(isRelatedTo));
isRelatedToManagement.setElementType(TypeUtility.getTypeName(isRelatedTo));
isRelatedToManagement.setJson(ElementMapper.marshal(isRelatedTo));
if(isRelatedTo.getUUID()!=null) {
isRelatedToManagement.setUUID(isRelatedTo.getUUID());
@ -472,7 +472,7 @@ public class ERManagementTest extends ContextTest {
ConsistsOf<EService, CPUFacet> consistsOf = new ConsistsOfImpl<EService, CPUFacet>(eService, cpuFacet);
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
consistsOfManagement.setElementType(Utility.getTypeName(consistsOf));
consistsOfManagement.setElementType(TypeUtility.getTypeName(consistsOf));
consistsOfManagement.setJson(ElementMapper.marshal(consistsOf));
String createdConsistsOfString = consistsOfManagement.create();
@ -488,7 +488,7 @@ public class ERManagementTest extends ContextTest {
UUID uuid = createdCpuFacet.getUUID();
FacetManagement facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(createdCpuFacet));
facetManagement.setElementType(TypeUtility.getTypeName(createdCpuFacet));
facetManagement.setUUID(uuid);
String readJson = facetManagement.read().toString();
@ -509,7 +509,7 @@ public class ERManagementTest extends ContextTest {
readCpuFacet.setAdditionalProperty(additionPropertyKey, additionPropertyValue);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(readCpuFacet));
facetManagement.setElementType(TypeUtility.getTypeName(readCpuFacet));
facetManagement.setUUID(uuid);
facetManagement.setJson(ElementMapper.marshal(readCpuFacet));
@ -526,7 +526,7 @@ public class ERManagementTest extends ContextTest {
Assert.assertTrue(updatedCpuFacet.getMetadata().getLastUpdateBy().compareTo(user) == 0);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(updatedCpuFacet));
facetManagement.setElementType(TypeUtility.getTypeName(updatedCpuFacet));
facetManagement.setUUID(uuid);
String readUpdatedJson = facetManagement.read().toString();
@ -540,7 +540,7 @@ public class ERManagementTest extends ContextTest {
Assert.assertTrue(uuid.compareTo(updatedCpuFacet.getUUID()) == 0);
facetManagement = new FacetManagement();
facetManagement.setElementType(Utility.getTypeName(readCpuFacet));
facetManagement.setElementType(TypeUtility.getTypeName(readCpuFacet));
facetManagement.setUUID(uuid);
facetManagement.delete();

View File

@ -10,12 +10,12 @@ import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entities.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
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.relations.ConsistsOfManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.TypeUtility;
import org.gcube.resourcemanagement.model.impl.entities.facets.CPUFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimpleFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SoftwareFacetImpl;
@ -166,7 +166,7 @@ public class InvalidOperationTest extends ERManagementTest {
IsIdentifiedBy<EService, SoftwareFacet> isIdentifiedBy = (IsIdentifiedBy<EService, SoftwareFacet>) eService.getConsistsOf(IsIdentifiedBy.class).get(0);
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement();
consistsOfManagement.setElementType(Utility.getTypeName(isIdentifiedBy));
consistsOfManagement.setElementType(TypeUtility.getTypeName(isIdentifiedBy));
consistsOfManagement.setUUID(isIdentifiedBy.getUUID());
consistsOfManagement.delete();
}finally {

View File

@ -90,7 +90,7 @@ public class SmartgearResourcesTest extends ERManagementTest {
@Test
public void testHostingNodeOperations() throws ResourceRegistryException,
IOException, URISyntaxException {
UUID uuid = UUIDManager.getInstance().generateValidRandomUUID();
UUID uuid = UUIDManager.getInstance().generateValidUUID();
HostingNode hostingNode = new HostingNodeImpl();
hostingNode.setUUID(uuid);

View File

@ -12,12 +12,12 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
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.relations.IsRelatedToManagement;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.TypeUtility;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.ActivatesImpl;
import org.gcube.resourcemanagement.model.reference.entities.resources.EService;
import org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode;
@ -84,7 +84,7 @@ public class ComplexTest extends MultiContextTest {
for(Facet facet : hostingNode.getFacets()) {
FacetManagement facetManagement = new FacetManagement();
facetManagement.setUUID(facet.getUUID());
String typeName = Utility.getTypeName(facet.getClass());
String typeName = TypeUtility.getTypeName(facet.getClass());
facetManagement.setElementType(typeName);
String facetContexts = facetManagement.getContexts();
logger.debug("Contexts of {} with UUID {} have the following UUID {}", typeName, facetManagement.getUUID(), facetContexts);
@ -98,7 +98,7 @@ public class ComplexTest extends MultiContextTest {
activatesManagement = new IsRelatedToManagement();
activatesManagement.setUUID(createdActivates.getUUID());
activatesManagement.setElementType(Utility.getTypeName(createdActivates.getClass()));
activatesManagement.setElementType(TypeUtility.getTypeName(createdActivates.getClass()));
String isRelatedToContexts = activatesManagement.getContexts();
logger.debug("Contexts of {} with UUID {} have the following UUID {}", Activates.NAME, activatesManagement.getUUID(), isRelatedToContexts);
Map<UUID, String> activatesContextMap = org.gcube.informationsystem.resourceregistry.api.contexts.ContextUtility.getContextMap(isRelatedToContexts);

View File

@ -14,14 +14,15 @@ import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.resourceregistry.api.exceptions.queries.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.utils.Utility;
import org.gcube.informationsystem.resourceregistry.instances.ERManagementTest;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
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.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.serialization.ElementMapper;
import org.gcube.informationsystem.utils.TypeUtility;
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.facets.SimplePropertyFacetImpl;
import org.gcube.resourcemanagement.model.impl.entities.resources.ConfigurationImpl;
@ -346,10 +347,10 @@ public class QueryTest extends ERManagementTest {
}
try {
String parentResourceType = Utility.getTypeName(ConfigurationTemplate.class);
String resourceType = Utility.getTypeName(Configuration.class);
String relationType = Utility.getTypeName(IsIdentifiedBy.class);
String referenceType = Utility.getTypeName(IdentifierFacet.class);
String parentResourceType = TypeUtility.getTypeName(ConfigurationTemplate.class);
String resourceType = TypeUtility.getTypeName(Configuration.class);
String relationType = TypeUtility.getTypeName(IsIdentifiedBy.class);
String referenceType = TypeUtility.getTypeName(IdentifierFacet.class);
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(resourceType);