View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.create;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.ArrayListMultimap;
26  import com.google.common.collect.Multimap;
27  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
28  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModelEntry;
29  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
30  import fr.ifremer.tutti.persistence.entities.referential.Species;
31  import fr.ifremer.tutti.type.WeightUnit;
32  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SelectedCategoryAble;
33  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.SpeciesOrBenthosBatchUISupport;
34  import fr.ifremer.tutti.ui.swing.content.operation.catches.species.split.SplitSpeciesBatchRowModel;
35  import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIModel;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  import java.util.List;
40  
41  import static org.nuiton.i18n.I18n.n;
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Model of {@link CreateSpeciesBatchUI}.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 0.3
49   */
50  public class CreateSpeciesBatchUIModel extends AbstractTuttiTableUIModel<CreateSpeciesBatchUIModel, SplitSpeciesBatchRowModel, CreateSpeciesBatchUIModel> implements SelectedCategoryAble {
51  
52      private final static Log log = LogFactory.getLog(CreateSpeciesBatchUIModel.class);
53  
54      private static final long serialVersionUID = 1L;
55  
56      public static final String PROPERTY_AVAILABLE_SPECIES = "availableSpecies";
57  
58      public static final String PROPERTY_SPECIES = "species";
59  
60      public static final String PROPERTY_SAMPLE_CATEGORY = "sampleCategory";
61  
62      public static final String PROPERTY_BATCH_SAMPLE_CATEGORY_WEIGHT = "batchSampleCategoryWeight";
63  
64      public static final String PROPERTY_LAST_SAMPLE_CATEGORY_USED = "lastSampleCategoryUsed";
65  
66      public static final String PROPERTY_BATCH_WEIGHT = "batchWeight";
67  
68      public static final String PROPERTY_BATCH_COUNT = "batchCount";
69  
70      public static final String PROPERTY_CATEGORIZATION_ENABLED = "categorizationEnabled";
71  
72      public static final String PROPERTY_CATEGORY = "category";
73  
74      public static final String PROPERTY_SELECTED_CATEGORY = "selectedCategory";
75  
76      public static final String PROPERTY_SAMPLE_WEIGHT = "sampleWeight";
77  
78      public static final String PROPERTY_SPECIES_PROTOCOL_FREQUENCY_MODE = "speciesProtocolFrequencyMode";
79  
80      /**
81       * Frequency mode according to the selected species and the protocol
82       */
83      public enum SpeciesProtocolFrequencyMode {
84          MEASURE(n("tutti.createSpeciesBatch.species.toMeasure")),
85          COUNT(n("tutti.createSpeciesBatch.species.toCount"));
86  
87          private String label;
88  
89          SpeciesProtocolFrequencyMode(String label) {
90              this.label = label;
91          }
92  
93          @Override
94          public String toString() {
95              return t(label);
96          }
97      }
98  
99      /**
100      * All available species (sets by protocol).
101      *
102      * @since 0.3
103      */
104     protected List<Species> availableSpecies;
105 
106     /**
107      * Species to select.
108      *
109      * @since 0.3
110      */
111     protected Species species;
112 
113     /**
114      * First sample category.
115      *
116      * @since 2.4
117      */
118     protected CaracteristicQualitativeValue sampleCategory;
119 
120     /**
121      * Last first category used.
122      *
123      * @since 4.2
124      */
125     protected CaracteristicQualitativeValue lastSampleCategoryUsed;
126 
127     /**
128      * Batch weight.
129      *
130      * @since 0.3
131      */
132     protected Float batchSampleCategoryWeight;
133 
134     /**
135      * Batch sample weight.
136      *
137      * @since 4.2
138      */
139     protected Float batchWeight;
140 
141     /**
142      * Batch count.
143      *
144      * @since 2.5
145      */
146     protected Integer batchCount;
147 
148     /**
149      * Sample categories.
150      *
151      * @since 2.5
152      */
153     protected List<SampleCategoryModelEntry> category;
154 
155     /**
156      * Selected Sample category.
157      *
158      * @since 2.5
159      */
160     protected SampleCategoryModelEntry selectedCategory;
161 
162     /**
163      * Sample weight of split batches.
164      *
165      * @since 2.5
166      */
167     protected Float sampleWeight;
168 
169     private final SpeciesOrBenthosBatchUISupport speciesOrBenthosBatchUISupport;
170     /**
171      * Model of sample categories.
172      *
173      * @since 2.5
174      */
175     protected SampleCategoryModel sampleCategoryModel;
176 
177     /**
178      * Already used species by sample category.
179      *
180      * @since 0.3
181      */
182     protected final Multimap<CaracteristicQualitativeValue, Species> speciesUsed = ArrayListMultimap.create();
183 
184     /**
185      * Frequency measurement mode according to the species
186      *
187      * @since 4.3
188      */
189     protected SpeciesProtocolFrequencyMode speciesProtocolFrequencyMode;
190 
191     public CreateSpeciesBatchUIModel(SpeciesOrBenthosBatchUISupport speciesOrBenthosBatchUISupport, SampleCategoryModel sampleCategoryModel) {
192         super(CreateSpeciesBatchUIModel.class, null, null);
193         this.speciesOrBenthosBatchUISupport = speciesOrBenthosBatchUISupport;
194         this.sampleCategoryModel = sampleCategoryModel;
195     }
196 
197     public SampleCategoryModel getSampleCategoryModel() {
198         return sampleCategoryModel;
199     }
200 
201     public List<Species> getAvailableSpecies() {
202         if (log.isDebugEnabled()) {
203             log.debug("getAvailableSpecies " + availableSpecies);
204         }
205         return availableSpecies;
206     }
207 
208     public void setAvailableSpecies(List<Species> availableSpecies) {
209 
210         if (log.isDebugEnabled()) {
211             log.debug("setAvailableSpecies " + availableSpecies);
212         }
213         Object oldValue = getAvailableSpecies();
214         this.availableSpecies = availableSpecies;
215         firePropertyChange(PROPERTY_AVAILABLE_SPECIES, oldValue, availableSpecies);
216     }
217 
218     public Species getSpecies() {
219         return species;
220     }
221 
222     public void setSpecies(Species species) {
223         Object oldSpecies = getSpecies();
224         Object oldCategorizationEnabled = isCategorizationEnabled();
225         this.species = species;
226         firePropertyChange(PROPERTY_SPECIES, oldSpecies, getSpecies());
227         firePropertyChange(PROPERTY_CATEGORIZATION_ENABLED, oldCategorizationEnabled, isCategorizationEnabled());
228     }
229 
230     public CaracteristicQualitativeValue getSampleCategory() {
231         return sampleCategory;
232     }
233 
234     public void setSampleCategory(CaracteristicQualitativeValue sampleCategory) {
235         Object oldValue = getSampleCategory();
236         this.sampleCategory = sampleCategory;
237         firePropertyChange(PROPERTY_SAMPLE_CATEGORY, oldValue, sampleCategory);
238     }
239 
240     public CaracteristicQualitativeValue getLastSampleCategoryUsed() {
241         return lastSampleCategoryUsed;
242     }
243 
244     public void setLastSampleCategoryUsed(CaracteristicQualitativeValue lastSampleCategoryUsed) {
245         Object oldValue = getLastSampleCategoryUsed();
246         this.lastSampleCategoryUsed = lastSampleCategoryUsed;
247         firePropertyChange(PROPERTY_LAST_SAMPLE_CATEGORY_USED, oldValue, lastSampleCategoryUsed);
248     }
249 
250     public Float getBatchWeight() {
251         return batchWeight;
252     }
253 
254     public void setBatchWeight(Float batchWeight) {
255         Object oldValue = getBatchWeight();
256         Object oldCategorizationEnabled = isCategorizationEnabled();
257         this.batchWeight = batchWeight;
258         firePropertyChange(PROPERTY_BATCH_WEIGHT, oldValue, batchWeight);
259         firePropertyChange(PROPERTY_CATEGORIZATION_ENABLED, oldCategorizationEnabled, isCategorizationEnabled());
260     }
261 
262     public Float getBatchSampleCategoryWeight() {
263         return batchSampleCategoryWeight;
264     }
265 
266     public void setBatchSampleCategoryWeight(Float batchSampleCategoryWeight) {
267         Object oldValue = getBatchSampleCategoryWeight();
268         this.batchSampleCategoryWeight = batchSampleCategoryWeight;
269         firePropertyChange(PROPERTY_BATCH_SAMPLE_CATEGORY_WEIGHT, oldValue, batchSampleCategoryWeight);
270     }
271 
272     public Integer getBatchCount() {
273         return batchCount;
274     }
275 
276     public void setBatchCount(Integer batchCount) {
277         Object oldValue = getBatchCount();
278         Object oldCategorizationEnabled = isCategorizationEnabled();
279         this.batchCount = batchCount;
280         firePropertyChange(PROPERTY_BATCH_COUNT, oldValue, batchCount);
281         firePropertyChange(PROPERTY_CATEGORIZATION_ENABLED, oldCategorizationEnabled, isCategorizationEnabled());
282     }
283 
284     public boolean isCategorizationEnabled() {
285         return getSpecies() != null && getBatchCount() == null && getBatchWeight() == null;
286     }
287 
288     public List<SampleCategoryModelEntry> getCategory() {
289         return category;
290     }
291 
292     public void setCategory(List<SampleCategoryModelEntry> category) {
293         Object oldValue = getCategory();
294         this.category = category;
295         firePropertyChange(PROPERTY_CATEGORY, oldValue, category);
296     }
297 
298     @Override
299     public SampleCategoryModelEntry getSelectedCategory() {
300         return selectedCategory;
301     }
302 
303     public void setSelectedCategory(SampleCategoryModelEntry selectedCategory) {
304         Object oldValue = getSelectedCategory();
305         this.selectedCategory = selectedCategory;
306         firePropertyChange(PROPERTY_SELECTED_CATEGORY, oldValue, selectedCategory);
307     }
308 
309     public Float getSampleWeight() {
310         return sampleWeight;
311     }
312 
313     public void setSampleWeight(Float sampleWeight) {
314         Object oldValue = getSampleWeight();
315         this.sampleWeight = sampleWeight;
316         firePropertyChange(PROPERTY_SAMPLE_WEIGHT, oldValue, sampleWeight);
317     }
318 
319     public Multimap<CaracteristicQualitativeValue, Species> getSpeciesUsed() {
320         return speciesUsed;
321     }
322 
323     public boolean isSpeciesAndCategoryAvailable() {
324         return species != null && sampleCategory != null &&
325                          isSpeciesAndCategoryAvailable(species, sampleCategory);
326     }
327 
328     public boolean isSpeciesAndCategoryAvailable(Species species,
329                                                  CaracteristicQualitativeValue sampleCategory) {
330         return !speciesUsed.containsEntry(sampleCategory, species);
331     }
332 
333     public boolean isCategoryIsFilled() {
334         boolean result = selectedCategory != null;
335         if (result) {
336             result = false;
337             for (SplitSpeciesBatchRowModel rowModel : getRows()) {
338                 if (rowModel.isValid()) {
339                     result = true;
340                     break;
341                 }
342             }
343         }
344         return result;
345     }
346 
347     public SpeciesProtocolFrequencyMode getSpeciesProtocolFrequencyMode() {
348         return speciesProtocolFrequencyMode;
349     }
350 
351     public void setSpeciesProtocolFrequencyMode(SpeciesProtocolFrequencyMode speciesProtocolFrequencyMode) {
352         Object oldValue = getSpeciesProtocolFrequencyMode();
353         this.speciesProtocolFrequencyMode = speciesProtocolFrequencyMode;
354         firePropertyChange(PROPERTY_SPECIES_PROTOCOL_FREQUENCY_MODE, oldValue, speciesProtocolFrequencyMode);
355     }
356 
357     @Override
358     protected CreateSpeciesBatchUIModel newEntity() {
359         return new CreateSpeciesBatchUIModel(speciesOrBenthosBatchUISupport, sampleCategoryModel);
360     }
361 
362     public SpeciesOrBenthosBatchUISupport getSpeciesOrBenthosBatchUISupport() {
363         return speciesOrBenthosBatchUISupport;
364     }
365 
366     public WeightUnit getWeightUnit() {
367         return speciesOrBenthosBatchUISupport.getWeightUnit();
368     }
369 }