View Javadoc
1   package fr.ifremer.tutti.service.csv;
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.Species;
28  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
29  import fr.ifremer.tutti.service.PersistenceService;
30  import org.nuiton.csv.ImportRuntimeException;
31  
32  import java.util.List;
33  import java.util.Map;
34  
35  /**
36   * Created on 2/14/15.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 3.14
40   */
41  public class SpeciesParserFormatter extends EntityParserFormatterSupport<Species> {
42  
43      public static SpeciesParserFormatter newFormatter() {
44          return new SpeciesParserFormatter(false, null, null);
45      }
46  
47      public static SpeciesParserFormatter newTechnicalFormatter() {
48          return new SpeciesParserFormatter(true, null, null);
49      }
50  
51      public static SpeciesParserFormatter newParser(PersistenceService persistenceService, Map<Integer, Integer> referenceTaxonIdTranslationMap) {
52          return new SpeciesParserFormatter(true, persistenceService, referenceTaxonIdTranslationMap);
53      }
54  
55      private final PersistenceService persistenceService;
56  
57      private final Map<Integer, Integer> referenceTaxonIdTranslationMap;
58  
59      protected SpeciesParserFormatter(boolean technical, PersistenceService persistenceService, Map<Integer, Integer> referenceTaxonIdTranslationMap) {
60          super("", technical, Species.class);
61          this.persistenceService = persistenceService;
62          this.referenceTaxonIdTranslationMap = referenceTaxonIdTranslationMap;
63      }
64  
65      @Override
66      protected List<Species> getEntities() {
67          return persistenceService.getAllReferentSpecies();
68      }
69  
70      @Override
71      protected List<Species> getEntitiesWithObsoletes() {
72          return persistenceService.getAllReferentSpeciesWithObsoletes();
73      }
74  
75      protected Map<String, Species> getEntitiesById() {
76          if (entitiesById == null) {
77  
78              List<Species> entities = getEntities();
79              entitiesById = Speciess.splitReferenceSpeciesByReferenceTaxonId(entities);
80  
81          }
82          return entitiesById;
83      }
84  
85      @Override
86      protected String formatBusiness(Species value) {
87          return value.getName();
88      }
89  
90      @Override
91      protected String formatTechnical(Species value) {
92          return Speciess.GET_REFERECE_TAXON_ID.apply(value);
93      }
94  
95      @Override
96      protected Species parseNotBlankValue(String value) {
97  
98          Integer referenceTaxonId;
99          try {
100             referenceTaxonId = Integer.valueOf(value);
101         } catch (NumberFormatException e) {
102             throw new ImportRuntimeException("Le format du Code taxon n'est pas valide, cela devrait ĂȘtre un entier: " + value);
103         }
104 
105         if (referenceTaxonIdTranslationMap.containsKey(referenceTaxonId)) {
106             value = String.valueOf(referenceTaxonIdTranslationMap.get(referenceTaxonId));
107         }
108         return super.parseNotBlankValue(value);
109 
110     }
111 
112 }