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.service.genericformat.GenericFormatContextSupport;
28  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
30  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForGearCaracteristic;
31  import fr.ifremer.tutti.service.genericformat.csv.GearCaracteristicRow;
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   * Created on 3/3/15.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 3.14
47   */
48  public class ValidateGearCaracteristicAction extends ImportActionSupport {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(ValidateGearCaracteristicAction.class);
52  
53      public ValidateGearCaracteristicAction(GenericFormatContextSupport importContext) {
54          super(importContext);
55      }
56  
57      @Override
58      protected boolean canExecute() {
59          return importContext.isTechnicalFilesValid() && importContext.getSurveyFileResult().isValid();
60      }
61  
62      @Override
63      protected void doExecute() {
64  
65          if (log.isInfoEnabled()) {
66              log.info("Validate gearCaracteristics.csv file.");
67          }
68  
69          int maximumRowsInErrorPerFile = importContext.getImportRequest().getMaximumRowsInErrorPerFile();
70  
71          GenericFormatCsvFileResult importFileResult = importContext.getGearCaracteristicFileResult();
72          try (CsvConsumerForGearCaracteristic consumer = importContext.loadGearCaracteristics(false)) {
73              for (ImportRow<GearCaracteristicRow> row : consumer) {
74  
75                  importContext.increments(t("tutti.service.genericFormat.validate.gearCaracteristics", row.getLineNumber()));
76  
77                  GenericFormatImportCruiseContext cruiseContext = consumer.validateRow(row, importContext);
78  
79                  if (cruiseContext != null) {
80  
81                      consumer.prepareRowForPersist(cruiseContext, row);
82  
83                  }
84  
85                  if (consumer.getNbRowsInErrors() > maximumRowsInErrorPerFile) {
86                      if (log.isWarnEnabled()) {
87                          log.warn("Too much errors, stop validating this file.");
88                      }
89                      break;
90                  }
91  
92              }
93  
94              flushConsumer(consumer, importFileResult);
95  
96          } catch (IOException e) {
97              throw new ApplicationTechnicalException("Could not close gearCaracteristic.csv file", e);
98          } catch (ImportRuntimeException e) {
99  
100             importFileResult.addGlobalError(e.getMessage());
101 
102         }
103 
104     }
105 
106 }