fixed type definitions creation

This commit is contained in:
Luca Frosini 2019-11-08 13:14:56 +01:00
parent f8427ea85b
commit 6bc15e3164
3 changed files with 136 additions and 121 deletions

View File

@ -2,8 +2,6 @@ package org.gcube.informationsystem.resourceregistry.instances.type.entities;
import java.util.HashMap; import java.util.HashMap;
import javax.ws.rs.NotAcceptableException;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
@ -50,6 +48,15 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
getWorkingContext(); getWorkingContext();
} }
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
return workingContext;
}
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@ -67,6 +74,39 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
return name; return name;
} }
@Override
public String serialize() throws ResourceRegistryException {
return serializeAsJson().toString();
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
return serializeSelfOnly();
// For Resources Definition include mandatory and suggested IsRelatedTo and ConsistsOf
}
@Override
protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
logger.debug("Going to create {} for {}", EntityTypeDefinition.NAME, getName());
return createVertex();
}
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", EntityTypeDefinition.NAME, getName());
OVertex entityTypeDefinition = getElement();
entityTypeDefinition = (OVertex) ERManagement.updateProperties(oClass, entityTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys);
return entityTypeDefinition;
}
@Override
protected boolean reallyDelete() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} for {}", EntityTypeDefinition.NAME, getName());
getElement().delete();
return true;
}
@Override @Override
public OVertex getElement() throws NotFoundException, ResourceRegistryException { public OVertex getElement() throws NotFoundException, ResourceRegistryException {
if(element == null) { if(element == null) {
@ -95,12 +135,11 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
throw new NotFoundException("null name does not allow to retrieve the Element"); throw new NotFoundException("null name does not allow to retrieve the Element");
} }
String select = "SELECT FROM " + elementType + " WHERE " + EntityTypeDefinition.NAME_PROPERTY String select = "SELECT FROM " + elementType + " WHERE " + EntityTypeDefinition.NAME_PROPERTY + " = \""
+ " = \"" + getName() + "\""; + getName() + "\"";
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>()); 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", elementType, getName()); String error = String.format("No %s with name %s was found", elementType, getName());
logger.info(error); logger.info(error);
@ -139,8 +178,7 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys); ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
logger.info("Created {} is {}", OVertex.class.getSimpleName(), logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true));
Utility.toJsonString(element, true));
return element; return element;
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
@ -153,12 +191,20 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
} }
@Override @Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException { public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
if(workingContext == null) { throw new UnsupportedOperationException();
workingContext = ContextUtility.getInstance() }
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
} @Override
return workingContext; protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
} }
@Override @Override
@ -166,62 +212,13 @@ public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityT
return new SchemaNotFoundException(e.getMessage(), e.getCause()); return new SchemaNotFoundException(e.getMessage(), e.getCause());
} }
@Override
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
throw new NotAcceptableException();
}
@Override @Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) { protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message); return new SchemaAlreadyPresentException(message);
} }
@Override @Override
public String serialize() throws ResourceRegistryException { protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
return serializeAsJson().toString();
}
@Override
public JsonNode serializeAsJson() throws ResourceRegistryException {
return serializeSelfOnly();
// For Resources Definition include mandatory and suggested IsRelatedTo and ConsistsOf
}
@Override
protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
logger.debug("Going to create {} for {}", EntityTypeDefinition.NAME, getName());
return createVertex();
}
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", EntityTypeDefinition.NAME, getName());
OVertex entityTypeDefinition = getElement();
entityTypeDefinition = (OVertex) ERManagement.updateProperties(oClass, entityTypeDefinition, jsonNode, ignoreKeys, ignoreStartWithKeys);
return entityTypeDefinition;
}
@Override
protected boolean reallyDelete() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} for {}", EntityTypeDefinition.NAME, getName());
getElement().delete();
return true;
}
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
throws ContextException, ResourceRegistryException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }

View File

@ -87,27 +87,12 @@ public class PropertyTypeDefinitionManagement extends ERManagement<OElement> {
@Override @Override
protected OElement reallyCreate() throws AlreadyPresentException, ResourceRegistryException { protected OElement reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
logger.debug("Going to create {} for {}", PropertyTypeDefinition.NAME, getName()); logger.debug("Going to create {} for {}", PropertyTypeDefinition.NAME, getName());
return createElement();
try {
this.element = new ODocument(elementType);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
logger.debug("Created {} is {}", PropertyTypeDefinition.NAME, Utility.toJsonString(element, true));
return element;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}", OElement.class.getSimpleName(),
accessType.getName(), elementType, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + elementType + " with " + jsonNode, e.getCause());
}
} }
@Override @Override
protected OElement reallyUpdate() throws NotFoundException, ResourceRegistryException { protected OElement reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", EntityTypeDefinition.NAME, getName()); logger.debug("Going to update {} for {}", PropertyTypeDefinition.NAME, getName());
OElement propertyTypeDefinition = getElement(); OElement propertyTypeDefinition = getElement();
propertyTypeDefinition = (OElement) ERManagement.updateProperties(oClass, propertyTypeDefinition, jsonNode, propertyTypeDefinition = (OElement) ERManagement.updateProperties(oClass, propertyTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys); ignoreKeys, ignoreStartWithKeys);
@ -180,6 +165,24 @@ public class PropertyTypeDefinitionManagement extends ERManagement<OElement> {
} }
} }
protected OElement createElement() throws AlreadyPresentException, ResourceRegistryException {
try {
this.element = new ODocument(elementType);
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
logger.debug("Created {} is {}", PropertyTypeDefinition.NAME, Utility.toJsonString(element, true));
return element;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
logger.trace("Error while creating {} for {} ({}) using {}", OElement.class.getSimpleName(),
accessType.getName(), elementType, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + elementType + " with " + jsonNode, e.getCause());
}
}
@Override @Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
@ -202,14 +205,14 @@ public class PropertyTypeDefinitionManagement extends ERManagement<OElement> {
return new SchemaNotFoundException(e.getMessage(), e.getCause()); return new SchemaNotFoundException(e.getMessage(), e.getCause());
} }
@Override
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
throw new UnsupportedOperationException();
}
@Override @Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) { protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message); return new SchemaAlreadyPresentException(message);
} }
@Override
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
throw new UnsupportedOperationException();
}
} }

View File

@ -9,6 +9,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAn
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.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
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.SchemaAlreadyPresentException;
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;
@ -27,8 +28,8 @@ import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResult; import com.orientechnologies.orient.core.sql.executor.OResult;
import com.orientechnologies.orient.core.sql.executor.OResultSet; import com.orientechnologies.orient.core.sql.executor.OResultSet;
public class RelationTypeDefinitionManagement extends public class RelationTypeDefinitionManagement
BaseRelationManagement<EntityTypeDefinitionManagement,EntityTypeDefinitionManagement> { extends BaseRelationManagement<EntityTypeDefinitionManagement,EntityTypeDefinitionManagement> {
protected String name; protected String name;
@ -42,6 +43,15 @@ public class RelationTypeDefinitionManagement extends
getWorkingContext(); getWorkingContext();
} }
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
this.workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
return workingContext;
}
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
@ -59,18 +69,9 @@ public class RelationTypeDefinitionManagement extends
return name; return name;
} }
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
this.workingContext = ContextUtility.getInstance()
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
}
return workingContext;
}
@Override @Override
protected OEdge reallyCreate() throws ResourceRegistryException { protected OEdge reallyCreate() throws ResourceRegistryException {
logger.debug("Going to create {} for {}", RelationTypeDefinition.NAME, getName());
if(sourceEntityManagement == null) { if(sourceEntityManagement == null) {
if(!jsonNode.has(Relation.SOURCE_PROPERTY)) { if(!jsonNode.has(Relation.SOURCE_PROPERTY)) {
@ -109,6 +110,23 @@ public class RelationTypeDefinitionManagement extends
return element; return element;
} }
@Override
protected OEdge reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", RelationTypeDefinition.NAME, getName());
OEdge relationTypeDefinition = getElement();
relationTypeDefinition = (OEdge) ERManagement.updateProperties(oClass, relationTypeDefinition, jsonNode,
ignoreKeys, ignoreStartWithKeys);
return relationTypeDefinition;
}
@Override
protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} for {}", RelationTypeDefinition.NAME, getName());
getElement().delete();
return true;
}
@Override @Override
public OEdge getElement() throws NotFoundException, ResourceRegistryException { public OEdge getElement() throws NotFoundException, ResourceRegistryException {
if(element == null) { if(element == null) {
@ -137,12 +155,11 @@ public class RelationTypeDefinitionManagement extends
throw new NotFoundException("null name does not allow to retrieve the Element"); throw new NotFoundException("null name does not allow to retrieve the Element");
} }
String select = "SELECT FROM " + elementType + " WHERE " + RelationTypeDefinition.NAME_PROPERTY String select = "SELECT FROM " + elementType + " WHERE " + RelationTypeDefinition.NAME_PROPERTY + " = \""
+ " = \"" + getName() + "\""; + getName() + "\"";
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>()); 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", elementType, getName()); String error = String.format("No %s with name %s was found", elementType, getName());
logger.info(error); logger.info(error);
@ -170,25 +187,8 @@ public class RelationTypeDefinitionManagement extends
} }
@Override @Override
protected EntityTypeDefinitionManagement newSourceEntityManagement() public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throws ResourceRegistryException { throw new UnsupportedOperationException();
return new EntityTypeDefinitionManagement(oDatabaseDocument);
}
@Override
protected EntityTypeDefinitionManagement newTargetEntityManagement()
throws ResourceRegistryException {
return new EntityTypeDefinitionManagement(oDatabaseDocument);
}
@Override
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message);
} }
@Override @Override
@ -203,14 +203,29 @@ public class RelationTypeDefinitionManagement extends
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
return new SchemaAlreadyPresentException(message);
}
@Override @Override
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) { protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override @Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException { protected EntityTypeDefinitionManagement newSourceEntityManagement() throws ResourceRegistryException {
throw new UnsupportedOperationException(); return new EntityTypeDefinitionManagement(oDatabaseDocument);
}
@Override
protected EntityTypeDefinitionManagement newTargetEntityManagement() throws ResourceRegistryException {
return new EntityTypeDefinitionManagement(oDatabaseDocument);
} }
} }