controllers.BaseParamController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.BaseParamController.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.BaseParam;
import data.entity.User;
import data.services.BaseParamService;
import data.services.UserService;
import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import logic.ParamType;
import logic.StaticType;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
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 support.Result;
import support.StringAdapter;

/**
 *
 * @author 
 */
@Controller
@RequestMapping("/BaseParam")
public class BaseParamController {

    @Autowired
    private BaseParamService baseParamService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        model.put("paramTypes", ParamType.values());
        model.put("staticTypes", StaticType.values());
    }

    /*@RequestMapping(value = "/addHeap", method = RequestMethod.POST)
    public String addHeap(Map<String, Object> model,String heap,RedirectAttributes ras) {
    baseParamService.addHeap(heap);
    ras.addFlashAttribute("error", baseParamService.getResult().getErrors());
    return "redirect:/BaseParam/show";
    }*/

    @RequestMapping("/show")
    public String showParams(Map<String, Object> model) {
        List<BaseParam> paramList = baseParamService.getParams();
        for (BaseParam bp : paramList) {
            bp.setUid(baseParamService.uidColorFiltr(bp.getUid()));
        }
        model.put("paramList", paramList);
        model.put("baseParam", new BaseParam());
        return "BaseParamShow";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addParam(Map<String, Object> model, @ModelAttribute("baseParam") BaseParam baseParam,
            BindingResult result, HttpServletRequest request, RedirectAttributes ras) throws Exception {
        baseParamService.createParam(baseParam);
        ras.addFlashAttribute("error", baseParamService.getResult().getErrors());
        model.put("baseParam", baseParam);
        return "redirect:/BaseParam/show";
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(Map<String, Object> model, Long baseParamId) {
        baseParamService.delete(baseParamId);
        return "redirect:/BaseParam/show";
    }

    @RequestMapping(value = "/deleteAllParams", method = RequestMethod.POST)
    public String deleteAll() {
        baseParamService.deleteAllParams();
        return "redirect:/BaseParam/show";
    }

    @RequestMapping(value = "/update")
    public String updateParam(Map<String, Object> model, HttpServletRequest request, RedirectAttributes ras)
            throws Exception {
        Object paramId = request.getParameter("baseParamId");
        Object paramName = request.getParameter("paramName");
        Object paramValue = request.getParameter("paramValue");
        baseParamService.updateParam(paramId, paramName, paramValue);
        ras.addFlashAttribute("error", baseParamService.getResult().getErrors());
        if (!baseParamService.getResult().hasErrors()) {
            model.put("noErrors", "success");
        }
        return "redirect:/BaseParam/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=BaseParamMap.xls");
        baseParamService.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/BaseParamMap");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                baseParamService.updateFromXml(newFile);
                ras.addFlashAttribute("error", baseParamService.getResult().getErrors());
            } catch (Exception e) {
                ras.addFlashAttribute("error",
                        "updateFromXml" + StringAdapter.getStackTraceException(e)/*e.getMessage()*/);
            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return "redirect:/BaseParam/show";
    }

}