Improving code

This commit is contained in:
luca.frosini 2023-11-24 14:30:51 +01:00
parent 6a1e0e5838
commit 6657543a63
4 changed files with 48 additions and 48 deletions

View File

@ -142,13 +142,13 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
select.append(parentId);
select.append(" MAXDEPTH 1) WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(ConditionalOperator.EQ.getConditionalOperator());
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");
select.append(LogicalOperator.AND.getLogicalOperator());
select.append(IdentifiableElement.ID_PROPERTY);
select.append(ConditionalOperator.NE.getConditionalOperator());
select.append(ConditionalOperator.NE.getDbConditionalOperator());
select.append("\"");
select.append(parentContext.uuid);
select.append("\"");
@ -164,7 +164,7 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
select.append(Context.NAME);
select.append(" WHERE ");
select.append(Context.NAME_PROPERTY);
select.append(ConditionalOperator.EQ.getConditionalOperator());
select.append(ConditionalOperator.EQ.getDbConditionalOperator());
select.append("\"");
select.append(getName());
select.append("\"");

View File

@ -327,9 +327,9 @@ public abstract class JsonQueryERElement {
throw new UnsupportedOperationException();
}
StringBuffer key = getKey(null, fieldNamePrefix);
StringBuffer value = getValue(jsonNode);
stringBuffer.append(addCondition(queryConditionalOperator, key, value));
String key = getKey(null, fieldNamePrefix);
String value = getValue(jsonNode);
stringBuffer.append(queryConditionalOperator.addCondition(key, value));
return stringBuffer;
}
@ -351,43 +351,34 @@ public abstract class JsonQueryERElement {
}
if(jsonNode.isTextual() || jsonNode.isNumber()) {
StringBuffer key = getKey(fieldName, fieldNamePrefix);
StringBuffer value = getValue(jsonNode);
stringBuffer.append(addCondition(ConditionalOperator.EQ, key, value));
String key = getKey(fieldName, fieldNamePrefix);
String value = getValue(jsonNode);
stringBuffer.append(ConditionalOperator.EQ.addCondition(key, value));
}
if(jsonNode.isNull()) {
StringBuffer key = getKey(fieldName, null);
stringBuffer.append(addCondition(ConditionalOperator.IS, key, null));
String key = getKey(fieldName, null);
stringBuffer.append(ConditionalOperator.IS.addCondition(key, null));
}
return stringBuffer;
}
protected StringBuffer addCondition(ConditionalOperator queryConditionalOperator, StringBuffer key, StringBuffer value) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(key);
stringBuffer.append(queryConditionalOperator.getConditionalOperator());
stringBuffer.append(value);
return stringBuffer;
}
protected StringBuffer getKey(String fieldName, String fieldNamePrefix) {
protected String getKey(String fieldName, String fieldNamePrefix) {
StringBuffer stringBuffer = new StringBuffer();
if(fieldNamePrefix!=null) {
stringBuffer.append(fieldNamePrefix);
if(fieldName!=null && fieldName.compareTo("")!=0) {
stringBuffer.append(fieldNamePrefix.trim());
if(fieldName!=null && fieldName.trim().length()==0) {
stringBuffer.append(".");
}
}
if(fieldName!=null) {
stringBuffer.append(fieldName);
stringBuffer.append(fieldName.trim());
}
return stringBuffer;
return stringBuffer.toString();
}
protected StringBuffer getValue(JsonNode jsonNode) {
protected String getValue(JsonNode jsonNode) {
StringBuffer stringBuffer = new StringBuffer();
String value = jsonNode.asText();
@ -398,7 +389,7 @@ public abstract class JsonQueryERElement {
stringBuffer.append(value);
stringBuffer.append("\"");
}
return stringBuffer;
return stringBuffer.toString();
}
protected List<JsonQueryERElement> getChildrenBreadcrumb() {

View File

@ -41,24 +41,24 @@ public enum ConditionalOperator {
IS_DEFINED("_isDefined", " IS DEFINED ", BaseTypeGroup.ANY, "Returns TRUE is a field is defined in a document"),
IS_NOT_DEFINED("_isNotDefined", " IS NOT DEFINED ", BaseTypeGroup.ANY, "Returns TRUE is a field is not defined in a document");
protected final String operator;
protected final String conditionalOperator;
protected final String operatorKey;
protected final String dbConditionalOperator;
protected final BaseTypeGroup allowed;
protected final String description;
private ConditionalOperator(String operator, String conditionalOperator, BaseTypeGroup allowed, String description) {
this.operator = operator;
this.conditionalOperator = conditionalOperator;
private ConditionalOperator(String operatorKey, String conditionalOperator, BaseTypeGroup allowed, String description) {
this.operatorKey = operatorKey;
this.dbConditionalOperator = conditionalOperator;
this.allowed = allowed;
this.description = description;
}
public String getOperator() {
return operator;
protected String getOperatorKey() {
return operatorKey;
}
public String getConditionalOperator() {
return conditionalOperator;
public String getDbConditionalOperator() {
return dbConditionalOperator;
}
public String getDescription() {
@ -73,8 +73,8 @@ public enum ConditionalOperator {
ConditionalOperator.operatorByKey = new HashMap<>();
for(ConditionalOperator queryComparisonOperator : ConditionalOperator.values()) {
ConditionalOperator.operators.add(queryComparisonOperator.getOperator());
ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperator(), queryComparisonOperator);
ConditionalOperator.operators.add(queryComparisonOperator.getOperatorKey());
ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator);
}
}
@ -85,4 +85,12 @@ public enum ConditionalOperator {
public static ConditionalOperator getQueryComparisonOperator(String key) {
return operatorByKey.get(key);
}
public StringBuffer addCondition(String key, String value) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(key);
stringBuffer.append(getDbConditionalOperator());
stringBuffer.append(value);
return stringBuffer;
}
}

View File

@ -1,7 +1,6 @@
package org.gcube.informationsystem.resourceregistry.queries.operators;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -48,39 +47,41 @@ public enum MatemathicsOperator {
public static final String AS_KEY = "as";
protected final String operatorKey;
protected final String dbQueryOperator;
protected final String dbMatemathicsOperator;
protected final String description;
private MatemathicsOperator(String operatorKey, String dbQueryOperator, String description) {
private MatemathicsOperator(String operatorKey, String dbMatemathicsOperator, String description) {
this.operatorKey = operatorKey;
this.dbQueryOperator = dbQueryOperator;
this.dbMatemathicsOperator = dbMatemathicsOperator;
this.description = description;
}
public String getOperatorKey() {
protected String getOperatorKey() {
return operatorKey;
}
public String getDbQueryOperator() {
return dbQueryOperator;
protected String getDbMatemathicsOperator() {
return dbMatemathicsOperator;
}
public String getDescription() {
return description;
}
private static Set<String> operators;
private static Map<String,MatemathicsOperator> operatorByKey;
static {
MatemathicsOperator.operatorByKey = new HashMap<>();
for(MatemathicsOperator matemathicsOperator : MatemathicsOperator.values()) {
MatemathicsOperator.operators.add(matemathicsOperator.getOperatorKey());
MatemathicsOperator.operatorByKey.put(matemathicsOperator.getOperatorKey(), matemathicsOperator);
}
}
public static Set<String> getOperators() {
return new HashSet<>(MatemathicsOperator.operatorByKey.keySet());
return operators;
}
public static MatemathicsOperator getMatemathicsOperator(String key) {
@ -160,13 +161,13 @@ public enum MatemathicsOperator {
if(i<(size-1)) {
buffer.append(" ");
buffer.append(dbQueryOperator);
buffer.append(dbMatemathicsOperator);
buffer.append(" ");
if(fieldsSeparator!=null) {
buffer.append("'");
buffer.append(fieldsSeparator);
buffer.append("' ");
buffer.append(dbQueryOperator);
buffer.append(dbMatemathicsOperator);
buffer.append(" ");
}
}