1 package fr.ifremer.tutti.service.genericformat.importactions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
28 import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
29 import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
30 import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForAccidentalCatch;
31 import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.csv.ImportRow;
35 import org.nuiton.csv.ImportRuntimeException;
36 import org.nuiton.jaxx.application.ApplicationTechnicalException;
37
38 import java.io.IOException;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45
46
47
48 public class ValidateAccidentalCatchAction extends ImportActionSupport {
49
50
51 private static final Log log = LogFactory.getLog(ValidateAccidentalCatchAction.class);
52
53 public ValidateAccidentalCatchAction(GenericFormatContextSupport importContext) {
54 super(importContext);
55 }
56
57 @Override
58 protected boolean canExecute() {
59 return importContext.isTechnicalFilesValid() && importContext.getOperationFileResult().isValid();
60 }
61
62 @Override
63 protected void doExecute() {
64
65 if (log.isInfoEnabled()) {
66 log.info("Validate accidentalCatch.csv file.");
67 }
68
69 int maximumRowsInErrorPerFile = importContext.getImportRequest().getMaximumRowsInErrorPerFile();
70
71 GenericFormatCsvFileResult importFileResult = importContext.getAccidentalCatchFileResult();
72 try (CsvConsumerForAccidentalCatch consumer = importContext.loadAccidentalCatches(false)) {
73 for (ImportRow<AccidentalCatchRow> row : consumer) {
74
75 importContext.increments(t("tutti.service.genericFormat.validate.accidentalCatches", row.getLineNumber()));
76
77 GenericFormatImportOperationContext operationContext = consumer.validateRow(row, importContext);
78
79 if (operationContext != null) {
80 consumer.prepareRowForPersist(operationContext, row);
81 }
82
83 if (consumer.getNbRowsInErrors() > maximumRowsInErrorPerFile) {
84 if (log.isWarnEnabled()) {
85 log.warn("Too much errors, stop validating this file.");
86 }
87 break;
88 }
89
90 }
91
92 flushConsumer(consumer, importFileResult);
93
94 } catch (IOException e) {
95 throw new ApplicationTechnicalException("Could not close accidentalCatch.csv file", e);
96 } catch (ImportRuntimeException e) {
97
98 importFileResult.addGlobalError(e.getMessage());
99
100 }
101
102 }
103
104 }