package org.xmx0632.deliciousfruit.web.admin;
import java.util.Date;
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.FruitStory;
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 org.xmx0632.deliciousfruit.service.FruitStoryProcedureService;
import org.xmx0632.deliciousfruit.service.FruitStoryService;
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/fruitStory")
public class FruitStoryController {
private static Logger log = LoggerFactory
.getLogger(FruitStoryController.class);
@Autowired
private ResourceBundleMessageSource messageSource;
private static Map<String, String> sortTypes = null;
private static Map<String, String> allStatus = Maps.newHashMap();
static {
allStatus.put(FruitStory.STATUS_CREATING, "creating");
allStatus.put(FruitStory.STATUS_OFFLINE, "offline");
allStatus.put(FruitStory.STATUS_ONLINE, "online");
}
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("fruitStory.name", null, locale));
}
return sortTypes;
}
@Autowired
private FruitStoryService fruitStoryService;
@Autowired
private FruitStoryMenuService fruitStoryMenuService;
@Autowired
private FruitStoryMaterialService fruitStoryMaterialService;
@Autowired
private FruitStoryProcedureService fruitStoryProcedureService;
@Autowired
private UploadFileService uploadFileService;
@Autowired
private UploadFilePathService uploadFilePathService;
@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) {
List<String> imgAddressList = Lists.newArrayList();
for (TerminalType t : TerminalType.values()) {
String imgAddress = PictureUrlHelper.genTerminalRootUrl(t,
configService);
imgAddressList.add(imgAddress);
}
model.addAttribute("imgAddressList", imgAddressList);
Map<String, Object> searchParams = Servlets.getParametersStartingWith(
request, "search_");
Page<FruitStory> fruitStorys = fruitStoryService.getFruitStorys(
searchParams, pageNumber,
configService.getIntByName(ConfigConstant.PAGE_SIZE), sortType);
model.addAttribute("fruitStorys", fruitStorys);
model.addAttribute("sortType", sortType);
model.addAttribute("sortTypes", getSortTypes(request.getLocale()));
model.addAttribute("allStatus", allStatus);
model.addAttribute("searchParams", Servlets
.encodeParameterStringWithPrefix(searchParams, "search_"));
return "admin/fruitStoryList";
}
@RequestMapping(value = "create", method = RequestMethod.GET)
public String createForm(Model model) {
FruitStory fruitStory = new FruitStory();
fruitStory.setStatus(FruitStory.STATUS_CREATING);
model.addAttribute("fruitStory", fruitStory);
model.addAttribute("allStatus", allStatus);
model.addAttribute("action", "create");
return "admin/fruitStoryForm";
}
@RequestMapping(value = "init", method = RequestMethod.GET)
public String init(Model model) {
FruitStory fruitStory = new FruitStory();
fruitStory.setStatus(FruitStory.STATUS_CREATING);
model.addAttribute("fruitStory", fruitStory);
model.addAttribute("allStatus", allStatus);
model.addAttribute("action", "init");
return "admin/fruitStoryInit";
}
@RequestMapping(value = "init", method = RequestMethod.POST)
public String init(@Valid FruitStory newFruitStory,
RedirectAttributes redirectAttributes) {
newFruitStory.setCreateTime(new Date());
fruitStoryService.saveFruitStory(newFruitStory);
FruitStoryMenu fruitStoryMenu = new FruitStoryMenu();
fruitStoryMenu.setFruitStory(newFruitStory);
fruitStoryMenuService.saveFruitStoryMenu(fruitStoryMenu);
newFruitStory.setFruitStoryMenu(fruitStoryMenu);
fruitStoryService.saveFruitStory(newFruitStory);
redirectAttributes.addFlashAttribute("message",
"创建fruitStory成功,请输入详细信息");
return "redirect:/admin/fruitStory/update/" + newFruitStory.getId();
}
@RequestMapping(value = "create", method = RequestMethod.POST)
public String create(@Valid FruitStory newFruitStory,
@RequestParam MultipartFile iosNormalIcon,
@RequestParam MultipartFile iosRetinaIcon,
RedirectAttributes redirectAttributes) {
try {
saveUploadPicAndUpdateData(newFruitStory, iosNormalIcon,
iosRetinaIcon);
redirectAttributes.addFlashAttribute("message", "创建fruitStory成功");
return "redirect:/admin/fruitStory/";
} catch (Exception e) {
log.error(e.getMessage(), e);
redirectAttributes.addFlashAttribute("error", e.getMessage());
return "redirect:/admin/fruitStory/update/" + newFruitStory.getId();
}
}
@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
public String updateForm(@PathVariable("id") Long id, Model model) {
model.addAttribute("fruitStory", fruitStoryService.getFruitStory(id));
model.addAttribute("allStatus", allStatus);
model.addAttribute("action", "update");
List<String> imgAddressList = Lists.newArrayList();
for (TerminalType t : TerminalType.values()) {
String imgAddress = PictureUrlHelper.genTerminalRootUrl(t,
configService);
imgAddressList.add(imgAddress);
}
model.addAttribute("imgAddressList", imgAddressList);
return "admin/fruitStoryForm";
}
@RequestMapping(value = "changeStatus/{id}/{status}", method = RequestMethod.GET)
public String changeStatus(@PathVariable("id") Long id,
@PathVariable("status") String status,
RedirectAttributes redirectAttributes) {
FruitStory fruitStory = fruitStoryService.getFruitStory(id);
if (FruitStory.STATUS_OFFLINE.equalsIgnoreCase(status)) {
fruitStory.setStatus(FruitStory.STATUS_OFFLINE);
}
if (FruitStory.STATUS_ONLINE.equalsIgnoreCase(status)) {
fruitStory.setStatus(FruitStory.STATUS_ONLINE);
}
fruitStoryService.saveFruitStory(fruitStory);
redirectAttributes.addFlashAttribute("message", "更新fruitStory状态成功");
return "redirect:/admin/fruitStory/update/" + fruitStory.getId();
}
@RequestMapping(value = "changeStatusToList/{id}/{status}", method = RequestMethod.GET)
public String changeStatusToList(@PathVariable("id") Long id,
@PathVariable("status") String status,
RedirectAttributes redirectAttributes) {
FruitStory fruitStory = fruitStoryService.getFruitStory(id);
if (FruitStory.STATUS_OFFLINE.equalsIgnoreCase(status)) {
fruitStory.setStatus(FruitStory.STATUS_OFFLINE);
}
if (FruitStory.STATUS_ONLINE.equalsIgnoreCase(status)) {
fruitStory.setStatus(FruitStory.STATUS_ONLINE);
}
fruitStoryService.saveFruitStory(fruitStory);
redirectAttributes.addFlashAttribute("message", "更新fruitStory状态成功");
return "redirect:/admin/fruitStory";
}
@RequestMapping(value = "getMenu/{id}", method = RequestMethod.GET)
public String getMenu(@PathVariable("id") Long id, Model model) {
List<String> imgAddressList = Lists.newArrayList();
for (TerminalType t : TerminalType.values()) {
String imgAddress = PictureUrlHelper.genTerminalRootUrl(t,
configService);
imgAddressList.add(imgAddress);
}
model.addAttribute("imgAddressList", imgAddressList);
model.addAttribute("fruitStoryMenu", fruitStoryService
.getFruitStory(id).getFruitStoryMenu());
model.addAttribute("action", "update");
return "admin/fruitStoryMenuForm";
}
@RequestMapping(value = "preview/{id}", method = RequestMethod.GET)
public String preview(@PathVariable("id") Long id, Model model) {
String imgAddress = PictureUrlHelper.genTerminalRootUrl(
TerminalType.IOS_NORMAL, configService);
model.addAttribute("imgAddress", imgAddress);
model.addAttribute("fruitStoryMenu", fruitStoryService
.getFruitStory(id).getFruitStoryMenu());
return "admin/fruitStoryPreview";
}
@RequestMapping(value = "update", method = RequestMethod.POST)
public String update(
@Valid @ModelAttribute("preloadFruitStory") FruitStory fruitStory,
@RequestParam MultipartFile iosNormalIcon,
@RequestParam MultipartFile iosRetinaIcon,
RedirectAttributes redirectAttributes) {
try {
saveUploadPicAndUpdateData(fruitStory, iosNormalIcon, iosRetinaIcon);
redirectAttributes.addFlashAttribute("message", "更新fruitStory成功");
return "redirect:/admin/fruitStory/update/" + fruitStory.getId();
} catch (Exception e) {
log.error(e.getMessage(), e);
redirectAttributes.addFlashAttribute("error", e.getMessage());
return "redirect:/admin/fruitStory/update/" + fruitStory.getId();
}
}
@RequestMapping(value = "delete/{id}")
public String delete(@PathVariable("id") Long id,
RedirectAttributes redirectAttributes) {
deleteStory(id);
redirectAttributes.addFlashAttribute("message", "删除fruitStory成功");
return "redirect:/admin/fruitStory";
}
private void deleteStory(Long id) {
fruitStoryService.deleteFruitStory(id);
}
private void saveUploadPicAndUpdateData(FruitStory fruitStory,
MultipartFile iosNormalIcon, MultipartFile iosRetinaIcon) {
uploadFileService.saveStoryImageFile(iosNormalIcon, fruitStory.getId(),
TerminalType.IOS_NORMAL);
uploadFileService.saveStoryImageFile(iosRetinaIcon, fruitStory.getId(),
TerminalType.IOS_RETINA);
String originalFilename = iosNormalIcon.getOriginalFilename();
if (!StringUtils.isNotBlank(originalFilename)) {
originalFilename = iosRetinaIcon.getOriginalFilename();
}
if (StringUtils.isNotBlank(originalFilename)) {
String picUrl = uploadFilePathService.getFruitStoryIconUrl(
fruitStory.getId(), originalFilename);
fruitStory.setPictureUrl(picUrl);
}
fruitStoryService.saveFruitStory(fruitStory);
}
/**
* 使用@ModelAttribute, 实现Struts2
* Preparable二次部分绑定的效果,先根据form的id从数据库查出FruitStory对象,再把Form提交的内容绑定到该对象上.
* 因为仅update()方法的form中有id属性,因此本方法在该方法中执行.
*/
@ModelAttribute("preloadFruitStory")
public FruitStory getFruitStory(
@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
return fruitStoryService.getFruitStory(id);
}
return null;
}
}