Skip to content

Commit

Permalink
fix download url, prefix, create logic, add jsonIO util
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 12, 2023
1 parent 239744a commit 22f1a5a
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 18 deletions.
Binary file removed 20231110-18-48-52_report.csv
Binary file not shown.
7 changes: 7 additions & 0 deletions springWarehouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@
<version>2.9.2</version>
</dependency>

<!-- json file IO -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

</dependencies>

<build>
Expand Down
6 changes: 3 additions & 3 deletions springWarehouse/sql/ddl/download_status.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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());
("20231112-17-15-24_report.json", "pending", now()),
("20231112-17-15-24_report.json", "failed", now()),
("20231112-17-15-24_report.json", "completed", now());
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String, Object> map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) {

Expand All @@ -53,38 +63,49 @@ public String list(Map<String, Object> 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<String, Object> 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<Resource> 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<DownloadStatus> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<String, Object> 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;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<body style="padding:8px;">
<h3 class="title">Report Download</h3>

<a href="/download/create">
<button class="create">Create Download</button>
<a href="/download/create_report">
<button class="create_report">Create Download</button>
</a>


Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);

}

}

0 comments on commit 22f1a5a

Please sign in to comment.