
net.sf.aguacate.function.spi.impl.FunctionScript Maven / Gradle / Ivy
package net.sf.aguacate.function.spi.impl;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.sf.aguacate.function.FunctionEvalResult;
import net.sf.aguacate.function.Parameter;
import net.sf.aguacate.function.spi.AbstractFunction;
import net.sf.aguacate.script.DynamicCacheCoupling;
import net.sf.aguacate.util.config.database.DatabaseInterface;
import net.sf.aguacate.util.dynamic.bridge.Dynamic;
public class FunctionScript extends AbstractFunction {
private static final Logger LOGGER = LogManager.getLogger(FunctionScript.class);
private Dynamic dynamic;
private String functionName;
public FunctionScript(Collection methods, String name, String message, List parameters,
String outputName, String scriptName, String functionName) {
super(methods, name, message, parameters, outputName);
dynamic = DynamicCacheCoupling.get(scriptName);
assert dynamic != null;
this.functionName = functionName;
}
@Override
public FunctionEvalResult evaluate(DatabaseInterface databaseInterface, DataSource dataSource,
Map context) {
Parameter[] parameters = getParameters();
int length = parameters.length;
Object args[] = new Object[length];
for (int i = 0; i < length; i++) {
Parameter parameter = parameters[i];
args[i] = context.get(parameter.getName());
}
String msg = MessageFormat.format(getMessage(), args);
Object value = dynamic.exec(functionName, args);
if (value instanceof Map) {
@SuppressWarnings("unchecked")
Map map = (Map) value;
String message = (String) map.get("message");
Object val = map.get("value");
if (message == null) {
if (val == null) {
logFailure(msg);
return new FunctionEvalResult("Missing state", null);
} else {
logSuccess(msg);
return new FunctionEvalResult(null, val);
}
} else {
if (val == null) {
logFailure(msg);
return new FunctionEvalResult(message, null);
} else {
logFailure(msg);
LOGGER.warn("Invalid state: message = {} & value = {}", message, val);
return new FunctionEvalResult("Invalid state", null);
}
}
} else {
logFailure(msg);
LOGGER.warn("Invalid state: {}", value);
return new FunctionEvalResult("Invalid state", null);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy