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.referential.Vessel;
28  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
29  import fr.ifremer.tutti.service.genericformat.GenericFormatReferentialImportResult;
30  import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
31  import fr.ifremer.tutti.service.referential.ReferentialImportRequest;
32  import fr.ifremer.tutti.service.referential.ReferentialImportResult;
33  import fr.ifremer.tutti.service.referential.consumer.CsvConsumerForTemporaryVessel;
34  import fr.ifremer.tutti.service.referential.csv.VesselRow;
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 ImportReferentialVesselAction extends ImportActionSupport {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(ImportReferentialVesselAction.class);
55  
56      private final GenericformatImportPersistenceHelper persistenceHelper;
57  
58      public ImportReferentialVesselAction(GenericFormatContextSupport importContext, GenericformatImportPersistenceHelper persistenceHelper) {
59          super(importContext);
60          this.persistenceHelper = persistenceHelper;
61      }
62  
63      @Override
64      protected boolean canExecute() {
65          return importContext.getReferentialTemporaryVesselFileResult().isFound();
66      }
67  
68      @Override
69      protected void skipExecute() {
70  
71          if (log.isInfoEnabled()) {
72              log.info("Skip import temporary vessels (no file found).");
73          }
74          importContext.increments(t("tutti.service.genericFormat.skip.import.temporaryVessels"));
75  
76      }
77  
78      @Override
79      protected void doExecute() {
80  
81          importContext.increments(t("tutti.service.genericFormat.import.temporaryVessels"));
82  
83          ReferentialImportRequest<Vessel, String> referentialImportRequest = persistenceHelper.createVesselsImportRequest();
84  
85          GenericFormatReferentialImportResult<Vessel, String> importFileResult = importContext.getReferentialTemporaryVesselFileResult();
86          try (CsvConsumerForTemporaryVessel consumer = importContext.loadTemporaryVessels(false)) {
87              for (ImportRow<VesselRow> row : consumer) {
88                  consumer.checkRowForGenericFormatImport(row, referentialImportRequest);
89              }
90  
91              importFileResult.flushErrors(consumer);
92  
93          } catch (IOException e) {
94              throw new ApplicationTechnicalException("Could not close vessel.csv file", e);
95          } catch (ImportRuntimeException e) {
96  
97              importFileResult.addGlobalError(e.getMessage());
98  
99          }
100 
101         if (importFileResult.isValid()) {
102 
103             ReferentialImportResult<Vessel> referentialImportResult = persistenceHelper.importVessels(referentialImportRequest);
104             importFileResult.flushResult(referentialImportRequest, referentialImportResult);
105 
106             if (log.isInfoEnabled()) {
107                 log.info("Temporary vessels import result: " + importFileResult.getReport());
108             }
109 
110         } else {
111 
112             if (log.isWarnEnabled()) {
113                 log.warn("Do not import temporary vessels (the incoming file is not valid)");
114             }
115 
116         }
117 
118     }
119 }