Improved: suppresses 2 warnings related to deprecated CSVFormat.DEFAULT.withHeader()

Also adds a try-with-ressource in InvoiceServices::importInvoice to properly
close csvReader on returns. Hence removes an useless csvReader.close().

Several comments about Eclipse giving wrong advice
<<Resource leak: '<unassigned Closeable value>' is not closed at this location>>
It's OK, we are inside try-with-ressources.

Finally removes a redundant final in PartyServices::importParty
This commit is contained in:
Jacques Le Roux
2021-12-26 18:22:48 +01:00
parent 21ebebc063
commit e4d43297a9
2 changed files with 16 additions and 7 deletions
@@ -3672,8 +3672,7 @@ public class InvoiceServices {
String organizationPartyId = (String) context.get("organizationPartyId");
String encoding = System.getProperty("file.encoding");
String csvString = Charset.forName(encoding).decode(fileBytes).toString();
final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
CSVFormat fmt = CSVFormat.DEFAULT;
List<String> errMsgs = new LinkedList<>();
List<String> newErrMsgs;
String lastInvoiceId = null;
@@ -3681,7 +3680,7 @@ public class InvoiceServices {
String newInvoiceId = null;
int invoicesCreated = 0;
try {
try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) {
for (final CSVRecord rec : fmt.parse(csvReader)) {
currentInvoiceId = rec.get("invoiceId");
if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) {
@@ -3758,11 +3757,18 @@ public class InvoiceServices {
try {
invoiceResult = dispatcher.runSync("createInvoice", invoice);
if (ServiceUtil.isError(invoiceResult)) {
// Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
// but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
// I prefer to not put @SuppressWarnings("resource") to the whole method
// BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(invoiceResult));
}
} catch (GenericServiceException e) {
csvReader.close();
Debug.logError(e, MODULE);
// Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
// but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
// I prefer to not put @SuppressWarnings("resource") to the whole method
// BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
return ServiceUtil.returnError(e.getMessage());
}
newInvoiceId = (String) invoiceResult.get("invoiceId");
@@ -3824,10 +3830,13 @@ public class InvoiceServices {
try {
Map<String, Object> result = dispatcher.runSync("createInvoiceItem", invoiceItem);
if (ServiceUtil.isError(result)) {
// Eclipse reports here: Resource leak: '<unassigned Closeable value>' is not closed at this location
// but it's OK. As csvReader is in a try-with-ressource it will be closed anyway
// I prefer to not put @SuppressWarnings("resource") to the whole method
// BTW to be consistent Eclipse should also reports the same issue in PartyService (see there)
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} catch (GenericServiceException e) {
csvReader.close();
Debug.logError(e, MODULE);
return ServiceUtil.returnError(e.getMessage());
}
@@ -2303,7 +2303,7 @@ public class PartyServices {
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
String encoding = System.getProperty("file.encoding");
String csvString = Charset.forName(encoding).decode(fileBytes).toString();
CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
CSVFormat fmt = CSVFormat.DEFAULT;
List<String> errMsgs = new LinkedList<>();
List<String> newErrMsgs = new LinkedList<>();
String lastPartyId = null; // last partyId read from the csv file
@@ -2333,7 +2333,7 @@ public class PartyServices {
try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) {
for (final CSVRecord rec : fmt.parse(csvReader)) {
for (CSVRecord rec : fmt.parse(csvReader)) {
if (UtilValidate.isNotEmpty(rec.get("partyId"))) {
currentPartyId = rec.get("partyId");
}