diff --git a/20231110-18-48-52_report.csv b/20231110-18-48-52_report.csv deleted file mode 100644 index 0e41fe6b1..000000000 Binary files a/20231110-18-48-52_report.csv and /dev/null differ diff --git a/springWarehouse/pom.xml b/springWarehouse/pom.xml index eb24a046d..e980f8aeb 100644 --- a/springWarehouse/pom.xml +++ b/springWarehouse/pom.xml @@ -186,6 +186,13 @@ 2.9.2 + + + com.googlecode.json-simple + json-simple + 1.1.1 + + diff --git a/springWarehouse/sql/ddl/download_status.sql b/springWarehouse/sql/ddl/download_status.sql index 7c69f2b76..67d77e5c8 100644 --- a/springWarehouse/sql/ddl/download_status.sql +++ b/springWarehouse/sql/ddl/download_status.sql @@ -13,6 +13,6 @@ CREATE TABLE IF NOT EXISTS download_status( INSERT INTO download_status(download_url, status, create_time) VALUES -("test.json", "pending", now()), -("test.json", "failed", now()), -("test.json", "completed", now()); \ No newline at end of file +("20231112-17-15-24_report.json", "pending", now()), +("20231112-17-15-24_report.json", "failed", now()), +("20231112-17-15-24_report.json", "completed", now()); \ No newline at end of file diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java index 0012e1ddf..4eeca69f2 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/controller/DownloadController.java @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.yen.springWarehouse.bean.DownloadStatus; +import com.yen.springWarehouse.mapper.DownloadStatusMapper; import com.yen.springWarehouse.service.DownloadStatusService; import com.yen.springWarehouse.util.DateTimeUtils; import com.yen.springWarehouse.util.FileUtil; @@ -19,8 +20,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.io.File; import java.io.IOException; import java.util.Date; +import java.util.HashMap; +import java.util.List; import java.util.Map; @Slf4j @@ -31,6 +35,12 @@ public class DownloadController { @Autowired DownloadStatusService downloadStatusService; + @Autowired + DownloadStatusMapper downloadStatusMapper; + + String userDirectory = new File("").getAbsolutePath(); + final String prefix = userDirectory + "/src/main/resources/report/"; + @GetMapping("/list") public String list(Map map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) { @@ -53,38 +63,49 @@ public String list(Map map, @RequestParam(value="pageNo", requir return "download/list_download"; } - @GetMapping("/create") - public String createDownload(){ + @GetMapping("/create_report") + public String createDownload() { DateTimeUtils dateTimeUtils = new DateTimeUtils(); String timestamp = dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS(); - FileUtil fileUtil = new FileUtil(); - final String prefix = "/Users/yennanliu/SpringPlayground/"; - String fileName = "/" + timestamp + "_report.csv"; - DownloadStatus downloadStatus = new DownloadStatus(); - downloadStatus.setStatus("completed"); - downloadStatus.setDownloadUrl(fileName); - downloadStatus.setCreateTime(new Date()); + // create report + String fileName = timestamp + "_report.json"; + Map map = new HashMap<>(); + map.put("name", "king"); + map.put("age", 17); // save file to local // TODO : save it to remote file system (e.g. S3) - if (fileUtil.writeFile("some data", prefix+fileName)){ + Boolean result = fileUtil.writeJsonFile(map, prefix + fileName); + if (result) { + DownloadStatus downloadStatus = new DownloadStatus(); + downloadStatus.setStatus("completed"); + downloadStatus.setDownloadUrl(fileName); + downloadStatus.setCreateTime(new Date()); log.info("save File OK"); // save to DB downloadStatusService.save(downloadStatus); - }else { + } else { log.info("save File failed"); } return "download/success"; } + // TODO : fix : make GET request with report id @GetMapping("/download_report") public ResponseEntity downloadFile() throws IOException { // Load the file from the classpath (assuming it's in the resources/static directory) - Resource resource = new ClassPathResource("/test.json"); +// Resource resource = new ClassPathResource("/test.json"); +// HttpHeaders headers = new HttpHeaders(); +// headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.json"); + + List downloadStatusList = downloadStatusMapper.getAllDownloadStatus(); + // TODO : fix below (currently only get 1st downloadStatus) + String downloadUrl = "/report/" + downloadStatusList.get(0).getDownloadUrl(); + Resource resource = new ClassPathResource(downloadUrl); HttpHeaders headers = new HttpHeaders(); - headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.json"); + headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=%s".format("test.json")); return ResponseEntity.ok() .headers(headers) diff --git a/springWarehouse/src/main/java/com/yen/springWarehouse/util/FileUtil.java b/springWarehouse/src/main/java/com/yen/springWarehouse/util/FileUtil.java index ec83b1896..7a0fbb0c8 100644 --- a/springWarehouse/src/main/java/com/yen/springWarehouse/util/FileUtil.java +++ b/springWarehouse/src/main/java/com/yen/springWarehouse/util/FileUtil.java @@ -1,10 +1,13 @@ package com.yen.springWarehouse.util; import lombok.extern.slf4j.Slf4j; +import org.json.simple.JSONObject; import java.io.BufferedOutputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; +import java.io.FileWriter; +import java.util.Map; @Slf4j public class FileUtil { @@ -24,4 +27,22 @@ public Boolean writeFile(String intputString, String fileName){ } } + // https://www.tutorialspoint.com/how-to-write-create-a-json-file-using-java + public Boolean writeJsonFile(Map map, String fileName){ + try{ + JSONObject jsonObject = new JSONObject(); + // insert map to jsonObject + for (String key : map.keySet()){ + jsonObject.put(key, map.get(key)); + } + FileWriter file = new FileWriter(fileName); + file.write(jsonObject.toJSONString()); + file.close(); + return true; + }catch (Exception e){ + log.error("writeJsonFile error : " + e); + return false; + } + } + } diff --git a/springWarehouse/src/main/resources/report/20231112-17-15-24_report.json b/springWarehouse/src/main/resources/report/20231112-17-15-24_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231112-17-15-24_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/report/20231112-18-00-25_report.json b/springWarehouse/src/main/resources/report/20231112-18-00-25_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231112-18-00-25_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/report/20231112-18-03-04_report.json b/springWarehouse/src/main/resources/report/20231112-18-03-04_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231112-18-03-04_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/report/20231112-18-13-33_report.json b/springWarehouse/src/main/resources/report/20231112-18-13-33_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231112-18-13-33_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/report/20231112-18-13-35_report.json b/springWarehouse/src/main/resources/report/20231112-18-13-35_report.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/20231112-18-13-35_report.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/report/test_output_2.json b/springWarehouse/src/main/resources/report/test_output_2.json new file mode 100644 index 000000000..f847a6e1c --- /dev/null +++ b/springWarehouse/src/main/resources/report/test_output_2.json @@ -0,0 +1 @@ +{"name":"king","age":17} \ No newline at end of file diff --git a/springWarehouse/src/main/resources/templates/download/list_download.html b/springWarehouse/src/main/resources/templates/download/list_download.html index f6e0ed429..0702d7b0a 100644 --- a/springWarehouse/src/main/resources/templates/download/list_download.html +++ b/springWarehouse/src/main/resources/templates/download/list_download.html @@ -8,8 +8,8 @@

Report Download

- - + + diff --git a/springWarehouse/src/test/java/com/yen/springWarehouse/util/FileUtilTest.java b/springWarehouse/src/test/java/com/yen/springWarehouse/util/FileUtilTest.java new file mode 100644 index 000000000..63fee645c --- /dev/null +++ b/springWarehouse/src/test/java/com/yen/springWarehouse/util/FileUtilTest.java @@ -0,0 +1,46 @@ +package com.yen.springWarehouse.util; + +import com.yen.springWarehouse.controller.DownloadController; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; + +class FileUtilTest { + + @Test + public void testWriteJsonFile() throws URISyntaxException { + + String userDirectory = new File("").getAbsolutePath(); + System.out.println(">>> current path = " + userDirectory); // >>> crrent path = /Users/yennanliu/SpringPlayground/springWarehouse + String prefix = userDirectory + "/src/main/resources/report"; + + FileUtil fileUtil = new FileUtil(); + String fileName = prefix + "/" + "test_output_2.json"; + Map map = new HashMap<>(); + map.put("name", "king"); + map.put("age", 17); + Boolean result = fileUtil.writeJsonFile(map, fileName); + System.out.println("write json to : " + fileName); + System.out.println(result); + } + + @Test + public void testWriteJsonFile2() throws URISyntaxException { + + URL url = this.getClass().getResource("/report"); + File parentDirectory = new File(new URI(url.toString())); + + System.out.println("url = " + url); + System.out.println("url.getPath() = " + url.getPath()); + System.out.println("parentDirectory = " + parentDirectory); + + } + +} \ No newline at end of file