Commit 7227666e authored by Nico Mack's avatar Nico Mack

Added key/value property to Declaration

SqlExecutor is able to return MapVariable results.
parent e521ffc6
......@@ -17,6 +17,8 @@ INVOKE_ELEMENT=invoke
INDEX_ATTRIBUTE=index
FORMAT_ATTRIBUTE=format
FIELD_ATTRIBUTE=field
KEY_ATTRIBUTE=key
VALUE_ATTRIBUTE=value
INVOKE_ELEMENT=invoke
INVOKING_ATTRIBUTE=invoking
EPSILON_ATTRIBUTE=epsilon
......
......@@ -19,6 +19,7 @@ import lu.list.itis.dkd.tui.cps.variable.VectorVariable;
import lu.list.itis.dkd.tui.utility.StringUtils;
import com.jgoodies.common.base.Preconditions;
import com.jgoodies.common.base.Strings;
/**
* Class modeling the fundamental properties for declaring variables in the context of equations.
......@@ -35,10 +36,17 @@ public class Declaration implements Cloneable {
protected String field;
protected String equate;
protected String format;
protected String key;
protected String value;
protected int index;
protected Variable<?> variable;
protected boolean invoking;
// ***************************************************************************
// * Constants *
// ***************************************************************************
// ---------------------------------------------------------------------------
// ***************************************************************************
// * Constructor(s) *
......@@ -64,6 +72,17 @@ public class Declaration implements Cloneable {
// ---------------------------------------------------------------------------
public Declaration(Variable<?> variable, String key, String value) {
Preconditions.checkArgument((variable instanceof MapVariable), StringUtils.build("Variable {} is not a MapVariable!", variable.getName())); //$NON-NLS-1$
this.variable = variable;
this.key = key;
this.value = value;
this.index = -1;
this.invoking = true;
}
// ---------------------------------------------------------------------------
public Declaration(Variable<?> variable, int index) {
Preconditions.checkArgument((variable instanceof VectorVariable), StringUtils.build("Variable {} is not a VectorVariable!", variable.getName())); //$NON-NLS-1$
this.variable = variable;
......@@ -147,6 +166,30 @@ public class Declaration implements Cloneable {
return field;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for field.
*
* @return The value of field.
*/
// ---------------------------------------------------------------------------
public String getKey() {
return key;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for field.
*
* @return The value of field.
*/
// ---------------------------------------------------------------------------
public String getValue() {
return value;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for index.
......@@ -186,8 +229,10 @@ public class Declaration implements Cloneable {
StringBuilder renderer = new StringBuilder();
renderer.append(variable.getName());
if ((field != null) && (!field.isEmpty())) {
if (Strings.isNotBlank(field)) {
renderer.append("{").append(field).append("}");
} else if (Strings.isNotBlank(key) && Strings.isNotBlank(value)) {
renderer.append("{").append(key).append("} = ").append(value);
} else if (index >= 0) {
renderer.append("[").append(index).append("]");
}
......
......@@ -302,6 +302,12 @@ public class EquationSystemBuilder {
attribute = this.getAttribute(element, EquationSystemBundle.FIELD_ATTRIBUTE);
attributes.put(EquationSystemBundle.FIELD_ATTRIBUTE, attribute);
attribute = this.getAttribute(element, EquationSystemBundle.KEY_ATTRIBUTE);
attributes.put(EquationSystemBundle.KEY_ATTRIBUTE, attribute);
attribute = this.getAttribute(element, EquationSystemBundle.VALUE_ATTRIBUTE);
attributes.put(EquationSystemBundle.VALUE_ATTRIBUTE, attribute);
attribute = this.getAttribute(element, EquationSystemBundle.INVOKING_ATTRIBUTE);
attributes.put(EquationSystemBundle.INVOKING_ATTRIBUTE, attribute);
......@@ -401,8 +407,12 @@ public class EquationSystemBuilder {
String field = attributes.get(EquationSystemBundle.FIELD_ATTRIBUTE);
String index = attributes.get(EquationSystemBundle.INDEX_ATTRIBUTE);
if ((field != null) && !field.isEmpty()) {
String key = attributes.get(EquationSystemBundle.KEY_ATTRIBUTE);
String value = attributes.get(EquationSystemBundle.VALUE_ATTRIBUTE);
if (!Strings.isNullOrEmpty(field)) {
declaration = new Declaration(variable, field);
} else if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value)) {
declaration = new Declaration(variable, key, value);
} else if ((index != null) && !index.isEmpty()) {
Integer numericIndex = StringUtils.getIntegerValue(index);
if (numericIndex == null) {
......
......@@ -16,6 +16,10 @@ abstract public class Executor {
private static Logger LOGGER = LoggerFactory.getLogger(Executor.class.getSimpleName());
protected static final String PARAMETER_TEMPLATE = "Parameter -> {} = {}";
protected static final String RESULT_TEMPLATE_PLAIN = "Result <- {} = {}";
protected static final String RESULT_TEMPLATE_EQUATE = "Result <- {}'{'{}'}' = {}";
public Executor() {
executionErrors = new CharArrayWriter();
}
......
......@@ -21,6 +21,7 @@ import lu.list.itis.dkd.tui.cps.variable.VectorVariable;
import com.google.common.base.Strings;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -87,6 +88,8 @@ public class SqlExecutor extends Executor {
private static final String VECTOR = "@"; //$NON-NLS-1$
private static final String MAP = "%"; //$NON-NLS-1$
private static final int ABBREVIATE_LENGTH = 1024;
// ***************************************************************************
// * Constructor(s) *
// ***************************************************************************
......@@ -381,6 +384,38 @@ public class SqlExecutor extends Executor {
// ---------------------------------------------------------------------------
private void retrieveMultipleResults(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
if (Strings.isNullOrEmpty(declaration.getEquate())) {
// No Equate specified! Check whether we have a key-value pair declaration
if (!Strings.isNullOrEmpty(declaration.getKey()) && !Strings.isNullOrEmpty(declaration.getValue())) {
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
int[] indices = new int[2];
indices[0] = columnNames.indexOf(declaration.getKey());
indices[1] = columnNames.indexOf(declaration.getValue());
this.setMap(map, null, indices);
} else {
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(variable.getName());
this.setVector(vector, index);
}
} else {
// Equate specified! Check whether we have a field declaration
if (!Strings.isNullOrEmpty(declaration.getField())) {
MapVariable<?, ?> map = (MapVariable<?, ?>) variable;
int[] indices = new int[1];
indices[0] = columnNames.indexOf(declaration.getEquate());
this.setMap(map, null, indices);
} else {
VectorVariable<?> vector = (VectorVariable<?>) variable;
int index = columnNames.indexOf(declaration.getEquate());
this.setVector(vector, index);
}
}
}
// ---------------------------------------------------------------------------
private void setVector(VectorVariable<?> vector, int columnIndex) {
// Suspend notification of input change listeners while populating vector
......@@ -392,12 +427,52 @@ public class SqlExecutor extends Executor {
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Result <- {} = {}", vector.getName(), vector.getValue()); //$NON-NLS-1$
List<?> value = vector.getValue();
String rendered = (value != null) ? StringUtils.abbreviate(value.toString(), ABBREVIATE_LENGTH) : "NULL";
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, vector.getName(), rendered); // $NON-NLS-1$
}
vector.suspendListenerNotification(false);
vector.notifyInputChangeListeners();
}
// ---------------------------------------------------------------------------
@SuppressWarnings("unchecked")
private <K, V> void setMap(MapVariable<K, V> map, String field, int[] indices) {
// Suspend notification of input change listeners while populating map
map.suspendListenerNotification(true);
map.clear();
if (!Strings.isNullOrEmpty(field) && indices.length == 1) {
List<V> list = new ArrayList<>();
for (Object[] values : columnValues) {
V value = (V) values[indices[0]];
list.add(value);
}
map.put((K) field, (V) list);
if (LOGGER.isInfoEnabled()) {
String rendered = StringUtils.abbreviate(list.toString(), ABBREVIATE_LENGTH);
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, map.getName(), rendered); // $NON-NLS-1$
}
} else if (indices.length == 2) {
for (Object[] values : columnValues) {
K key = (K) values[indices[0]];
V value = (V) values[indices[1]];
map.put(key, value);
}
if (LOGGER.isInfoEnabled()) {
String rendered = StringUtils.abbreviate(map.toString(), ABBREVIATE_LENGTH);
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, map.getName(), rendered); // $NON-NLS-1$
}
} else {
LOGGER.error("Failed to set Map {}! Declaration must specify either a field or a key-value pair!");
}
map.suspendListenerNotification(false);
map.notifyInputChangeListeners();
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -428,7 +503,7 @@ public class SqlExecutor extends Executor {
}
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Parameter -> {} = {}", identifier, value); //$NON-NLS-1$
LOGGER.info(Executor.PARAMETER_TEMPLATE, identifier, value); // $NON-NLS-1$
}
}
......@@ -498,33 +573,28 @@ public class SqlExecutor extends Executor {
*/
@Override
public Variable<?> get(Declaration declaration) {
Variable<?> variable = declaration.getVariable();
String column = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
int index = columnNames.indexOf(column);
if (index >= 0) {
if (columnValues.size() > 1) {
// Column Values are vectors, not scalars
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable;
Variable<?> variable = declaration.getVariable();
this.setVector(vector, index);
} else {
LOGGER.warn("Query returned multiple rows but specified variable {} is scalar!", variable.getName()); //$NON-NLS-1$
}
} else {
// Column value is a scalar
if (columnValues.size() > 1) {
this.retrieveMultipleResults(declaration);
} else {
String column = (Strings.isNullOrEmpty(declaration.getEquate())) ? variable.getName() : declaration.getEquate();
int index = columnNames.indexOf(column);
if (index >= 0) {
if (!columnValues.isEmpty()) {
Object[] values = columnValues.get(0);
Object value = ((index >= 0) && (index < values.length)) ? values[index] : null;
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Result <- {} = {}", variable.getName(), values[index]); //$NON-NLS-1$
String rendered = (value != null) ? StringUtils.abbreviate(value.toString(), 256) : "NULL";
LOGGER.info(Executor.RESULT_TEMPLATE_PLAIN, variable.getName(), rendered); // $NON-NLS-1$
}
if (variable instanceof VectorVariable) {
VectorVariable<?> vector = (VectorVariable<?>) variable;
this.setVector(vector, index);
} else {
variable.setValueFromObject(values[index]);
variable.setValueFromObject(value);
}
} else {
LOGGER.info("Query returned no results!"); //$NON-NLS-1$
......
......@@ -48,6 +48,8 @@ public class EquationSystemBundle extends NLS {
public static String INDEX_ATTRIBUTE;
public static String FORMAT_ATTRIBUTE;
public static String FIELD_ATTRIBUTE;
public static String KEY_ATTRIBUTE;
public static String VALUE_ATTRIBUTE;
public static String INVOKING_ATTRIBUTE;
public static String INVOKE_ELEMENT;
public static String EXECUTOR_ATTRIBUTE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment