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.CaracteristicMap;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatImportContext;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
32  import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
33  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForParameter;
34  import fr.ifremer.tutti.service.genericformat.csv.ParameterRow;
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  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Created on 3/3/15.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.14
50   */
51  public class ImportParameterAction extends ImportActionSupport {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(ImportParameterAction.class);
55  
56      private final GenericformatImportPersistenceHelper persistenceHelper;
57  
58      public ImportParameterAction(GenericFormatImportContext importContext, GenericformatImportPersistenceHelper persistenceHelper) {
59          super(importContext);
60          this.persistenceHelper = persistenceHelper;
61      }
62  
63      @Override
64      protected boolean canExecute() {
65          return importContext.isTechnicalFilesValid()
66                 && importContext.getSurveyFileResult().isValid()
67                 && importContext.getOperationFileResult().isValid();
68      }
69  
70      @Override
71      protected void skipExecute() {
72          importContext.increments(t("tutti.service.genericFormat.skip.import.parameters"));
73      }
74  
75      @Override
76      protected void doExecute() {
77  
78          if (log.isInfoEnabled()) {
79              log.info("Import parameter.csv file.");
80          }
81          importContext.increments(t("tutti.service.genericFormat.import.parameters"));
82          GenericFormatCsvFileResult importFileResult = importContext.getParameterFileResult();
83          try (CsvConsumerForParameter consumer = importContext.loadParameters(true)) {
84              for (ImportRow<ParameterRow> row : consumer) {
85  
86                  GenericFormatImportOperationContext operationContext = consumer.validateRow(row, importContext);
87                  if (operationContext != null) {
88                      consumer.prepareRowForPersist(operationContext, row);
89                  }
90  
91              }
92          } catch (IOException e) {
93              throw new ApplicationTechnicalException("Could not close parameter.csv file", e);
94          } catch (ImportRuntimeException e) {
95  
96              importFileResult.addGlobalError(e.getMessage());
97  
98          }
99  
100         persistFishingOperationParameters();
101 
102     }
103 
104     public void persistFishingOperationParameters() {
105 
106         importContext.doActionOnCruiseContexts((cruiseContext, progressionModel) -> {
107 
108             boolean updateOperations = importContext.getImportRequest().isUpdateOperations();
109 
110             for (GenericFormatImportOperationContext fishingOperationContext : cruiseContext) {
111 
112                 String cruiseStr = cruiseContext.getCruiseLabel();
113                 String operationStr = fishingOperationContext.getFishingOperationLabel();
114 
115                 importContext.increments(t("tutti.service.genericFormat.persist.operation.parameters", cruiseStr, operationStr));
116 
117                 if (updateOperations) {
118 
119                     boolean persist = false;
120 
121                     FishingOperation fishingOperation = fishingOperationContext.getFishingOperation();
122 
123                     if (fishingOperationContext.withGearFeatures()) {
124 
125                         CaracteristicMap gearUseFeatures = fishingOperationContext.getGearUseFeatures();
126                         fishingOperation.setGearUseFeatures(gearUseFeatures);
127                         if (log.isInfoEnabled()) {
128                             log.info("Persist " + gearUseFeatures.size() + " gear use features of " + operationStr + " for cruise: " + cruiseStr);
129                         }
130 
131                         persist = true;
132 
133                     }
134 
135                     if (fishingOperationContext.withVesselFeatures()) {
136 
137                         CaracteristicMap vesselUseFeatures = fishingOperationContext.getVesselUseFeatures();
138                         fishingOperation.setVesselUseFeatures(vesselUseFeatures);
139                         if (log.isInfoEnabled()) {
140                             log.info("Persist " + vesselUseFeatures.size() + " vessel use features of " + operationStr + " for cruise: " + cruiseStr);
141                         }
142 
143                         persist = true;
144 
145                     }
146 
147                     if (persist) {
148 
149                         persistenceHelper.saveFishingOperation(fishingOperation);
150 
151                     }
152 
153                 }
154 
155             }
156         });
157 
158     }
159 
160 }