-
Notifications
You must be signed in to change notification settings - Fork 416
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
#1146 [Order] Common export csv file for basic table #1191
Conversation
96d1fc4
to
2d76b93
Compare
Order Coverage Report
|
|
||
public class CsvExporter { | ||
|
||
public static <T> byte[] exportToCsv(List<T> dataList, Class<T> clazz) throws IOException { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(","); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
common-library/pom.xml
Outdated
<dependency> | ||
<groupId>com.opencsv</groupId> | ||
<artifactId>opencsv</artifactId> | ||
<version>5.7.1</version> |
There was a problem hiding this comment.
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 { | ||
|
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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);
}
}`
Storefront BFF Coverage Report
|
Recommendation Coverage Report
|
Media Coverage Report
|
Customer Coverage Report
|
Webhook Coverage Report
|
Inventory Coverage Report
|
Promotion Coverage Report
|
Rating Coverage Report
|
Location Coverage Report
|
Payment Paypal Coverage Report
|
Tax Coverage Report
|
Product Coverage Report
|
Cart Coverage Report
|
Search Coverage Report
|
Payment Coverage Report
|
051da30
to
d8418f6
Compare
Quality Gate passed for 'storefront-bff'Issues Measures |
Quality Gate passed for 'backoffice-bff'Issues Measures |
Quality Gate passed for 'sampledata'Issues Measures |
Quality Gate passed for 'payment-paypal'Issues Measures |
Quality Gate passed for 'webhook'Issues Measures |
Quality Gate passed for 'customer'Issues Measures |
Quality Gate passed for 'order'Issues Measures |
Quality Gate passed for 'rating'Issues Measures |
Quality Gate passed for 'payment'Issues Measures |
Quality Gate passed for 'location'Issues Measures |
Quality Gate passed for 'promotion'Issues Measures |
Quality Gate passed for 'inventory'Issues Measures |
Quality Gate passed for 'search'Issues Measures |
Quality Gate passed for 'tax'Issues Measures |
Quality Gate passed for 'media'Issues Measures |
Quality Gate passed for 'product'Issues Measures |
Quality Gate passed for 'recommendation'Issues Measures |
No description provided.