package org.xmx0632.deliciousfruit.web.admin;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletRequest;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
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.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;
import org.xmx0632.deliciousfruit.api.v1.bo.TerminalType;
import org.xmx0632.deliciousfruit.api.v1.helper.PictureUrlHelper;
import org.xmx0632.deliciousfruit.entity.FruitCategory;
import org.xmx0632.deliciousfruit.global.ConfigConstant;
import org.xmx0632.deliciousfruit.service.ConfigService;
import org.xmx0632.deliciousfruit.service.FruitCategoryService;
import org.xmx0632.deliciousfruit.service.UploadFilePathService;
import org.xmx0632.deliciousfruit.service.UploadFileService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Controller
@RequestMapping(value = "/admin/fruitCategory")
public class FruitCategoryController {
private static Logger log = LoggerFactory
.getLogger(FruitCategoryController.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("name", messageSource.getMessage(
"fruitCategory.name", null, locale));
}
return sortTypes;
}
@Autowired
private FruitCategoryService fruitCategoryService;
@Autowired
private ConfigService configService;
@Autowired
private UploadFileService uploadFileService;
@Autowired
private UploadFilePathService uploadFilePathService;
@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) {
List<String> imgAddressList = Lists.newArrayList();
for (TerminalType t : TerminalType.values()) {
String imgAddress = PictureUrlHelper.genTerminalRootUrl(t,
configService);
log.debug("imgAddress:{}", imgAddress);
imgAddressList.add(imgAddress);
}
model.addAttribute("imgAddressList", imgAddressList);
Map<String, Object> searchParams = Servlets.getParametersStartingWith(
request, "search_");
Page<FruitCategory> fruitCategorys = fruitCategoryService
.getFruitCategorys(searchParams, pageNumber,
configService.getIntByName(ConfigConstant.PAGE_SIZE),
sortType);
model.addAttribute("fruitCategorys", fruitCategorys);
model.addAttribute("sortType", sortType);
model.addAttribute("sortTypes", getSortTypes(request.getLocale()));
model.addAttribute("searchParams", Servlets
.encodeParameterStringWithPrefix(searchParams, "search_"));
return "admin/fruitCategoryList";
}
@RequestMapping(value = "create", method = RequestMethod.GET)
public String createForm(Model model) {
model.addAttribute("fruitCategory", new FruitCategory());
model.addAttribute("action", "create");
return "admin/fruitCategoryForm";
}
@RequestMapping(value = "create", method = RequestMethod.POST)
public String create(@Valid FruitCategory newFruitCategory,
@RequestParam MultipartFile iosNormalIcon,
@RequestParam MultipartFile iosRetinaIcon,
RedirectAttributes redirectAttributes) {
try {
fruitCategoryService.saveFruitCategory(newFruitCategory);
saveUploadIconAndUpdateData(newFruitCategory, iosNormalIcon,
iosRetinaIcon);
redirectAttributes.addFlashAttribute("message", "创建["
+ newFruitCategory.getName() + "]成功");
return "redirect:/admin/fruitCategory/";
} catch (Exception e) {
log.error(e.getMessage(), e);
redirectAttributes.addFlashAttribute("error", e.getMessage());
return "redirect:/admin/fruitCategory/update/"
+ newFruitCategory.getId();
}
}
@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model model) {
model.addAttribute("fruitCategory",
fruitCategoryService.getFruitCategory(id));
model.addAttribute("action", "update");
List<String> imgAddressList = Lists.newArrayList();
for (TerminalType t : TerminalType.values()) {
String imgAddress = configService
.getByName(ConfigConstant.IMG_SERVER_ROOT_ADDRESS)
+ configService.getByName(t);
imgAddressList.add(imgAddress);
}
model.addAttribute("imgAddressList", imgAddressList);
return "admin/fruitCategoryForm";
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public String update(
@Valid @ModelAttribute("preloadFruitCategory") FruitCategory fruitCategory,
@RequestParam MultipartFile iosNormalIcon,
@RequestParam MultipartFile iosRetinaIcon,
RedirectAttributes redirectAttributes) {
try {
saveUploadIconAndUpdateData(fruitCategory, iosNormalIcon,
iosRetinaIcon);
redirectAttributes.addFlashAttribute("message", "更新"
+ fruitCategory.getName() + "成功");
return "redirect:/admin/fruitCategory/";
} catch (Exception e) {
log.error(e.getMessage(), e);
redirectAttributes.addFlashAttribute("error", e.getMessage());
return "redirect:/admin/fruitCategory/update/"
+ fruitCategory.getId();
}
}
private void saveUploadIconAndUpdateData(FruitCategory fruitCategory,
MultipartFile iosNormalIcon, MultipartFile iosRetinaIcon) {
uploadFileService.saveCategoryImageFile(iosNormalIcon,
fruitCategory.getId(), TerminalType.IOS_NORMAL);
uploadFileService.saveCategoryImageFile(iosRetinaIcon,
fruitCategory.getId(), TerminalType.IOS_RETINA);
String originalFilename = iosNormalIcon.getOriginalFilename();
if (StringUtils.isNotBlank(originalFilename)) {
String iconUrl = uploadFilePathService.getCategoryIconUrl(
fruitCategory.getId(), originalFilename);
fruitCategory.setIconUrl(iconUrl);
}
fruitCategoryService.saveFruitCategory(fruitCategory);
}
@RequestMapping(value = "delete/{id}")
public String delete(@PathVariable("id") Long id,
RedirectAttributes redirectAttributes) {
fruitCategoryService.deleteFruitCategory(id);
redirectAttributes.addFlashAttribute("message", "删除fruitCategory成功");
return "redirect:/admin/fruitCategory";
}
/**
* 使用@ModelAttribute, 实现Struts2
* Preparable二次部分绑定的效果,先根据form的id从数据库查出FruitCategory对象,再把Form提交的内容绑定到该对象上.
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
*/
@ModelAttribute("preloadFruitCategory")
public FruitCategory getFruitCategory(
@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
return fruitCategoryService.getFruitCategory(id);
}
return null;
}
}