package ca.uwaterloo.iss4e.demand.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ca.uwaterloo.iss4e.demand.dao.ieso.ZonalDemandAndWeatherDAO;
import ca.uwaterloo.iss4e.demand.model.ieso.TransmissionZone;
import ca.uwaterloo.iss4e.demand.model.ieso.ZonalDemandAndWeather;

@Controller
@RequestMapping("/zone/{zoneString}/**")
public class ZonalDemandController implements ApplicationContextAware {
	Logger logger = LogManager.getLogger(this.getClass());
	private ApplicationContext applicationContext;

	@RequestMapping("/html")
	public String html(@PathVariable String zoneString, Model model) {		
		List<String> zoneStrings = new ArrayList<String>(10);
		zoneStrings.add("Bruce");
		zoneStrings.add("East");
		zoneStrings.add("Essa");
		zoneStrings.add("Niagara");
		zoneStrings.add("Northeast");
		zoneStrings.add("Northwest");
		zoneStrings.add("Ottawa");
		zoneStrings.add("Southwest");
		zoneStrings.add("Toronto");
		zoneStrings.add("West");
		model.addAttribute("zoneStrings", zoneStrings);
		
		model.addAttribute("zoneString", zoneString);
		return "zonal";
	}

	@RequestMapping(value = "/year/{year}/json", method = RequestMethod.GET)
	public @ResponseBody
	List<ZonalDemandAndWeather> zoneJson(@PathVariable String zoneString,
			@PathVariable Integer year) {
		ZonalDemandAndWeatherDAO zonalDemandAndWeatherDAO = (ZonalDemandAndWeatherDAO) applicationContext
				.getBean("zonalDemandAndWeatherDAO");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TransmissionZone transmissionZone = TransmissionZone.valueOf(zoneString
				.toUpperCase());

		List<ZonalDemandAndWeather> zonalDemandAndWeathers = null;
		try {
			Date startDate = sdf.parse(year + "-01-01 00:00:00");
			Date endDate = sdf.parse(year + "-12-31 23:59:59");
			zonalDemandAndWeathers = zonalDemandAndWeatherDAO
					.getZonalDemandAndWeather(transmissionZone, startDate,
							endDate);
			logger.debug("zonalDemandAndWeathers size="
					+ zonalDemandAndWeathers.size());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return zonalDemandAndWeathers;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}

@@ -435,16 +435,153 @@
 tAware;%0A
+import org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0A
 import o
@@ -618,24 +618,24 @@
 Controller;%0A
-
 import org.s
@@ -848,69 +848,8 @@
 hod;
-%0Aimport org.springframework.web.bind.annotation.ResponseBody;
 %0A%0Aim
@@ -1367,18 +1367,16 @@
 model) %7B
-%09%09
 %0A%09%09List%3C
@@ -1772,11 +1772,9 @@
 s);%0A
-%09%09
 %0A
+
 %09%09mo
@@ -1925,17 +1925,16 @@
 lic 
-@
 Response
 Body
@@ -1933,14 +1933,15 @@
 onse
-Body%0A%09
+Entity%3C
 List
@@ -1959,24 +1959,25 @@
 dAndWeather%3E
+%3E
  zoneJson(@P
@@ -1974,16 +1974,20 @@
 oneJson(
+%0A%09%09%09
 @PathVar
@@ -2010,20 +2010,17 @@
 eString,
-%0A%09%09%09
+ 
 @PathVar
@@ -2850,37 +2850,325 @@
 %0A%0A%09%09
-return zonalDemandAndWeathers
+// Attempt to mark JSON content cacheable to client since it doesn't change often%0A%09%09HttpHeaders responseHeaders = new HttpHeaders();%0A%09%09responseHeaders.set(%22cache-control%22, %22public, max-age=3600%22);%0A%09%09%0A%09%09return new ResponseEntity%3CList%3CZonalDemandAndWeather%3E%3E(zonalDemandAndWeathers, responseHeaders, HttpStatus.CREATED)
 ;%0A%09%7D

Set headers on ZonalDemandController JSON response so that it is marked cacheable.