View Javadoc
1   package fr.ifremer.tutti.service.genericformat.importactions;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
32  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForOperation;
33  import fr.ifremer.tutti.service.genericformat.csv.OperationRow;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.nuiton.csv.ImportRow;
37  import org.nuiton.csv.ImportRuntimeException;
38  import org.nuiton.jaxx.application.ApplicationTechnicalException;
39  
40  import java.io.IOException;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Created on 3/3/15.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 3.14
49   */
50  public class ValidateOperationAction extends ImportActionSupport {
51  
52      /** Logger. */
53      private static final Log log = LogFactory.getLog(ValidateOperationAction.class);
54  
55      public ValidateOperationAction(GenericFormatContextSupport importContext) {
56          super(importContext);
57      }
58  
59      @Override
60      protected boolean canExecute() {
61          return importContext.isTechnicalFilesValid() && importContext.getSurveyFileResult().isValid();
62      }
63  
64      @Override
65      protected void doExecute() {
66  
67          if (log.isInfoEnabled()) {
68              log.info("Validate operation.csv file.");
69          }
70  
71          boolean importSpecies = importContext.getImportRequest().isImportSpecies();
72          boolean importBenthos = importContext.getImportRequest().isImportBenthos();
73          boolean importMarineLitter = importContext.getImportRequest().isImportMarineLitter();
74  
75          int maximumRowsInErrorPerFile = importContext.getImportRequest().getMaximumRowsInErrorPerFile();
76  
77          GenericFormatCsvFileResult importFileResult = importContext.getOperationFileResult();
78          try (CsvConsumerForOperation consumer = importContext.loadOperations(false)) {
79              for (ImportRow<OperationRow> row : consumer) {
80  
81                  importContext.increments(t("tutti.service.genericFormat.validate.operations", row.getLineNumber()));
82  
83                  GenericFormatImportCruiseContext cruiseContext = consumer.validateRow(row, importContext);
84  
85                  if (cruiseContext != null) {
86  
87                      consumer.prepareRowForPersist(row, importSpecies, importBenthos, importMarineLitter);
88                      OperationRow bean = row.getBean();
89  
90                      FishingOperation fishingOperation = bean.getFishingOperation();
91  
92                      CatchBatch catchBatch = bean.getCatchBatch();
93  
94                      importContext.addImportedFishingOperation(fishingOperation, catchBatch);
95  
96                  }
97  
98                  if (consumer.getNbRowsInErrors() > maximumRowsInErrorPerFile) {
99                      if (log.isWarnEnabled()) {
100                         log.warn("Too much errors, stop validating this file.");
101                     }
102                     break;
103                 }
104 
105             }
106 
107             flushConsumer(consumer, importFileResult);
108 
109         } catch (IOException e) {
110             throw new ApplicationTechnicalException("Could not close operation.csv file", e);
111         } catch (ImportRuntimeException e) {
112 
113             importFileResult.addGlobalError(e.getMessage());
114 
115         }
116 
117     }
118 
119 }