controllers.CarPropertyController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.CarPropertyController.java

Source

/*
 * 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.CarPropertyGroup;
import data.services.CarPropertyGroupService;
import data.services.CarPropertyService;
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("/CarProperty")
public class CarPropertyController {

    @Autowired
    private CarPropertyService carPropertyService;
    @Autowired
    private CarPropertyGroupService carPropertyGroupService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        Map<String, Object> res = new LinkedHashMap();
        res.put(null, "?");
        for (CarPropertyGroup cpg : carPropertyGroupService.getGroups(null)) {
            res.put(StringAdapter.getString(cpg.getId()), cpg.getTitle());
        }
        model.put("groupDict", res);
    }

    @RequestMapping("/show")
    public String show(Map<String, Object> model, @RequestParam(value = "groupId", required = false) Long groupId)
            throws Exception {
        List<CarPropertyGroup> cpgList = carPropertyGroupService.getGroups(groupId);
        model.put("cpgList", cpgList);
        model.put("oldgroupId", groupId);
        return "CarPropertyShow";
    }

    /*@RequestMapping("/updateAllInGroup")
     public String updateAllInGroup(Map<String, Object> model,@RequestParam(value = "groupId", required = false) Long groupId,Long cmgId,@RequestParam("guid") String guid,
     @RequestParam("gparamValue") String gparamValue,@RequestParam("gpercentValue") Long gpercentValue,@RequestParam("gradical") Long gradical,RedirectAttributes ras) throws Exception {
     carPropertyGroupService.updateAllInGroup(groupId,guid, gparamValue, gpercentValue, gradical);
     ras.addFlashAttribute("error", carPropertyGroupService.getResult().getErrors());
     ras.addAttribute("oldgroupId", groupId);
     return "redirect:/CarProperty/show";
     }*/
    @RequestMapping("/update")
    public String update(Map<String, Object> model, @RequestParam(value = "groupId", required = false) Long groupId,
            Long cmgId, @RequestParam("uid") String uid, @RequestParam("paramValue") String paramValue,
            @RequestParam("percentValue") Long percentValue, @RequestParam("radical") String radical,
            @RequestParam("cpId") Long cpId, RedirectAttributes ras) throws Exception {
        carPropertyService.update(cpId, uid, paramValue, percentValue, radical);
        ras.addFlashAttribute("error", carPropertyService.getResult().getErrors());
        ras.addAttribute("groupId", groupId);
        return "redirect:/CarProperty/show";
    }

    @RequestMapping("/getXls")
    public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=PROPERTY.xls");
        carPropertyService.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/CarProperty");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                carPropertyService.updateFromXml(newFile);
                ras.addFlashAttribute("error", carPropertyService.getResult().getErrors());
            } catch (Exception e) {
                ras.addFlashAttribute("error", "updateFromXml" + StringAdapter.getStackTraceException(e));
            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return "redirect:/CarProperty/show";
    }
}