package eu.dariah.de.colreg.controller;

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

import java.util.Locale;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
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 eu.dariah.de.colreg.model.Collection;
import eu.dariah.de.colreg.service.CollectionService;

@Controller
@RequestMapping("/collections/")
public class CollectionController {
	@Autowired private CollectionService collectionService;
	
	
	@RequestMapping(value="", method=RequestMethod.GET)
	public String getList(Model model, Locale locale) {		
		model.addAttribute("collections", collectionService.findAllCurrent());
		
		return "collection/list";
	}
	
	@RequestMapping(value="{id}", method=RequestMethod.GET)
	public String editCollection(@PathVariable String id, Model model, Locale locale) {
		Collection c;
		if (id.toLowerCase().equals("new")) {
			c = collectionService.createCollection();
		} else {
			c = collectionService.findCurrentByCollectionId(id);
		}
		
		model.addAttribute("c", c);
		
		return "collection/edit";
	}
	
	// TODO Implement as POST-REDIRECT-GET Pattern
	@RequestMapping(value="{id}", method=RequestMethod.POST)
	public String saveCollection(@Valid Collection c, Model model, Locale locale) {
		collectionService.save(c);
		
		model.addAttribute("c", c);
		
		return "collection/edit";
	}
	
	@RequestMapping(method=GET, value={"/includes/editAccess"})
	public String getEditAccessForm() {
		return "collection/edit/incl/edit_access";
	}
	
	@RequestMapping(method=GET, value={"/includes/editAccrual"})
	public String getEditAccrualForm() {
		return "collection/edit/incl/edit_accrual";
	}
	
	@RequestMapping(method=GET, value={"/includes/editAgent"})
	public String getEditAgentForm() {
		return "collection/edit/incl/edit_agent";
	}
	
	@RequestMapping(method=GET, value={"/includes/editDescription"})
	public String getEditDescriptionForm() {
		return "collection/edit/incl/edit_description";
	}
	
	@RequestMapping(method=GET, value={"/includes/editItemLanguage"})
	public String getEditItemLanguageForm() {
		return "collection/edit/incl/edit_itemlanguage";
	}
}
@@ -133,16 +133,84 @@
 Locale;%0A
+import java.util.Map;%0A%0Aimport javax.servlet.http.HttpServletRequest;
 %0Aimport 
@@ -569,16 +569,155 @@
 tMethod;
+%0Aimport org.springframework.web.servlet.mvc.support.RedirectAttributes;%0Aimport org.springframework.web.servlet.support.RequestContextUtils;
 %0A%0Aimport
@@ -1311,30 +1311,254 @@
 cale
-) %7B%0A%09%09Collection c;%0A%09%09
+, HttpServletRequest request) %7B%0A%09%09Map%3CString, ?%3E inputFlashMap = RequestContextUtils.getInputFlashMap(request);%0A%09%09%0A%09%09Collection c;%0A%09%09if (inputFlashMap!=null && inputFlashMap.containsKey(%22c%22)) %7B%0A%09%09%09c = (Collection)inputFlashMap.get(%22c%22);%0A%09%09%7D else 
 if (
@@ -1963,103 +1963,269 @@
 cale
-) %7B%0A%09%09collectionService.save(c);%0A%09%09%0A%09%09model.addAttribute(%22c%22, c);%0A%09%09%0A%09%09return %22collection/edit%22
+, final RedirectAttributes redirectAttributes) %7B%0A%09%09collectionService.save(c);%0A%09%09%0A%09%09// Flash attribute only required on error / otherwise load by id in GET%0A%09%09redirectAttributes.addFlashAttribute(%22c%22, c);%0A%09%09%0A%09%09return %22redirect:/collections/%22 + c.getCollectionId()
 ;%0A%09%7D

Implement POST-Redirect-GET pattern for collection editor