package de.taimos.maven_redmine_plugin.model;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;

/**
 * @author thoeger
 * 
 */
public class DateDeserializer extends JsonDeserializer<Date> {

	@Override
	public Date deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException, JsonProcessingException {
		final String text = jp.getText();
		return DateDeserializer.parse(text);
	}

	static Date parse(final String text) throws JsonParseException {
		// BEWARE THIS IS UGLY CODE STYLE
		Date parsed = null;

		try {
			// Redmine 2.x 2012-01-06T14:43:04Z
			final SimpleDateFormat newDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
			parsed = newDF.parse(text);
		} catch (final ParseException e) {
			// cannot parse date so we try old format
		}

		if (parsed == null) {
			try {
				// Redmine 2.x Date only 2012-01-06
				final SimpleDateFormat newDF = new SimpleDateFormat("yyyy-MM-dd");
				parsed = newDF.parse(text);
			} catch (final ParseException e) {
				// cannot parse date so we try old format
			}
		}

		if (parsed == null) {
			try {
				// Redmine 1.x 2012/10/09 09:29:19 +0200
				final SimpleDateFormat oldDF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss Z");
				parsed = oldDF.parse(text);
			} catch (final ParseException e) {
				throw new RuntimeException("Cannot parse date");
			}
		}
		if (parsed == null) {
			try {
				// Redmine 1.x Date only 2012/10/09
				final SimpleDateFormat oldDF = new SimpleDateFormat("yyyy/MM/dd");
				parsed = oldDF.parse(text);
			} catch (final ParseException e) {
				throw new RuntimeException("Cannot parse date");
			}
		}

		return parsed;
	}
}

@@ -1068,34 +1068,36 @@
 date so we try o
-ld
+ther
  format%0A%09%09%7D%0A%0A%09%09i
@@ -1343,18 +1343,20 @@
 we try o
-ld
+ther
  format%0A
@@ -1603,56 +1603,51 @@
 %09%09%09%09
-throw new RuntimeException(%22Cannot parse date%22);
+// cannot parse date so we try other format
 %0A%09%09%09
@@ -1867,16 +1867,96 @@
 n e) %7B%0A%09
+%09%09%09// cannot parse date so we try other format%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (parsed == null) %7B%0A
 %09%09%09throw
@@ -1999,21 +1999,16 @@
 date%22);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09r

fix parse error