0% found this document useful (0 votes)
4 views

Report Resource

Uploaded by

Amina Lee
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Report Resource

Uploaded by

Amina Lee
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.projectS.tools.web.

rest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import com.projectS.tools.model.Report;
import com.projectS.tools.service.ReportService;
import com.projectS.tools.util.Json2Json;

@RestController
@RequestMapping("/api")
public class ReportResource {
private final Logger logger = LoggerFactory.getLogger(ReportResource.class);

@Autowired
private ReportService reportService;

@PostMapping("/saveReport")
public void saveReport(@RequestParam("file") MultipartFile file, String type)
{
//reportService.saveReport(report);
logger.info("start treat nodes ...");
logger.info("message You successfully uploaded {} ",
file.getOriginalFilename() );

if (!file.isEmpty()) {
try {

/**
* Try switch origin report and get appropriate template
*/
URL resource =
getClass().getClassLoader().getResource("ws_template.json");
JsonNode jsonNode = JsonLoader.fromURL(resource);
logger.info("saveReport(), template : {}",
jsonNode.toString());
logger.info("saveReport(), input : {} ", file.gecomame());

ByteArrayInputStream stream = new


ByteArrayInputStream(file.getBytes());
String fileString = IOUtils.toString(stream, "UTF-8");
logger.info("saveReport(), input : {} ", fileString);
String stringReport = Json2Json.transformJson(fileString,
jsonNode.toString());
logger.info("formatted json to internal form {}",
stringReport);

Gson gson = new Gson();


JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(stringReport);//
response will be the json String
Report reportModel = gson.fromJson(object, Report.class);
reportModel.setDetails(fileString);
reportService.saveReport(reportModel);
logger.info("end save details, ");

} catch (IOException e) {
e.printStackTrace();
}
}

}
}

You might also like