package org.openforis.collect.web.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletResponse;

import org.openforis.collect.manager.SessionManager;
import org.openforis.collect.manager.SurveyManager;
import org.openforis.collect.metamodel.SurveyViewGenerator;
import org.openforis.collect.metamodel.SurveyViewGenerator.SurveyView;
import org.openforis.collect.model.CollectSurvey;
import org.openforis.collect.persistence.RecordUnlockedException;
import org.openforis.commons.web.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
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.context.WebApplicationContext;

/**
 * @author S. Ricci
 * 
 */
@Controller
@Scope(WebApplicationContext.SCOPE_SESSION)
@RequestMapping(value = "/session/")
public class SessionController {
	
	@Autowired
	private SessionManager sessionManager;
	@Autowired
	private SurveyManager surveyManager;
	
	@RequestMapping(value = "ping.json", method = RequestMethod.GET)
	public @ResponseBody Response ping(@RequestParam(value="editing", required = false, defaultValue = "false" ) Boolean editing) throws RecordUnlockedException {
		if ( editing ) {
			sessionManager.checkIsActiveRecordLocked();
		}
		return new Response();
	}
	
	@RequestMapping(value = "survey.json", method = RequestMethod.POST)
	public @ResponseBody Response setActiveSurvey(@RequestParam int surveyId) {
		CollectSurvey survey = surveyManager.getOrLoadSurveyById(surveyId);
		sessionManager.setActiveSurvey(survey);
		return new Response();
	}
	
	@RequestMapping(value = "survey.json", method = RequestMethod.GET)
	public @ResponseBody SurveyView getActiveSurvey(HttpServletResponse response) {
		CollectSurvey survey = sessionManager.getActiveSurvey();
		Locale locale = sessionManager.getSessionState().getLocale();
		if (locale == null) {
			locale = Locale.ENGLISH;
		}
		SurveyViewGenerator viewGenerator = new SurveyViewGenerator(locale);
		if (survey == null) {
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			return null;
		} else {
			SurveyView view = viewGenerator.generateView(survey);
			return view;
		}
	}
	
}

@@ -478,16 +478,65 @@
 ption;%0D%0A
+import org.openforis.commons.web.HttpResponses;%0D%0A
 import o
@@ -2139,34 +2139,26 @@
 urvey = 
-sessionManager.get
+getUpdated
 ActiveSu
@@ -2166,16 +2166,119 @@
 vey();%0D%0A
+%09%09if (survey == null) %7B%0D%0A%09%09%09HttpResponses.setNoContentStatus(response);%0D%0A%09%09%09return null;%0D%0A%09%09%7D else %7B%0D%0A%09
 %09%09Locale
@@ -2332,24 +2332,25 @@
 ocale();%0D%0A%09%09
+%09
 if (locale =
@@ -2360,16 +2360,17 @@
 ull) %7B%0D%0A
+%09
 %09%09%09local
@@ -2392,19 +2392,21 @@
 ISH;%0D%0A%09%09
+%09
 %7D%0D%0A
+%09
 %09%09Survey
@@ -2475,191 +2475,819 @@
 %0D%0A%09%09
-if (survey == null) %7B%0D%0A%09%09%09response.setStatus(HttpServletResponse.SC_NO_CONTENT);%0D%0A%09%09%09return null;%0D%0A%09%09%7D else %7B%0D%0A%09%09%09SurveyView view = viewGenerator.generateView(survey);%0D%0A%09%09%09return view
+%09SurveyView view = viewGenerator.generateView(survey);%0D%0A%09%09%09return view;%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%09%0D%0A%09private CollectSurvey getUpdatedActiveSurvey() %7B%0D%0A%09%09CollectSurvey sessionSurvey = sessionManager.getActiveSurvey();%0D%0A%09%09if (sessionSurvey == null) %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D%0D%0A%09%09CollectSurvey storedSurvey;%0D%0A%09%09if (sessionSurvey.isTemporary()) %7B%0D%0A%09%09%09storedSurvey = surveyManager.loadSurvey(sessionSurvey.getId());%0D%0A%09%09%7D else %7B%0D%0A%09%09%09storedSurvey = surveyManager.getById(sessionSurvey.getId());%0D%0A%09%09%7D%0D%0A%09%09if (storedSurvey == null %7C%7C storedSurvey.isTemporary() != sessionSurvey.isTemporary()) %7B%0D%0A%09%09%09return null;%0D%0A%09%09%7D else if (storedSurvey.getModifiedDate().compareTo(sessionSurvey.getModifiedDate()) %3E 0) %7B%0D%0A%09%09%09//survey updated%0D%0A%09%09%09sessionManager.setActiveSurvey(storedSurvey);%0D%0A%09%09%09return storedSurvey;%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return sessionSurvey
 ;%0D%0A%09
@@ -3294,14 +3294,13 @@
 %09%7D%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%7D%0D%0A

Check that session survey has not been changed