/*
 * $Id$
 * $URL$
 */

package org.subethamail.core.filter;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.subethamail.core.lists.i.ListData;
import org.subethamail.core.plugin.i.Filter;
import org.subethamail.core.plugin.i.FilterContext;
import org.subethamail.core.util.Transmute;
import org.subethamail.entity.EnabledFilter;
import org.subethamail.entity.FilterArgument;
import org.subethamail.entity.MailingList;


/**
 * Implementation of the FilterContext
 * 
 * @author Jeff Schnitzer
 */
class FilterContextImpl implements FilterContext
{
	/** */
	EnabledFilter enabledFilter;
	Filter filter;
	MimeMessage msg;

	/** 
	 */
	public FilterContextImpl(EnabledFilter enabledFilter, Filter filter, MimeMessage msg)
	{
		this.enabledFilter = enabledFilter;
		this.filter = filter;
		this.msg = msg;
	}
	
	/**
	 * @see FilterContext#getListData()
	 */
	public ListData getListData()
	{
		MailingList ml = this.enabledFilter.getList();
		return Transmute.mailingList(ml);
	}
	
	/**
	 * @see FilterContext#getMimeMessage()
	 */
	public MimeMessage getMimeMessage()
	{
		return this.msg;
	}

	/**
	 * @see FilterContext#getArgument(String)
	 */
	public Object getArgument(String name)
	{
		FilterArgument arg = this.enabledFilter.getArguments().get(name);
		if (arg == null)
			return null;
		else
			return arg.getValue();
	}

	/**
	 * @see FilterContext#expand(String)
	 */
	public String expand(String data, Map<String, Object> context)
	{
		return "";
	}
}
@@ -59,24 +59,54 @@
 .filter;%0D%0A%0D%0A
+import java.io.StringWriter;%0D%0A
 import java.
@@ -157,24 +157,111 @@
 Message;%0D%0A%0D%0A
+import org.apache.velocity.VelocityContext;%0D%0Aimport org.apache.velocity.app.Velocity;%0D%0A
 import org.s
@@ -657,16 +657,40 @@
 nitzer%0D%0A
+ * @author Jon Stevens%0D%0A
  */%0D%0Acla
@@ -815,16 +815,38 @@
 e msg;%0D%0A
+%09ListData listData;%0D%0A%09
 %0D%0A%09/** %0D
@@ -1120,16 +1120,96 @@
 ()%0D%0A%09%7B%0D%0A
+%09%09// internally cache the object for speed%0D%0A%09%09if (this.listData == null)%0D%0A%09%09%7B%0D%0A%09
 %09%09Mailin
@@ -1252,22 +1252,32 @@
 t();%0D%0A%09%09
-return
+%09this.listData =
  Transmu
@@ -1293,24 +1293,54 @@
 gList(ml);%0D%0A
+%09%09%7D%0D%0A%09%09return this.listData;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09/**%0D
@@ -1741,24 +1741,53 @@
 xpand(String
+, Map%3CString, Object%3E context
 )%0D%0A%09 */%0D%0A%09pu
@@ -1816,12 +1816,16 @@
 ing 
-data
+template
 , Ma
@@ -1862,17 +1862,564 @@
 %0D%0A%09%09
-return %22%22
+VelocityContext vctx = new VelocityContext();%0D%0A%09    for (Map.Entry%3CString, Object%3E e : context.entrySet())%0D%0A%09    %7B%0D%0A%09    %09if (e.getKey().equals(%22mail%22) %7C%7C e.getKey().equals(%22list%22))%0D%0A%09    %09%09continue;%0D%0A%09    %09vctx.put(e.getKey(),e.getValue());%0D%0A%09    %7D%0D%0A%09    vctx.put(%22mail%22, this.msg);%0D%0A%09    vctx.put(%22list%22, this.getListData());%0D%0A%0D%0A%09    StringWriter writer = new StringWriter(4096);%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09Velocity.mergeTemplate(template, %22UTF-8%22, vctx, writer);%0D%0A%09%09%7D%0D%0A%09%09catch (Exception e1)%0D%0A%09%09%7B%0D%0A%09%09%09// TODO: how to handle this?%0D%0A%09%09%7D%0D%0A%0D%0A%09%09return writer.toString()
 ;%0D%0A%09

implement the expand() method.