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