All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.aguacate.context.impl.ContextValidatorSql Maven / Gradle / Ivy

package net.sf.aguacate.context.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.sf.aguacate.context.ContextValidationResult;
import net.sf.aguacate.context.ContextValidator;
import net.sf.aguacate.function.Function;
import net.sf.aguacate.function.FunctionContext;
import net.sf.aguacate.function.FunctionEvalResult;
import net.sf.aguacate.util.config.database.DatabaseBridge;

public class ContextValidatorSql implements ContextValidator {

	private static final Logger LOGGER = LogManager.getLogger(ContextValidatorSql.class);

	private final DatabaseBridge databaseBridge;

	private final Function[] functions;

	public ContextValidatorSql(DatabaseBridge databaseBridge, Collection functions) {
		this.databaseBridge = databaseBridge;
		this.functions = functions.toArray(new Function[functions.size()]);
	}

	@Override
	public ContextValidationResult validate(String method, Map context) {
		FunctionContext internalContext = new FunctionContext(databaseBridge);
		try {
			for (Function function : functions) {
				String name = function.getName();
				if (function.validFor(method)) {
					LOGGER.trace("execution {}", name);
					FunctionEvalResult result = function.evaluate(internalContext,
							context);
					boolean success = result.isSuccess();
					LOGGER.debug("execution {}: {}", name, success);
					if (success) {
						String outputName = function.getOutputName();
						if (outputName != null) {
							context.put(outputName, result.getData());
						}
						LOGGER.trace("new context: {}", context);
					} else {
						return new ContextValidationResult(Collections.singletonList(result), null);
					}
				} else {
					LOGGER.info("avoid execution: {}", name);
				}
			}
		} finally {
			internalContext.close();
		}
		return new ContextValidationResult(null, context);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy