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

View File

@ -1,12 +1,16 @@
package org.gcube.informationsystem.resourceregistry.query.templates; package org.gcube.informationsystem.resourceregistry.query.templates;
import java.util.HashMap;
import org.gcube.com.fasterxml.jackson.databind.JsonNode; import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.context.reference.entities.Context; import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.query.templates.reference.entities.QueryTemplate; 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.AlreadyPresentException;
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.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility; import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext;
import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext; 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.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.record.OVertex; import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResultSet;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
@ -27,13 +32,9 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
protected String name; protected String name;
private void init() {
this.typeName = QueryTemplate.NAME;
}
public QueryTemplateManagement() { public QueryTemplateManagement() {
super(AccessType.QUERY_TEMPLATE); super(AccessType.QUERY_TEMPLATE);
init(); this.typeName = QueryTemplate.NAME;
} }
public QueryTemplateManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { public QueryTemplateManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException {
@ -42,6 +43,23 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
getWorkingContext(); 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 @Override
protected SecurityContext getWorkingContext() throws ResourceRegistryException { protected SecurityContext getWorkingContext() throws ResourceRegistryException {
if (workingContext == null) { if (workingContext == null) {
@ -51,27 +69,72 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
return workingContext; return workingContext;
} }
public void setName(String queryTemplateName) {
// TODO Auto-generated method stub
}
@Override @Override
protected JsonNode createCompleteJsonNode() throws ResourceRegistryException { protected JsonNode createCompleteJsonNode() throws ResourceRegistryException {
// TODO Auto-generated method stub JsonNode queryTemplate = serializeSelfAsJsonNode();
return null; 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 @Override
protected OVertex reallyCreate() throws AlreadyPresentException, ResourceRegistryException { protected OVertex reallyCreate() throws AlreadyPresentException, InvalidQueryException, ResourceRegistryException {
// TODO Auto-generated method stub try {
return null; 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 @Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException { protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub try {
return null;
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 @Override
@ -82,14 +145,12 @@ public class QueryTemplateManagement extends EntityElementManagement<Context, En
@Override @Override
protected NotFoundException getSpecificNotFoundException(NotFoundException e) { protected NotFoundException getSpecificNotFoundException(NotFoundException e) {
// TODO Auto-generated method stub return new NotFoundException(e);
return null;
} }
@Override @Override
protected AlreadyPresentException getSpecificAlreadyPresentException(String message) { protected AlreadyPresentException getSpecificAlreadyPresentException(String message) {
// TODO Auto-generated method stub return new AlreadyPresentException(message);
return null;
} }
@Override @Override