resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/types/properties/PropertyTypeDefinitionManag...

203 lines
6.7 KiB
Java
Raw Normal View History

2020-01-27 17:04:45 +01:00
package org.gcube.informationsystem.resourceregistry.types.properties;
2019-11-05 09:28:55 +01:00
import java.util.Map;
import java.util.UUID;
2019-11-08 12:29:32 +01:00
2020-07-07 17:15:22 +02:00
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
2019-11-08 12:29:32 +01:00
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException;
2020-01-27 17:07:37 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
2023-05-11 18:35:56 +02:00
import org.gcube.informationsystem.resourceregistry.contexts.security.TypeSecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
2021-02-10 15:54:52 +01:00
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
2023-05-11 18:35:56 +02:00
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.gcube.informationsystem.types.reference.properties.PropertyType;
2019-11-08 12:29:32 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2023-05-11 18:35:56 +02:00
import com.arcadedb.database.Document;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.remote.RemoteDatabase;
2019-11-08 12:29:32 +01:00
2021-01-20 17:08:59 +01:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
2023-05-11 18:35:56 +02:00
public class PropertyTypeDefinitionManagement extends ElementManagement<Document, PropertyType<?>> {
2019-11-08 12:29:32 +01:00
private static Logger logger = LoggerFactory.getLogger(PropertyTypeDefinitionManagement.class);
protected String name;
public PropertyTypeDefinitionManagement() {
super(AccessType.PROPERTY_TYPE);
2021-02-18 09:42:51 +01:00
this.typeName = PropertyType.NAME;
2019-11-08 12:29:32 +01:00
}
2023-05-11 18:35:56 +02:00
public PropertyTypeDefinitionManagement(SecurityContext securityContext, RemoteDatabase database) throws ResourceRegistryException {
2019-11-08 12:29:32 +01:00
this();
2023-05-11 18:35:56 +02:00
this.database = database;
2019-11-08 18:14:45 +01:00
setWorkingContext(securityContext);
2019-11-08 12:29:32 +01:00
}
@Override
public Map<UUID,JsonNode> getAffectedInstances() {
throw new UnsupportedOperationException();
}
2019-11-08 12:29:32 +01:00
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if(workingContext == null) {
2021-10-22 20:05:55 +02:00
workingContext = TypeSecurityContext.getInstance();
2019-11-08 12:29:32 +01:00
}
return workingContext;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
if(name == null) {
if(element == null) {
if(jsonNode != null) {
name = jsonNode.get(PropertyType.NAME_PROPERTY).asText();
2019-11-08 12:29:32 +01:00
}
} else {
2023-05-11 18:35:56 +02:00
name = element.getString(PropertyType.NAME_PROPERTY);
2019-11-08 12:29:32 +01:00
}
}
return name;
}
@Override
2021-02-17 11:29:43 +01:00
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
return serializeSelfAsJsonNode();
2019-11-08 12:29:32 +01:00
}
@Override
2023-05-11 18:35:56 +02:00
protected Document reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
logger.debug("Going to create {} for {}", PropertyType.NAME, getName());
2019-11-08 13:14:56 +01:00
return createElement();
2019-11-08 12:29:32 +01:00
}
@Override
2023-05-11 18:35:56 +02:00
protected Document reallyUpdate() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to update {} for {}", PropertyType.NAME, getName());
2023-05-11 18:35:56 +02:00
Document propertyTypeDefinition = getElement();
propertyTypeDefinition = updateProperties(documentType, propertyTypeDefinition, jsonNode,
2019-11-08 12:29:32 +01:00
ignoreKeys, ignoreStartWithKeys);
return propertyTypeDefinition;
}
@Override
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
logger.debug("Going to remove {} for {}", EntityType.NAME, getName());
2019-11-08 12:29:32 +01:00
getElement().delete();
}
@Override
2023-05-11 18:35:56 +02:00
public Document getElement() throws NotFoundException, ResourceRegistryException {
2019-11-08 12:29:32 +01:00
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
2023-05-11 18:35:56 +02:00
public Document retrieveElement() throws NotFoundException, ResourceRegistryException {
2019-11-08 12:29:32 +01:00
try {
if(getName() == null) {
throw new NotFoundException("null name does not allow to retrieve the Element");
}
2021-02-18 09:42:51 +01:00
String select = "SELECT FROM " + typeName + " WHERE " + PropertyType.NAME_PROPERTY + " = \""
2019-11-08 12:29:32 +01:00
+ getName() + "\"";
2023-05-11 18:35:56 +02:00
ResultSet resultSet = database.query("sql", select);
2019-11-08 12:29:32 +01:00
if(resultSet == null || !resultSet.hasNext()) {
2021-02-18 09:42:51 +01:00
String error = String.format("No %s with name %s was found", typeName, getName());
2019-11-08 12:29:32 +01:00
logger.info(error);
throw new NotFoundException(error);
}
2023-05-11 18:35:56 +02:00
Result oResult = resultSet.next();
Document element = (Document) ElementManagementUtility.getElementFromOptional(oResult.getElement());
2019-11-08 12:29:32 +01:00
2023-05-11 18:35:56 +02:00
logger.trace("{} with id {} is : {}", typeName, getName(), DBUtility.getAsStringForLogging(element));
2019-11-08 12:29:32 +01:00
if(resultSet.hasNext()) {
2021-02-18 09:42:51 +01:00
throw new ResourceRegistryException("Found more than one " + typeName + " with name " + getName()
2019-11-08 12:29:32 +01:00
+ ". This is a fatal error please contact Admnistrator");
}
return element;
} catch(NotFoundException e) {
2021-03-05 10:46:59 +01:00
throw getSpecificNotFoundException(e);
2019-11-08 12:29:32 +01:00
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
throw new ResourceRegistryException(e);
}
}
2023-05-11 18:35:56 +02:00
protected Document createElement() throws AlreadyPresentException, ResourceRegistryException {
2019-11-08 13:14:56 +01:00
try {
2023-05-11 18:35:56 +02:00
this.element = database.newDocument(typeName);
2019-11-08 13:14:56 +01:00
2023-05-11 18:35:56 +02:00
updateProperties(documentType, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
2019-11-08 13:14:56 +01:00
2023-05-11 18:35:56 +02:00
logger.debug("Created {} is {}", PropertyType.NAME, DBUtility.getAsStringForLogging(element));
2019-11-08 13:14:56 +01:00
return element;
} catch(ResourceRegistryException e) {
throw e;
} catch(Exception e) {
2023-05-11 18:35:56 +02:00
logger.trace("Error while creating {} for {} ({}) using {}", Document.class.getSimpleName(),
2021-02-18 09:42:51 +01:00
accessType.getName(), typeName, jsonNode, e);
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
2019-11-08 13:14:56 +01:00
}
}
2019-11-08 12:29:32 +01:00
@Override
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
throw new UnsupportedOperationException();
}
@Override
2021-03-05 10:46:59 +01:00
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
2019-11-08 12:29:32 +01:00
return new SchemaNotFoundException(e.getMessage(), e.getCause());
}
@Override
2021-03-05 10:46:59 +01:00
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
2019-11-08 13:14:56 +01:00
return new SchemaAlreadyPresentException(message);
2019-11-08 12:29:32 +01:00
}
2021-02-18 18:22:39 +01:00
@Override
2021-02-19 19:32:23 +01:00
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
2021-02-18 18:22:39 +01:00
// Nothing to do
}
2021-02-19 19:32:23 +01:00
2019-11-05 09:28:55 +01:00
}