controllers.SceneController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.SceneController.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.Event;
import data.entity.Scene;
import data.services.SceneService;
import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.validation.BindingResult;
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("/Scene")
public class SceneController {

    @Autowired
    private SceneService sceneService;

    @RequestMapping("/show")
    public String showSceneForm(Map<String, Object> model,
            @RequestParam(value = "sceneId", required = false) Long sceneId) throws Exception {

        List<Scene> sceneList = sceneService.getScenes();
        model.put("sceneList", sceneList);
        model.put("scene", new Scene());
        if (sceneId != null && sceneService.find(sceneId) != null) {
            model.put("sceneOne", sceneService.find(sceneId));
            model.put("vrmap", sceneService.getMapForScene(sceneId));
            //model.put("error",StringAdapter.getString(sceneService.getBpListNotIncludedInScene(sceneId).size()));
            model.put("newBpList", sceneService.getBpListNotIncludedInScene(sceneId));
            //model.put("vrlist", sceneService.getParamsForScene(sceneId));
        }
        return "SceneShow";
    }

    @RequestMapping(value = "/add")
    public String addScene(Map<String, Object> model, @ModelAttribute("scene") Scene sceneOne, BindingResult result,
            HttpServletRequest request, RedirectAttributes ras) throws Exception {
        sceneService.create(sceneOne);
        ras.addFlashAttribute("error", sceneService.getResult().getErrors());
        ras.addAttribute("sceneId", sceneOne.getSceneId());
        //model.put("sceneOne", sceneOne);
        return "redirect:/Scene/show";
    }

    @RequestMapping(value = "/addHeap")
    public String addHeap(Map<String, Object> model, @RequestParam("sceneId") Long sceneId,
            @RequestParam("heap") String heap, RedirectAttributes ras) throws Exception {
        sceneService.addValuesFromHeap(sceneId, heap);
        ras.addFlashAttribute("error", sceneService.getResult().getErrors());
        ras.addAttribute("sceneId", sceneId);
        return "redirect:/Scene/show";
    }

    @RequestMapping(value = "/addValueRange")
    public String addValueRange(Map<String, Object> model, @RequestParam("newuid") String newuid,
            @RequestParam("sceneId") Long sceneId, @RequestParam("amin") Long amin,
            @RequestParam("min") Long valueMin, @RequestParam("max") Long valueMax, @RequestParam("amax") Long amax,
            RedirectAttributes ras) throws Exception {
        sceneService.addValueRange(sceneId, newuid, amin, valueMin, valueMax, amax);
        ras.addFlashAttribute("error", sceneService.getResult().getErrors());
        ras.addAttribute("sceneId", sceneId);
        //model.put("sceneOne", sceneOne);
        return "redirect:/Scene/show";
    }

    @RequestMapping(value = "/delete")
    public String delete(Map<String, Object> model, @RequestParam("sceneId") Long sceneId, RedirectAttributes ras)
            throws Exception {
        Scene s = sceneService.getScene(sceneId);
        if (s.getEvents().isEmpty()) {
            sceneService.delete(sceneId);
            ras.addFlashAttribute("error", sceneService.getResult().getErrors());
            return "redirect:/Scene/show";
        } else {
            String err = " ???  : ";
            for (Event ev : s.getEvents()) {
                err += ev.getId() + " - " + ev.getName() + "; ";
            }
            List<String> ers = sceneService.getResult().getErrors();
            ers.add(err);
            ras.addFlashAttribute("error", ers);
            return "redirect:/Scene/show";
        }
    }

    @RequestMapping(value = "/deleteValueRange")
    public String deleteValueRange(Map<String, Object> model, @RequestParam("valueRangeId") Long valueRangeId,
            @RequestParam("sceneId") Long sceneId, RedirectAttributes ras) throws Exception {
        sceneService.deleteValueRange(valueRangeId);
        ras.addFlashAttribute("error", sceneService.getResult().getErrors());
        ras.addAttribute("sceneId", sceneId);
        //model.put("sceneOne", sceneOne);
        return "redirect:/Scene/show";
    }

    @RequestMapping(value = "/addValuesToScene")
    public String addValuesToScene(Map<String, Object> model, @RequestParam("sceneId") Long sceneId,
            @RequestParam("sceneIdFrom") Long sceneIdFrom, RedirectAttributes ras) throws Exception {
        sceneService.addValuesToScene(sceneId, sceneIdFrom);
        ras.addFlashAttribute("error", sceneService.getResult().getErrors());
        ras.addAttribute("sceneId", sceneId);
        //model.put("sceneOne", sceneOne);
        return "redirect:/Scene/show";
    }

    @RequestMapping("/getXls")
    public void getXls(Map<String, Object> model, HttpServletResponse response,
            @RequestParam(value = "sceneId", required = true) Long sceneId, RedirectAttributes ras)
            throws Exception {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=scene_" + sceneId + ".xls");
        sceneService.getXls(sceneId).write(response.getOutputStream());
    }

    @RequestMapping("/updateFromXml")
    public String updateFromXml(Map<String, Object> model,
            @RequestParam(value = "xlsFile", required = false) MultipartFile file,
            @RequestParam(value = "sceneId", required = true) Long sceneId, RedirectAttributes ras) {
        if (file == null || file.isEmpty()) {
            ras.addFlashAttribute("error", "File not found");
        } else {
            File newFile = new File("/usr/local/etc/sceneParamsList");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                sceneService.updateFromXml(newFile, sceneId);
                ras.addFlashAttribute("error", sceneService.getResult().getErrors());
            } catch (Exception e) {
                ras.addFlashAttribute("error",
                        "updateFromXml" + StringAdapter.getStackTraceException(e)/*e.getMessage()*/);
            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        ras.addAttribute("sceneId", sceneId);
        return "redirect:/Scene/show";
    }

}