55import java .beans .IntrospectionException ;
66import java .beans .PropertyDescriptor ;
77import java .io .File ;
8- import java .io .FileInputStream ;
98import java .io .FileReader ;
109import java .io .FileWriter ;
1110import java .io .IOException ;
12- import java .io .InputStreamReader ;
1311import java .io .Writer ;
1412import java .lang .reflect .InvocationTargetException ;
13+ import java .nio .file .Files ;
1514import java .nio .file .Path ;
1615import java .nio .file .Paths ;
1716import java .text .ParseException ;
@@ -139,70 +138,51 @@ public void run() {
139138 ImportResultStatus importResult = runImport ();
140139
141140 // Display a window presenting the import result
142- currentUI .access (new Runnable () {
143-
144- @ Override
145- public void run () {
146- window .setClosable (true );
147- progressLayout .makeClosable (() -> {
148- window .close ();
149- });
150-
151- if (importResult == ImportResultStatus .COMPLETED ) {
152- progressLayout .displaySuccessIcon ();
153- progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportSuccessful ));
154- } else if (importResult == ImportResultStatus .COMPLETED_WITH_ERRORS ) {
155- progressLayout .displayWarningIcon ();
156- progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportPartiallySuccessful ));
157- } else if (importResult == ImportResultStatus .CANCELED ) {
158- progressLayout .displaySuccessIcon ();
159- progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportCanceled ));
160- } else {
161- progressLayout .displayWarningIcon ();
162- progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportCanceledErrors ));
163- }
141+ currentUI .access (() -> {
142+ window .setClosable (true );
143+ progressLayout .makeClosable (window ::close );
144+
145+ if (importResult == ImportResultStatus .COMPLETED ) {
146+ progressLayout .displaySuccessIcon ();
147+ progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportSuccessful ));
148+ } else if (importResult == ImportResultStatus .COMPLETED_WITH_ERRORS ) {
149+ progressLayout .displayWarningIcon ();
150+ progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportPartiallySuccessful ));
151+ } else if (importResult == ImportResultStatus .CANCELED ) {
152+ progressLayout .displaySuccessIcon ();
153+ progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportCanceled ));
154+ } else {
155+ progressLayout .displayWarningIcon ();
156+ progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportCanceledErrors ));
157+ }
164158
165- window .addCloseListener (e -> {
166- if (importResult == ImportResultStatus .COMPLETED_WITH_ERRORS
167- || importResult == ImportResultStatus .CANCELED_WITH_ERRORS ) {
168- StreamResource streamResource = createErrorReportStreamResource ();
169- errorReportConsumer .accept (streamResource );
170- }
171- });
159+ window .addCloseListener (e -> {
160+ if (importResult == ImportResultStatus .COMPLETED_WITH_ERRORS || importResult == ImportResultStatus .CANCELED_WITH_ERRORS ) {
161+ StreamResource streamResource = createErrorReportStreamResource ();
162+ errorReportConsumer .accept (streamResource );
163+ }
164+ });
172165
173- currentUI .setPollInterval (-1 );
174- }
166+ currentUI .setPollInterval (-1 );
175167 });
176168 } catch (InvalidColumnException e ) {
177- currentUI .access (new Runnable () {
178-
179- @ Override
180- public void run () {
181- window .setClosable (true );
182- progressLayout .makeClosable (() -> {
183- window .close ();
184- });
185- progressLayout .displayErrorIcon ();
186- progressLayout
187- .setInfoLabelText (String .format (I18nProperties .getString (Strings .messageImportInvalidColumn ), e .getColumnName ()));
188- currentUI .setPollInterval (-1 );
189- }
169+ currentUI .access (() -> {
170+ window .setClosable (true );
171+ progressLayout .makeClosable (window ::close );
172+ progressLayout .displayErrorIcon ();
173+ progressLayout
174+ .setInfoLabelText (String .format (I18nProperties .getString (Strings .messageImportInvalidColumn ), e .getColumnName ()));
175+ currentUI .setPollInterval (-1 );
190176 });
191177 } catch (Exception e ) {
192178 logger .error (e .getMessage (), e );
193179
194- currentUI .access (new Runnable () {
195-
196- @ Override
197- public void run () {
198- window .setClosable (true );
199- progressLayout .makeClosable (() -> {
200- window .close ();
201- });
202- progressLayout .displayErrorIcon ();
203- progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportFailedFull ));
204- currentUI .setPollInterval (-1 );
205- }
180+ currentUI .access (() -> {
181+ window .setClosable (true );
182+ progressLayout .makeClosable (window ::close );
183+ progressLayout .displayErrorIcon ();
184+ progressLayout .setInfoLabelText (I18nProperties .getString (Strings .messageImportFailedFull ));
185+ currentUI .setPollInterval (-1 );
206186 });
207187 }
208188 }
@@ -215,13 +195,14 @@ public void run() {
215195 * To be called by async import thread or unit test
216196 */
217197 public ImportResultStatus runImport () throws IOException , InvalidColumnException , InterruptedException , CsvValidationException {
218- logger .debug ("runImport - " + inputFile .getAbsolutePath ());
219- Date methodDate = new Date ();
198+ logger .debug ("runImport - {}" , inputFile .getAbsolutePath ());
199+
200+ long t0 = System .currentTimeMillis ();
220201
221202 CSVReader csvReader = null ;
222203 try {
223204 csvReader = CSVUtils .createCSVReader (
224- new InputStreamReader ( new FileInputStream ( inputFile ), UTF_8 ),
205+ Files . newBufferedReader ( inputFile . toPath ( ), UTF_8 ),
225206 FacadeProvider .getConfigFacade ().getCsvSeparator (),
226207 new CSVCommentLineValidator ());
227208 errorReportCsvWriter = CSVUtils .createCSVWriter (createErrorReportWriter (), FacadeProvider .getConfigFacade ().getCsvSeparator ());
@@ -255,7 +236,7 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
255236 int lineCounter = 0 ;
256237 while (nextLine != null ) {
257238 ImportLineResult lineResult = importDataFromCsvLine (nextLine , entityClasses , entityProperties , entityPropertyPaths , lineCounter == 0 );
258- logger .debug ("runImport - line " + lineCounter );
239+ logger .debug ("runImport - line {}" , lineCounter );
259240 if (importedLineCallback != null ) {
260241 importedLineCallback .accept (lineResult );
261242 }
@@ -266,8 +247,11 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
266247 lineCounter ++;
267248 }
268249
269- logger .debug ("runImport - done" );
270- logger .debug ("import took - " + (new Date ().getTime () - methodDate .getTime ()) / 1000d );
250+ if (logger .isDebugEnabled ()) {
251+ logger .debug ("runImport - done" );
252+ long dt = System .currentTimeMillis () - t0 ;
253+ logger .debug ("import of {} lines took {} ms ({} ms/line)" , lineCounter , dt , lineCounter > 0 ? dt / lineCounter : -1 );
254+ }
271255
272256 if (cancelAfterCurrent ) {
273257 if (!hasImportError ) {
@@ -285,7 +269,6 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
285269 csvReader .close ();
286270 }
287271 if (errorReportCsvWriter != null ) {
288- errorReportCsvWriter .flush ();
289272 errorReportCsvWriter .close ();
290273 }
291274 }
0 commit comments