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

View File

@ -327,9 +327,9 @@ public abstract class JsonQueryERElement {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
StringBuffer key = getKey(null, fieldNamePrefix); String key = getKey(null, fieldNamePrefix);
StringBuffer value = getValue(jsonNode); String value = getValue(jsonNode);
stringBuffer.append(addCondition(queryConditionalOperator, key, value)); stringBuffer.append(queryConditionalOperator.addCondition(key, value));
return stringBuffer; return stringBuffer;
} }
@ -351,43 +351,34 @@ public abstract class JsonQueryERElement {
} }
if(jsonNode.isTextual() || jsonNode.isNumber()) { if(jsonNode.isTextual() || jsonNode.isNumber()) {
StringBuffer key = getKey(fieldName, fieldNamePrefix); String key = getKey(fieldName, fieldNamePrefix);
StringBuffer value = getValue(jsonNode); String value = getValue(jsonNode);
stringBuffer.append(addCondition(ConditionalOperator.EQ, key, value)); stringBuffer.append(ConditionalOperator.EQ.addCondition(key, value));
} }
if(jsonNode.isNull()) { if(jsonNode.isNull()) {
StringBuffer key = getKey(fieldName, null); String key = getKey(fieldName, null);
stringBuffer.append(addCondition(ConditionalOperator.IS, key, null)); stringBuffer.append(ConditionalOperator.IS.addCondition(key, null));
} }
return stringBuffer; return stringBuffer;
} }
protected StringBuffer addCondition(ConditionalOperator queryConditionalOperator, StringBuffer key, StringBuffer value) { protected String getKey(String fieldName, String fieldNamePrefix) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(key);
stringBuffer.append(queryConditionalOperator.getConditionalOperator());
stringBuffer.append(value);
return stringBuffer;
}
protected StringBuffer getKey(String fieldName, String fieldNamePrefix) {
StringBuffer stringBuffer = new StringBuffer(); StringBuffer stringBuffer = new StringBuffer();
if(fieldNamePrefix!=null) { if(fieldNamePrefix!=null) {
stringBuffer.append(fieldNamePrefix); stringBuffer.append(fieldNamePrefix.trim());
if(fieldName!=null && fieldName.compareTo("")!=0) { if(fieldName!=null && fieldName.trim().length()==0) {
stringBuffer.append("."); stringBuffer.append(".");
} }
} }
if(fieldName!=null) { 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(); StringBuffer stringBuffer = new StringBuffer();
String value = jsonNode.asText(); String value = jsonNode.asText();
@ -398,7 +389,7 @@ public abstract class JsonQueryERElement {
stringBuffer.append(value); stringBuffer.append(value);
stringBuffer.append("\""); stringBuffer.append("\"");
} }
return stringBuffer; return stringBuffer.toString();
} }
protected List<JsonQueryERElement> getChildrenBreadcrumb() { 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_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"); 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 operatorKey;
protected final String conditionalOperator; protected final String dbConditionalOperator;
protected final BaseTypeGroup allowed; protected final BaseTypeGroup allowed;
protected final String description; protected final String description;
private ConditionalOperator(String operator, String conditionalOperator, BaseTypeGroup allowed, String description) { private ConditionalOperator(String operatorKey, String conditionalOperator, BaseTypeGroup allowed, String description) {
this.operator = operator; this.operatorKey = operatorKey;
this.conditionalOperator = conditionalOperator; this.dbConditionalOperator = conditionalOperator;
this.allowed = allowed; this.allowed = allowed;
this.description = description; this.description = description;
} }
public String getOperator() { protected String getOperatorKey() {
return operator; return operatorKey;
} }
public String getConditionalOperator() { public String getDbConditionalOperator() {
return conditionalOperator; return dbConditionalOperator;
} }
public String getDescription() { public String getDescription() {
@ -73,8 +73,8 @@ public enum ConditionalOperator {
ConditionalOperator.operatorByKey = new HashMap<>(); ConditionalOperator.operatorByKey = new HashMap<>();
for(ConditionalOperator queryComparisonOperator : ConditionalOperator.values()) { for(ConditionalOperator queryComparisonOperator : ConditionalOperator.values()) {
ConditionalOperator.operators.add(queryComparisonOperator.getOperator()); ConditionalOperator.operators.add(queryComparisonOperator.getOperatorKey());
ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperator(), queryComparisonOperator); ConditionalOperator.operatorByKey.put(queryComparisonOperator.getOperatorKey(), queryComparisonOperator);
} }
} }
@ -85,4 +85,12 @@ public enum ConditionalOperator {
public static ConditionalOperator getQueryComparisonOperator(String key) { public static ConditionalOperator getQueryComparisonOperator(String key) {
return operatorByKey.get(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; package org.gcube.informationsystem.resourceregistry.queries.operators;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -48,39 +47,41 @@ public enum MatemathicsOperator {
public static final String AS_KEY = "as"; public static final String AS_KEY = "as";
protected final String operatorKey; protected final String operatorKey;
protected final String dbQueryOperator; protected final String dbMatemathicsOperator;
protected final String description; protected final String description;
private MatemathicsOperator(String operatorKey, String dbQueryOperator, String description) { private MatemathicsOperator(String operatorKey, String dbMatemathicsOperator, String description) {
this.operatorKey = operatorKey; this.operatorKey = operatorKey;
this.dbQueryOperator = dbQueryOperator; this.dbMatemathicsOperator = dbMatemathicsOperator;
this.description = description; this.description = description;
} }
public String getOperatorKey() { protected String getOperatorKey() {
return operatorKey; return operatorKey;
} }
public String getDbQueryOperator() { protected String getDbMatemathicsOperator() {
return dbQueryOperator; return dbMatemathicsOperator;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
private static Set<String> operators;
private static Map<String,MatemathicsOperator> operatorByKey; private static Map<String,MatemathicsOperator> operatorByKey;
static { static {
MatemathicsOperator.operatorByKey = new HashMap<>(); MatemathicsOperator.operatorByKey = new HashMap<>();
for(MatemathicsOperator matemathicsOperator : MatemathicsOperator.values()) { for(MatemathicsOperator matemathicsOperator : MatemathicsOperator.values()) {
MatemathicsOperator.operators.add(matemathicsOperator.getOperatorKey());
MatemathicsOperator.operatorByKey.put(matemathicsOperator.getOperatorKey(), matemathicsOperator); MatemathicsOperator.operatorByKey.put(matemathicsOperator.getOperatorKey(), matemathicsOperator);
} }
} }
public static Set<String> getOperators() { public static Set<String> getOperators() {
return new HashSet<>(MatemathicsOperator.operatorByKey.keySet()); return operators;
} }
public static MatemathicsOperator getMatemathicsOperator(String key) { public static MatemathicsOperator getMatemathicsOperator(String key) {
@ -160,13 +161,13 @@ public enum MatemathicsOperator {
if(i<(size-1)) { if(i<(size-1)) {
buffer.append(" "); buffer.append(" ");
buffer.append(dbQueryOperator); buffer.append(dbMatemathicsOperator);
buffer.append(" "); buffer.append(" ");
if(fieldsSeparator!=null) { if(fieldsSeparator!=null) {
buffer.append("'"); buffer.append("'");
buffer.append(fieldsSeparator); buffer.append(fieldsSeparator);
buffer.append("' "); buffer.append("' ");
buffer.append(dbQueryOperator); buffer.append(dbMatemathicsOperator);
buffer.append(" "); buffer.append(" ");
} }
} }