package org.openforis.collect.io.metadata.collectearth;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.openforis.collect.earth.app.server.AbstractPlacemarkDataController;
import org.openforis.collect.earth.app.server.PlacemarkUpdateRequest;
import org.openforis.collect.earth.app.service.EarthSurveyService;
import org.openforis.collect.io.metadata.collectearth.balloon.CollectEarthBalloonGenerator;
import org.openforis.collect.manager.SessionManager;
import org.openforis.collect.manager.SurveyManager;
import org.openforis.collect.model.CollectSurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class CollectEarthBalloonPreviewController extends AbstractPlacemarkDataController {
	
	@Autowired
	private EarthSurveyService earthSurveyService;
	@Autowired
	@Qualifier("sessionManager")
	private SessionManager sessionManager;
	@Autowired
	private SurveyManager surveyManager;

	@RequestMapping(value="/preview_placemark-info-expanded", method = GET)
	protected void placemarkInfoExpanded(@RequestParam("id") String placemarkId, HttpServletResponse response) throws IOException {
		CollectSurvey survey = sessionManager.getActiveDesignerSurvey();
		earthSurveyService.setCollectSurvey(survey);
		super.placemarkInfoExpanded(placemarkId, response);
	}

	@RequestMapping(value="/preview_save-data-expanded", method = POST)
	public void saveDataExpanded(PlacemarkUpdateRequest updateRequest, HttpServletResponse response) throws IOException {
		super.saveDataExpanded(updateRequest, response);
	}
	
	@RequestMapping(value = "/collectearthpreview.html", method = GET)
	public void showCollectEarthBalloonPreview(HttpServletResponse response, 
			@RequestParam("surveyId") Integer surveyId, @RequestParam("lang") String languageCode) throws IOException  {
		CollectSurvey survey = surveyManager.loadSurvey(surveyId);
		CollectEarthBalloonGenerator generator = new CollectEarthBalloonGenerator(survey, languageCode, true);
		String html = generator.generateHTML();
		html = html.replace("earth.js", "earth_new.js");
		writeHtmlToResponse(response, html);
	}

	private void writeHtmlToResponse(HttpServletResponse response, String html) throws IOException {
		PrintWriter writer = null;
		try {
			response.setContentType(MediaType.TEXT_HTML_VALUE);
			writer = new PrintWriter(response.getOutputStream());
			writer.print(html);
			writer.flush();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			IOUtils.closeQuietly(writer);
		}
	}
}

@@ -54,156 +54,8 @@
 h;%0A%0A
-import static org.springframework.web.bind.annotation.RequestMethod.GET;%0Aimport static org.springframework.web.bind.annotation.RequestMethod.POST;%0A%0A
 impo
@@ -159,46 +159,8 @@
 e;%0A%0A
-import org.apache.commons.io.IOUtils;%0A
 impo
@@ -884,21 +884,77 @@
 otation.
-Reque
+GetMapping;%0Aimport org.springframework.web.bind.annotation.Po
 stMappin
@@ -1122,17 +1122,16 @@
 oller %7B%0A
-%09
 %0A%09@Autow
@@ -1314,30 +1314,37 @@
 Manager;%0A%0A%09@
-Reques
+Override%0A%09@Ge
 tMapping(val
@@ -1384,34 +1384,17 @@
 ded%22
-, method = GET)%0A%09protected
+)%0A%09public
  voi
@@ -1682,21 +1682,29 @@
 ;%0A%09%7D%0A%0A%09@
-Reque
+Override%0A%09@Po
 stMappin
@@ -1744,23 +1744,8 @@
 ded%22
-, method = POST
 )%0A%09p
@@ -1919,18 +1919,13 @@
 %0A%09%7D%0A
-%09
 %0A%09@
-Reques
+Ge
 tMap
@@ -1968,22 +1968,8 @@
 tml%22
-, method = GET
 )%0A%09p
@@ -2039,17 +2039,16 @@
 esponse,
- 
 %0A%09%09%09@Req
@@ -2554,16 +2554,22 @@
 ion %7B%0A%09%09
+try ( 
 PrintWri
@@ -2586,19 +2586,52 @@
  = n
-ull;%0A%09%09try 
+ew PrintWriter(response.getOutputStream()) )
 %7B%0A%09%09
@@ -2687,65 +2687,8 @@
 E);%0A
-%09%09%09writer = new PrintWriter(response.getOutputStream());%0A
 %09%09%09w
@@ -2789,55 +2789,8 @@
 e);%0A
-%09%09%7D finally %7B%0A%09%09%09IOUtils.closeQuietly(writer);%0A
 %09%09%7D%0A

Update to modern request mapping