Improving code

This commit is contained in:
Luca Frosini 2021-02-19 19:32:23 +01:00
parent a0ffe74776
commit 44631557da
15 changed files with 372 additions and 336 deletions

View File

@ -4,7 +4,6 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
@ -89,9 +88,9 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
public String getName() {
if(name == null) {
if(element == null) {
if(jsonNode != null) {
if (name == null) {
if (element == null) {
if (jsonNode != null) {
name = jsonNode.get(Context.NAME_PROPERTY).asText();
}
} else {
@ -103,7 +102,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID);
}
@ -123,7 +122,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
protected void checkContext(ContextManagement parentContext)
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
if(parentContext != null) {
if (parentContext != null) {
String parentId = parentContext.getElement().getIdentity().toString();
String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + ") FROM " + parentId
@ -142,18 +141,17 @@ public class ContextManagement extends EntityElementManagement<Context> {
OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>());
if(resultSet != null && resultSet.hasNext()) {
if (resultSet != null && resultSet.hasNext()) {
throw new ContextAlreadyPresentException(message.toString());
}
} else {
String select = "SELECT FROM " + Context.NAME + " WHERE "
+ Context.NAME_PROPERTY + " = \"" + getName() + "\"" + " AND in(\"" + IsParentOf.NAME
+ "\").size() = 0";
String select = "SELECT FROM " + Context.NAME + " WHERE " + Context.NAME_PROPERTY + " = \"" + getName()
+ "\"" + " AND in(\"" + IsParentOf.NAME + "\").size() = 0";
OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>());
if(resultSet != null && resultSet.hasNext()) {
if (resultSet != null && resultSet.hasNext()) {
throw new ContextAlreadyPresentException(
"A root context with the same name (" + this.getName() + ") already exist");
}
@ -169,35 +167,35 @@ public class ContextManagement extends EntityElementManagement<Context> {
int count = 0;
Iterable<OEdge> parents = getElement().getEdges(ODirection.IN);
for(OEdge edge : parents) {
if(++count > 1) {
for (OEdge edge : parents) {
if (++count > 1) {
throw new ContextException("A " + Context.NAME + " can not have more than one parent");
}
try {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
isParentOfManagement.setElement(edge);
JsonNode isParentOf = isParentOfManagement.createCompleteJsonNode(true, false);
if(isParentOf!=null) {
if (isParentOf != null) {
((ObjectNode) context).replace(Context.PARENT_PROPERTY, isParentOf);
}
} catch(Exception e) {
} catch (Exception e) {
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ContextException("");
}
}
Iterable<OEdge> childrenEdges = getElement().getEdges(ODirection.OUT);
for(OEdge edge : childrenEdges) {
for (OEdge edge : childrenEdges) {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
isParentOfManagement.setElement(edge);
try {
JsonNode isParentOf = isParentOfManagement.serializeAsJsonNode();
context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY);
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw e;
} catch(Exception e) {
} catch (Exception e) {
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(e);
}
@ -214,7 +212,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
try {
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
if(isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
JsonNode parentJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
ContextManagement parentContextManagement = new ContextManagement(oDatabaseDocument);
@ -222,9 +220,8 @@ public class ContextManagement extends EntityElementManagement<Context> {
UUID parentUUID = parentContextManagement.uuid;
parentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(parentUUID);
checkContext(parentContextManagement);
if(uuid == null) {
if (uuid == null) {
uuid = UUID.randomUUID();
}
@ -249,11 +246,11 @@ public class ContextManagement extends EntityElementManagement<Context> {
ContextUtility.getInstance().addSecurityContext(securityContext);
return getElement();
} catch(Exception e) {
} catch (Exception e) {
oDatabaseDocument.rollback();
if(securityContext != null) {
if (securityContext != null) {
securityContext.delete(oDatabaseDocument);
if(parentSecurityContext!=null && securityContext!=null) {
if (parentSecurityContext != null && securityContext != null) {
parentSecurityContext.getChildren().remove(securityContext);
}
ContextCache.getInstance().cleanCache();
@ -272,8 +269,8 @@ public class ContextManagement extends EntityElementManagement<Context> {
boolean found = false;
Iterable<OVertex> iterable = getElement().getVertices(ODirection.IN, IsParentOf.NAME);
for(OVertex p : iterable) {
if(found) {
for (OVertex p : iterable) {
if (found) {
String message = String.format("{} has more than one parent. {}", Context.NAME,
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
throw new ResourceRegistryException(message.toString());
@ -283,7 +280,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
ContextManagement actualParentContextManagement = null;
if(parent != null) {
if (parent != null) {
actualParentContextManagement = new ContextManagement(oDatabaseDocument);
actualParentContextManagement.setElement(parent);
}
@ -292,26 +289,26 @@ public class ContextManagement extends EntityElementManagement<Context> {
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
JsonNode parentContextJsonNode = null;
if(isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
if (isParentOfJsonNode != null && !(isParentOfJsonNode instanceof NullNode)) {
parentContextJsonNode = isParentOfJsonNode.get(Relation.SOURCE_PROPERTY);
}
if(parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) {
if (parentContextJsonNode != null && !(parentContextJsonNode instanceof NullNode)) {
UUID parentUUID = org.gcube.informationsystem.utils.Utility.getUUIDFromJsonNode(parentContextJsonNode);
if(actualParentContextManagement != null) {
if(parentUUID.compareTo(actualParentContextManagement.uuid) != 0) {
if (actualParentContextManagement != null) {
if (parentUUID.compareTo(actualParentContextManagement.uuid) != 0) {
parentChanged = true;
}
} else {
parentChanged = true;
}
if(parentChanged) {
if (parentChanged) {
newParentContextManagement = new ContextManagement(oDatabaseDocument);
newParentContextManagement.setJsonNode(parentContextJsonNode);
}
} else {
if(actualParentContextManagement != null) {
if (actualParentContextManagement != null) {
parentChanged = true;
newParentContextManagement = null;
}
@ -320,21 +317,20 @@ public class ContextManagement extends EntityElementManagement<Context> {
String oldName = getElement().getProperty(Context.NAME_PROPERTY);
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
if(oldName.compareTo(newName) != 0) {
if (oldName.compareTo(newName) != 0) {
nameChanged = true;
name = newName;
}
if(parentChanged || nameChanged) {
if (parentChanged || nameChanged) {
checkContext(newParentContextManagement);
}
if(parentChanged) {
if (parentChanged) {
move(newParentContextManagement, false);
}
element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys,
ignoreStartWithKeys);
element = (OVertex) updateProperties(oClass, getElement(), jsonNode, ignoreKeys, ignoreStartWithKeys);
ContextCache.getInstance().cleanCache();
@ -343,7 +339,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
private void move(ContextManagement newParentContextManagement, boolean check)
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
if(check) {
if (check) {
checkContext(newParentContextManagement);
}
@ -351,27 +347,28 @@ public class ContextManagement extends EntityElementManagement<Context> {
// Removing the old parent relationship if any
Iterable<OEdge> edges = getElement().getEdges(ODirection.IN, IsParentOf.NAME);
if(edges != null && edges.iterator().hasNext()) {
if (edges != null && edges.iterator().hasNext()) {
Iterator<OEdge> edgeIterator = edges.iterator();
OEdge edge = edgeIterator.next();
IsParentOfManagement isParentOfManagement = new IsParentOfManagement();
isParentOfManagement.setElement(edge);
isParentOfManagement.internalDelete();
if(edgeIterator.hasNext()) {
if (edgeIterator.hasNext()) {
throw new ContextException(
"Seems that the Context has more than one Parent. " + Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
if(newParentContextManagement != null) {
if (newParentContextManagement != null) {
JsonNode isParentOfJsonNode = jsonNode.get(Context.PARENT_PROPERTY);
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(oDatabaseDocument);
isParentOfManagement.setJsonNode(isParentOfJsonNode);
isParentOfManagement.setSourceEntityManagement(newParentContextManagement);
isParentOfManagement.setTargetEntityManagement(this);
isParentOfManagement.internalCreate();
newParentSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(newParentContextManagement.uuid);
newParentSecurityContext = ContextUtility.getInstance()
.getSecurityContextByUUID(newParentContextManagement.uuid);
}
SecurityContext thisSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(uuid);
@ -382,7 +379,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
protected boolean reallyDelete() throws NotFoundException, ResourceRegistryException {
Iterable<OEdge> iterable = getElement().getEdges(ODirection.OUT);
Iterator<OEdge> iterator = iterable.iterator();
while(iterator.hasNext()) {
while (iterator.hasNext()) {
throw new ContextException("Cannot remove a " + Context.NAME + " having children");
}
@ -403,20 +400,20 @@ public class ContextManagement extends EntityElementManagement<Context> {
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode();
Iterable<ODocument> iterable = oDatabaseDocument.browseClass(typeName, polymorphic);
for(ODocument vertex : iterable) {
for (ODocument vertex : iterable) {
ContextManagement contextManagement = new ContextManagement();
contextManagement.setElement((OVertex) vertex);
try {
JsonNode jsonObject = contextManagement.serializeAsJsonNode();
arrayNode.add(jsonObject);
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
vertex.toString(), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
}
}
try {
return objectMapper.writeValueAsString(arrayNode);
} catch(JsonProcessingException e) {
} catch (JsonProcessingException e) {
throw new ResourceRegistryException(e);
}
}
@ -435,11 +432,13 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
}
public String readFromServer() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
public String readFromServer()
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
return super.read().toString();
}
public String readAsString() throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
public String readAsString()
throws NotFoundException, AvailableInAnotherContextException, ResourceRegistryException {
try {
ContextCache contextCache = ContextCache.getInstance();
return ElementMapper.marshal(contextCache.getContextByUUID(uuid));
@ -449,8 +448,7 @@ public class ContextManagement extends EntityElementManagement<Context> {
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}
}

View File

@ -1,8 +1,5 @@
package org.gcube.informationsystem.resourceregistry.contexts.relations;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -27,7 +24,7 @@ import com.orientechnologies.orient.core.record.OVertex;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class IsParentOfManagement extends RelationElementManagement<ContextManagement,ContextManagement> {
public class IsParentOfManagement extends RelationElementManagement<ContextManagement, ContextManagement> {
public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF, Context.class, Context.class);
@ -41,7 +38,7 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.CONTEXT_SECURITY_CONTEXT_UUID);
}
@ -64,28 +61,31 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
}
@Override
public JsonNode createCompleteJsonNode(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
public JsonNode createCompleteJsonNode(boolean includeSource, boolean includeTarget)
throws ResourceRegistryException {
JsonNode relation = serializeSelfAsJsonNode();
try {
OVertex source = element.getVertex(ODirection.OUT);
ContextManagement sourceContextManagement = new ContextManagement(oDatabaseDocument);
sourceContextManagement.setElement(source);
if(includeSource) {
((ObjectNode)relation).replace(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfAsJsonNode());
if (includeSource) {
((ObjectNode) relation).replace(Relation.SOURCE_PROPERTY,
sourceContextManagement.serializeSelfAsJsonNode());
}
OVertex target = element.getVertex(ODirection.IN);
ContextManagement targetContextManagement = new ContextManagement(oDatabaseDocument);
targetContextManagement.setElement(target);
if(includeTarget) {
((ObjectNode)relation).replace(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfAsJsonNode());
if (includeTarget) {
((ObjectNode) relation).replace(Relation.TARGET_PROPERTY,
targetContextManagement.serializeSelfAsJsonNode());
}
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw e;
} catch(Exception e) {
} catch (Exception e) {
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
throw new ResourceRegistryException(e);
}
@ -109,8 +109,7 @@ public class IsParentOfManagement extends RelationElementManagement<ContextManag
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}

View File

@ -448,7 +448,9 @@ public abstract class ElementManagement<El extends OElement> {
public abstract String reallyGetAll(boolean polymorphic) throws ResourceRegistryException;
public abstract void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException;
public abstract void sanityCheck() throws SchemaViolationException, ResourceRegistryException;
//public abstract void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException;
public String all(boolean polymorphic) throws ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
@ -513,7 +515,7 @@ public abstract class ElementManagement<El extends OElement> {
element = internalCreate();
}
sanityCheck(null);
sanityCheck();
oDatabaseDocument.commit();
@ -556,7 +558,7 @@ public abstract class ElementManagement<El extends OElement> {
element = internalCreate();
sanityCheck(null);
sanityCheck();
oDatabaseDocument.commit();
@ -621,7 +623,7 @@ public abstract class ElementManagement<El extends OElement> {
element = internalUpdate();
sanityCheck(null);
sanityCheck();
oDatabaseDocument.commit();

View File

@ -33,7 +33,8 @@ public interface ERManagement {
public Map<UUID, JsonNode> removeFromContext(UUID contextUUID)
throws SchemaViolationException, NotFoundException, ContextException, ResourceRegistryException;
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException;
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException;
//public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException;
public AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message);

View File

@ -10,7 +10,6 @@ import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.rest.SharingPath.SharingOperation;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
@ -39,6 +38,7 @@ public class ERManagementUtility {
SecurityContext targetSecurityContext = ContextUtility.getInstance().getSecurityContextByUUID(contextUUID);
Map<UUID, ElementManagement<?>> instancesManagement = new HashMap<>();
for(UUID uuid : expectedInstances.keySet()) {
String type = expectedInstances.get(uuid).get(Element.CLASS_PROPERTY).asText();
ElementManagement<?> elementManagement = ElementManagementUtility.getERManagement(type);
@ -49,7 +49,13 @@ public class ERManagementUtility {
((ERManagement) elementManagement).setHonourPropagationConstraintsInContextSharing(false);
((ERManagement) elementManagement).setDryRunContextSharing(dryRun);
((ERManagement) elementManagement).internalAddToContext(targetSecurityContext);
((ERManagement) elementManagement).sanityCheck(expectedInstances);
instancesManagement.put(uuid, elementManagement);
}
for(UUID uuid : expectedInstances.keySet()) {
ElementManagement<?> elementManagement = instancesManagement.get(uuid);
// TODO
elementManagement.sanityCheck();
}
/*
@ -120,14 +126,16 @@ public class ERManagementUtility {
affectedInstances.putAll(((ERManagement) elementManagement).internalRemoveFromContext(targetSecurityContext));
((ERManagement) elementManagement).sanityCheck(expectedInstances);
((ERManagement) elementManagement).sanityCheck();
}
/*
SharingOperationValidator operationValidator = new SharingOperationValidator(expectedInstances, SharingOperation.REMOVE);
if(operationValidator.isValidOperation(affectedInstances)) {
oDatabaseDocument.commit();
}
*/
staticLogger.info("{} successfully removed from Context with UUID {} not following Propagation Constraints", instances, contextUUID);

View File

@ -1,8 +1,5 @@
package org.gcube.informationsystem.resourceregistry.instances.model.entities;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.entities.Facet;
@ -63,7 +60,7 @@ public class FacetManagement extends EntityManagement<Facet> {
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws ResourceRegistryException {
public void sanityCheck() throws ResourceRegistryException {
// TODO Auto-generated method stub
}

View File

@ -12,11 +12,9 @@ import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
@ -311,25 +309,6 @@ public class ResourceManagement extends EntityManagement<Resource> {
}
}
private String facetMustBePresentErrorMessage(String consistsOfType, UUID consistsOfUUID, String facetType, UUID facetUUID) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a ");
stringBuffer.append(ConsistsOf.NAME);
stringBuffer.append(" relation (i.e. ");
stringBuffer.append(consistsOfType);
stringBuffer.append(" with UUID ");
stringBuffer.append(consistsOfUUID.toString());
stringBuffer.append(") without indicating the target ");
stringBuffer.append(Facet.NAME);
stringBuffer.append(" (i.e. ");
stringBuffer.append(facetType);
stringBuffer.append(" with UUID ");
stringBuffer.append(facetUUID.toString());
stringBuffer.append(").");
return stringBuffer.toString();
}
protected Set<LinkedEntity> getResourceTypeConstraint() throws SchemaException, ResourceRegistryException{
Set<LinkedEntity> constraints = new HashSet<>();
@ -420,6 +399,76 @@ public class ResourceManagement extends EntityManagement<Resource> {
return true;
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
JsonNode resourceInstance = createCompleteJsonNode();
TypesCache typesCache = TypesCache.getInstance();
Set<LinkedEntity> consistsOfFacetConstraints = getResourceTypeConstraint();
Map<LinkedEntity, Integer> satisfiedConsistsOfFacet = new HashMap<>();
ArrayNode consistsOfArrayNode = (ArrayNode) resourceInstance.get(Resource.CONSISTS_OF_PROPERTY);
for(JsonNode consistsOfJsonNode : consistsOfArrayNode) {
String consistsOfType = consistsOfJsonNode.get(Element.CLASS_PROPERTY).asText();
JsonNode facetJsonNode = consistsOfJsonNode.get(Relation.TARGET_PROPERTY);
String facetType = facetJsonNode.get(Element.CLASS_PROPERTY).asText();
for(LinkedEntity constraint : consistsOfFacetConstraints) {
if(constraintSatisfied(typesCache, constraint, consistsOfType, facetType)) {
Integer integer = satisfiedConsistsOfFacet.get(constraint);
if(integer==null) {
satisfiedConsistsOfFacet.put(constraint, 1);
}else {
satisfiedConsistsOfFacet.put(constraint, ++integer);
}
}
}
}
consistsOfFacetConstraints.removeAll(satisfiedConsistsOfFacet.keySet());
if(!consistsOfFacetConstraints.isEmpty()) {
String message = constraintNotSatisfiedErrorMessage(consistsOfFacetConstraints.iterator().next(), 0);
throw new SchemaViolationException(message);
}
for(LinkedEntity linkedEntity : satisfiedConsistsOfFacet.keySet()) {
Integer satisfiedTimes = satisfiedConsistsOfFacet.get(linkedEntity);
if(satisfiedTimes<linkedEntity.getMin()) {
String message = constraintNotSatisfiedErrorMessage(linkedEntity, satisfiedTimes);
throw new SchemaViolationException(message);
}
Integer max = linkedEntity.getMax();
if((max!=null && max>0) && satisfiedTimes>max) {
String message = constraintNotSatisfiedErrorMessage(linkedEntity, satisfiedTimes);
throw new SchemaViolationException(message);
}
}
}
/*
private String facetMustBePresentErrorMessage(String consistsOfType, UUID consistsOfUUID, String facetType, UUID facetUUID) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("To avoid to have an incosistent graph, add to context no follows cannot add a ");
stringBuffer.append(ConsistsOf.NAME);
stringBuffer.append(" relation (i.e. ");
stringBuffer.append(consistsOfType);
stringBuffer.append(" with UUID ");
stringBuffer.append(consistsOfUUID.toString());
stringBuffer.append(") without indicating the target ");
stringBuffer.append(Facet.NAME);
stringBuffer.append(" (i.e. ");
stringBuffer.append(facetType);
stringBuffer.append(" with UUID ");
stringBuffer.append(facetUUID.toString());
stringBuffer.append(").");
return stringBuffer.toString();
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException {
JsonNode resourceInstance = createCompleteJsonNode();
@ -519,7 +568,6 @@ public class ResourceManagement extends EntityManagement<Resource> {
throw new SchemaViolationException(stringBuffer.toString());
}
}
*/
}

View File

@ -1,9 +1,5 @@
package org.gcube.informationsystem.resourceregistry.instances.model.relations;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
@ -60,7 +56,7 @@ public class ConsistsOfManagement extends RelationManagement<FacetManagement> {
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// TODO Auto-generated method stub
}

View File

@ -1,9 +1,5 @@
package org.gcube.informationsystem.resourceregistry.instances.model.relations;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl;
import org.gcube.informationsystem.model.reference.entities.Resource;
@ -60,7 +56,7 @@ public class IsRelatedToManagement extends RelationManagement<ResourceManagement
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances) throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// TODO Auto-generated method stub
}

View File

@ -1,8 +1,6 @@
package org.gcube.informationsystem.resourceregistry.types.entities;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -44,7 +42,7 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
if (workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
@ -56,9 +54,9 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
}
public String getName() {
if(name == null) {
if(element == null) {
if(jsonNode != null) {
if (name == null) {
if (element == null) {
if (jsonNode != null) {
name = jsonNode.get(EntityType.NAME_PROPERTY).asText();
}
} else {
@ -83,8 +81,8 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", this.typeName, getName());
OVertex entityTypeDefinition = getElement();
entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys);
entityTypeDefinition = (OVertex) updateProperties(oClass, entityTypeDefinition, jsonNode, ignoreKeys,
ignoreStartWithKeys);
return entityTypeDefinition;
}
@ -97,19 +95,19 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
@Override
public OVertex getElement() throws NotFoundException, ResourceRegistryException {
if(element == null) {
if (element == null) {
try {
element = retrieveElement();
} catch(NotFoundException e) {
} catch (NotFoundException e) {
throw e;
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
} else {
if(reload) {
if (reload) {
element.reload();
}
}
@ -119,16 +117,16 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
@Override
public OVertex retrieveElement() throws NotFoundException, ResourceRegistryException {
try {
if(getName() == null) {
if (getName() == null) {
throw new NotFoundException("null name does not allow to retrieve the Element");
}
String select = "SELECT FROM " + typeName + " WHERE " + EntityType.NAME_PROPERTY + " = \""
+ getName() + "\"";
String select = "SELECT FROM " + typeName + " WHERE " + EntityType.NAME_PROPERTY + " = \"" + getName()
+ "\"";
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>());
if(resultSet == null || !resultSet.hasNext()) {
if (resultSet == null || !resultSet.hasNext()) {
String error = String.format("No %s with name %s was found", typeName, getName());
logger.info(error);
throw new NotFoundException(error);
@ -139,17 +137,17 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
logger.trace("{} with id {} is : {}", typeName, getName(), Utility.toJsonString(element, true));
if(resultSet.hasNext()) {
if (resultSet.hasNext()) {
throw new ResourceRegistryException("Found more than one " + typeName + " with name " + getName()
+ ". This is a fatal error please contact Admnistrator");
}
return element;
} catch(NotFoundException e) {
} catch (NotFoundException e) {
throw getSpecificElementNotFoundException(e);
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@ -169,9 +167,9 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true));
return element;
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}", OVertex.class.getSimpleName(),
accessType.getName(), typeName, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
@ -194,8 +192,7 @@ public abstract class EntityTypeDefinitionManagement<E extends EntityType> exten
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}

View File

@ -1,9 +1,5 @@
package org.gcube.informationsystem.resourceregistry.types.entities;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.types.reference.entities.ResourceType;
@ -18,8 +14,7 @@ public class ResourceTypeDefinitionManagement extends EntityTypeDefinitionManage
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// TODO
// We should check that a constraint defined here is not in contrast with the constraint of supertypes
}

View File

@ -1,8 +1,6 @@
package org.gcube.informationsystem.resourceregistry.types.properties;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
@ -195,8 +193,8 @@ public class PropertyTypeDefinitionManagement extends ElementManagement<OElement
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}
}

View File

@ -1,6 +1,7 @@
package org.gcube.informationsystem.resourceregistry.types.relations;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.types.entities.FacetTypeDefinitionManagement;
import org.gcube.informationsystem.types.reference.entities.FacetType;
@ -33,4 +34,9 @@ public class ConsistsOfTypeDefinitionManagement
return ftdm;
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}
}

View File

@ -1,6 +1,7 @@
package org.gcube.informationsystem.resourceregistry.types.relations;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.types.entities.ResourceTypeDefinitionManagement;
import org.gcube.informationsystem.types.reference.entities.ResourceType;
@ -33,4 +34,8 @@ public class IsRelatedToTypeDefinitionManagement
return rtdm;
}
@Override
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}
}

View File

@ -1,10 +1,7 @@
package org.gcube.informationsystem.resourceregistry.types.relations;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
@ -13,7 +10,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaViolationException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
@ -45,7 +41,8 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
this.typeName = RelationType.NAME;
}
public RelationTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument, Class<TT> clz) throws ResourceRegistryException {
public RelationTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument,
Class<TT> clz) throws ResourceRegistryException {
this(clz);
this.oDatabaseDocument = oDatabaseDocument;
setWorkingContext(securityContext);
@ -53,7 +50,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
if (workingContext == null) {
this.workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
@ -65,9 +62,9 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
}
public String getName() {
if(name == null) {
if(element == null) {
if(jsonNode != null) {
if (name == null) {
if (element == null) {
if (jsonNode != null) {
name = jsonNode.get(RelationType.NAME_PROPERTY).asText();
}
} else {
@ -80,8 +77,8 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
@Override
protected OEdge reallyCreate() throws ResourceRegistryException {
logger.debug("Going to create {} for {}", RelationType.NAME, getName());
if(sourceEntityManagement == null) {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)) {
if (sourceEntityManagement == null) {
if (!jsonNode.has(Relation.SOURCE_PROPERTY)) {
throw new ResourceRegistryException("Error while creating relation. No source definition found");
}
@ -90,8 +87,8 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
sourceEntityManagement.setJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
}
if(targetEntityManagement == null) {
if(!jsonNode.has(Relation.TARGET_PROPERTY)) {
if (targetEntityManagement == null) {
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
throw new ResourceRegistryException(
"Error while creating " + typeName + ". No target definition found");
}
@ -104,8 +101,7 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
OVertex source = (OVertex) getSourceEntityManagement().getElement();
OVertex target = (OVertex) getTargetEntityManagement().getElement();
logger.trace("Creating {} beetween {} -> {}", typeName, source.toString(),
target.toString());
logger.trace("Creating {} beetween {} -> {}", typeName, source.toString(), target.toString());
element = oDatabaseDocument.newEdge(source, target, typeName);
@ -118,8 +114,8 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
protected OEdge reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", RelationType.NAME, getName());
OEdge relationTypeDefinition = getElement();
relationTypeDefinition = (OEdge) updateProperties(oClass, relationTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys);
relationTypeDefinition = (OEdge) updateProperties(oClass, relationTypeDefinition, jsonNode, ignoreKeys,
ignoreStartWithKeys);
return relationTypeDefinition;
}
@ -133,19 +129,19 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
@Override
public OEdge getElement() throws NotFoundException, ResourceRegistryException {
if(element == null) {
if (element == null) {
try {
element = retrieveElement();
} catch(NotFoundException e) {
} catch (NotFoundException e) {
throw e;
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
} else {
if(reload) {
if (reload) {
element.reload();
}
}
@ -155,16 +151,16 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
@Override
public OEdge retrieveElement() throws NotFoundException, ResourceRegistryException {
try {
if(getName() == null) {
if (getName() == null) {
throw new NotFoundException("null name does not allow to retrieve the Element");
}
String select = "SELECT FROM " + typeName + " WHERE " + RelationType.NAME_PROPERTY + " = \""
+ getName() + "\"";
String select = "SELECT FROM " + typeName + " WHERE " + RelationType.NAME_PROPERTY + " = \"" + getName()
+ "\"";
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>());
if(resultSet == null || !resultSet.hasNext()) {
if (resultSet == null || !resultSet.hasNext()) {
String error = String.format("No %s with name %s was found", typeName, getName());
logger.info(error);
throw new NotFoundException(error);
@ -175,17 +171,17 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
logger.trace("{} with id {} is : {}", typeName, getName(), Utility.toJsonString(element, true));
if(resultSet.hasNext()) {
if (resultSet.hasNext()) {
throw new ResourceRegistryException("Found more than one " + typeName + " with name " + getName()
+ ". This is a fatal error please contact Admnistrator");
}
return element;
} catch(NotFoundException e) {
} catch (NotFoundException e) {
throw getSpecificElementNotFoundException(e);
} catch(ResourceRegistryException e) {
} catch (ResourceRegistryException e) {
throw e;
} catch(Exception e) {
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
}
@ -213,10 +209,4 @@ public abstract class RelationTypeDefinitionManagement<T extends EntityTypeDefin
return rtdm;
}
@Override
public void sanityCheck(Map<UUID, JsonNode> expectedInstances)
throws SchemaViolationException, ResourceRegistryException {
// Nothing to do
}
}