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

net.sf.aguacate.context.spi.sql.ContextProcessorSql Maven / Gradle / Ivy

There is a newer version: 0.10.9
Show newest version
package net.sf.aguacate.context.spi.sql;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import net.sf.aguacate.configuration.field.format.FieldFormat;
import net.sf.aguacate.context.ContextProcessor;
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 ContextProcessorSql implements ContextProcessor {

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

	private final DatabaseBridge databaseBridge;

	private final Function[] sentences;

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

	@Override
	public Object process(HttpServletRequest request, HttpServletResponse response, String method,
			Map context, Map outputFields) {
		FunctionContext functionContext = new FunctionContext(method, databaseBridge, request, response, outputFields);
		try {
			return process(method, context, functionContext);
		} catch (RuntimeException | SQLException e) {
			LOGGER.warn("Rollback on exception");
			functionContext.rollback();
			throw new IllegalStateException(e);
		} finally {
			functionContext.close();
		}
	}

	Object process(String method, Map context, FunctionContext functionContext) throws SQLException {
		FunctionEvalResult last = null;
		for (Function sentence : sentences) {
			String name = sentence.getName();
			if (sentence.validFor(method)) {
				LOGGER.trace("trying to execute {} with: {}", name, context);
				FunctionEvalResult sentenceResult = sentence.evaluate(functionContext, context);
				if (sentenceResult.isSuccess()) {
					LOGGER.debug("succesful execution of {}", name);
					String outputName = sentence.getOutputName();
					if (outputName != null) {
						String[] outputContext = sentence.getOutputContext();
						if (outputContext != null && outputContext.length > 0) {
							Map ctx = context;
							for (String ctxName : outputContext) {
								@SuppressWarnings("unchecked")
								Map temp = (Map) ctx.get(ctxName);
								// TODO: check if temp is null
								ctx = temp;
							}
							LOGGER.trace("working ctx: {}", ctx);
							ctx.put(outputName, sentenceResult.getData());
						} else {
							context.put(outputName, sentenceResult.getData());
						}
					}
					LOGGER.trace("context after {}: {}", name, context);
					last = sentenceResult;
				} else {
					LOGGER.warn("unsuccesful execution of {}. Rollback transaction", name);
					functionContext.rollback();
					return null;
				}
			} else {
				LOGGER.trace("avoid execution: {}", name);
			}
		}
		if (last == null) {
			return null;
		} else {
			return last.getData();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy