-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from yennanliu/warehouse-dev-003-add-report
warehouse-dev-003-add-report : Minor fix
- Loading branch information
Showing
15 changed files
with
288 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
springWarehouse/src/main/java/com/yen/springWarehouse/util/DateTimeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.yen.springWarehouse.util; | ||
|
||
|
||
import org.joda.time.DateTime; | ||
import org.joda.time.DateTimeZone; | ||
|
||
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
public class DateTimeUtils { | ||
|
||
public static SimpleDateFormat getFormatter(String formatPattern){ | ||
SimpleDateFormat formatter = new SimpleDateFormat(formatPattern); | ||
return formatter; | ||
} | ||
|
||
public static String getCurrentDate(){ | ||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | ||
formatter.setTimeZone(TimeZone.getTimeZone("GMT")); | ||
Date date = new Date(); | ||
String currentDate = formatter.format(date); | ||
return currentDate; | ||
} | ||
|
||
public static String getCurrentDateYYYYMMDDHHMMSS(){ | ||
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd-HH-mm-ss"); | ||
//formatter.setTimeZone(TimeZone.getTimeZone("GMT")); | ||
Date date = new Date(); | ||
String currentDate = formatter.format(date); | ||
return currentDate; | ||
} | ||
|
||
public static String getCurrentDateUTC(){ | ||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); | ||
DateTime now = DateTime.now( DateTimeZone.UTC ); | ||
String currentDate = formatter.format(now); | ||
return currentDate; | ||
} | ||
|
||
public static String getNDayBeforeDate(Integer dateBefore){ | ||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
Calendar cal = Calendar.getInstance(); | ||
cal.add(Calendar.DATE, -1 * dateBefore); | ||
String yesterday_date = dateFormat.format(cal.getTime()); | ||
return yesterday_date; | ||
} | ||
|
||
public static String getYesterdayDate(){ | ||
return getNDayBeforeDate(1); | ||
} | ||
|
||
public static String getYesterdayDateWithoutDash(){ | ||
return getYesterdayDate().replace("-", ""); | ||
} | ||
|
||
public static String addDashToDateTime(String dateTime){ | ||
|
||
DateFormat fromFormat = new SimpleDateFormat("yyyyMMdd"); | ||
DateFormat toFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
|
||
Date d = null; | ||
String d1 = null; | ||
|
||
try { | ||
d = fromFormat.parse(dateTime); | ||
} catch (ParseException e) { | ||
throw new RuntimeException(e); | ||
} | ||
d1 = toFormat.format(d); | ||
return d1; | ||
} | ||
|
||
} | ||
|
48 changes: 48 additions & 0 deletions
48
springWarehouse/src/main/java/com/yen/springWarehouse/util/FileUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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 { | ||
|
||
public Boolean writeFile(String intputString, String fileName){ | ||
|
||
try{ | ||
String value = intputString; //"Hello"; | ||
FileOutputStream fos = new FileOutputStream(fileName); | ||
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos)); | ||
outStream.writeUTF(value); | ||
outStream.close(); | ||
return true; | ||
}catch (Exception e){ | ||
log.error("writeFile error : " + e); | ||
return false; | ||
} | ||
} | ||
|
||
// 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; | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
springWarehouse/src/main/resources/report/20231112-17-15-24_report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
1 change: 1 addition & 0 deletions
1
springWarehouse/src/main/resources/report/20231112-18-00-25_report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
1 change: 1 addition & 0 deletions
1
springWarehouse/src/main/resources/report/20231112-18-03-04_report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
1 change: 1 addition & 0 deletions
1
springWarehouse/src/main/resources/report/20231112-18-13-33_report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
1 change: 1 addition & 0 deletions
1
springWarehouse/src/main/resources/report/20231112-18-13-35_report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"king","age":17} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
springWarehouse/src/main/resources/templates/download/success.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" xmlns:th="http://www.thymeleaf.org"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Create Download OK</title> | ||
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}" /> | ||
</head> | ||
<body style="padding:8px;"> | ||
<h3 class="title">Create Download OK</h3> | ||
|
||
<script type="text/javascript" th:src="@{/js/jquery-3.1.1.min.js}"></script> | ||
</body> | ||
</html> |
24 changes: 24 additions & 0 deletions
24
springWarehouse/src/test/java/com/yen/springWarehouse/controller/DownloadControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.yen.springWarehouse.controller; | ||
|
||
import com.yen.springWarehouse.util.DateTimeUtils; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.text.SimpleDateFormat; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class DownloadControllerTest { | ||
|
||
@Test | ||
public void testPrepareDownloadFile(){ | ||
|
||
String timeStamp = new SimpleDateFormat("yyyyMMdd-HH-mm-ss").format(new java.util.Date()); | ||
System.out.println(timeStamp); | ||
|
||
System.out.println(); | ||
|
||
DateTimeUtils dateTimeUtils = new DateTimeUtils(); | ||
System.out.println(dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS()); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
springWarehouse/src/test/java/com/yen/springWarehouse/util/FileUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
} |