View Javadoc
1   package fr.ifremer.tutti.service.genericformat;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 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.entities.data.AccidentalBatch;
28  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
29  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
30  import fr.ifremer.tutti.persistence.entities.data.Cruise;
31  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
32  import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
33  import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch;
34  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
35  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
36  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
37  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
38  import fr.ifremer.tutti.service.PersistenceService;
39  import fr.ifremer.tutti.service.catches.WeightComputingService;
40  import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
41  import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
42  import fr.ifremer.tutti.service.genericformat.csv.CatchRow;
43  import fr.ifremer.tutti.service.genericformat.csv.IndividualObservationRow;
44  import fr.ifremer.tutti.service.genericformat.csv.MarineLitterRow;
45  import fr.ifremer.tutti.service.genericformat.csv.OperationRow;
46  import fr.ifremer.tutti.service.genericformat.csv.ParameterRow;
47  import org.apache.commons.collections4.CollectionUtils;
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  import java.util.ArrayList;
52  import java.util.HashSet;
53  import java.util.List;
54  
55  /**
56   * Created on 2/5/15.
57   *
58   * @author Tony Chemit - chemit@codelutin.com
59   * @since 3.13
60   */
61  public class GenericFormatExportOperationContext {
62  
63      /** Logger. */
64      private static final Log log = LogFactory.getLog(GenericFormatExportOperationContext.class);
65  
66      private final Cruise cruise;
67  
68      private final FishingOperation operation;
69  
70      private final CatchBatch catchBatch;
71  
72      private BatchContainer<MarineLitterBatch> rootMarineLitterBatch;
73  
74      private BatchContainer<SpeciesBatch> rootSpeciesBatch;
75  
76      private BatchContainer<SpeciesBatch> rootBenthosBatch;
77  
78      private List<IndividualObservationBatch> individualObservations;
79  
80      private List<AccidentalBatch> accidentalBatches;
81  
82      private boolean withCatchBatch;
83  
84      private final PersistenceService persistenceService;
85  
86      protected final Caracteristic weightMeasuredCaracteristic;
87  
88      protected final Caracteristic pmfmIdCaracteristic;
89  
90      protected final Caracteristic deadOrAliveCaracteristic;
91  
92      protected final Caracteristic genderCaracteristic;
93      private final Caracteristic copyIndividualObservationModeCaracteristic;
94      private final Caracteristic sampleCodeCaracteristic;
95  
96      private final SampleCategoryModel sampleCategoryModel;
97  
98      private final String operationLabel;
99  
100     private OperationRow operationRow;
101 
102     private List<ParameterRow> parameterRows;
103 
104     private List<MarineLitterRow> marineLitterRows;
105 
106     private List<IndividualObservationRow> individualObservationRows;
107 
108     private List<AccidentalCatchRow> accidentalCatchRows;
109 
110     private List<CatchRow> catchRows;
111 
112     private final List<AttachmentRow> attachmentRows;
113 
114     public GenericFormatExportOperationContext(Cruise cruise,
115                                                FishingOperation operation,
116                                                String operationLabel,
117                                                PersistenceService persistenceService,
118                                                WeightComputingService weightComputingService,
119                                                SampleCategoryModel sampleCategoryModel,
120                                                Caracteristic weightMeasuredCaracteristic,
121                                                Caracteristic pmfmIdCaracteristic,
122                                                Caracteristic deadOrAliveCaracteristic,
123                                                Caracteristic genderCaracteristic,
124                                                Caracteristic copyIndividualObservationModeCaracteristic,
125                                                Caracteristic sampleCodeCaracteristic) {
126         this.cruise = cruise;
127         this.operation = operation;
128         this.operationLabel = operationLabel;
129         this.sampleCategoryModel = sampleCategoryModel;
130         this.persistenceService = persistenceService;
131         this.weightMeasuredCaracteristic = weightMeasuredCaracteristic;
132         this.pmfmIdCaracteristic = pmfmIdCaracteristic;
133         this.deadOrAliveCaracteristic = deadOrAliveCaracteristic;
134         this.genderCaracteristic = genderCaracteristic;
135         this.copyIndividualObservationModeCaracteristic = copyIndividualObservationModeCaracteristic;
136         this.sampleCodeCaracteristic = sampleCodeCaracteristic;
137         this.attachmentRows = new ArrayList<>();
138 
139         Integer operationId = operation.getIdAsInt();
140 
141         accidentalBatches = persistenceService.getAllAccidentalBatch(operationId);
142 
143         withCatchBatch = persistenceService.isFishingOperationWithCatchBatch(operationId);
144 
145         if (withCatchBatch) {
146 
147 
148             // load batches
149 
150             catchBatch = persistenceService.getCatchBatchFromFishingOperation(operationId);
151 
152             boolean withError = false;
153 
154             try {
155                 rootSpeciesBatch = weightComputingService.getComputedSpeciesBatches(operationId);
156             } catch (Exception e) {
157                 if (log.isWarnEnabled()) {
158                     log.warn("Could not getComputedSpeciesBatches", e);
159                 }
160                 withError = true;
161                 rootSpeciesBatch = persistenceService.getRootSpeciesBatch(operationId, false);
162             }
163 
164             try {
165                 rootBenthosBatch = weightComputingService.getComputedBenthosBatches(operationId);
166             } catch (Exception e) {
167                 if (log.isWarnEnabled()) {
168                     log.warn("Could not getComputedBenthosBatches", e);
169                 }
170                 withError = true;
171                 rootBenthosBatch = persistenceService.getRootBenthosBatch(operationId, false);
172             }
173 
174             try {
175                 rootMarineLitterBatch = weightComputingService.getComputedMarineLitterBatches(operationId, catchBatch.getMarineLitterTotalWeight());
176             } catch (Exception e) {
177                 if (log.isWarnEnabled()) {
178                     log.warn("Could not getComputedMarineLitterBatches", e);
179                 }
180                 withError = true;
181                 rootMarineLitterBatch = persistenceService.getRootMarineLitterBatch(operationId);
182             }
183 
184             // apply compute weights
185 
186             if (!withError) {
187                 try {
188                     weightComputingService.computeCatchBatchWeights(catchBatch, rootSpeciesBatch, rootBenthosBatch, rootMarineLitterBatch);
189                 } catch (Exception e) {
190                     if (log.isWarnEnabled()) {
191                         log.warn("Could not computeCatchBatchWeights", e);
192                     }
193                 }
194             }
195 
196             individualObservations = persistenceService.getAllIndividualObservationBatchsForFishingOperation(operation.getIdAsInt());
197 
198         } else {
199             if (log.isWarnEnabled()) {
200                 log.warn("Skip fishing operation " + operationId + " since no catchBatch associated.");
201             }
202             catchBatch = null;
203             rootSpeciesBatch = null;
204             rootBenthosBatch = null;
205             rootMarineLitterBatch = null;
206         }
207 
208         // poussin 20170102 fixes #8611: rewrite rankOrder to ensure unicity
209         if (rootSpeciesBatch != null) {
210             reOrder(rootSpeciesBatch.getChildren());
211         }
212         if (rootBenthosBatch != null) {
213             reOrder(rootBenthosBatch.getChildren());
214         }
215     }
216 
217     private void reOrder(List<SpeciesBatch> l) {
218         if (l != null) {
219             HashSet<Integer> check = new HashSet<Integer>();
220             int rankOrder = 1;
221             for (SpeciesBatch b : l) {
222                 if (log.isDebugEnabled()) {
223                     Integer oldRankOrder = b.getRankOrder();
224                     boolean alreadyUse = !check.add(oldRankOrder);
225                     if(alreadyUse || oldRankOrder != rankOrder) {
226                         log.debug("Bad rank order (" + oldRankOrder + ") already use: " + alreadyUse + " good current: " + rankOrder);
227                     }
228                 }
229                 b.setRankOrder(rankOrder++);
230                 reOrder(b.getChildBatchs());
231             }
232         }
233     }
234 
235     public Cruise getCruise() {
236         return cruise;
237     }
238 
239     public FishingOperation getOperation() {
240         return operation;
241     }
242 
243     public CatchBatch getCatchBatch() {
244         return catchBatch;
245     }
246 
247     public BatchContainer<MarineLitterBatch> getRootMarineLitterBatch() {
248         return rootMarineLitterBatch;
249     }
250 
251     public BatchContainer<SpeciesBatch> getRootSpeciesBatch() {
252         return rootSpeciesBatch;
253     }
254 
255     public BatchContainer<SpeciesBatch> getRootBenthosBatch() {
256         return rootBenthosBatch;
257     }
258 
259     public boolean isWithCatchBatch() {
260         return withCatchBatch;
261     }
262 
263     public List<IndividualObservationBatch> getIndividualObservations() {
264         return individualObservations;
265     }
266 
267     public Caracteristic getWeightMeasuredCaracteristic() {
268         return weightMeasuredCaracteristic;
269     }
270 
271     public Caracteristic getPmfmIdCaracteristic() {
272         return pmfmIdCaracteristic;
273     }
274 
275     public Caracteristic getDeadOrAliveCaracteristic() {
276         return deadOrAliveCaracteristic;
277     }
278 
279     public Caracteristic getGenderCaracteristic() {
280         return genderCaracteristic;
281     }
282 
283     public Caracteristic getCopyIndividualObservationModeCaracteristic() {
284         return copyIndividualObservationModeCaracteristic;
285     }
286 
287     public Caracteristic getSampleCodeCaracteristic() {
288         return sampleCodeCaracteristic;
289     }
290 
291     public List<AccidentalBatch> getAccidentalBatches() {
292         return accidentalBatches;
293     }
294 
295     public boolean isVracBatch(SpeciesBatch batch) {
296         return persistenceService.isVracBatch(batch);
297     }
298 
299     public List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer id) {
300         return persistenceService.getAllBenthosBatchFrequency(id);
301     }
302 
303     public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency(Integer id) {
304         return persistenceService.getAllSpeciesBatchFrequency(id);
305     }
306 
307     public SampleCategoryModel getSampleCategoryModel() {
308         return sampleCategoryModel;
309     }
310 
311     public String getOperationLabel() {
312         return operationLabel;
313     }
314 
315     public void setOperationRow(OperationRow operationRow) {
316         this.operationRow = operationRow;
317     }
318 
319     public OperationRow getOperationRow() {
320         return operationRow;
321     }
322 
323     public void setParameterRows(List<ParameterRow> parameterRows) {
324         this.parameterRows = parameterRows;
325     }
326 
327     public List<ParameterRow> getParameterRows() {
328         return parameterRows;
329     }
330 
331     public void setMarineLitterRows(List<MarineLitterRow> marineLitterRows) {
332         this.marineLitterRows = marineLitterRows;
333     }
334 
335     public List<MarineLitterRow> getMarineLitterRows() {
336         return marineLitterRows;
337     }
338 
339     public void setIndividualObservationRows(List<IndividualObservationRow> individualObservationRows) {
340         this.individualObservationRows = individualObservationRows;
341     }
342 
343     public List<IndividualObservationRow> getIndividualObservationRows() {
344         return individualObservationRows;
345     }
346 
347     public void setAccidentalCatchRows(List<AccidentalCatchRow> accidentalCatchRows) {
348         this.accidentalCatchRows = accidentalCatchRows;
349     }
350 
351     public List<AccidentalCatchRow> getAccidentalCatchRows() {
352         return accidentalCatchRows;
353     }
354 
355     public void setCatchRows(List<CatchRow> catchRows) {
356         this.catchRows = catchRows;
357     }
358 
359     public List<CatchRow> getCatchRows() {
360         return catchRows;
361     }
362 
363     public List<AttachmentRow> getAttachmentRows() {
364         return attachmentRows;
365     }
366 
367     public void addAttachmentRows(List<AttachmentRow> attachmentRows) {
368 
369         if (CollectionUtils.isNotEmpty(attachmentRows)) {
370             this.attachmentRows.addAll(attachmentRows);
371         }
372 
373     }
374 }