controllers.ColorController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.ColorController.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 com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import data.entity.BaseParam;
import data.entity.Color;
import data.entity.ColorGroup;
import data.entity.Feature;
import data.services.ColorGroupService;
import data.services.ColorService;
import data.services.FeatureService;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Array;
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.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("/Color")
public class ColorController {

    @Autowired
    private ColorService colorService;

    /*@Autowired
     private ParseController parseController;*/
    @RequestMapping("/show")
    public String showParseForm(Map<String, Object> model) throws Exception {
        List<Color> cllist = colorService.getColors();
        model.put("clList", cllist);
        return "ColorShow";
    }

    @RequestMapping("/updateColor")
    public String updateColor(Map<String, Object> model, @RequestParam("colorId") Long colorId,
            @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 {
        colorService.updateColor(colorId, uid, paramValue, percentValue, radical, audial, visual, kinestet);
        ras.addFlashAttribute("error", colorService.getResult().getErrors());
        return "redirect:/Color/show";
    }

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