package controllers.modules2;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;

import play.mvc.results.BadRequest;

import controllers.modules2.framework.ProcessedFlag;
import controllers.modules2.framework.TSRelational;
import controllers.modules2.framework.VisitorInfo;
import controllers.modules2.framework.procs.ProcessorSetup;
import controllers.modules2.framework.procs.PushOrPullProcessor;
import controllers.modules2.framework.procs.PushProcessorAbstract;

public class DateFormatMod extends PushOrPullProcessor {

	private String timeColumnName;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
	
	@Override
	public String init(String pathStr, ProcessorSetup nextInChain, VisitorInfo visitor, HashMap<String, String> options) {
		String newPath = super.init(pathStr, nextInChain, visitor, options);
		//default to the superclasses timecolumn
		timeColumnName = timeColumn;
		//only if overridden in the options use that col name instead
		String timeColNameOption = options.get("columnName");
		if (StringUtils.isNotBlank(timeColNameOption))
			timeColumnName = timeColNameOption;
		
		String dateFormatOption = options.get("dateFormat");
		if (StringUtils.isNotBlank(dateFormatOption)) {
			try {
				dateFormat = new SimpleDateFormat(dateFormatOption);
			}
			catch (IllegalArgumentException iae) {
				throw new BadRequest("The date format you specified ("+dateFormatOption+") is not a valid date format.  See this page for information on valid date formats:  http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
			}
		}
		
		return newPath;
	}
	
	@Override
	protected TSRelational modifyRow(TSRelational row) {
		BigInteger val = (BigInteger)row.get(timeColumnName);
		if(val != null)
			row.put(timeColumnName, formatDate(val));
		
		return row;
	}

	private String formatDate(BigInteger val) {
		return dateFormat.format(new Date(val.longValue()));
	}

}

@@ -138,32 +138,59 @@
 mpleDateFormat;%0A
+import java.util.Calendar;%0A
 import java.util
@@ -221,16 +221,43 @@
 HashMap;
+%0Aimport java.util.TimeZone;
 %0A%0Aimport
@@ -771,16 +771,108 @@
 mnName;%0A
+%09private String dateFormatString = %22yyyyMMdd'T'HHmmss'Z'%22;%0A%09private String timeZone = null;%0A
 %09private
@@ -923,38 +923,32 @@
 eFormat(
-%22yyyyMMdd'T'HHmmss'Z'%22
+dateFormatString
 );%0A%09%0A%09@O
@@ -1545,16 +1545,57 @@
 %09%09try %7B%0A
+%09%09%09%09dateFormatString = dateFormatOption;%0A
 %09%09%09%09date
@@ -1936,24 +1936,157 @@
 %09%09%09%7D%0A%09%09%7D%0A%09%09%0A
+%09%09String timeZoneOption = options.get(%22timeZone%22);%0A%09%09if (StringUtils.isNotBlank(timeZoneOption))%0A%09%09%09timeZone = timeZoneOption;%0A%09%09%0A%09%09%0A
 %09%09return new
@@ -2346,16 +2346,432 @@
  val) %7B%0A
+%09%09TimeZone tz = TimeZone.getTimeZone(%22GMT%22);%0A%09%09Date dateValue = new Date(val.longValue());%0A%09%09Calendar calValue = Calendar.getInstance(tz);%0A%09%09//Calendar calValue = Calendar.getInstance();%0A%09%09calValue.setTime(dateValue);%0A%09%09%0A%09%09if (dateFormatString.endsWith(%22'Z'%22))%0A%09%09%09dateFormat.setTimeZone(TimeZone.getTimeZone(%22GMT%22));%0A%09%09if (StringUtils.isNotBlank(timeZone))%0A%09%09%09dateFormat.setTimeZone(TimeZone.getTimeZone(timeZone));%0A
 %09%09return

handle timezones manually