controllers.ColorGroupController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.ColorGroupController.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.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("/ColorGroup")
public class ColorGroupController {

    @Autowired
    private ColorGroupService colorGroupService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        Map<String, Object> res = new LinkedHashMap();
        for (ColorGroup cg : colorGroupService.getColorGroups()) {
            res.put(StringAdapter.getString(cg.getOldGroupId()), cg.getTitle());
        }
        model.put("groupDict", res);
    }

    /*@Autowired
     private ParseController parseController;*/
    @RequestMapping("/show")
    public String showParseForm(Map<String, Object> model) throws Exception {
        List<ColorGroup> cllist = colorGroupService.getColorGroups();
        model.put("cGrlList", cllist);
        return "ColorGroupShow";
    }

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