View Javadoc
1   package fr.ifremer.tutti.service.genericformat.exportactions;
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.adagio.core.dao.referential.ObjectTypeCode;
28  import fr.ifremer.tutti.persistence.entities.data.Attachment;
29  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
30  import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
31  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
32  import fr.ifremer.tutti.service.PersistenceService;
33  import fr.ifremer.tutti.service.genericformat.GenericFormatExportContext;
34  import fr.ifremer.tutti.service.genericformat.GenericFormatExportOperationContext;
35  import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
36  import fr.ifremer.tutti.service.genericformat.csv.CatchRow;
37  import fr.ifremer.tutti.service.genericformat.producer.CsvProducerForAttachment;
38  import fr.ifremer.tutti.service.genericformat.producer.CsvProducerForCatch;
39  import fr.ifremer.tutti.service.genericformat.producer.CsvProducerForSpecies;
40  import fr.ifremer.tutti.util.Numbers;
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  /**
48   * Created on 3/28/15.
49   *
50   * @author Tony Chemit - chemit@codelutin.com
51   * @since 3.14.3
52   */
53  public class CreateCatchBatchRowsAction extends ExportFishingOperationActionSupport {
54  
55      /** Logger. */
56      private static final Log log = LogFactory.getLog(CreateCatchBatchRowsAction.class);
57  
58      private final PersistenceService persistenceService;
59  
60      public CreateCatchBatchRowsAction(PersistenceService persistenceService) {
61          this.persistenceService = persistenceService;
62      }
63  
64      @Override
65      public boolean doExecute(GenericFormatExportContext exportContext, GenericFormatExportOperationContext operationContext) {
66          return (exportContext.isExportSpecies() || exportContext.isExportBenthos()) && operationContext.isWithCatchBatch();
67      }
68  
69      @Override
70      public void execute(GenericFormatExportContext exportContext, GenericFormatExportOperationContext operationContext) {
71  
72          boolean exportSpecies = exportContext.isExportSpecies();
73          boolean exportBenthos = exportContext.isExportBenthos();
74          boolean exportAll = exportSpecies && exportBenthos;
75  
76          if (exportSpecies) {
77              prepareSpeciesBatches(exportContext, operationContext);
78          }
79  
80          if (exportBenthos) {
81              prepareBenthosBatches(exportContext, operationContext);
82          }
83  
84          CatchBatch catchBatch = operationContext.getCatchBatch();
85  
86          Float catchRaisingfactor = computeCatchRaisingfactor(catchBatch);
87  
88          Float speciesCatchRaisingFactor = null;
89          if (exportSpecies) {
90              speciesCatchRaisingFactor = computeSpeciesRaisingFactor(catchBatch, catchRaisingfactor);
91          }
92  
93          Float benthosCatchRaisingFactor = null;
94          if (exportBenthos) {
95              benthosCatchRaisingFactor = computeBenthosRaisingFactor(catchBatch, catchRaisingfactor);
96          }
97  
98          CsvProducerForCatch producerForCatch = exportContext.getProducerForCatch();
99  
100         if (exportAll) {
101 
102             if (log.isInfoEnabled()) {
103                 log.info(String.format("Operation %s [catchRF: %s / speciesRF: %s / benthosRF: %s ]", operationContext.getOperation().getId(), catchRaisingfactor, speciesCatchRaisingFactor, benthosCatchRaisingFactor));
104             }
105 
106             List<CatchRow> catchRows = producerForCatch.getDataToExport(operationContext, speciesCatchRaisingFactor, benthosCatchRaisingFactor);
107             operationContext.setCatchRows(catchRows);
108 
109         } else if (exportSpecies) {
110 
111             if (log.isInfoEnabled()) {
112                 log.info(String.format("Operation %s [catchRF: %s / speciesRF: %s ]", operationContext.getOperation().getId(), catchRaisingfactor, speciesCatchRaisingFactor));
113             }
114 
115             List<CatchRow> catchRows = producerForCatch.getSpeciesOnlyDataToExport(operationContext, speciesCatchRaisingFactor);
116             operationContext.setCatchRows(catchRows);
117 
118         } else if (exportBenthos) {
119 
120             if (log.isInfoEnabled()) {
121                 log.info(String.format("Operation %s [catchRF: %s / benthosRF: %s ]", operationContext.getOperation().getId(), catchRaisingfactor, benthosCatchRaisingFactor));
122             }
123 
124             List<CatchRow> catchRows = producerForCatch.getBenthosOnlyDataToExport(operationContext, benthosCatchRaisingFactor);
125             operationContext.setCatchRows(catchRows);
126 
127         }
128 
129     }
130 
131     private void prepareSpeciesBatches(GenericFormatExportContext exportContext, GenericFormatExportOperationContext operationContext) {
132 
133         BatchContainer<SpeciesBatch> rootSpeciesBatch = operationContext.getRootSpeciesBatch();
134 
135         // Warning! This will also add the survey code to batch species
136         CsvProducerForSpecies producerForSpecies = exportContext.getProducerForSpecies();
137         producerForSpecies.prepareSpeciesBatchRows(rootSpeciesBatch);
138 
139         if (exportContext.isExportAttachments()) {
140 
141             CsvProducerForAttachment producerForAttachment = exportContext.getProducerForAttachment();
142 
143             List<AttachmentRow> attachmentRows = new ArrayList<>();
144 
145             for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) {
146                 List<Attachment> attachments = persistenceService.getAllAttachments(ObjectTypeCode.BATCH, speciesBatch.getIdAsInt());
147                 producerForAttachment.addAttachments(attachments, attachmentRows);
148             }
149 
150             operationContext.addAttachmentRows(attachmentRows);
151 
152         }
153 
154     }
155 
156     private void prepareBenthosBatches(GenericFormatExportContext exportContext, GenericFormatExportOperationContext operationContext) {
157 
158         BatchContainer<SpeciesBatch> rootSpeciesBatch = operationContext.getRootBenthosBatch();
159 
160         // Warning! This will also add the survey code to batch species
161         CsvProducerForSpecies producerForSpecies = exportContext.getProducerForSpecies();
162         producerForSpecies.prepareBenthosBatchRows(rootSpeciesBatch);
163 
164         CsvProducerForAttachment producerForAttachment = exportContext.getProducerForAttachment();
165 
166         if (exportContext.isExportAttachments()) {
167 
168             List<AttachmentRow> attachmentRows = new ArrayList<>();
169 
170             for (SpeciesBatch benthosBatch : rootSpeciesBatch.getChildren()) {
171                 List<Attachment> attachments = persistenceService.getAllAttachments(ObjectTypeCode.BATCH, benthosBatch.getIdAsInt());
172                 producerForAttachment.addAttachments(attachments, attachmentRows);
173             }
174 
175             operationContext.addAttachmentRows(attachmentRows);
176 
177         }
178 
179 
180     }
181 
182     private Float computeSpeciesRaisingFactor(CatchBatch catchBatch, Float catchRaisingFactor) {
183 
184         Float totalSortedSpeciesWeight = Numbers.getValueOrComputedValue(
185                 catchBatch.getSpeciesTotalSortedWeight(),
186                 catchBatch.getSpeciesTotalSortedComputedWeight());
187 
188         Float totalSampleSortedSpeciesWeight = catchBatch.getSpeciesTotalSampleSortedComputedWeight();
189 
190         Float speciesCatchRaisingFactor;
191         if (totalSampleSortedSpeciesWeight == null || totalSortedSpeciesWeight == null) {
192             speciesCatchRaisingFactor = 1f;
193         } else {
194             if (totalSampleSortedSpeciesWeight == 0) {
195                 speciesCatchRaisingFactor = 0f;
196             } else {
197                 speciesCatchRaisingFactor = (totalSortedSpeciesWeight / totalSampleSortedSpeciesWeight) * catchRaisingFactor;
198             }
199         }
200         return speciesCatchRaisingFactor;
201 
202     }
203 
204     private Float computeBenthosRaisingFactor(CatchBatch catchBatch, Float catchRaisingFactor) {
205 
206         Float totalSampleSortedBenthosWeight = catchBatch.getBenthosTotalSampleSortedComputedWeight();
207 
208         Float totalSortedBenthosWeight = Numbers.getValueOrComputedValue(
209                 catchBatch.getBenthosTotalSortedWeight(),
210                 catchBatch.getBenthosTotalSortedComputedWeight());
211 
212         Float benthosCatchRaisingFactor;
213 
214         if (totalSampleSortedBenthosWeight == null || totalSortedBenthosWeight == null) {
215             benthosCatchRaisingFactor = 1f;
216         } else {
217             if (totalSampleSortedBenthosWeight == 0) {
218                 benthosCatchRaisingFactor = 0f;
219             } else {
220                 benthosCatchRaisingFactor = (totalSortedBenthosWeight / totalSampleSortedBenthosWeight) * catchRaisingFactor;
221             }
222         }
223 
224         return benthosCatchRaisingFactor;
225 
226     }
227 
228     private Float computeCatchRaisingfactor(CatchBatch catchBatch) {
229 
230         Float totalSortedWeight = catchBatch.getCatchTotalSortedComputedWeight();
231         Float totalSortedSortedWeight = catchBatch.getCatchTotalSortedSortedComputedWeight();
232 
233         // tchemit 2015-04-28 see http://forge.codelutin.com/issues/7021
234 
235         return totalSortedWeight == null || totalSortedSortedWeight == null ? 1 : totalSortedWeight / totalSortedSortedWeight;
236 
237     }
238 
239 }