View Javadoc
1   package fr.ifremer.tutti.service.cruise;
2   
3   /*
4    * #%L
5    * Tutti :: Service
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 com.google.common.base.MoreObjects;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
30  import fr.ifremer.tutti.service.sampling.CruiseSamplingCache;
31  import fr.ifremer.tutti.service.sampling.SamplingCodeCache;
32  import org.apache.commons.io.IOUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import java.util.Collection;
37  import java.util.Objects;
38  import java.util.Optional;
39  
40  /**
41   * Le cache de données pour une campagne. Ce cache contient tous les caches plus spécifiques
42   * (codes de prélèvement, algorithme de prélèvement de pièces calcifiées,...).
43   *
44   * Created on 16/04/16.
45   *
46   * @author Tony Chemit - chemit@codelutin.com
47   * @since 4.5
48   */
49  public class CruiseCache implements CruiseCacheAble {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(CruiseCache.class);
53      /**
54       * L'identifiant de la campagne associée à ce cache.
55       */
56      private final int cruiseId;
57      /**
58       * L'identificant du protocol associé à ce cache.
59       */
60      private final String protocolId;
61      /**
62       * Le cache optioanel lié à l'algorithme de prélèvement des pièces calcifiées.
63       */
64      private final CruiseSamplingCache cruiseSamplingCache;
65      /**
66       * Le cache des codes de prélèvement disponibles.
67       */
68      private final SamplingCodeCache samplingCodeCache;
69  
70      public CruiseCache(Integer cruiseId, String protocolId, CruiseSamplingCache cruiseSamplingCache, SamplingCodeCache samplingCodeCache) {
71          Objects.requireNonNull(cruiseId);
72          Objects.requireNonNull(samplingCodeCache);
73          this.cruiseId = cruiseId;
74          this.protocolId = protocolId;
75          this.cruiseSamplingCache = cruiseSamplingCache;
76          this.samplingCodeCache = samplingCodeCache;
77      }
78  
79      public boolean isCacheUpToDate(Integer cruiseId, String protocolId) {
80          return Objects.equals(this.cruiseId, cruiseId) && Objects.equals(this.protocolId, protocolId);
81      }
82  
83      public boolean useSamplingCache() {
84          return cruiseSamplingCache != null;
85      }
86  
87      public Integer getCruiseId() {
88          return cruiseId;
89      }
90  
91      public String getProtocolId() {
92          return protocolId;
93      }
94  
95      public Optional<CruiseSamplingCache> getSamplingCruiseCache() {
96          return Optional.ofNullable(cruiseSamplingCache);
97      }
98  
99      public SamplingCodeCache getSamplingCodeCache() {
100         return samplingCodeCache;
101     }
102 
103     @Override
104     public void addIndividualObservations(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
105 
106         if (useSamplingCache()) {
107             cruiseSamplingCache.addIndividualObservations(fishingOperation, individualObservations);
108         }
109 
110         samplingCodeCache.addIndividualObservations(fishingOperation, individualObservations);
111 
112     }
113 
114     @Override
115     public void removeIndividualObservations(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
116 
117         if (useSamplingCache()) {
118             cruiseSamplingCache.removeIndividualObservations(fishingOperation, individualObservations);
119         }
120 
121         samplingCodeCache.removeIndividualObservations(fishingOperation, individualObservations);
122 
123     }
124 
125     @Override
126     public void addFishingOperation(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
127 
128         if (useSamplingCache()) {
129             cruiseSamplingCache.addFishingOperation(fishingOperation, individualObservations);
130         }
131 
132         samplingCodeCache.addFishingOperation(fishingOperation, individualObservations);
133 
134     }
135 
136     @Override
137     public void removeFishingOperation(FishingOperation fishingOperation, Collection<IndividualObservationBatch> individualObservations) {
138 
139         if (useSamplingCache()) {
140             cruiseSamplingCache.removeFishingOperation(fishingOperation, individualObservations);
141         }
142 
143         samplingCodeCache.removeFishingOperation(fishingOperation, individualObservations);
144 
145     }
146 
147     @Override
148     public void close() {
149 
150         if (log.isInfoEnabled()) {
151             log.info("Closing cruise cache: " + this);
152         }
153         if (useSamplingCache()) {
154             IOUtils.closeQuietly(cruiseSamplingCache);
155         }
156         IOUtils.closeQuietly(samplingCodeCache);
157     }
158 
159     @Override
160     public String toString() {
161         MoreObjects.ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
162         if (cruiseSamplingCache != null) {
163             toStringHelper.add("cruiseSamplingCache", cruiseSamplingCache);
164         }
165         return toStringHelper.add("samplingCodeCache", samplingCodeCache).toString();
166     }
167 }