controllers.FreeOptionController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.FreeOptionController.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.Feature;
import data.services.FreeOptionService;
import java.io.File;
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("/FreeOption")
public class FreeOptionController {

    @Autowired
    private FreeOptionService freeOptionService;

    @RequestMapping("/show")
    public String showParseForm(Map<String, Object> model) throws Exception {
        return "FreeOptionShow";
    }

    @RequestMapping("/getXls")
    public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception {
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=Options.xls");
        freeOptionService.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/Option");
            if (newFile.exists()) {
                newFile.delete();
            }
            try {
                FileUtils.writeByteArrayToFile(newFile, file.getBytes());
                freeOptionService.updateFromXml(newFile);
                ras.addFlashAttribute("error", freeOptionService.getResult().getErrors());
            } catch (Exception e) {
                ras.addFlashAttribute("error", "updateFromXml " + StringAdapter.getStackTraceException(e));
            }
            if (newFile.exists()) {
                newFile.delete();
            }
        }
        return "redirect:/FreeOption/show";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Map<String, Object> model) throws Exception {
        freeOptionService.deleteAll();
        return "FreeOptionShow";
    }

}