<commit_before>package org.dainst.gazetteer.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dainst.gazetteer.converter.JsonPlaceDeserializer;
import org.dainst.gazetteer.dao.PlaceRepository;
import org.dainst.gazetteer.domain.Place;
import org.dainst.gazetteer.domain.ValidationResult;
import org.dainst.gazetteer.helpers.IdGenerator;
import org.dainst.gazetteer.helpers.LocalizedLanguagesHelper;
import org.dainst.gazetteer.search.ElasticSearchServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
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.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.RedirectView;


@Controller
public class DocumentController {

	private static final Logger logger = LoggerFactory.getLogger(DocumentController.class);
	
	@Autowired
	private PlaceRepository placeDao;
	
	@Autowired
	private JsonPlaceDeserializer jsonPlaceDeserializer;
	
	@Autowired
	private ElasticSearchServer elasticSearchServer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private LocalizedLanguagesHelper langHelper;
	
	@Value("${baseUri}")
	private String baseUri;
	
	@Value("${googleMapsApiKey}")
	private String googleMapsApiKey;

	@RequestMapping(value="/doc/{placeId}", method=RequestMethod.GET)
	public ModelAndView getPlace(@PathVariable String placeId,
			@RequestParam(required=false) String layout,
			@RequestParam(defaultValue="10") int limit,
			@RequestParam(defaultValue="0") int offset,
			@RequestParam(required=false) String q,
			@RequestParam(required=false) String fuzzy,
			@RequestParam(required=false, defaultValue="map,table") String view,
			@RequestHeader("User-Agent") String userAgent,
			@RequestHeader("Accept") String accept,
			HttpServletRequest request) {
		
		RequestContext requestContext = new RequestContext(request);
		Locale locale = requestContext.getLocale();

		ModelAndView mav;
		
		// redirect browsers to app
		if (accept.contains("text/html") && !userAgent.contains("bot")) {
			RedirectView redirectView = new RedirectView(baseUri + "app/#!/show/" + placeId, true, true);
			redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
			return new ModelAndView(redirectView);
		}
		
		long time = System.currentTimeMillis();
		
		Place place = placeDao.findOne(placeId);
		
		logger.debug("findOne: {}", System.currentTimeMillis() - time);
		time = System.currentTimeMillis();
		
		if (place == null) {
			
			throw new ResourceNotFoundException();
			
		} else if (place.getReplacedBy() != null && !place.getReplacedBy().isEmpty()) {
			
			String suffix = "";
			String uri = request.getRequestURI();
			logger.debug("uri: " + uri);
			if (uri.lastIndexOf(".") > 0) {
				suffix = uri.substring(uri.lastIndexOf("."));
				logger.debug("suffix: " + suffix);
			}
			
			RedirectView redirectView = new RedirectView("/doc/" + place.getReplacedBy() + suffix, true, true);
			redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
			mav = new ModelAndView(redirectView);
		
		// places that need to be reviewed should not be available to the public
		//} else if (place.isNeedsReview()) {
			
		//	throw new ResourceNotFoundException();
			
		} else {
			
			//List<Place> children = placeDao.findByParent(place.getId());
			
			logger.debug("findByParent: {}", System.currentTimeMillis() - time);
			time = System.currentTimeMillis();
			
			List<Place> relatedPlaces = placeDao.findByIdIn(place.getRelatedPlaces());
			
			logger.debug("findByIdIn: {}", System.currentTimeMillis() - time);
			time = System.currentTimeMillis();
			
			Place parent = null;
			if (place.getParent() != null) parent = placeDao.findOne(place.getParent());
			
			mav = new ModelAndView("place/get");
			if (layout != null) {
				mav.setViewName("place/"+layout);
			}
			mav.addObject("place", place);
			//mav.addObject("children", children);
			mav.addObject("relatedPlaces", relatedPlaces);
			mav.addObject("parent", parent);
			mav.addObject("baseUri", baseUri);
			mav.addObject("language", locale.getISO3Language());
			mav.addObject("limit", limit);
			mav.addObject("offset", offset);
			mav.addObject("view", view);
			mav.addObject("q", q);
			mav.addObject("nativePlaceName", place.getNameMap().get(locale.getISO3Language()));
			mav.addObject("googleMapsApiKey", googleMapsApiKey);
			mav.addObject("languages", langHelper.getLocalizedLanguages(locale));
			
		}
		
		return mav;

	}
	
	@RequestMapping(value="/doc", method={RequestMethod.POST, RequestMethod.PUT})
	public ModelAndView createPlace(@RequestBody Place place,
			HttpServletResponse response) {
		
		place.setId(idGenerator.generate(place));
		place = placeDao.save(place);
		
		logger.debug(place.getId());
		
		// add count for children (for scoring)
		while (place.getParent() != null) {
			place = placeDao.findOne(place.getParent());
			place.setChildren(place.getChildren()+1);
			placeDao.save(place);
			logger.debug("updated children count: {}", place.getChildren());
		}
		
		response.setStatus(201);
		response.setHeader("Location", baseUri + "place/" + place.getId());
		
		ModelAndView mav = new ModelAndView("place/get");
		mav.addObject("place", place);
		mav.addObject("baseUri", baseUri);
		return mav;
		
	}
	
	@RequestMapping(value="/doc/{placeId}", method={RequestMethod.POST, RequestMethod.PUT})
	public ModelAndView updateOrCreatePlace(@RequestBody Place place, 
			@PathVariable String placeId,
			HttpServletResponse response) {
		
		place.setId(placeId);
		place = placeDao.save(place);
		
		logger.debug("saved place {}", place);
		
		// add count for children (for scoring)
		while (place.getParent() != null) {
			Place parent = placeDao.findOne(place.getParent());
			place.setChildren(place.getChildren()+1);
			placeDao.save(place);				
			logger.debug("updated children count: {}", place.getChildren());<commit_msg>fixed bug that occurred when saving a place ... again ...<commit_after>package org.dainst.gazetteer.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dainst.gazetteer.converter.JsonPlaceDeserializer;
import org.dainst.gazetteer.dao.PlaceRepository;
import org.dainst.gazetteer.domain.Place;
import org.dainst.gazetteer.domain.ValidationResult;
import org.dainst.gazetteer.helpers.IdGenerator;
import org.dainst.gazetteer.helpers.LocalizedLanguagesHelper;
import org.dainst.gazetteer.search.ElasticSearchServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
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.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.view.RedirectView;


@Controller
public class DocumentController {

	private static final Logger logger = LoggerFactory.getLogger(DocumentController.class);
	
	@Autowired
	private PlaceRepository placeDao;
	
	@Autowired
	private JsonPlaceDeserializer jsonPlaceDeserializer;
	
	@Autowired
	private ElasticSearchServer elasticSearchServer;
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private LocalizedLanguagesHelper langHelper;
	
	@Value("${baseUri}")
	private String baseUri;
	
	@Value("${googleMapsApiKey}")
	private String googleMapsApiKey;

	@RequestMapping(value="/doc/{placeId}", method=RequestMethod.GET)
	public ModelAndView getPlace(@PathVariable String placeId,
			@RequestParam(required=false) String layout,
			@RequestParam(defaultValue="10") int limit,
			@RequestParam(defaultValue="0") int offset,
			@RequestParam(required=false) String q,
			@RequestParam(required=false) String fuzzy,
			@RequestParam(required=false, defaultValue="map,table") String view,
			@RequestHeader("User-Agent") String userAgent,
			@RequestHeader("Accept") String accept,
			HttpServletRequest request) {
		
		RequestContext requestContext = new RequestContext(request);
		Locale locale = requestContext.getLocale();

		ModelAndView mav;
		
		// redirect browsers to app
		if (accept.contains("text/html") && !userAgent.contains("bot")) {
			RedirectView redirectView = new RedirectView(baseUri + "app/#!/show/" + placeId, true, true);
			redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
			return new ModelAndView(redirectView);
		}
		
		long time = System.currentTimeMillis();
		
		Place place = placeDao.findOne(placeId);
		
		logger.debug("findOne: {}", System.currentTimeMillis() - time);
		time = System.currentTimeMillis();
		
		if (place == null) {
			
			throw new ResourceNotFoundException();
			
		} else if (place.getReplacedBy() != null && !place.getReplacedBy().isEmpty()) {
			
			String suffix = "";
			String uri = request.getRequestURI();
			logger.debug("uri: " + uri);
			if (uri.lastIndexOf(".") > 0) {
				suffix = uri.substring(uri.lastIndexOf("."));
				logger.debug("suffix: " + suffix);
			}
			
			RedirectView redirectView = new RedirectView("/doc/" + place.getReplacedBy() + suffix, true, true);
			redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
			mav = new ModelAndView(redirectView);
		
		// places that need to be reviewed should not be available to the public
		//} else if (place.isNeedsReview()) {
			
		//	throw new ResourceNotFoundException();
			
		} else {
			
			//List<Place> children = placeDao.findByParent(place.getId());
			
			logger.debug("findByParent: {}", System.currentTimeMillis() - time);
			time = System.currentTimeMillis();
			
			List<Place> relatedPlaces = placeDao.findByIdIn(place.getRelatedPlaces());
			
			logger.debug("findByIdIn: {}", System.currentTimeMillis() - time);
			time = System.currentTimeMillis();
			
			Place parent = null;
			if (place.getParent() != null) parent = placeDao.findOne(place.getParent());
			
			mav = new ModelAndView("place/get");
			if (layout != null) {
				mav.setViewName("place/"+layout);
			}
			mav.addObject("place", place);
			//mav.addObject("children", children);
			mav.addObject("relatedPlaces", relatedPlaces);
			mav.addObject("parent", parent);
			mav.addObject("baseUri", baseUri);
			mav.addObject("language", locale.getISO3Language());
			mav.addObject("limit", limit);
			mav.addObject("offset", offset);
			mav.addObject("view", view);
			mav.addObject("q", q);
			mav.addObject("nativePlaceName", place.getNameMap().get(locale.getISO3Language()));
			mav.addObject("googleMapsApiKey", googleMapsApiKey);
			mav.addObject("languages", langHelper.getLocalizedLanguages(locale));
			
		}
		
		return mav;

	}
	
	@RequestMapping(value="/doc", method={RequestMethod.POST, RequestMethod.PUT})
	public ModelAndView createPlace(@RequestBody Place place,
			HttpServletResponse response) {
		
		place.setId(idGenerator.generate(place));
		place = placeDao.save(place);
		
		logger.debug(place.getId());
		
		// add count for children (for scoring)
		while (place.getParent() != null) {
			place = placeDao.findOne(place.getParent());
			place.setChildren(place.getChildren()+1);
			placeDao.save(place);
			logger.debug("updated children count: {}", place.getChildren());
		}
		
		response.setStatus(201);
		response.setHeader("Location", baseUri + "place/" + place.getId());
		
		ModelAndView mav = new ModelAndView("place/get");
		mav.addObject("place", place);
		mav.addObject("baseUri", baseUri);
		return mav;
		
	}
	
	@RequestMapping(value="/doc/{placeId}", method={RequestMethod.POST, RequestMethod.PUT})
	public ModelAndView updateOrCreatePlace(@RequestBody Place place, 
			@PathVariable String placeId,
			HttpServletResponse response) {
		
		place.setId(placeId);
		place = placeDao.save(place);
		
		logger.debug("saved place {}", place);
		
		// add count for children (for scoring)
		while (place.getParent() != null) {
			place = placeDao.findOne(place.getParent());
			place.setChildren(place.getChildren()+1);
			placeDao.save(place);				
			logger.debug("updated children count: {}", place.getChildren());
{'fraction_non_alphanumeric': 0.13018544723318687, 'fraction_numerical': 0.0019363968123929395, 'mean_word_length': 5.070524412296564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5452716', 'n_tokens_mistral': 4478, 'n_tokens_neox': 3991, 'n_words': 743}