View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 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.service.sampling.IndividualObservationSamplingCacheRequest;
28  
29  import java.util.Objects;
30  
31  /**
32   * Pour effectuer depuis une modification ou sélection d'une observation individuelle, les actions sur les caches.
33   *
34   * Created on 20/04/16.
35   *
36   * @author Tony Chemit - chemit@codelutin.com
37   */
38  public class IndividualObservationToSamplingCacheEngine {
39  
40      private final SpeciesFrequencyUIModel speciesFrequencyUIModel;
41      private final IndividualObservationUICache individualObservationUICache;
42      private final SamplingCodeUICache samplingCodeUICache;
43  
44      public IndividualObservationToSamplingCacheEngine(SpeciesFrequencyUIModel speciesFrequencyUIModel) {
45  
46          this.speciesFrequencyUIModel = speciesFrequencyUIModel;
47          this.individualObservationUICache = speciesFrequencyUIModel.getIndividualObservationUICache();
48          this.samplingCodeUICache = speciesFrequencyUIModel.getSamplingCodeUICache();
49  
50      }
51  
52      public boolean computeSamplingCacheUpdate(IndividualObservationBatchRowState oldState, IndividualObservationBatchRowState newState) {
53  
54          IndividualObservationSamplingCacheRequest addIndividualObservation = null;
55          IndividualObservationSamplingCacheRequest removeIndividualObservation = null;
56          IndividualObservationSamplingCacheRequest addSamplingAction = null;
57          IndividualObservationSamplingCacheRequest removeSamplingAction = null;
58          IndividualObservationSamplingCacheRequest addSamplingCodeAction = null;
59          IndividualObservationSamplingCacheRequest removeSamplingCodeAction = null;
60  
61          boolean sizeChanged = !Objects.equals(oldState.getSize(), newState.getSize());
62          boolean maturityChanged = !Objects.equals(oldState.getMaturity(), newState.getMaturity());
63          boolean genderChanged = !Objects.equals(oldState.getGender(), newState.getGender());
64          boolean samplingCodeChanged = !Objects.equals(oldState.getSamplingCode(), newState.getSamplingCode());
65          boolean withOldSamplingCode = oldState.withSamplingCode();
66          boolean withNewSamplingCode = newState.withSamplingCode();
67  
68          if (sizeChanged || maturityChanged || genderChanged) {
69  
70              removeIndividualObservation = toRequest(oldState);
71              addIndividualObservation = toRequest(newState);
72  
73          } else if (samplingCodeChanged) {
74  
75              boolean removeSampling = withOldSamplingCode && !withNewSamplingCode;
76              boolean addSampling = withNewSamplingCode && !withOldSamplingCode;
77  
78              if (removeSampling) {
79  
80                  removeSamplingAction = toRequest(oldState);
81  
82              } else if (addSampling) {
83  
84                  addSamplingAction = toRequest(newState);
85  
86              } else {
87  
88                  removeSamplingCodeAction = toRequest(oldState);
89                  addSamplingCodeAction = toRequest(newState);
90  
91              }
92  
93          }
94  
95          boolean actionDone = false;
96          if (removeIndividualObservation != null) {
97              individualObservationUICache.removeIndividualObservation(removeIndividualObservation);
98              actionDone=true;
99          }
100         if (addIndividualObservation != null) {
101             individualObservationUICache.addIndividualObservation(addIndividualObservation);
102             actionDone=true;
103         }
104         if (removeSamplingAction != null) {
105             individualObservationUICache.removeSampling(removeSamplingAction);
106             samplingCodeUICache.removeSampling(removeSamplingAction);
107             actionDone=true;
108         }
109         if (addSamplingAction != null) {
110             individualObservationUICache.addSampling(addSamplingAction);
111             samplingCodeUICache.addSampling(addSamplingAction);
112             actionDone=true;
113         }
114         if (removeSamplingCodeAction != null) {
115             samplingCodeUICache.removeSampling(removeSamplingCodeAction);
116             actionDone=true;
117         }
118         if (addSamplingCodeAction != null) {
119             samplingCodeUICache.addSampling(addSamplingCodeAction);
120             actionDone=true;
121         }
122 
123         return actionDone;
124 
125     }
126 
127     private IndividualObservationSamplingCacheRequest toRequest(IndividualObservationBatchRowState state) {
128 
129         return new IndividualObservationSamplingCacheRequest(speciesFrequencyUIModel.getFishingOperation(),
130                                                              speciesFrequencyUIModel.getBatch().getSpecies(),
131                                                              speciesFrequencyUIModel.getLengthStepInMm(state.getSize()),
132                                                              state.getMaturity(),
133                                                              state.getGender(),
134                                                              state.getSamplingCode());
135     }
136 
137 }