package ca.corefacility.bioinformatics.irida.ria.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.Arrays;

/**
 * Replace JSP's with Thymeleaf templating.
 * 
 * @author Josh Adam <josh.adam@phac-aspc.gc.ca>
 */
@Configuration
public class ThymeleafConfiguration {
	private static final String TEMPLATE_MODE = "HTML5";
	private static final String TEMPLATE_PREFIX = "/pages/";
	private static final String TEMPLATE_SUFFIX = ".html";
	private static final int TEMPLATE_ORDER = 1;
	private static final boolean TEMPLATE_DEV_CACHEABLE = false;

	/**
	 * Add Thymeleaf as the view resolver.
	 * 
	 * @return {@link ViewResolver}
	 */
	@Bean
	public ViewResolver viewResolver() {
		ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
		resolver.setViewResolvers(Arrays.asList(thymeleafViewResolver()));
		return resolver;
	}

	/**
	 * Create a new {@link ServletContextTemplateResolver} and set defaults.
	 * 
	 * @return {@link ServletContextTemplateResolver}
	 */
	private ServletContextTemplateResolver templateResolver() {
		ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
		resolver.setPrefix(TEMPLATE_PREFIX);
		resolver.setSuffix(TEMPLATE_SUFFIX);
		resolver.setTemplateMode(TEMPLATE_MODE);
		resolver.setOrder(TEMPLATE_ORDER);

		// TODO: In production this needs to be set to true. Use environment
		// variable like jhipster?
		resolver.setCacheable(TEMPLATE_DEV_CACHEABLE);
		return resolver;
	}

	/**
	 * Thymeleaf needs a template engine, get a {@link SpringTemplateEngine}
	 * 
	 * @return {@link SpringTemplateEngine}
	 */
	private SpringTemplateEngine templateEngine() {
		SpringTemplateEngine engine = new SpringTemplateEngine();
		engine.setTemplateResolver(templateResolver());
		return engine;
	}

	/**
	 * Create the actual {@link ThymeleafViewResolver}
	 * 
	 * @return {@link ViewResolver}
	 */
	private ViewResolver thymeleafViewResolver() {
		ThymeleafViewResolver resolver = new ThymeleafViewResolver();
		resolver.setTemplateEngine(templateEngine());
		return resolver;
	}
}

@@ -51,16 +51,105 @@
 onfig;%0A%0A
+import java.util.Arrays;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -192,16 +192,16 @@
 n.Bean;%0A
-
 import o
@@ -253,16 +253,65 @@
 ration;%0A
+import org.springframework.core.env.Environment;%0A
 import o
@@ -614,34 +614,8 @@
 r;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -982,16 +982,81 @@
 ER = 1;%0A
+%09private static final String SPRING_PROFILE_DEVELOPMENT = %22dev%22;%0A
 %09private
@@ -1078,35 +1078,35 @@
 oolean TEMPLATE_
-DEV
+NOT
 _CACHEABLE = fal
@@ -1110,16 +1110,55 @@
 false;%0A%0A
+%09@Autowired%0A%09private Environment env;%0A%0A
 %09/**%0A%09 *
@@ -1907,103 +1907,179 @@
 %09// 
-TODO: In production this needs to be set to true. Use environment%0A%09%09// variable like jhipster?%0A
+Determine the spring profile that is being run.%0A%09%09// If it is in development we do not want the templates cached%0A%09%09if (env.acceptsProfiles(SPRING_PROFILE_DEVELOPMENT)) %7B%0A%09
 %09%09re
@@ -2107,19 +2107,19 @@
 EMPLATE_
-DEV
+NOT
 _CACHEAB
@@ -2119,24 +2119,28 @@
 CACHEABLE);%0A
+%09%09%7D%0A
 %09%09return res

Check to see if there is an active dev profile. If so we need to turn caching off so that we can refresh the browser when there is a change to the template as opposed to restarting Jetty.