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.Cruise;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.persistence.model.CruiseDataModel;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
32  import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
33  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForSurvey;
34  import fr.ifremer.tutti.service.genericformat.csv.SurveyRow;
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  import org.nuiton.csv.ImportRow;
38  import org.nuiton.csv.ImportRuntimeException;
39  import org.nuiton.jaxx.application.ApplicationTechnicalException;
40  
41  import java.io.IOException;
42  import java.util.Set;
43  
44  import static org.nuiton.i18n.I18n.t;
45  
46  /**
47   * Created on 3/3/15.
48   *
49   * @author Tony Chemit - chemit@codelutin.com
50   * @since 3.14
51   */
52  public class ValidateSurveyAction extends ImportActionSupport {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(ValidateSurveyAction.class);
56  
57      private static int cruiseId = -1;
58  
59      private final GenericformatImportPersistenceHelper persistenceHelper;
60  
61      public ValidateSurveyAction(GenericFormatContextSupport importContext, GenericformatImportPersistenceHelper persistenceHelper) {
62          super(importContext);
63          this.persistenceHelper = persistenceHelper;
64      }
65  
66      @Override
67      protected boolean canExecute() {
68          return importContext.isTechnicalFilesValid();
69      }
70  
71      @Override
72      protected void doExecute() {
73  
74          if (log.isInfoEnabled()) {
75              log.info("Validate survey.csv file.");
76          }
77  
78          int maximumRowsInErrorPerFile = importContext.getImportRequest().getMaximumRowsInErrorPerFile();
79  
80          GenericFormatCsvFileResult importFileResult = importContext.getSurveyFileResult();
81          try (CsvConsumerForSurvey consumer = importContext.loadSurveys(false)) {
82              for (ImportRow<SurveyRow> row : consumer) {
83  
84                  importContext.increments(t("tutti.service.genericFormat.validate.cruises", row.getLineNumber()));
85  
86                  consumer.validateRow(row, importContext);
87                  consumer.prepareRowForPersist(row);
88  
89                  Cruise cruise = row.getBean().getCruise();
90  
91                  CruiseDataModel existingCruiseData = importContext.getImportRequest().getExistingCruiseData(cruise);
92  
93                  Set<FishingOperation> existingFishingOperations;
94  
95                  if (existingCruiseData == null) {
96  
97                      existingFishingOperations = null;
98  
99                      // add a temporary id to simulate persist behaviour
100                     cruise.setId(getNextCruiseId());
101 
102                 } else {
103 
104                     existingFishingOperations = persistenceHelper.getFishingOperations(existingCruiseData.getIdAsInt());
105 
106                 }
107 
108                 importContext.addImportedCruise(cruise, null, existingCruiseData, existingFishingOperations);
109 
110                 if (consumer.getNbRowsInErrors() > maximumRowsInErrorPerFile) {
111                     if (log.isWarnEnabled()) {
112                         log.warn("Too much errors, stop validating this file.");
113                     }
114                     break;
115                 }
116 
117             }
118 
119             flushConsumer(consumer, importFileResult);
120 
121         } catch (IOException e) {
122             throw new ApplicationTechnicalException("Could not close survey.csv file", e);
123         } catch (ImportRuntimeException e) {
124 
125             importFileResult.addGlobalError(e.getMessage());
126 
127         }
128 
129     }
130 
131     private static int getNextCruiseId() {
132         return cruiseId--;
133     }
134 }