2019-11-04 18:01:20 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.instances.type.entities;
|
|
|
|
|
2019-11-08 12:29:32 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2019-11-04 18:01:20 +01:00
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException;
|
|
|
|
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;
|
2019-11-08 12:29:32 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
|
2019-11-04 18:01:20 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ERManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.entities.BaseEntityManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.context.ContextUtility;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.security.SecurityContext;
|
2019-11-08 12:29:32 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
2019-11-04 18:01:20 +01:00
|
|
|
import org.gcube.informationsystem.types.reference.entities.EntityTypeDefinition;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2019-11-05 18:36:44 +01:00
|
|
|
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
|
|
|
|
import com.orientechnologies.orient.core.record.OVertex;
|
2019-11-08 12:29:32 +01:00
|
|
|
import com.orientechnologies.orient.core.sql.executor.OResult;
|
|
|
|
import com.orientechnologies.orient.core.sql.executor.OResultSet;
|
2019-11-04 18:01:20 +01:00
|
|
|
|
2019-11-08 12:29:32 +01:00
|
|
|
public class EntityTypeDefinitionManagement extends BaseEntityManagement<EntityTypeDefinition> {
|
2019-11-04 18:01:20 +01:00
|
|
|
|
2019-11-08 12:29:32 +01:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(EntityTypeDefinitionManagement.class);
|
2019-11-04 18:01:20 +01:00
|
|
|
|
|
|
|
protected String name;
|
|
|
|
|
|
|
|
public EntityTypeDefinitionManagement() {
|
|
|
|
super(AccessType.ENTITY_TYPE_DEFINITION);
|
2019-11-08 18:14:45 +01:00
|
|
|
this.elementType = EntityTypeDefinition.NAME;
|
|
|
|
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 18:14:45 +01:00
|
|
|
public EntityTypeDefinitionManagement(SecurityContext securityContext, ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
|
2019-11-04 18:01:20 +01:00
|
|
|
this();
|
2019-11-08 12:29:32 +01:00
|
|
|
this.oDatabaseDocument = oDatabaseDocument;
|
2019-11-08 18:14:45 +01:00
|
|
|
setWorkingContext(securityContext);
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 13:14:56 +01:00
|
|
|
@Override
|
|
|
|
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
|
|
|
|
if(workingContext == null) {
|
|
|
|
workingContext = ContextUtility.getInstance()
|
|
|
|
.getSecurityContextByUUID(DatabaseEnvironment.SCHEMA_SECURITY_CONTEXT_UUID);
|
|
|
|
}
|
|
|
|
return workingContext;
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:29:32 +01:00
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2019-11-04 18:01:20 +01:00
|
|
|
public String getName() {
|
|
|
|
if(name == null) {
|
|
|
|
if(element == null) {
|
|
|
|
if(jsonNode != null) {
|
|
|
|
name = jsonNode.get(EntityTypeDefinition.NAME_PROPERTY).asText();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = element.getProperty(EntityTypeDefinition.NAME_PROPERTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:14:56 +01:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:29:32 +01:00
|
|
|
@Override
|
|
|
|
public OVertex getElement() throws NotFoundException, ResourceRegistryException {
|
|
|
|
if(element == null) {
|
|
|
|
try {
|
|
|
|
element = retrieveElement();
|
|
|
|
} catch(NotFoundException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(reload) {
|
|
|
|
element.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OVertex retrieveElement() throws NotFoundException, ResourceRegistryException {
|
|
|
|
try {
|
|
|
|
if(getName() == null) {
|
|
|
|
throw new NotFoundException("null name does not allow to retrieve the Element");
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:14:56 +01:00
|
|
|
String select = "SELECT FROM " + elementType + " WHERE " + EntityTypeDefinition.NAME_PROPERTY + " = \""
|
|
|
|
+ getName() + "\"";
|
2019-11-08 12:29:32 +01:00
|
|
|
|
|
|
|
OResultSet resultSet = oDatabaseDocument.query(select, new HashMap<>());
|
|
|
|
|
|
|
|
if(resultSet == null || !resultSet.hasNext()) {
|
|
|
|
String error = String.format("No %s with name %s was found", elementType, getName());
|
|
|
|
logger.info(error);
|
|
|
|
throw new NotFoundException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
OResult oResult = resultSet.next();
|
|
|
|
OVertex element = (OVertex) ERManagement.getElementFromOptional(oResult.getElement());
|
|
|
|
|
|
|
|
logger.trace("{} with id {} is : {}", elementType, getName(), Utility.toJsonString(element, true));
|
|
|
|
|
|
|
|
if(resultSet.hasNext()) {
|
|
|
|
throw new ResourceRegistryException("Found more than one " + elementType + " with name " + getName()
|
|
|
|
+ ". This is a fatal error please contact Admnistrator");
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
|
|
|
} catch(NotFoundException e) {
|
|
|
|
throw getSpecificElementNotFoundException(e);
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected OVertex createVertex() throws EntityAlreadyPresentException, ResourceRegistryException {
|
|
|
|
|
|
|
|
logger.trace("Going to create {} for {} ({}) using {}", OVertex.class.getSimpleName(), accessType.getName(),
|
|
|
|
elementType, jsonNode);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
this.element = oDatabaseDocument.newVertex(elementType);
|
|
|
|
|
|
|
|
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
|
|
|
|
|
2019-11-08 13:14:56 +01:00
|
|
|
logger.info("Created {} is {}", OVertex.class.getSimpleName(), Utility.toJsonString(element, true));
|
2019-11-08 12:29:32 +01:00
|
|
|
|
|
|
|
return element;
|
|
|
|
} catch(ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
logger.trace("Error while creating {} for {} ({}) using {}", OVertex.class.getSimpleName(),
|
|
|
|
accessType.getName(), elementType, jsonNode, e);
|
|
|
|
throw new ResourceRegistryException("Error Creating " + elementType + " with " + jsonNode, e.getCause());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 18:01:20 +01:00
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
|
|
|
throw new UnsupportedOperationException();
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
protected boolean reallyAddToContext(SecurityContext targetSecurityContext)
|
|
|
|
throws ContextException, ResourceRegistryException {
|
|
|
|
throw new UnsupportedOperationException();
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
protected boolean reallyRemoveFromContext(SecurityContext targetSecurityContext)
|
|
|
|
throws ContextException, ResourceRegistryException {
|
|
|
|
throw new UnsupportedOperationException();
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
protected NotFoundException getSpecificElementNotFoundException(NotFoundException e) {
|
|
|
|
return new SchemaNotFoundException(e.getMessage(), e.getCause());
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
protected AlreadyPresentException getSpecificERAlreadyPresentException(String message) {
|
|
|
|
return new SchemaAlreadyPresentException(message);
|
2019-11-04 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-11-08 13:14:56 +01:00
|
|
|
protected AvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(String message) {
|
2019-11-04 18:01:20 +01:00
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|