package de.tblsoft.solr.pipeline.filter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrSubstitutor;

import com.google.common.base.Strings;

import de.tblsoft.solr.pipeline.AbstractFilter;
import de.tblsoft.solr.pipeline.bean.Document;
import de.tblsoft.solr.pipeline.bean.Field;

public class MappingFilter extends AbstractFilter {

	private Map<String, List<String>> mapping = new HashMap<String, List<String>>();
	private Map<String, List<String>> mappingFunctions = new HashMap<String, List<String>>();
	private Map<String, String> joins = new HashMap<String, String>();

	@Override
	public void init() {

		List<String> tempMapping = getPropertyAsList("mapping",
				new ArrayList<String>());
		for (String v : tempMapping) {
			if (v.startsWith("join:")) {
				v = v.replace("join:", "");
				String[] s = v.split("=");
				joins.put(s[0], s[1]);
			} else {
				String[] s = v.split("->");

				String[] f = s[1].split(Pattern.quote("|"));

				List<String> mappingList = mapping.get(s[0]);
				if (mappingList == null) {
					mappingList = new ArrayList<String>();
				}
				mappingList.add(f[0]);
				mapping.put(s[0], mappingList);
				List<String> functions = new ArrayList<String>();
				for (int i = 1; i < f.length; i++) {
					functions.add(f[i]);
				}
				mappingFunctions.put(f[0], functions);
			}
		}
		super.init();
	}

	public String executeFunction(String function, String value) {
		switch (function) {
		case "md5":
			return DigestUtils.md5Hex(value);
		case "lowercase":
			return StringUtils.lowerCase(value);
		case "trim":
			return StringUtils.trim(value);
		}
		throw new IllegalArgumentException("The function: " + function
				+ " is not implemented.");
	}

	@Override
	public void document(Document document) {
		Document mappedDocument = new Document();
		for (Field f : document.getFields()) {
			List<String> mappedNameList = mapping.get(f.getName());
			if(mappedNameList == null) {
				continue;
			}

			for (String mappedName : mappedNameList) {
				List<String> mappedFunctions = mappingFunctions.get(mappedName);
				if (!Strings.isNullOrEmpty(mappedName)) {
					List<String> newValues = new ArrayList<String>();
					for (String value : f.getValues()) {
						String newValue = value;
						for (String function : mappedFunctions) {
							newValue = executeFunction(function, newValue);
						}
						newValues.add(newValue);
					}

					mappedDocument.setField(mappedName, newValues);
				}
			}
		}

		for (Map.Entry<String, String> entry : joins.entrySet()) {
			Map<String, String> documentMap = new HashMap<String, String>();
			for (Field field : document.getFields()) {
				documentMap.put(field.getName(), field.getValue());
			}
			StrSubstitutor sub = new StrSubstitutor(documentMap);
			String value = sub.replace(entry.getValue());
			mappedDocument.addField(entry.getKey(), value);
		}

		if (mappedDocument.getFields().size() != 0) {
			super.document(mappedDocument);
		}
	}

}

@@ -46,132 +46,178 @@
 ort 
-java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.regex.Pattern;%0A
+com.google.common.base.Strings;%0Aimport de.tblsoft.solr.pipeline.AbstractFilter;%0Aimport de.tblsoft.solr.pipeline.bean.Document;%0Aimport de.tblsoft.solr.pipeline.bean.Field;
 %0Aimp
@@ -375,178 +375,130 @@
 ort 
-com.google.common.base.Strings
+java.util.ArrayList
 ;%0A
-%0A
 import 
-de.tblsoft.solr.pipeline.AbstractFilter;%0Aimport de.tblsoft.solr.pipeline.bean.Document;%0Aimport de.tblsoft.solr.pipeline.bean.Field
+java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.regex.Pattern
 ;%0A%0Ap
@@ -1038,16 +1038,18 @@
 plit(%22=%22
+,2
 );%0A%09%09%09%09j
@@ -1626,17 +1626,32 @@
 ) %7B%0A
-%09%09switch 
+%0A        if(%22md5%22.equals
 (fun
@@ -1660,28 +1660,24 @@
 ion)
+)
  %7B%0A
-%09%09case %22md5%22:%0A%09%09%09
+            
 retu
@@ -1710,86 +1710,166 @@
 e);%0A
-%09%09case %22lowercase%22:%0A%09%09%09return StringUtils.lowerCase(value);%0A%09%09case %22trim%22:%0A%09%09%09
+        %7D else if (%22lowercase%22.equals(function)) %7B%0A            return StringUtils.lowerCase(value);%0A        %7D else if (%22trim%22.equals(function)) %7B%0A            
 retu
@@ -1896,19 +1896,26 @@
 value);%0A
-%09%09%7D
+        %7D%0A
 %0A%09%09throw

make the mapping filter java 6 compatible