203 lines
6.7 KiB
Java
203 lines
6.7 KiB
Java
package org.gcube.informationsystem.resourceregistry.types.properties;
|
|
|
|
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.resourceregistry.api.exceptions.AlreadyPresentException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
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;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
|
|
import org.gcube.informationsystem.resourceregistry.contexts.security.TypeSecurityContext;
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement;
|
|
import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagementUtility;
|
|
import org.gcube.informationsystem.resourceregistry.utils.DBUtility;
|
|
import org.gcube.informationsystem.types.reference.entities.EntityType;
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyType;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.arcadedb.database.Document;
|
|
import com.arcadedb.query.sql.executor.Result;
|
|
import com.arcadedb.query.sql.executor.ResultSet;
|
|
import com.arcadedb.remote.RemoteDatabase;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class PropertyTypeDefinitionManagement extends ElementManagement<Document, PropertyType<?>> {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(PropertyTypeDefinitionManagement.class);
|
|
|
|
protected String name;
|
|
|
|
public PropertyTypeDefinitionManagement() {
|
|
super(AccessType.PROPERTY_TYPE);
|
|
this.typeName = PropertyType.NAME;
|
|
}
|
|
|
|
public PropertyTypeDefinitionManagement(SecurityContext securityContext, RemoteDatabase database) throws ResourceRegistryException {
|
|
this();
|
|
this.database = database;
|
|
setWorkingContext(securityContext);
|
|
}
|
|
|
|
@Override
|
|
public Map<UUID,JsonNode> getAffectedInstances() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
@Override
|
|
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
|
|
if(workingContext == null) {
|
|
workingContext = TypeSecurityContext.getInstance();
|
|
}
|
|
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();
|
|
}
|
|
} else {
|
|
name = element.getString(PropertyType.NAME_PROPERTY);
|
|
}
|
|
}
|
|
return name;
|
|
}
|
|
|
|
@Override
|
|
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
|
|
return serializeSelfAsJsonNode();
|
|
}
|
|
|
|
@Override
|
|
protected Document reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
|
|
logger.debug("Going to create {} for {}", PropertyType.NAME, getName());
|
|
return createElement();
|
|
}
|
|
|
|
@Override
|
|
protected Document reallyUpdate() throws NotFoundException, ResourceRegistryException {
|
|
logger.debug("Going to update {} for {}", PropertyType.NAME, getName());
|
|
Document propertyTypeDefinition = getElement();
|
|
propertyTypeDefinition = updateProperties(documentType, propertyTypeDefinition, jsonNode,
|
|
ignoreKeys, ignoreStartWithKeys);
|
|
return propertyTypeDefinition;
|
|
}
|
|
|
|
@Override
|
|
protected void reallyDelete() throws NotFoundException, ResourceRegistryException {
|
|
logger.debug("Going to remove {} for {}", EntityType.NAME, getName());
|
|
getElement().delete();
|
|
}
|
|
|
|
@Override
|
|
public Document 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 Document retrieveElement() throws NotFoundException, ResourceRegistryException {
|
|
try {
|
|
if(getName() == null) {
|
|
throw new NotFoundException("null name does not allow to retrieve the Element");
|
|
}
|
|
|
|
String select = "SELECT FROM " + typeName + " WHERE " + PropertyType.NAME_PROPERTY + " = \""
|
|
+ getName() + "\"";
|
|
|
|
ResultSet resultSet = database.query("sql", select);
|
|
|
|
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);
|
|
}
|
|
|
|
Result oResult = resultSet.next();
|
|
Document element = (Document) ElementManagementUtility.getElementFromOptional(oResult.getElement());
|
|
|
|
logger.trace("{} with id {} is : {}", typeName, getName(), DBUtility.getAsStringForLogging(element));
|
|
|
|
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) {
|
|
throw getSpecificNotFoundException(e);
|
|
} catch(ResourceRegistryException e) {
|
|
throw e;
|
|
} catch(Exception e) {
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
}
|
|
|
|
protected Document createElement() throws AlreadyPresentException, ResourceRegistryException {
|
|
try {
|
|
this.element = database.newDocument(typeName);
|
|
|
|
updateProperties(documentType, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
|
|
|
|
logger.debug("Created {} is {}", PropertyType.NAME, DBUtility.getAsStringForLogging(element));
|
|
|
|
return element;
|
|
} catch(ResourceRegistryException e) {
|
|
throw e;
|
|
} catch(Exception e) {
|
|
logger.trace("Error while creating {} for {} ({}) using {}", Document.class.getSimpleName(),
|
|
accessType.getName(), typeName, jsonNode, e);
|
|
throw new ResourceRegistryException("Error Creating " + typeName + " with " + jsonNode, e.getCause());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
@Override
|
|
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
|
|
return new SchemaNotFoundException(e.getMessage(), e.getCause());
|
|
}
|
|
|
|
@Override
|
|
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
|
|
return new SchemaAlreadyPresentException(message);
|
|
}
|
|
|
|
@Override
|
|
public void sanityCheck() throws SchemaViolationException, ResourceRegistryException {
|
|
// Nothing to do
|
|
}
|
|
|
|
}
|