package com.ctrip.ops.sysdev.filters;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.ctrip.ops.sysdev.baseplugin.BaseFilter;

import org.apache.log4j.Logger;
import com.ctrip.ops.sysdev.render.FreeMarkerRender;
import com.ctrip.ops.sysdev.render.TemplateRender;

@SuppressWarnings("ALL")
public class Add extends BaseFilter {
	private static final Logger logger = Logger.getLogger(Add.class.getName());

	public Add(Map config) {
		super(config);
	}

	private Map<String, TemplateRender> f;

	protected void prepare() {
		f = new HashMap<String, TemplateRender>();

		Map<String, String> fields = (Map<String, String>) config.get("fields");
		Iterator<Entry<String, String>> it = fields.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();

			String field = entry.getKey();
			String value = entry.getValue();

			try {
				this.f.put(field, new FreeMarkerRender(value, value));
			} catch (IOException e) {
				logger.fatal(e.getMessage());
				System.exit(1);
			}
		}
	}

	@Override
	protected Map filter(final Map event) {
		Iterator<Entry<String, TemplateRender>> it = f.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, TemplateRender> entry = it.next();

			String field = entry.getKey();
			TemplateRender render = entry.getValue();
			event.put(field, render.render(event));
		}
		return event;
	}
}

@@ -416,17 +416,20 @@
 ilter %7B%0A
-%09
+    
 private 
@@ -497,17 +497,20 @@
 me());%0A%0A
-%09
+    
 public A
@@ -526,18 +526,24 @@
 nfig) %7B%0A
-%09%09
+        
 super(co
@@ -549,21 +549,27 @@
 onfig);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 private 
@@ -600,17 +600,20 @@
 er%3E f;%0A%0A
-%09
+    
 protecte
@@ -631,18 +631,24 @@
 are() %7B%0A
-%09%09
+        
 f = new 
@@ -683,18 +683,24 @@
 er%3E();%0A%0A
-%09%09
+        
 Map%3CStri
@@ -764,18 +764,24 @@
 elds%22);%0A
-%09%09
+        
 Iterator
@@ -831,34 +831,40 @@
 t().iterator();%0A
-%09%09
+        
 while (it.hasNex
@@ -862,35 +862,44 @@
 it.hasNext()) %7B%0A
-%09%09%09
+            
 Map.Entry%3CString
@@ -920,35 +920,44 @@
 y = it.next();%0A%0A
-%09%09%09
+            
 String field = e
@@ -971,19 +971,28 @@
 tKey();%0A
-%09%09%09
+            
 String v
@@ -1021,79 +1021,106 @@
 );%0A%0A
-%09%09%09try %7B%0A%09%09%09%09this.f.put(field, new FreeMarkerRender(value, value));%0A%09%09%09
+            try %7B%0A                this.f.put(field, TemplateRender.getRender(value));%0A            
 %7D ca
@@ -1141,20 +1141,32 @@
 on e) %7B%0A
-%09%09%09%09
+                
 logger.f
@@ -1191,42 +1191,75 @@
 ));%0A
-%09%09%09%09System.exit(1);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09
+                System.exit(1);%0A            %7D%0A        %7D%0A    %7D%0A%0A    
 @Ove
@@ -1264,17 +1264,20 @@
 verride%0A
-%09
+    
 protecte
@@ -1308,18 +1308,24 @@
 vent) %7B%0A
-%09%09
+        
 Iterator
@@ -1386,18 +1386,24 @@
 ator();%0A
-%09%09
+        
 while (i
@@ -1417,19 +1417,28 @@
 xt()) %7B%0A
-%09%09%09
+            
 Map.Entr
@@ -1483,19 +1483,28 @@
 ext();%0A%0A
-%09%09%09
+            
 String f
@@ -1526,19 +1526,28 @@
 tKey();%0A
-%09%09%09
+            
 Template
@@ -1584,11 +1584,20 @@
 ();%0A
-%09%09%09
+            
 even
@@ -1636,14 +1636,26 @@
 ));%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 retu
@@ -1668,9 +1668,12 @@
 nt;%0A
-%09
+    
 %7D%0A%7D%0A

use new template