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.referential.Gear;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
30  import fr.ifremer.tutti.service.genericformat.GenericFormatImportContext;
31  import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
32  import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
33  import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForGearCaracteristic;
34  import fr.ifremer.tutti.service.genericformat.csv.GearCaracteristicRow;
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 ImportGearCaracteristicAction extends ImportActionSupport {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(ImportGearCaracteristicAction.class);
56  
57      private final GenericformatImportPersistenceHelper persistenceHelper;
58  
59      public ImportGearCaracteristicAction(GenericFormatImportContext importContext, GenericformatImportPersistenceHelper persistenceHelper) {
60          super(importContext);
61          this.persistenceHelper = persistenceHelper;
62      }
63  
64      @Override
65      protected boolean canExecute() {
66          return importContext.isTechnicalFilesValid() && importContext.getSurveyFileResult().isValid();
67      }
68  
69      @Override
70      protected void skipExecute() {
71          importContext.increments(t("tutti.service.genericFormat.skip.import.gearCaracteristics"));
72      }
73  
74      @Override
75      protected void doExecute() {
76  
77          if (log.isInfoEnabled()) {
78              log.info("Import gearCaracteristics.csv file.");
79          }
80          importContext.increments(t("tutti.service.genericFormat.import.gearCaracteristics"));
81          GenericFormatCsvFileResult importFileResult = importContext.getGearCaracteristicFileResult();
82          try (CsvConsumerForGearCaracteristic consumer = importContext.loadGearCaracteristics(true)) {
83              for (ImportRow<GearCaracteristicRow> row : consumer) {
84  
85                  GenericFormatImportCruiseContext cruiseContext = consumer.validateRow(row, importContext);
86  
87                  if (cruiseContext != null) {
88  
89                      consumer.prepareRowForPersist(cruiseContext, row);
90  
91                  }
92  
93              }
94          } catch (IOException e) {
95              throw new ApplicationTechnicalException("Could not close gearCaracteristic.csv file", e);
96          } catch (ImportRuntimeException e) {
97  
98              importFileResult.addGlobalError(e.getMessage());
99  
100         }
101 
102         persistGearCaracteristics();
103 
104     }
105 
106     public void persistGearCaracteristics() {
107 
108         importContext.doActionOnCruiseContexts((cruiseContext, progressionModel) -> {
109 
110             importContext.increments(t("tutti.service.genericFormat.persist.gearCaracteristics", cruiseContext.getCruiseLabel()));
111 
112             if (cruiseContext.withGearCaracteristics()) {
113 
114                 Set<Gear> gears = cruiseContext.getGearsWithcaracteristics();
115 
116                 Cruise cruise = cruiseContext.getCruise();
117 
118                 for (Gear gear : gears) {
119 
120                     if (log.isInfoEnabled()) {
121                         log.info("Persist " + gear.getCaracteristics().size() + " gear caracteristics for gear: " + gear.getName() + " for cruise: " + cruiseContext.getCruiseLabel());
122                     }
123                     persistenceHelper.saveGearCaracteristics(gear, cruise);
124 
125                 }
126 
127             }
128 
129         });
130 
131     }
132 
133 }