1 package fr.ifremer.tutti.persistence.entities.referential;
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.TuttiPersistence;
28 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
29 import fr.ifremer.tutti.persistence.entities.protocol.SpeciesProtocol;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.TreeMap;
34
35
36
37
38
39
40
41 public class TaxonCache {
42
43 protected final boolean loadVernacularCode;
44
45 protected final TuttiPersistence persistenceService;
46
47 protected final Map<Integer, SpeciesProtocol> protocolMap;
48
49 protected final Map<Integer, Species> speciesByReferenceTaxonId;
50
51 TaxonCache(boolean loadVernacularCode, TuttiPersistence persistenceService, Map<Integer, SpeciesProtocol> protocolMap) {
52 this.loadVernacularCode = loadVernacularCode;
53 this.persistenceService = persistenceService;
54 this.protocolMap = protocolMap;
55 this.speciesByReferenceTaxonId = new TreeMap<>();
56 }
57
58 public String getLengthStepPmfmId(Species species) {
59
60 SpeciesProtocol speciesProtocol = protocolMap.get(species.getReferenceTaxonId());
61 return speciesProtocol == null ? null : speciesProtocol.getLengthStepPmfmId();
62
63 }
64
65 public Float getLengthStep(Species species) {
66
67 SpeciesProtocol speciesProtocol = protocolMap.get(species.getReferenceTaxonId());
68 return speciesProtocol == null ? null : speciesProtocol.getLengthStep();
69
70 }
71
72 public void load(List<Species> speciesList) {
73
74 for (Species species : speciesList) {
75 load(species);
76 }
77
78 }
79
80 public void loadInBatches(List<SpeciesBatch> speciesAbleBatches) {
81
82 for (SpeciesBatch speciesAbleBatch : speciesAbleBatches) {
83 load(speciesAbleBatch.getSpecies());
84 }
85
86 }
87
88 public void load(Species species) {
89
90 Integer referenceTaxonId = species.getReferenceTaxonId();
91
92 Species speciesLoaded = speciesByReferenceTaxonId.get(referenceTaxonId);
93
94 if (speciesLoaded == null) {
95
96 if (loadVernacularCode) {
97
98 Species speciesWithVerncularCode =
99 persistenceService.getSpeciesByReferenceTaxonIdWithVernacularCode(referenceTaxonId);
100 species.setVernacularCode(speciesWithVerncularCode.getVernacularCode());
101
102 }
103
104 if (protocolMap.containsKey(species.getReferenceTaxonId())) {
105
106 SpeciesProtocol speciesProtocol = protocolMap.get(species.getReferenceTaxonId());
107 String surveyCode = speciesProtocol.getSpeciesSurveyCode();
108 species.setSurveyCode(surveyCode);
109
110 }
111
112 speciesByReferenceTaxonId.put(species.getReferenceTaxonId(), species);
113
114 } else {
115
116 species.setVernacularCode(speciesLoaded.getVernacularCode());
117 species.setSurveyCode(speciesLoaded.getSurveyCode());
118
119 }
120
121 }
122
123 public boolean containsLengthStepPmfmId(Species species) {
124 return getLengthStepPmfmId(species)!=null;
125 }
126 }