1 package fr.ifremer.tutti.service.csv;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
37
38
39
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 }