|
1 | 1 | package de.symeda.sormas.ui.importer; |
2 | 2 |
|
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | + |
3 | 5 | import java.beans.IntrospectionException; |
4 | 6 | import java.beans.PropertyDescriptor; |
5 | 7 | import java.io.File; |
|
20 | 22 | import java.util.function.Consumer; |
21 | 23 | import java.util.function.Function; |
22 | 24 |
|
23 | | -import com.opencsv.exceptions.CsvValidationException; |
24 | | -import de.symeda.sormas.api.utils.CSVCommentLineValidator; |
25 | 25 | import org.apache.commons.lang3.StringUtils; |
26 | 26 | import org.slf4j.Logger; |
27 | 27 | import org.slf4j.LoggerFactory; |
28 | 28 |
|
29 | 29 | import com.opencsv.CSVReader; |
30 | 30 | import com.opencsv.CSVWriter; |
| 31 | +import com.opencsv.exceptions.CsvValidationException; |
31 | 32 | import com.vaadin.server.Sizeable.Unit; |
32 | 33 | import com.vaadin.server.StreamResource; |
33 | 34 | import com.vaadin.ui.UI; |
|
46 | 47 | import de.symeda.sormas.api.region.RegionReferenceDto; |
47 | 48 | import de.symeda.sormas.api.user.UserDto; |
48 | 49 | import de.symeda.sormas.api.user.UserReferenceDto; |
| 50 | +import de.symeda.sormas.api.utils.CSVCommentLineValidator; |
49 | 51 | import de.symeda.sormas.api.utils.CSVUtils; |
50 | 52 | import de.symeda.sormas.api.utils.DataHelper; |
51 | 53 | import de.symeda.sormas.api.utils.DateHelper; |
52 | 54 | import de.symeda.sormas.ui.utils.DownloadUtil; |
53 | 55 | import de.symeda.sormas.ui.utils.VaadinUiUtil; |
54 | 56 |
|
55 | | -import static java.nio.charset.StandardCharsets.UTF_8; |
56 | | - |
57 | 57 | /** |
58 | 58 | * Base class for all importers that are used to get data from CSV files into SORMAS. |
59 | 59 | * |
@@ -220,7 +220,8 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException |
220 | 220 |
|
221 | 221 | CSVReader csvReader = null; |
222 | 222 | try { |
223 | | - csvReader = CSVUtils.createCSVReader(new InputStreamReader(new FileInputStream(inputFile), UTF_8), |
| 223 | + csvReader = CSVUtils.createCSVReader( |
| 224 | + new InputStreamReader(new FileInputStream(inputFile), UTF_8), |
224 | 225 | FacadeProvider.getConfigFacade().getCsvSeparator(), |
225 | 226 | new CSVCommentLineValidator()); |
226 | 227 | errorReportCsvWriter = CSVUtils.createCSVWriter(createErrorReportWriter(), FacadeProvider.getConfigFacade().getCsvSeparator()); |
@@ -318,9 +319,8 @@ protected StreamResource createErrorReportStreamResource() { |
318 | 319 | */ |
319 | 320 | protected int readImportFileLength(File inputFile) throws IOException, CsvValidationException { |
320 | 321 | int importFileLength = 0; |
321 | | - try (CSVReader caseCountReader = CSVUtils.createCSVReader(new FileReader(inputFile), |
322 | | - FacadeProvider.getConfigFacade().getCsvSeparator(), |
323 | | - new CSVCommentLineValidator())) { |
| 322 | + try (CSVReader caseCountReader = |
| 323 | + CSVUtils.createCSVReader(new FileReader(inputFile), FacadeProvider.getConfigFacade().getCsvSeparator(), new CSVCommentLineValidator())) { |
324 | 324 |
|
325 | 325 | while (readNextValidLine(caseCountReader) != null) { |
326 | 326 | importFileLength++; |
@@ -366,21 +366,19 @@ protected abstract ImportLineResult importDataFromCsvLine( |
366 | 366 | "unchecked", |
367 | 367 | "rawtypes" }) |
368 | 368 | protected boolean executeDefaultInvokings(PropertyDescriptor pd, Object element, String entry, String[] entryHeaderPath) |
369 | | - throws InvocationTargetException, IllegalAccessException, ParseException, ImportErrorException { |
| 369 | + throws InvocationTargetException, IllegalAccessException, ImportErrorException { |
370 | 370 | Class<?> propertyType = pd.getPropertyType(); |
371 | 371 |
|
372 | 372 | if (propertyType.isEnum()) { |
373 | 373 | pd.getWriteMethod().invoke(element, Enum.valueOf((Class<? extends Enum>) propertyType, entry.toUpperCase())); |
374 | 374 | return true; |
375 | 375 | } |
376 | 376 | if (propertyType.isAssignableFrom(Date.class)) { |
377 | | - // If the string is smaller than the length of the expected date format, throw an exception |
378 | | - if (entry.length() < 10) { |
379 | | - throw new ImportErrorException( |
380 | | - I18nProperties.getValidationError(Validations.importInvalidDate, buildEntityProperty(entryHeaderPath))); |
381 | | - } else { |
| 377 | + try { |
382 | 378 | pd.getWriteMethod().invoke(element, DateHelper.parseDateWithException(entry)); |
383 | 379 | return true; |
| 380 | + } catch (ParseException e) { |
| 381 | + throw new ImportErrorException(I18nProperties.getValidationError(Validations.importInvalidDate, pd.getName())); |
384 | 382 | } |
385 | 383 | } |
386 | 384 | if (propertyType.isAssignableFrom(Integer.class)) { |
@@ -535,7 +533,8 @@ private String[] readNextValidLine(CSVReader csvReader) throws IOException, CsvV |
535 | 533 | throw e; |
536 | 534 | } |
537 | 535 | } |
538 | | - } while(isCommentLine); |
| 536 | + } |
| 537 | + while (isCommentLine); |
539 | 538 | return nextValidLine; |
540 | 539 | } |
541 | 540 | } |
0 commit comments