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 fr.ifremer.tutti.persistence.ProgressionModel;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
30  import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
31  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
32  import fr.ifremer.tutti.service.DecoratorService;
33  import fr.ifremer.tutti.service.PersistenceService;
34  import fr.ifremer.tutti.service.sampling.CruiseSamplingCache;
35  import fr.ifremer.tutti.service.sampling.SamplingCodeCache;
36  import java.util.Collection;
37  import java.util.List;
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.nuiton.decorator.Decorator;
41  
42  import static org.nuiton.i18n.I18n.t;
43  
44  /**
45   * Pour charger le cache de données de campagnes.
46   *
47   * Created on 20/03/16.
48   *
49   * @author Tony Chemit - chemit@codelutin.com
50   * @since 4.5
51   */
52  public class CruiseCacheLoader {
53  
54      /** Logger. */
55      private static final Log log = LogFactory.getLog(CruiseCacheLoader.class);
56  
57      private final PersistenceService persistenceService;
58      private final DecoratorService decoratorService;
59      private final ProgressionModel progressionModel;
60      private final CruiseCache cruiseCache;
61  
62      public static CruiseCacheLoader newCacheLoader(PersistenceService persistenceService,
63                                                     DecoratorService decoratorService,
64                                                     ProgressionModel progressionModel,
65                                                     TuttiProtocol protocol,
66                                                     Integer cruiseId) {
67  
68          boolean loadSamplingCache = protocol != null && protocol.isUseCalcifiedPieceSampling();
69  
70          CruiseSamplingCache cruiseSamplingCache;
71  
72          if (loadSamplingCache) {
73  
74              Caracteristic sexCaracteristic = persistenceService.getSexCaracteristic();
75  
76              Collection<Caracteristic> maturityCaracteristics = persistenceService.getMaturityCaracteristics(persistenceService.getAllCaracteristic());
77              cruiseSamplingCache = new CruiseSamplingCache(protocol, sexCaracteristic, maturityCaracteristics);
78  
79          } else {
80  
81              cruiseSamplingCache = null;
82  
83          }
84  
85          CruiseCache cruiseCache = new CruiseCache(cruiseId, protocol == null ? null : protocol.getId(), cruiseSamplingCache, new SamplingCodeCache());
86          return new CruiseCacheLoader(persistenceService, decoratorService, progressionModel, cruiseCache);
87      }
88  
89      public static CruiseCacheLoader newCacheLoader(PersistenceService persistenceService,
90                                                     DecoratorService decoratorService,
91                                                     ProgressionModel progressionModel,
92                                                     CruiseCache cruiseCache) {
93          return new CruiseCacheLoader(persistenceService, decoratorService, progressionModel, cruiseCache);
94      }
95  
96      public CruiseCache loadCruiseCache() {
97  
98          if (log.isInfoEnabled()) {
99              log.info("Loading cruise cache: " + cruiseCache);
100         }
101 
102         // FIXME poussin 20160608 a reecrire pour de meilleur performance, il faut remonter
103         // tous les fishingOperation en une fois et non pas faire N requete
104         Decorator<FishingOperation> fishingOperationDecorator = decoratorService.getDecoratorByType(FishingOperation.class);
105 
106         persistenceService.getAllFishingOperationIds(cruiseCache.getCruiseId()).forEach(fishingOperationId -> {
107 
108             FishingOperation fishingOperation = persistenceService.getFishingOperation(fishingOperationId);
109 
110             progressionModel.increments(t("tutti.cruise.cacheLoader.loading.fishingOperation", fishingOperationDecorator.toString(fishingOperation)));
111 
112             loadCruiseCacheForFishingOperation(fishingOperation);
113 
114         });
115 
116         if (log.isInfoEnabled()) {
117             log.info("Cruise cache loaded: " + cruiseCache);
118         }
119 
120         return cruiseCache;
121     }
122 
123     public CruiseCache loadCruiseCacheForFishingOperation(FishingOperation fishingOperation) {
124 
125         if (log.isInfoEnabled()) {
126             log.info("Loading cruise cache for fishing operation: " + fishingOperation + " → " + cruiseCache);
127         }
128 
129         List<IndividualObservationBatch> individualObservations =
130                 persistenceService.getAllIndividualObservationBatchsForFishingOperation(fishingOperation.getIdAsInt());
131 
132         cruiseCache.addFishingOperation(fishingOperation, individualObservations);
133 
134         if (log.isInfoEnabled()) {
135             log.info("Cruise cache loaded for fishing operation: " + fishingOperation + " → " + cruiseCache);
136         }
137 
138         return cruiseCache;
139 
140     }
141 
142     private CruiseCacheLoader(PersistenceService persistenceService,
143                               DecoratorService decoratorService,
144                               ProgressionModel progressionModel,
145                               CruiseCache cruiseCache) {
146         this.persistenceService = persistenceService;
147         this.decoratorService = decoratorService;
148         this.progressionModel = progressionModel;
149         this.cruiseCache = cruiseCache;
150     }
151 
152 }