controllers.PropertyController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.PropertyController.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.CarProperty;
import data.entity.PropertyName;
import data.services.BaseParamService;
import data.services.CarPropertyService;
import data.services.PropertyNameService;
import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import logic.ParamType;
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("/PropertyName")
public class PropertyController {

    @Autowired
    private PropertyNameService propertyNameService;
    @Autowired
    private CarPropertyService carPropertyService;
    @Autowired
    private BaseParamService baseParamService;

    @ModelAttribute
    public void setInfo(Map<String, Object> model) {
        /*Map<String, Object> res = new LinkedHashMap();
        for (CarProperty cp : carPropertyService.getAllProps()) {
        res.put(StringAdapter.getString(cp.getId()), cp.getTitle());
        }*/
        model.put("propDict", carPropertyService.getAllProps());
    }

    /*@ModelAttribute
    public void setInfo(Map<String, Object> model) {
    model.put("carList", propertyNameService.getCars());
    }*/

    @RequestMapping("/show")
    public String showProperties(Map<String, Object> model,
            @RequestParam(value = "propId", required = false) Long propId) throws Exception {
        if (propId != null) {

            //List<String> pnamelist = propertyNameService.getUniquePropertyNames(propId);
            List<PropertyName> pnamelist = propertyNameService.getUniquePropertyNames(propId);
            if (!propId.equals((long) 0)) {
                CarProperty cp = carPropertyService.find(propId);
                BaseParam bp = baseParamService.getBaseParam(cp.getUid().trim());
                if (bp.getParamType().equals(ParamType.PERCENTE)) {
                    model.put("PercentParam", "true");
                    model.put("baseParam", bp);
                }
            }
            model.put("uniquePropNames", pnamelist);
            model.put("oldPropertyId", propId);
        }
        return "PropertiesShow";
    }

    @RequestMapping("/updatePropertyNames")
    public String updatePropertyNames(Map<String, Object> model,
            @RequestParam(value = "propId", required = true) Long propId,
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "audial", required = false) Long audial,
            @RequestParam(value = "visual", required = false) Long visual,
            @RequestParam(value = "kinestet", required = false) Long kinestet,
            @RequestParam(value = "percentValue", required = false) Long percentValue,
            @RequestParam(value = "paramValue", required = false) Double paramValue,
            @RequestParam(value = "rad", required = false) String rad, RedirectAttributes ras) throws Exception {
        //List<String> pnamelist = propertyNameService.getUniquePropertyNames(pnId);

        //List<PropertyName> pnamelist = propertyNameService.getUniquePropertyNames(propId);
        if (propId != null && name != null && !name.equals("")) {
            propertyNameService.updateByNameAndPropertyId(name, propId, percentValue, paramValue, audial, visual,
                    kinestet, rad);
            /*List<PropertyName> plist = propertyNameService.getProperties(carId);
            model.put("props", plist);*/
        }
        ras.addAttribute("propId", propId);
        /*model.put("uniquePropNames", pnamelist);
        model.put("oldPropertyId", propId);*/
        return "redirect:/PropertyName/show";
    }

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

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

}