View Javadoc
1   package fr.ifremer.tutti.service.genericformat.consumer;
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 com.google.common.base.Predicate;
28  import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId;
29  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
30  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
31  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
32  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys;
33  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs;
34  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
35  import fr.ifremer.tutti.persistence.entities.referential.Species;
36  import fr.ifremer.tutti.service.csv.CsvComsumer;
37  import fr.ifremer.tutti.service.genericformat.GenericFormatContextSupport;
38  import fr.ifremer.tutti.service.genericformat.GenericFormatImportEntityParserFactory;
39  import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
40  import fr.ifremer.tutti.service.genericformat.csv.CatchModel;
41  import fr.ifremer.tutti.service.genericformat.csv.CatchRow;
42  import fr.ifremer.tutti.service.genericformat.csv.ExportSampleCategory;
43  import fr.ifremer.tutti.type.WeightUnit;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.nuiton.csv.ImportRow;
47  
48  import java.nio.file.Path;
49  
50  /**
51   * Created on 2/11/15.
52   *
53   * @author Tony Chemit - chemit@codelutin.com
54   * @since 3.14
55   */
56  public class CsvConsumerForCatch extends CsvComsumer<CatchRow, CatchModel> {
57  
58      /** Logger. */
59      private static final Log log = LogFactory.getLog(CsvConsumerForCatch.class);
60  
61      private final Predicate<CatchRow> catchRowVracPredicate;
62  
63      public CsvConsumerForCatch(Path file, char separator, SampleCategoryModel sampleCategoryModel, GenericFormatImportEntityParserFactory parserFactory, boolean reportError) {
64  
65          super(file, CatchModel.forImport(separator, sampleCategoryModel, parserFactory), reportError);
66  
67          this.catchRowVracPredicate = input -> {
68              ExportSampleCategory exportSampleCategory = input.getSampleCategory().get(0);
69              return QualitativeValueId.SORTED_VRAC.getValue().equals(((CaracteristicQualitativeValue) exportSampleCategory.getCategoryValue()).getIdAsInt());
70          };
71  
72      }
73  
74      public GenericFormatImportOperationContext validateRow(ImportRow<CatchRow> row, GenericFormatContextSupport importContext) {
75  
76          GenericFormatImportOperationContext operationContext = importContext.getValidationHelper().getFishingOperationContext(this, row, importContext);
77  
78          if (operationContext != null) {
79  
80              //TODO checks!
81              CatchRow bean = row.getBean();
82  
83              for (ExportSampleCategory sampleCategory : bean.getSampleCategory()) {
84  
85                  if (sampleCategory.isFilled()) {
86  
87                      if (sampleCategory.getCategoryValue() == null) {
88                          //TODO
89                      }
90                  }
91              }
92  
93              if (bean.withFrequency()) {
94  
95                  // Mandatory batchNumber (see https://forge.codelutin.com/issues/7058)
96  
97                  if (bean.getBatchNumber() == null) {
98  
99                      addCheckError(row, new MissingBatchNumberForFrequencyException(operationContext.getFishingOperation()));
100 
101                 }
102 
103                 if (bean.getFrequencyWeight() != null && WeightUnit.KG.isZero(bean.getFrequencyWeight())) {
104 
105                     // Cant have a Weight 0.0
106                     addCheckError(row, new FrequencyWeigthNullValueException(operationContext.getFishingOperation()));
107                 }
108             }
109 
110         }
111 
112         reportError(row);
113 
114         return operationContext;
115 
116     }
117 
118     public void prepareRowForPersist(GenericFormatImportOperationContext operationContext, ImportRow<CatchRow> row) {
119 
120         CatchRow bean = row.getBean();
121 
122         boolean vrac = catchRowVracPredicate.apply(bean);
123         bean.setVrac(vrac);
124 
125         boolean withFrequency = bean.withFrequency();
126 
127         if (bean.isBenthos()) {
128 
129             prepareBenthosRowForPersist(operationContext, bean, withFrequency);
130 
131         } else {
132 
133             prepareSpeciesRowForPersist(operationContext, bean, withFrequency);
134 
135         }
136 
137     }
138 
139     protected void prepareSpeciesRowForPersist(GenericFormatImportOperationContext operationContext, CatchRow bean, boolean withFrequency) {
140 
141         Species species = bean.getSpecies();
142         boolean vrac = bean.isVrac();
143 
144         SpeciesBatch batch = operationContext.getSpeciesBatch(vrac, species.getReferenceTaxonId());
145 
146         if (batch == null) {
147 
148             // create root batch
149 
150             batch = SpeciesBatchs.newSpeciesBatch();
151             batch.setId(getNextBatchId());
152             batch.setSpecies(species);
153             batch.setSpeciesToConfirm(bean.isSpeciesToConfirm());
154             batch.setFishingOperation(operationContext.getFishingOperation());
155 
156             if (log.isInfoEnabled()) {
157                 log.info("Create species root batch (" + (vrac ? "VRAC" : "HORS VRAC") + ") batch species: " + species.getName() + " for " + operationContext.getFishingOperationLabel());
158             }
159 
160             operationContext.addSpeciesBatch(vrac, batch);
161 
162         }
163 
164         batch = fillBatchCategories(operationContext, batch, bean);
165 
166         if (withFrequency) {
167 
168             SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newSpeciesBatchFrequency();
169             frequency.setBatch(batch);
170             frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic());
171             frequency.setLengthStep(bean.getFrequencyLengthStep());
172             frequency.setWeight(bean.getFrequencyWeight());
173             frequency.setNumber(bean.getBatchNumber());
174 
175             if (log.isInfoEnabled()) {
176                 log.info("Create species frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue());
177             }
178 
179             operationContext.addSpeciesFrequency(batch, frequency);
180 
181         } else {
182 
183             batch.setNumber(bean.getBatchNumber());
184 
185         }
186     }
187 
188     protected void prepareBenthosRowForPersist(GenericFormatImportOperationContext operationContext, CatchRow bean, boolean withFrequency) {
189 
190         Species species = bean.getSpecies();
191         boolean vrac = bean.isVrac();
192 
193         SpeciesBatch batch = operationContext.getBenthosBatch(vrac, species.getReferenceTaxonId());
194 
195         if (batch == null) {
196 
197             // create root batch
198 
199             batch = SpeciesBatchs.newBenthosBatch();
200             batch.setId(getNextBatchId());
201             batch.setSpecies(species);
202             batch.setSpeciesToConfirm(bean.isSpeciesToConfirm());
203             batch.setFishingOperation(operationContext.getFishingOperation());
204 
205             if (log.isInfoEnabled()) {
206                 log.info("Create benthos root batch (" + (vrac ? "VRAC" : "HORS VRAC") + ") batch species: " + species.getName() + " for " + operationContext.getFishingOperationLabel());
207             }
208 
209             operationContext.addBenthosBatch(vrac, batch);
210 
211         }
212 
213         batch = fillBatchCategories(operationContext, batch, bean);
214 
215         if (withFrequency) {
216 
217             SpeciesBatchFrequency frequency = SpeciesBatchFrequencys.newBenthosBatchFrequency();
218             frequency.setBatch(batch);
219             frequency.setLengthStepCaracteristic(bean.getFrequencyLengthStepCaracteristic());
220             frequency.setLengthStep(bean.getFrequencyLengthStep());
221             frequency.setWeight(bean.getFrequencyWeight());
222             frequency.setNumber(bean.getBatchNumber());
223 
224             if (log.isInfoEnabled()) {
225                 log.info("Create benthos frequency " + frequency.getNumber() + " for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue());
226             }
227 
228             operationContext.addBenthosFrequency(batch, frequency);
229 
230         } else {
231 
232             batch.setNumber(bean.getBatchNumber());
233 
234         }
235 
236     }
237 
238     protected SpeciesBatch fillBatchCategories(GenericFormatImportOperationContext operationContext, SpeciesBatch batch, CatchRow bean) {
239 
240         for (ExportSampleCategory exportSampleCategory : bean.getFilledSampleCategories()) {
241             batch = fillBatchCategories(operationContext, batch, exportSampleCategory);
242         }
243 
244         return batch;
245 
246     }
247 
248     protected SpeciesBatch fillBatchCategories(GenericFormatImportOperationContext operationContext, SpeciesBatch batch, ExportSampleCategory sampleCategory) {
249 
250         SpeciesBatch result = null;
251 
252         if (batch.getSampleCategoryId() == null) {
253 
254             // category not filled in batch, fill it from the incoming category
255             batch.setSampleCategoryId(sampleCategory.getCategoryId());
256             batch.setSampleCategoryValue(sampleCategory.getCategoryValue());
257             batch.setSampleCategoryWeight(sampleCategory.getCategoryWeight());
258             batch.setWeight(sampleCategory.getSampleWeight());
259             batch.setRankOrder(sampleCategory.getRankOrder());
260             batch.setComment(sampleCategory.getComment());
261             result = batch;
262             operationContext.registerBatchObjectId(batch.getIdAsInt(), sampleCategory.getBatchId());
263 
264         } else {
265 
266             if (batch.getSampleCategoryId().equals(sampleCategory.getCategoryId()) &&
267                     batch.getSampleCategoryValue().equals(sampleCategory.getCategoryValue())) {
268 
269                 result = batch;
270 
271             } else if (!batch.isChildBatchsEmpty()) {
272 
273                 // got some childs, try to find a matching one
274                 for (SpeciesBatch childBatch : batch.getChildBatchs()) {
275 
276                     if (childBatch.getSampleCategoryId().equals(sampleCategory.getCategoryId()) &&
277                             childBatch.getSampleCategoryValue().equals(sampleCategory.getCategoryValue())) {
278 
279                         // matching child
280                         result = childBatch;
281                         break;
282                     }
283                 }
284 
285             }
286 
287             if (result == null) {
288 
289                 // add a child
290                 result = SpeciesBatchs.createNewChild(batch);
291                 result.setId(getNextBatchId());
292                 result.setRankOrder(sampleCategory.getRankOrder());
293                 result.setSampleCategoryId(sampleCategory.getCategoryId());
294                 result.setSampleCategoryValue(sampleCategory.getCategoryValue());
295                 result.setSampleCategoryWeight(sampleCategory.getCategoryWeight());
296                 result.setWeight(sampleCategory.getSampleWeight());
297                 result.setComment(sampleCategory.getComment());
298                 operationContext.registerBatchObjectId(result.getIdAsInt(), sampleCategory.getBatchId());
299                 if (log.isInfoEnabled()) {
300                     log.info("Create child batch for batch: " + batch.getSpecies().getName() + " - " + batch.getSampleCategoryId() + " - " + batch.getSampleCategoryValue());
301                 }
302 
303             }
304 
305         }
306 
307         return result;
308 
309     }
310 
311     private int batchId;
312 
313     private int getNextBatchId() {
314         return batchId++;
315     }
316 
317 }