Moved Query Conditional and Logical operators in a dedicated package

This commit is contained in:
Luca Frosini 2021-10-22 11:11:01 +02:00
parent d2324da565
commit 6e985eec65
5 changed files with 141 additions and 54 deletions

View File

@ -33,6 +33,8 @@ import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentO
import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
import org.gcube.informationsystem.resourceregistry.instances.base.entities.EntityElementManagement;
import org.gcube.informationsystem.resourceregistry.query.operators.QueryConditionalOperator;
import org.gcube.informationsystem.resourceregistry.query.operators.QueryLogicalOperator;
import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.gcube.informationsystem.types.reference.entities.EntityType;
import org.gcube.informationsystem.utils.ElementMapper;
@ -128,41 +130,63 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
protected void checkContext(ContextManagement parentContext)
throws ContextNotFoundException, ContextAlreadyPresentException, ResourceRegistryException {
StringBuffer select = new StringBuffer();
StringBuffer errorMessage = new StringBuffer();
if (parentContext != null) {
String parentId = parentContext.getElement().getIdentity().toString();
String select = "SELECT FROM (TRAVERSE out(" + IsParentOf.NAME + ") FROM " + parentId
+ " MAXDEPTH 1) WHERE " + Context.NAME_PROPERTY + "=\"" + getName() + "\" AND "
+ Context.HEADER_PROPERTY + "." + Header.UUID_PROPERTY + "<>\"" + parentContext.uuid + "\"";
logger.trace(select);
StringBuilder message = new StringBuilder();
message.append("A context with name (");
message.append(getName());
message.append(") has been already created as child of ");
message.append(parentContext.getElement().toString());
logger.trace("Checking if {} -> {}", message, select);
OResultSet resultSet = oDatabaseDocument.command(select, new HashMap<>());
if (resultSet != null && resultSet.hasNext()) {
throw new ContextAlreadyPresentException(message.toString());
}
select.append("SELECT FROM (TRAVERSE out(");
select.append(IsParentOf.NAME);
select.append(") FROM ");
select.append(parentId);
select.append(" MAXDEPTH 1) WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(QueryConditionalOperator.EQ.getConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append(Context.HEADER_PROPERTY);
select.append(Header.UUID_PROPERTY);
select.append(QueryConditionalOperator.NE.getConditionalOperator());
select.append("\"");
select.append(parentContext.uuid);
select.append("\"");
errorMessage.append("A root ");
errorMessage.append(Context.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());
errorMessage.append(" has been already created as child of ");
errorMessage.append(parentContext.getElement().toString());
} else {
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()) {
throw new ContextAlreadyPresentException(
"A root context with the same name (" + this.getName() + ") already exist");
}
select.append("SELECT FROM ");
select.append(Context.NAME);
select.append(" WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(QueryConditionalOperator.EQ.getConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(QueryLogicalOperator.AND.getLogicalOperator());
select.append("in(\"");
select.append(IsParentOf.NAME);
select.append("\").size() = 0");
errorMessage.append("A root ");
errorMessage.append(Context.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());
errorMessage.append(" already exist");
}
logger.trace("Checking if {} -> {}", errorMessage, select);
OResultSet resultSet = oDatabaseDocument.command(select.toString(), new HashMap<>());
if (resultSet != null && resultSet.hasNext()) {
throw new ContextAlreadyPresentException(errorMessage.toString());
}
}

View File

@ -15,6 +15,8 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.query.operators.QueryConditionalOperator;
import org.gcube.informationsystem.resourceregistry.query.operators.QueryLogicalOperator;
import org.gcube.informationsystem.resourceregistry.types.TypesCache;
public abstract class JsonQueryERElement {

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.resourceregistry.query.json.base;
package org.gcube.informationsystem.resourceregistry.query.operators;
import java.util.HashMap;
import java.util.HashSet;

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.resourceregistry.query.json.base;
package org.gcube.informationsystem.resourceregistry.query.operators;
import java.util.HashMap;
import java.util.HashSet;

View File

@ -1,12 +1,16 @@
package org.gcube.informationsystem.resourceregistry.query.templates;
import java.util.HashMap;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.query.templates.reference.entities.QueryTemplate;
import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache;
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.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext;
@ -17,6 +21,7 @@ import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
/**
* @author Luca Frosini (ISTI - CNR)
@ -27,13 +32,9 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
protected String name;
private void init() {
this.typeName = QueryTemplate.NAME;
}
public QueryTemplateManagement() {
super(AccessType.QUERY_TEMPLATE);
init();
this.typeName = QueryTemplate.NAME;
}
public QueryTemplateManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
@ -42,6 +43,23 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
getWorkingContext();
}
public void setName(String name) {
this.name = name;
}
public String getName() {
if (name == null) {
if (element == null) {
if (jsonNode != null) {
name = jsonNode.get(Context.NAME_PROPERTY).asText();
}
} else {
name = element.getProperty(Context.NAME_PROPERTY);
}
}
return name;
}
@Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) {
@ -51,27 +69,72 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
return workingContext;
}
public void setName(String queryTemplateName) {
// TODO Auto-generated method stub
@Override
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
JsonNode queryTemplate = serializeSelfAsJsonNode();
return queryTemplate;
}
protected void checkIfNameAlreadyExists() throws AlreadyPresentException {
StringBuffer select = new StringBuffer();
select.append("SELECT FROM ");
select.append(QueryTemplate.NAME);
select.append(" WHERE ");
select.append(QueryTemplate.NAME_PROPERTY);
select.append(" = ");
select.append("\"");
select.append(getName());
select.append("\"");
StringBuffer errorMessage = new StringBuffer();
errorMessage.append("A ");
errorMessage.append(QueryTemplate.NAME);
errorMessage.append(" with ");
errorMessage.append(this.getName());
errorMessage.append(" already exists");
logger.trace("Checking if {} -> {}", errorMessage, select);
OResultSet resultSet = oDatabaseDocument.command(select.toString(), new HashMap<>());
if (resultSet != null && resultSet.hasNext()) {
throw new AlreadyPresentException(errorMessage.toString());
}
}
protected void tryTemplate() throws InvalidQueryException {
}
@Override
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
// TODO Auto-generated method stub
return null;
}
@Override
protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException {
// TODO Auto-generated method stub
return null;
protected OVertex reallyCreate() throws AlreadyPresentException, InvalidQueryException, ResourceRegistryException {
try {
checkIfNameAlreadyExists();
tryTemplate();
createVertex();
return getElement();
} catch(ResourceRegistryException e) {
throw 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());
}
}
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
return null;
try {
tryTemplate();
return getElement();
} catch(ResourceRegistryException e) {
throw 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());
}
}
@Override
@ -82,14 +145,12 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
@Override
protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
// TODO Auto-generated method stub
return null;
return new NotFoundException(e);
}
@Override
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
// TODO Auto-generated method stub
return null;
return new AlreadyPresentException(message);
}
@Override