Example usage for org.springframework.util DefaultPropertiesPersister store

List of usage examples for org.springframework.util DefaultPropertiesPersister store

Introduction

In this page you can find the example usage for org.springframework.util DefaultPropertiesPersister store.

Prototype

@Override
    public void store(Properties props, Writer writer, String header) throws IOException 

Source Link

Usage

From source file:com.teasoft.teavote.controller.ElectionInfoController.java

@RequestMapping(value = "api/teavote/election-info", method = RequestMethod.POST)
@ResponseBody//from   w  w  w.  ja v  a 2s  .c o  m
public JSONResponse saveElectionInfo(@RequestParam("logo") MultipartFile file,
        @RequestParam("commissioner") String commissioner, @RequestParam("orgName") String orgName,
        @RequestParam("electionDate") String electionDate, @RequestParam("startTime") String startTime,
        @RequestParam("endTime") String endTime, @RequestParam("pollingStation") String pollingStation,
        @RequestParam("startTimeInMillis") String startTimeInMillis,
        @RequestParam("endTimeInMillis") String endTimeInMillis, HttpServletRequest request) throws Exception {

    JSONResponse jSONResponse = new JSONResponse();
    //Candidate candidate = new Candidate();
    Map<String, String> errorMessages = new HashMap<>();

    if (!file.isEmpty()) {
        BufferedImage image = ImageIO.read(file.getInputStream());
        Integer width = image.getWidth();
        Integer height = image.getHeight();

        if (Math.abs(width - height) > MAX_IMAGE_DIM_DIFF) {
            errorMessages.put("image", "Invalid Image Dimension - Max abs(Width - height) must be 50 or less");
        } else {
            //Resize image
            BufferedImage originalImage = ImageIO.read(file.getInputStream());
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImagePng = resizeImage(originalImage, type);
            ConfigLocation configLocation = new ConfigLocation();
            //String rootPath = request.getSession().getServletContext().getRealPath("/");
            File serverFile = new File(configLocation.getConfigPath() + File.separator + "teavote-logo.jpg");

            switch (file.getContentType()) {
            case "image/png":
                ImageIO.write(resizeImagePng, "png", serverFile);
                break;
            case "image/jpeg":
                ImageIO.write(resizeImagePng, "jpg", serverFile);
                break;
            default:
                ImageIO.write(resizeImagePng, "png", serverFile);
                break;
            }
        }
    } else {
        //            errorMessages.put("image", "File Empty");
    }

    //Load properties
    Properties prop = appProp.getProperties();
    ConfigLocation configLocation = new ConfigLocation();
    prop.load(configLocation.getExternalProperties());

    prop.setProperty("teavote.orgName", orgName);
    prop.setProperty("teavote.commissioner", commissioner);
    prop.setProperty("teavote.electionDate", electionDate);
    prop.setProperty("teavote.startTime", startTime);
    prop.setProperty("teavote.endTime", endTime);
    prop.setProperty("teavote.pollingStation", pollingStation);
    prop.setProperty("teavote.startTimeInMillis", startTimeInMillis);
    prop.setProperty("teavote.endTimeInMillis", endTimeInMillis);

    File f = new File(configLocation.getConfigPath() + File.separator + "application.properties");
    OutputStream out = new FileOutputStream(f);
    DefaultPropertiesPersister p = new DefaultPropertiesPersister();
    p.store(prop, out, "Header Comment");

    if (errorMessages.isEmpty()) {
        return new JSONResponse(true, 0, null, Enums.JSONResponseMessage.SUCCESS.toString());
    }

    return new JSONResponse(false, 0, errorMessages, Enums.JSONResponseMessage.SERVER_ERROR.toString());
}