package org.openforis.collect.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.openforis.collect.io.metadata.collectearth.balloon.CollectEarthBalloonGenerator;
import org.openforis.collect.manager.RecordManager;
import org.openforis.collect.manager.SurveyManager;
import org.openforis.collect.model.CollectRecord;
import org.openforis.collect.model.CollectSurvey;
import org.openforis.collect.model.SurveySummary;
import org.openforis.idm.metamodel.EntityDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @author S. Ricci
 *
 */
@Controller
public class SurveyController extends BasicController {
	
	private static final String EDIT_SURVEY_VIEW = "editSurvey";

	@Autowired
	private RecordManager recordManager;
	@Autowired
	private SurveyManager surveyManager;

	@RequestMapping(value = "/surveys/summaries.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody
	List<Map<String, Object>> loadSummaries() throws Exception {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<SurveySummary> surveys = surveyManager.getSurveySummaries(Locale.ENGLISH.getLanguage());
		for (SurveySummary s : surveys) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", s.getId());
			map.put("uri", s.getUri());
			map.put("record_ids", getRecordIds(s));
			result.add(map);
		}
		return result;
	}

	private List<Integer> getRecordIds(SurveySummary s) {
		List<Integer> recordIds = new ArrayList<Integer>();
		CollectSurvey survey = surveyManager.getById(s.getId());
		List<EntityDefinition> rootEntities = survey.getSchema().getRootEntityDefinitions();
		EntityDefinition rootEntity = rootEntities.get(0);
		String rootEntityName = rootEntity.getName();
		List<CollectRecord> summaries = recordManager.loadSummaries(survey, rootEntityName);
		for (CollectRecord r : summaries) {
			recordIds.add(r.getId());
		}
		return recordIds;
	}
	
	@RequestMapping(value = "/survey/temp/{surveyId}/edit.htm", method = RequestMethod.GET)
	public ModelAndView editTemp(@PathVariable("surveyId") Integer surveyId, Model model) {
		model.addAttribute("temp_id", surveyId);
		return new ModelAndView(EDIT_SURVEY_VIEW);
	}
	
	@RequestMapping(value = "/survey/{surveyId}/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable("surveyId") Integer surveyId, Model model) {
		model.addAttribute("id", surveyId);
		return new ModelAndView(EDIT_SURVEY_VIEW);
	}
	
	@RequestMapping(value = "/collectearthpreview.html", method = RequestMethod.GET)
	public void showCollectEarthBalloonPreview(HttpServletResponse response, @RequestParam("surveyId") Integer surveyId)  {
		try {
			CollectSurvey survey = surveyManager.loadSurveyWork(surveyId);
			CollectEarthBalloonGenerator generator = new CollectEarthBalloonGenerator(survey);
			String html = generator.generateHTML();
			PrintWriter writer = new PrintWriter(response.getOutputStream());
			writer.print(html);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

@@ -3362,16 +3362,45 @@
   %7B%0A
+%09%09PrintWriter writer = null;%0A
 %09%09try %7B%0A
 %09%09%09C
@@ -3395,16 +3395,16 @@
 %09%09try %7B%0A
-
 %09%09%09Colle
@@ -3593,28 +3593,16 @@
 L();%0A%09%09%09
-PrintWriter 
 writer =
@@ -3670,16 +3670,35 @@
 (html);%0A
+%09%09%09writer.flush();%0A
 %09%09%7D catc
@@ -3715,16 +3715,16 @@
 on e) %7B%0A
-
 %09%09%09throw
@@ -3749,16 +3749,49 @@
 ion(e);%0A
+%09%09%7D finally %7B%0A%09%09%09writer.close();%0A
 %09%09%7D%0A%09%7D%0A%7D

Fix issue with Collect Earth survey preview not showing sometimes (flush writer)