controllers.CarOptionValueController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.CarOptionValueController.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.CarOptionValue;
import data.services.CarOptionValueService;
import java.io.File;
import java.util.ArrayList;
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;

/**
 *
 * @author bezdatiuzer
 */
@Controller
@RequestMapping("/CarOptionValue")
public class CarOptionValueController {

    @Autowired
    private CarOptionValueService carOptionValueService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        /*Map<String, Object> res = new LinkedHashMap();
        for (CarProperty cp : carPropertyService.getAllProps()) {
        res.put(StringAdapter.getString(cp.getId()), cp.getTitle());
        }*/
        model.put("ccoDict", carOptionValueService.getUniqueCCOs());
    }

    @RequestMapping("/show")
    public String showOptions(Map<String, Object> model,
            @RequestParam(value = "ccoId", required = false) Long ccoId) throws Exception {
        if (ccoId != null) {

            List<CarOptionValue> covlist = carOptionValueService.getUniqueOptionNames(ccoId);
            /*if(!ccoId.equals((long) 0)){
            CarOptionValue cov = carPropertyService.find(propId);
            BaseParam bp = baseParamService.getBaseParam(cp.getUid().trim());
            if(bp.getParamType().equals(ParamType.PERCENTE)){
                model.put("PercentParam", "true");
                model.put("baseParam", bp);
            }
            }*/
            model.put("uniqueCovNames", covlist);
            model.put("oldCcoId", ccoId);
            //model.put("error",StringAdapter.getString(covlist.size()));
        }
        return "CarOptionValueShow";
    }

    @RequestMapping("/updateCovsNames")
    public String updateCovNames(Map<String, Object> model,
            @RequestParam(value = "oldCcoId", required = true) Long oldCcoId,
            /*@RequestParam(value = "ccoId", required = true) Long ccoId,@RequestParam(value = "desc", required = true) String desc*/@RequestParam(value = "covId", required = true) Long covId,
            @RequestParam(value = "audial", required = false) Integer audial,
            @RequestParam(value = "visual", required = false) Integer visual,
            @RequestParam(value = "kinestet", required = false) Integer kinestet,
            @RequestParam(value = "percentValue", required = false) Long percentValue,
            @RequestParam(value = "paramValue", required = false) Double paramValue,
            @RequestParam(value = "rad", required = false) String rad, RedirectAttributes ras) throws Exception {
        carOptionValueService.updateByDescAndCcoId(covId, percentValue, paramValue, audial, visual, kinestet, rad,
                0);
        ras.addFlashAttribute("error", carOptionValueService.getResult().getErrors());
        //ras.addFlashAttribute("error", "desc1="+desc);

        ras.addAttribute("ccoId", oldCcoId);
        return "redirect:/CarOptionValue/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=options.xls");
        carOptionValueService.getXls().write(response.getOutputStream());
    }

    @RequestMapping("/updateFromXml")
    public String updateFromXml(Map<String, Object> model,
            @RequestParam(value = "oldCcoId", required = false) Long oldCcoId,
            @RequestParam(value = "xlsFile", required = false) MultipartFile file, RedirectAttributes ras)
            throws Exception {
        if (file == null || file.isEmpty()) {
            ras.addFlashAttribute("error", "File not found");
        } else {
            File newFile = new File("/usr/local/etc/covs");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                carOptionValueService.updateFromXml(newFile);
                ras.addFlashAttribute("error", carOptionValueService.getResult().getErrors());
            } catch (Exception e) {
                List<String> erList = new ArrayList();
                erList.addAll(carOptionValueService.getResult().getErrors());
                erList.add(e.getMessage());
                ras.addFlashAttribute("error",
                        "updateFromXml: " + /*StringAdapter.getStackTraceException(e)*/erList);

            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        ras.addAttribute("ccoId", oldCcoId);
        return "redirect:/CarOptionValue/show";
    }

}