/*
 *  Copyright (C) 2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */
package org.waterforpeople.mapping.app.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.converter.HttpMessageNotReadableException;
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.waterforpeople.mapping.app.util.DtoMarshaller;
import org.waterforpeople.mapping.app.web.rest.dto.PlacemarkDetailDto;

import com.gallatinsystems.surveyal.dao.SurveyedLocaleDao;
import com.gallatinsystems.surveyal.domain.SurveyalValue;
import com.gallatinsystems.surveyal.domain.SurveyedLocale;


/**
 * This service is providing the details for a particular Placemark, due to the
 * lack of partial loading in Ember-Data See:
 * https://github.com/emberjs/data/issues/51
 */
@Controller
@RequestMapping("/placemark_details")
public class PlacemarkDetailsRestService {

	private static final Logger log = Logger
			.getLogger(PlacemarkDetailsRestService.class.getName());

	@Inject
	SurveyedLocaleDao localeDao;

	@RequestMapping(method = RequestMethod.GET, value = "")
	@ResponseBody
	public Map<String, Object> getDetails(
			@RequestParam(value = "placemarkId", defaultValue = "") String placemarkId) {

		if (StringUtils.isEmpty(placemarkId)) {
			final String msg = "[placemarkId] is a required parameter";
			log.log(Level.SEVERE, msg);
			throw new HttpMessageNotReadableException(msg);
		}

		final SurveyedLocale sl = localeDao.getById(Long.valueOf(placemarkId));

		if (sl == null) {
			final String msg = "placemarkId : " + placemarkId + " not found";
			log.log(Level.SEVERE, msg);
			throw new HttpMessageNotReadableException(msg);
		}

		final Map<String, Object> response = new HashMap<String, Object>();

		response.put("placemark_details", getPlacemarkDetails(sl));

		return response;
	}

	private List<PlacemarkDetailDto> getPlacemarkDetails(SurveyedLocale sl) {
		final List<PlacemarkDetailDto> details = new ArrayList<PlacemarkDetailDto>();

		if (sl.getSurveyalValues() == null) {
			return details;
		}

		for (SurveyalValue sv : sl.getSurveyalValues()) {
			PlacemarkDetailDto pmDto = new PlacemarkDetailDto();
			DtoMarshaller.copyToDto(sv, pmDto);
			pmDto.setPlacemarkId(sl.getKey().getId());
			pmDto.setOrder(sv.getQuestionGroupOrder() * 1000 + sv.getQuestionOrder());
			details.add(pmDto);
		}

		return details;
	}
}

@@ -3025,24 +3025,61 @@
 etailDto%3E();
+%0A%09%09Integer qgOrder;%0A%09%09Integer qOrder;
 %0A%0A%09%09if (sl.g
@@ -3332,23 +3332,18 @@
 %0A%09%09%09
-pmDto.set
+qg
 Order
-(
+ = 
 sv.g
@@ -3368,17 +3368,21 @@
 er()
- * 1000 +
+;%0A%09%09%09qOrder =
  sv.
@@ -3399,16 +3399,106 @@
 nOrder()
+;%0A%09%09%09pmDto.setOrder((qgOrder == null ? 0 : qgOrder) * 1000 + (qOrder == null ? 0 : qOrder)
 );%0A%09%09%09de

Check for null value of orders