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

com.redislabs.riot.batch.processor.SpelProcessor Maven / Gradle / Ivy

package com.redislabs.riot.batch.processor;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionInvocationTargetException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import com.redislabs.riot.batch.MapAccessor;

import lombok.Getter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Accessors(fluent = true)
public class SpelProcessor implements ItemProcessor, Map> {

	private SpelExpressionParser parser = new SpelExpressionParser();
	private StandardEvaluationContext context;
	private Map expressions = new LinkedHashMap<>();
	@Getter
	private long index = 0;

	public SpelProcessor(DateFormat dateFormat, Map variables, Map fields) {
		context = new StandardEvaluationContext();
		context.setVariable("date", dateFormat);
		context.setVariable("context", this);
		variables.forEach((k, v) -> context.setVariable(k, parser.parseExpression(v).getValue(context)));
		Method geoMethod;
		try {
			geoMethod = getClass().getDeclaredMethod("geo", new Class[] { String.class, String.class });
			context.registerFunction("geo", geoMethod);
		} catch (NoSuchMethodException | SecurityException e) {
			log.error("Could not register geo function", e);
		}
		context.setPropertyAccessors(Arrays.asList(new MapAccessor()));
		fields.forEach((k, v) -> expressions.put(k, parser.parseExpression(v)));
	}

	public Map process(Map item) throws Exception {
		synchronized (context) {
			for (Entry entry : expressions.entrySet()) {
				try {
					Object value = entry.getValue().getValue(context, item);
					if (value != null) {
						item.put(entry.getKey(), value);
					}
				} catch (ExpressionInvocationTargetException e) {
					log.error("Error while evaluating field {} with item {}", entry.getKey(), item, e);
				}
			}
			index++;
		}
		return item;
	}

	public static String geo(String longitude, String latitude) {
		if (longitude == null || latitude == null) {
			return null;
		}
		return longitude + "," + latitude;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy