package org.ctrip.ops.sysdev.filters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

public class BaseFilter{

	private static final Logger logger = Logger.getLogger(BaseFilter.class
			.getName());

	protected Map config;
	protected List<TemplateRender> IF;
	protected TemplateRender render;

	public BaseFilter(Map config) {
		this.config = config;

		if (this.config.containsKey("if")) {
			IF = new ArrayList<TemplateRender>();
			for (String c : (List<String>) this.config.get("if")) {
				try {
					IF.add(new FreeMarkerRender(c, c));
				} catch (IOException e) {
					logger.fatal(e.getMessage());
					System.exit(1);
				}
			}
		} else {
			IF = null;
		}

		this.prepare();
	}

	protected void prepare() {
	};

	public Map process(Map event) {
		boolean succuess = true;
		if (this.IF != null) {
			for (TemplateRender render : this.IF) {
				if (!render.render(event).equals("true")) {
					succuess = false;
					break;
				}
			}
		}
		if (succuess == true) {
			event = this.filter(event);
		}

		return event;
	};

	protected Map filter(Map event) {
		return null;
	}
}

@@ -84,24 +84,49 @@
 .ArrayList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -132,24 +132,24 @@
 .util.List;%0A
-
 import java.
@@ -319,16 +319,17 @@
 seFilter
+ 
 %7B%0A%0A%09priv
@@ -504,16 +504,91 @@
  render;
+%0A%09protected String tagOnFailure;%0A%09protected ArrayList%3CString%3E removeFields;
 %0A%0A%09publi
@@ -956,16 +956,251 @@
 l;%0A%09%09%7D%0A%0A
+%09%09if (config.containsKey(%22tag_on_failure%22)) %7B%0A%09%09%09this.tagOnFailure = (String) config.get(%22tag_on_failure%22);%0A%09%09%7D else %7B%0A%09%09%09this.tagOnFailure = null;%0A%09%09%7D%0A%0A%09%09this.removeFields = (ArrayList%3CString%3E) this.config%0A%09%09%09%09.get(%22remove_fields%22);%0A%0A
 %09%09this.p
@@ -1590,16 +1590,16 @@
 vent) %7B%0A
-
 %09%09return
@@ -1607,11 +1607,603 @@
 null;%0A%09%7D
+%0A%0A%09public void postProcess(Map event, boolean ifsuccess) %7B%0A%09%09if (ifsuccess == false) %7B%0A%09%09%09if (this.tagOnFailure == null) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%09if (!event.containsKey(%22tags%22)) %7B%0A%09%09%09%09event.put(%22tags%22,%0A%09%09%09%09%09%09new ArrayList%3CString%3E(Arrays.asList(this.tagOnFailure)));%0A%09%09%09%7D else %7B%0A%09%09%09%09Object tags = event.get(%22tags%22);%0A%09%09%09%09if (tags.getClass() == ArrayList.class%0A%09%09%09%09%09%09&& ((ArrayList) tags).indexOf(this.tagOnFailure) == -1) %7B%0A%09%09%09%09%09((ArrayList) tags).add(this.tagOnFailure);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D else if (this.removeFields != null) %7B%0A%09%09%09for (String f : this.removeFields) %7B%0A%09%09%09%09event.remove(f);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

add postProcess in BaseFiler