Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1146 [Order] Common export csv file for basic table #1191

Merged
merged 11 commits into from
Oct 21, 2024

Conversation

nashtech-longlevanquoc1
Copy link
Contributor

No description provided.

@nashtech-longlevanquoc1 nashtech-longlevanquoc1 force-pushed the #1146-Common-export-csv-file-for-basictable branch from 96d1fc4 to 2d76b93 Compare October 15, 2024 10:01
Copy link

github-actions bot commented Oct 15, 2024

Order Coverage Report

Overall Project 75.77% -2.85%
Files changed 26.6%

File Coverage
OrderListVm.java 100% 🍏
OrderController.java 100% 🍏
OrderService.java 79.13% -12.52%


public class CsvExporter {

public static <T> byte[] exportToCsv(List<T> dataList, Class<T> clazz) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use CSV library (i.e opencsv). CSVWriter will help to escapes special characters like , \ in the content. In addition, please initialize Writer, ByteArrayOutputStream in try so they will be closed properly.

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
     CSVWriter writer = new CSVWriter(new OutputStreamWriter(baos))) {


public static <T> byte[] exportToCsv(List<T> dataList, Class<T> clazz) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(byteArrayOutputStream, StandardCharsets.UTF_8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please close OutputStreamWriter in case Exception (consider using try-with-resources)

for (Field field : fields) {
CsvColumn columnAnnotation = field.getAnnotation(CsvColumn.class);
if (columnAnnotation != null) {
writer.append(columnAnnotation.columnName()).append(",");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"A,B,C,"
Consider the unnecessary comma at the end of the sentence.
Should check for generate data below.

try {
field.setAccessible(true);
Object value = field.get(data);
if (value instanceof List) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check value is null, will not be showing "null"

}

public static <T> String createFileName(Class<T> clazz) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "dd-MM-yyyy_HH-mm-ss" because there are a lot of files created in a day

field.setAccessible(true);
Object value = field.get(data);
if (value instanceof List) {
writer.append(String.join(",", (List<String>) value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be considered in case of special characters.

@nashtech-longlevanquoc1 nashtech-longlevanquoc1 changed the title [Order] Common export csv file for basic table #1146 [Order] Common export csv file for basic table Oct 17, 2024
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.7.1</version>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the version to parent pom.xml?
<opencsv.version>5.7.1</opencsv.version>

package com.yas.commonlibrary.csv;

public class BaseCsv {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at least we should have some common fields for this class , for example id. Then remove id in children classes
@CsvColumn(columnName = "id")
private Long id;

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy_HH-mm-ss");
String fromDate = LocalDateTime.now().format(dateFormatter);
CsvName csvName = clazz.getAnnotation(CsvName.class);
return csvName.fileName() + "_" + fromDate + ".csv";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just minor comment, use String.format instead of string concatenation
String.format("%s_%s.csv", csvName.fileName(), timestamp)


public static <T> String createFileName(Class<T> clazz) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy_HH-mm-ss");
String fromDate = LocalDateTime.now().format(dateFormatter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a DateTimeUtils that will be used for other places?

`public class DateTimeUtils {

private static final String DEFAULT_PATTERN = "dd-MM-yyyy_HH-mm-ss";

public static String format(LocalDateTime dateTime) {
    return format(dateTime, DEFAULT_PATTERN);
}

public static String format(LocalDateTime dateTime, String pattern) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
    return dateTime.format(formatter);
}

}`

Copy link

Storefront BFF Coverage Report

Overall Project NaN% NaN% 🍏

There is no coverage information present for the Files changed

Copy link

Recommendation Coverage Report

Overall Project NaN% NaN% 🍏

There is no coverage information present for the Files changed

Copy link

Media Coverage Report

Overall Project 87.19% 🍏

There is no coverage information present for the Files changed

Copy link

Customer Coverage Report

Overall Project 88.75% 🍏

There is no coverage information present for the Files changed

Copy link

Webhook Coverage Report

Overall Project 77.63%

There is no coverage information present for the Files changed

Copy link

Inventory Coverage Report

Overall Project 90.52% 🍏

There is no coverage information present for the Files changed

Copy link

Promotion Coverage Report

Overall Project 82.95% 🍏

There is no coverage information present for the Files changed

Copy link

Rating Coverage Report

Overall Project 81.94% 🍏

There is no coverage information present for the Files changed

Copy link

Location Coverage Report

Overall Project 93.28% 🍏

There is no coverage information present for the Files changed

Copy link

Payment Paypal Coverage Report

Overall Project 84.47% 🍏

There is no coverage information present for the Files changed

Copy link

Tax Coverage Report

Overall Project 88.12% 🍏

There is no coverage information present for the Files changed

Copy link

Product Coverage Report

Overall Project 77.99%

There is no coverage information present for the Files changed

Copy link

Cart Coverage Report

Overall Project 83.72% 🍏

There is no coverage information present for the Files changed

Copy link

Search Coverage Report

Overall Project 94.07% 🍏

There is no coverage information present for the Files changed

Copy link

Payment Coverage Report

Overall Project 73.77%

There is no coverage information present for the Files changed

@nashtech-longlevanquoc1 nashtech-longlevanquoc1 force-pushed the #1146-Common-export-csv-file-for-basictable branch from 051da30 to d8418f6 Compare October 18, 2024 10:21
Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

Copy link

sonarcloud bot commented Oct 18, 2024

@khanhtrand khanhtrand merged commit cfcfd40 into main Oct 21, 2024
53 checks passed
@khanhtrand khanhtrand deleted the #1146-Common-export-csv-file-for-basictable branch October 21, 2024 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants