View Javadoc
1   package fr.ifremer.tutti.persistence.entities.referential;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
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.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   * Created on 2/17/15.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
39   * @since 3.14
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 }