package org.xmx0632.deliciousfruit.web.admin;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import org.xmx0632.deliciousfruit.entity.FruitStoryMaterial;
import org.xmx0632.deliciousfruit.entity.FruitStoryMenu;
import org.xmx0632.deliciousfruit.global.ConfigConstant;
import org.xmx0632.deliciousfruit.service.ConfigService;
import org.xmx0632.deliciousfruit.service.FruitStoryMaterialService;
import org.xmx0632.deliciousfruit.service.FruitStoryMenuService;
import com.google.common.collect.Maps;
@Controller
@RequestMapping(value = "/admin/fruitStoryMaterial")
public class FruitStoryMaterialController {
private static Logger log = LoggerFactory
.getLogger(FruitStoryMaterialController.class);
@Autowired
private ResourceBundleMessageSource messageSource;
private static Map<String, String> sortTypes = null;
private Map<String, String> getSortTypes(Locale locale) {
if (sortTypes == null) {
sortTypes = Maps.newLinkedHashMap();
sortTypes.put("auto",
messageSource.getMessage("common.auto", null, locale));
sortTypes.put("fruitStoryMenuId", messageSource.getMessage(
"fruitStoryMaterial.fruitStoryMenuId", null, locale));
}
return sortTypes;
}
@Autowired
private FruitStoryMaterialService fruitStoryMaterialService;
@Autowired
private FruitStoryMenuService fruitStoryMenuService;
@Autowired
private ConfigService configService;
@RequestMapping(method = RequestMethod.GET)
public String list(
@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
@RequestParam(value = "page", defaultValue = "1") int pageNumber,
Model model, ServletRequest request) {
Map<String, Object> searchParams = Servlets.getParametersStartingWith(
request, "search_");
Page<FruitStoryMaterial> fruitStoryMaterials = fruitStoryMaterialService
.getFruitStoryMaterials(searchParams, pageNumber,
configService.getIntByName(ConfigConstant.PAGE_SIZE),
sortType);
model.addAttribute("fruitStoryMaterials", fruitStoryMaterials);
model.addAttribute("sortType", sortType);
model.addAttribute("sortTypes", getSortTypes(request.getLocale()));
model.addAttribute("searchParams", Servlets
.encodeParameterStringWithPrefix(searchParams, "search_"));
return "admin/fruitStoryMaterialList";
}
@RequestMapping(value = "create/{menuId}", method = RequestMethod.GET)
public String createForm(@PathVariable("menuId") Long menuId, Model model) {
FruitStoryMenu fruitStoryMenu = fruitStoryMenuService
.getFruitStoryMenu(menuId);
FruitStoryMaterial FruitStoryMaterial = new FruitStoryMaterial();
FruitStoryMaterial.setFruitStoryMenu(fruitStoryMenu);
model.addAttribute("fruitStoryMaterial", FruitStoryMaterial);
model.addAttribute("action", "create");
model.addAttribute("fruitStoryMenuId", menuId);
return "admin/fruitStoryMaterialForm";
}
@RequestMapping(value = "create/{menuId}", method = RequestMethod.POST)
public String create(@PathVariable("menuId") Long menuId,
@Valid FruitStoryMaterial newFruitStoryMaterial,
RedirectAttributes redirectAttributes) {
FruitStoryMenu fruitStoryMenu = fruitStoryMenuService
.getFruitStoryMenu(menuId);
log.info("menuId:" + menuId + ", fruitStoryMenu:" + fruitStoryMenu);
newFruitStoryMaterial.setFruitStoryMenu(fruitStoryMenu);
fruitStoryMaterialService.saveFruitStoryMaterial(newFruitStoryMaterial);
redirectAttributes.addFlashAttribute("message",
"创建fruitStoryMaterial成功");
return "redirect:/admin/fruitStoryMenu/update/"
+ newFruitStoryMaterial.getFruitStoryMenu().getId();
}
@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model model) {
model.addAttribute("fruitStoryMaterial",
fruitStoryMaterialService.getFruitStoryMaterial(id));
model.addAttribute("action", "update");
return "admin/fruitStoryMaterialForm";
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public String update(
@Valid @ModelAttribute("preloadFruitStoryMaterial") FruitStoryMaterial fruitStoryMaterial,
RedirectAttributes redirectAttributes) {
fruitStoryMaterialService.saveFruitStoryMaterial(fruitStoryMaterial);
redirectAttributes.addFlashAttribute("message",
"更新fruitStoryMaterial成功");
return "redirect:/admin/fruitStoryMenu/update/"
+ fruitStoryMaterial.getFruitStoryMenu().getId();
}
@RequestMapping(value = "delete/{id}")
public String delete(@PathVariable("id") Long id,
RedirectAttributes redirectAttributes) {
long menuId = fruitStoryMaterialService.getFruitStoryMaterial(id)
.getFruitStoryMenu().getId();
fruitStoryMaterialService.deleteFruitStoryMaterial(id);
redirectAttributes.addFlashAttribute("message",
"删除fruitStoryMaterial成功");
return "redirect:/admin/fruitStory/getMenu/" + menuId;
}
/**
* 使用@ModelAttribute, 实现Struts2
* Preparable二次部分绑定的效果,先根据form的id从数据库查出FruitStoryMaterial对象
* ,再把Form提交的内容绑定到该对象上. 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
*/
@ModelAttribute("preloadFruitStoryMaterial")
public FruitStoryMaterial getFruitStoryMaterial(
@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
return fruitStoryMaterialService.getFruitStoryMaterial(id);
}
return null;
}
}