Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package controllers; import data.entity.CarCompletionOption; import data.entity.Feature; import data.services.FeatureService; import java.io.File; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import support.StringAdapter; /** * * @author bezdatiuzer */ @Controller @RequestMapping("/Feature") public class FeatureController { @Autowired private FeatureService featureService; @ModelAttribute public void setInfo(Map<String, Object> model) { Map<String, Object> cmgRes = new LinkedHashMap(); cmgRes.put("", "? "); for (Long l : featureService.getCmgs()) { if (l != null) { cmgRes.put(StringAdapter.getString(l), l); } else { cmgRes.put("0", "? "); } } model.put("cmgDict", cmgRes); Map<String, Object> ccoRes = new LinkedHashMap(); ccoRes.put("", "? "); for (CarCompletionOption cco : featureService.getCcos()) { if (cco != null) { ccoRes.put(StringAdapter.getString(cco.getOldId()), cco.getTitle()); } else { ccoRes.put("0", "? "); } } model.put("ccoDict", ccoRes); } @RequestMapping("/show") public String showParseForm(Map<String, Object> model) throws Exception { List<Feature> featList = featureService.getGroupOfFeatures(Long.valueOf("0"), Long.valueOf("0")); model.put("featList", featList); return "FeatureShow"; } @RequestMapping("/updateFeature") public String updateFeature(Map<String, Object> model, @RequestParam("featureId") Long featureId, @RequestParam("uid") String uid, @RequestParam("paramValue") String paramValue, @RequestParam("percentValue") Long percentValue, @RequestParam(value = "audial", required = false) Integer audial, @RequestParam(value = "visual", required = false) Integer visual, @RequestParam(value = "kinestet", required = false) Integer kinestet, @RequestParam("radical") String radical, RedirectAttributes ras, @RequestParam(value = "cmgId", required = false) Long cmgId, @RequestParam(value = "ccoId", required = false) Long ccoId) throws Exception { featureService.updateFeature(featureId, uid, paramValue, percentValue, radical, audial, visual, kinestet); ras.addFlashAttribute("error", featureService.getResult().getErrors()); ras.addAttribute("cmgId", cmgId); ras.addAttribute("ccoId", ccoId); /*model.put("oldcmgId", cmgId); model.put("oldccoId", ccoId);*/ return "redirect:/Feature/showGroup"; } @RequestMapping("/showGroup") public String showFeatureGroup(Map<String, Object> model, @RequestParam(value = "cmgId", required = false) Long cmgId, @RequestParam(value = "ccoId", required = false) Long ccoId) throws Exception { List<Feature> featList = featureService.getGroupOfFeatures(cmgId, ccoId); model.put("featList", featList); model.put("error", featureService.getResult().getErrors()); model.put("oldcmgId", cmgId); model.put("oldccoId", ccoId); return "FeatureShow"; } @RequestMapping("/updateAllInGroup") public String updateAllInGroup(Map<String, Object> model, @RequestParam(value = "ccoId", required = false) Long ccoId, @RequestParam(value = "cmgId", required = false) Long cmgId, @RequestParam("guid") String guid, @RequestParam(value = "audial", required = false) Integer audial, @RequestParam(value = "visual", required = false) Integer visual, @RequestParam(value = "kinestet", required = false) Integer kinestet, @RequestParam("gparamValue") String gparamValue, @RequestParam("gpercentValue") Long gpercentValue, @RequestParam("gradical") String gradical, RedirectAttributes ras) throws Exception { featureService.updateAllInGroup(cmgId, ccoId, guid, gparamValue, gpercentValue, gradical, audial, visual, kinestet); ras.addFlashAttribute("error", featureService.getResult().getErrors()); ras.addAttribute("ccoId", ccoId); ras.addAttribute("cmgId", cmgId); return "redirect:/Feature/showGroup"; } @RequestMapping("/getXls") public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=Feature.xls"); featureService.getXls().write(response.getOutputStream()); } @RequestMapping("/updateFromXml") public String updateFromXml(Map<String, Object> model, @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras) { if (file == null || file.isEmpty()) { ras.addFlashAttribute("error", "File not found"); } else { File newFile = new File("/usr/local/etc/Feature"); if (newFile.exists()) { newFile.delete(); } try { FileUtils.writeByteArrayToFile(newFile, file.getBytes()); featureService.updateFromXml(newFile); ras.addFlashAttribute("error", featureService.getResult().getErrors()); } catch (Exception e) { ras.addFlashAttribute("error", "updateFromXml " + StringAdapter.getStackTraceException(e)); } if (newFile.exists()) { newFile.delete(); } } return "redirect:/Feature/show"; } }