View Javadoc
1   package fr.ifremer.tutti.service.pupitri;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 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 com.google.common.collect.ListMultimap;
28  import com.google.common.collect.Lists;
29  import fr.ifremer.tutti.persistence.entities.referential.Species;
30  import org.apache.commons.lang3.mutable.MutableFloat;
31  
32  import java.util.LinkedHashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  /**
37   * Created on 11/21/14.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
40   * @since 3.10
41   */
42  public class CarrouselImportRequestResult {
43  
44      private final ListMultimap<String, Species> speciesByCode;
45  
46      /**
47       * Nombre d'espèces importées depuis le fichier carrousel.
48       */
49      private int nbCarrousselImported;
50  
51      /**
52       * Poids total du VRAC cumulé depuis le fichier carrousel.
53       */
54      private MutableFloat carrouselSortedWeight;
55  
56      /**
57       * Poids total du HORS-VRAC cumulé depuis le fichier carrousel.
58       */
59      private MutableFloat carrouselUnsortedWeight;
60  
61      /**
62       * Ensemble de identifiants d'espèces non importés depuis el fichier carrousel.
63       */
64      private final Set<String> notImportedSpeciesIds;
65  
66      /**
67       * Liste des lots lus depuis le fichier carrousel. Chaque lot est un tuple (espece,trie).
68       */
69      private final List<PupitriSpeciesContext> catches;
70  
71      public CarrouselImportRequestResult(ListMultimap<String, Species> speciesByCode) {
72  
73          this.speciesByCode = speciesByCode;
74          this.notImportedSpeciesIds = new LinkedHashSet<>();
75          this.catches = Lists.newArrayList();
76      }
77  
78      public void incrementNbCarrousselImported() {
79          nbCarrousselImported++;
80      }
81  
82      public void addCarrouselSortedWeight(Float beanWeight) {
83          if (carrouselSortedWeight == null) {
84              carrouselSortedWeight = new MutableFloat();
85          }
86          if (beanWeight != null) {
87  
88              carrouselSortedWeight.add(beanWeight);
89          }
90      }
91  
92      public void addCarrouselUnsortedWeight(Float beanWeight) {
93          if (carrouselUnsortedWeight == null) {
94              carrouselUnsortedWeight = new MutableFloat();
95          }
96          if (beanWeight != null) {
97  
98              carrouselUnsortedWeight.add(beanWeight);
99          }
100     }
101 
102     public List<Species> getSpecies(String speciesId) {
103         return speciesByCode.get(speciesId);
104     }
105 
106     public void addNotImportedSpeciesId(String speciesId) {
107         notImportedSpeciesIds.add(speciesId);
108     }
109 
110     public PupitriSpeciesContext getOrCreateCatch(List<Species> speciesList,
111                                                   boolean createMissingSigns,
112                                                   boolean sorted) {
113 
114         // on utilise la première espèce trouvée dans la liste des candidates
115         //FIXME Bien s'assurer que cela est ok
116         Species species = speciesList.get(0);
117         PupitriSpeciesContext pupitriSpeciesContext = new PupitriSpeciesContext(species,
118                                                                                 createMissingSigns,
119                                                                                 sorted);
120         int catchIndex = catches.indexOf(pupitriSpeciesContext);
121         if (catchIndex >= 0) {
122             pupitriSpeciesContext = catches.get(catchIndex);
123         } else {
124             catches.add(pupitriSpeciesContext);
125         }
126         return pupitriSpeciesContext;
127 
128     }
129 
130     public float getCarrouselSortedWeight() {
131         return carrouselSortedWeight == null ? 0f : carrouselSortedWeight.floatValue();
132     }
133 
134     public float getCarrouselUnsortedWeight() {
135         return carrouselUnsortedWeight == null ? 0f : carrouselUnsortedWeight.floatValue();
136     }
137 
138     public int getNbCarrousselImported() {
139         return nbCarrousselImported;
140     }
141 
142     public Set<String> getNotImportedSpeciesIds() {
143         return notImportedSpeciesIds;
144     }
145 
146     public List<PupitriSpeciesContext> getCatches() {
147         return catches;
148     }
149 }