controllers.CCOController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.CCOController.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.CarCompletionGroup;
import data.entity.CarCompletionOption;
import data.services.BaseParamService;
import data.services.CarCompletionGroupService;
import data.services.CarCompletionOptionService;
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("/CCO")
public class CCOController {

    @Autowired
    private CarCompletionOptionService carCompletionOptionService;
    @Autowired
    private CarCompletionGroupService carCompletionGroupService;
    @Autowired
    private BaseParamService baseParamService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        Map<String, Object> res = new LinkedHashMap();
        for (CarCompletionGroup ccog : carCompletionGroupService.getGroups()) {
            res.put(StringAdapter.getString(ccog.getOldId()), ccog.getTitle());
        }
        model.put("groupDict", res);
    }

    /*@Autowired
     private ParseController parseController;*/
    @RequestMapping("/show")
    public String showParseForm(Map<String, Object> model) throws Exception {
        List<CarCompletionOption> ccoList = carCompletionOptionService.getOptions();
        /*for(CarCompletionOption cco:ccoList){
        cco.setUid(baseParamService.uidSpaceFiltr(cco.getUid()));
        }*/
        model.put("ccoList", ccoList);
        return "CCOShow";
    }

    @RequestMapping("/updateOption")
    public String updateOption(Map<String, Object> model, @RequestParam("ccoId") Long ccoId,
            @RequestParam("uid") String uid, @RequestParam("paramValue") String paramValue,
            @RequestParam("percentValue") Long percentValue, @RequestParam("radical") String radical,
            @RequestParam(value = "audial", required = false) Integer audial,
            @RequestParam(value = "visual", required = false) Integer visual,
            @RequestParam(value = "kinestet", required = false) Integer kinestet, RedirectAttributes ras)
            throws Exception {
        carCompletionOptionService.update(ccoId, uid, paramValue, percentValue, radical, audial, visual, kinestet);
        ras.addFlashAttribute("error", carCompletionOptionService.getResult().getErrors());
        return "redirect:/CCO/show";
    }

    @RequestMapping("/updateAllInGroup")
    public String updateAllInGroup(Map<String, Object> model, @RequestParam("oldGroupId") Long oldGroupId,
            @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) throws Exception {
        carCompletionGroupService.updateAllInGroup(oldGroupId, uid, paramValue, percentValue, radical, audial,
                visual, kinestet);
        ras.addFlashAttribute("error", carCompletionGroupService.getResult().getErrors());
        return "redirect:/CCO/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=CARCOMPOPTION.xls");
        carCompletionOptionService.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/CCO");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                carCompletionOptionService.updateFromXml(newFile);
                ras.addFlashAttribute("error", carCompletionOptionService.getResult().getErrors());
            } catch (Exception e) {
                ras.addFlashAttribute("error", "updateFromXml" + e.getMessage());
            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return "redirect:/CCO/show";
    }
}