View Javadoc
1   package fr.ifremer.tutti.persistence.service;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.collect.Multimap;
26  import fr.ifremer.tutti.persistence.InvalidBatchModelException;
27  import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
28  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
29  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
30  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
31  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequencys;
32  import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs;
33  import fr.ifremer.tutti.persistence.entities.referential.Species;
34  import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper;
35  import fr.ifremer.tutti.persistence.service.util.tree.BenthosBatchTreeHelper;
36  import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelperSupport;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.springframework.stereotype.Service;
40  
41  import javax.annotation.Resource;
42  import java.util.Collection;
43  import java.util.List;
44  
45  /**
46   * Default implementation of {@link BenthosBatchPersistenceService}.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 1.2
50   */
51  @Service("benthosBatchPersistenceService")
52  public class BenthosBatchPersistenceServiceImpl extends SpeciesBatchPersistenceServiceSupport
53          implements BenthosBatchPersistenceService {
54  
55      /** Logger. */
56      private static final Log log = LogFactory.getLog(BenthosBatchPersistenceServiceImpl.class);
57  
58      @Resource(name = "benthosBatchTreeHelper")
59      protected BenthosBatchTreeHelper batchTreeHelper;
60  
61      public BenthosBatchPersistenceServiceImpl() {
62          super("Benthos",  SpeciesBatchs::newBenthosBatch, SpeciesBatchFrequencys::newBenthosBatchFrequency);
63      }
64  
65      //------------------------------------------------------------------------//
66      //-- Benthos Batch methods                                              --//
67      //------------------------------------------------------------------------//
68  
69      @Override
70      protected SpeciesBatchTreeHelperSupport getBatchTreeHelper() {
71          return batchTreeHelper;
72      }
73  
74      @Override
75      protected void validate(BatchPersistenceHelper batchHelper, SampleCategoryModel sampleCategoryModel, BatchContainer<SpeciesBatch> result) {
76          batchHelper.validateBenthos(sampleCategoryModel, result);
77      }
78  
79      @Override
80      public BatchContainer<SpeciesBatch> getRootBenthosBatch(Integer fishingOperationId, boolean validateTree) {
81  
82          return getRootSpeciesBatch0(fishingOperationId, validateTree);
83  
84  //        Preconditions.checkNotNull(fishingOperationId);
85  //
86  //        DateFormat df = new SimpleDateFormat("dd/MM/yyy");
87  //
88  //        CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false);
89  //
90  //        // -- Vrac > Benthos > Alive Itemized
91  //        SortingBatch vracBenthosBatch = batchTreeHelper.getBenthosVracAliveItemizedRootBatch(catchBatch);
92  //
93  //        SampleCategoryModel sampleCategoryModel = getSampleCategoryModel();
94  //
95  //        BatchContainer<SpeciesBatch> result = new BatchContainer<>();
96  //        if (vracBenthosBatch != null) {
97  //            result.setId(vracBenthosBatch.getId());
98  //
99  //            for (Batch batch : vracBenthosBatch.getChildBatchs()) {
100 //                SortingBatch source = (SortingBatch) batch;
101 //                ReferenceTaxon referenceTaxon = source.getReferenceTaxon();
102 //                Preconditions.checkNotNull(referenceTaxon, "Can't have a rootBenthosBatch with a null taxon, but was for " + batch.getId());
103 //                Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId());
104 //                if (species == null) {
105 //                    FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId);
106 //                    String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate());
107 //                    throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId()));
108 //                }
109 //                SpeciesBatch target = SpeciesBatchs.newBenthosBatch();
110 //                target.setSpecies(species);
111 //
112 //                batchHelper.entityToBean(sampleCategoryModel, source, target);
113 //                result.addChildren(target);
114 //                if (log.isDebugEnabled()) {
115 //                    log.debug("Loaded CatchBatch Vrac > Benthos > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId());
116 //                }
117 //            }
118 //        }
119 //
120 //        // -- Hors Vrac > Benthos
121 //        SortingBatch horsVracBenthosBatch = batchTreeHelper.getBenthosHorsVracRootBatch(catchBatch);
122 //
123 //        if (horsVracBenthosBatch != null) {
124 //            for (Batch batch : horsVracBenthosBatch.getChildBatchs()) {
125 //                SortingBatch source = (SortingBatch) batch;
126 //                ReferenceTaxon referenceTaxon = source.getReferenceTaxon();
127 //                Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId());
128 //                Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId());
129 //                if (species == null) {
130 //                    FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId);
131 //                    String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate());
132 //                    throw new InvalidBatchModelException(t("tutti.persistence.benthosBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId()));
133 //                }
134 //
135 //                SpeciesBatch target = SpeciesBatchs.newBenthosBatch();
136 //                target.setSpecies(species);
137 //
138 //                batchHelper.entityToBean(sampleCategoryModel, source, target);
139 //                result.addChildren(target);
140 //                if (log.isDebugEnabled()) {
141 //                    log.debug("Loaded CatchBatch Hors Vrac > Benthos > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId());
142 //                }
143 //            }
144 //        }
145 //
146 //        if (validateTree) {
147 //
148 //            // validate with given sample category model
149 //            batchHelper.validateBenthos(sampleCategoryModel, result);
150 //        }
151 //
152 //        return result;
153     }
154 
155     @Override
156     public SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) {
157 
158         return createSpeciesBatch0(bean, parentBatchId, computeRankOrder);
159 
160 //        Preconditions.checkNotNull(bean);
161 //        Preconditions.checkArgument(TuttiEntities.isNew(bean));
162 //        Preconditions.checkNotNull(bean.getSpecies());
163 //        Preconditions.checkNotNull(bean.getSpecies().getId());
164 //        Preconditions.checkNotNull(bean.getFishingOperation());
165 //        Preconditions.checkNotNull(bean.getFishingOperation().getId());
166 //
167 //        CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false);
168 //
169 //        return createBenthosBatch(bean, parentBatchId, catchBatch, computeRankOrder);
170     }
171 
172     @Override
173     public Collection<SpeciesBatch> createBenthosBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) {
174 
175         return createSpeciesBatches0(fishingOperationId, beans);
176 
177 //        Preconditions.checkNotNull(beans);
178 //        Preconditions.checkNotNull(fishingOperationId);
179 //
180 //        CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false);
181 //
182 //        Collection<SpeciesBatch> result = new ArrayList<>();
183 //        for (SpeciesBatch bean : beans) {
184 //
185 //            SpeciesBatch created = createBenthosBatch(bean, null, catchBatch, true);
186 //            result.add(created);
187 //
188 //        }
189 //        return result;
190 
191     }
192 
193 //    protected SpeciesBatch createBenthosBatch(SpeciesBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) {
194 //
195 //        Preconditions.checkNotNull(bean);
196 //        Preconditions.checkArgument(TuttiEntities.isNew(bean));
197 //        Preconditions.checkNotNull(bean.getSpecies());
198 //        Preconditions.checkNotNull(bean.getSpecies().getId());
199 //        Preconditions.checkNotNull(bean.getFishingOperation());
200 //        Preconditions.checkNotNull(bean.getFishingOperation().getId());
201 //
202 //        SortingBatch batch = SortingBatch.Factory.newInstance();
203 //        beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder);
204 //        bean = batchHelper.createSortingBatch(bean, catchBatch, batch);
205 //
206 //        return bean;
207 //    }
208 
209     @Override
210     public SpeciesBatch saveBenthosBatch(SpeciesBatch bean) {
211 
212         return saveSpeciesBatch0(bean);
213 
214 //        Preconditions.checkNotNull(bean);
215 //        Preconditions.checkNotNull(bean.getId());
216 //
217 //        Integer batchId = bean.getIdAsInt();
218 //        CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId);
219 //        SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId);
220 //
221 //        Integer parentBatchId = null;
222 //        if (bean.getParentBatch() != null) {
223 //            parentBatchId = bean.getParentBatch().getIdAsInt();
224 //        }
225 //        beanToEntity(bean, batch, parentBatchId, catchBatch, true);
226 //        batchHelper.updateSortingBatch(batch, catchBatch);
227 //
228 //        return bean;
229     }
230 
231     @Override
232     public void deleteBenthosBatch(Integer id) {
233         deleteSpeciesBatch0(id);
234 //        Preconditions.checkNotNull(id);
235 //        batchHelper.deleteBatch(id);
236     }
237 
238     @Override
239     public void deleteBenthosSubBatch(Integer id) {
240         deleteSpeciesSubBatch0(id);
241 //        Preconditions.checkNotNull(id);
242 //        batchHelper.deleteSpeciesSubBatch(id);
243     }
244 
245     @Override
246     public void changeBenthosBatchSpecies(Integer id, Species species) {
247         changeSpeciesBatchSpecies0(id, species);
248 //        Preconditions.checkNotNull(id);
249 //        Preconditions.checkNotNull(species);
250 //        Preconditions.checkNotNull(species.getReferenceTaxonId());
251 //        batchHelper.changeBatchSpecies(id, species);
252     }
253 
254     @Override
255     public List<SpeciesBatch> getAllBenthosBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException {
256         return getAllSpeciesBatchToConfirm0(fishingOperationId);
257 //        List<SpeciesBatch> batchesToConfirm = new ArrayList<>();
258 //
259 //        BatchContainer<SpeciesBatch> rootBenthosBatch = getRootBenthosBatch(fishingOperationId, false);
260 //        for (SpeciesBatch benthosBatch : rootBenthosBatch.getChildren()) {
261 //            findBenthosBatchesToConfirm(benthosBatch, batchesToConfirm);
262 //        }
263 //
264 //        return batchesToConfirm;
265     }
266 
267 //    protected void findBenthosBatchesToConfirm(SpeciesBatch benthosBatch, List<SpeciesBatch> batchesToConfirm) {
268 //        if (benthosBatch.isSpeciesToConfirm()) {
269 //            batchesToConfirm.add(benthosBatch);
270 //
271 //        } else if (!benthosBatch.isChildBatchsEmpty()) {
272 //            for (SpeciesBatch batch : benthosBatch.getChildBatchs()) {
273 //                findBenthosBatchesToConfirm(batch, batchesToConfirm);
274 //            }
275 //        }
276 //    }
277 
278     //------------------------------------------------------------------------//
279     //-- SpeciesBatchFrequency methods (for benthos)                        --//
280     //------------------------------------------------------------------------//
281 
282     @Override
283     public List<SpeciesBatchFrequency> getAllBenthosBatchFrequency(Integer benthosBatchId) {
284         return getAllSpeciesBatchFrequency0(benthosBatchId);
285 //        Preconditions.checkNotNull(benthosBatchId);
286 //
287 //        List<SortingBatch> frequencyChilds = batchHelper.getFrequencies(
288 //                benthosBatchId);
289 //        List<SpeciesBatchFrequency> results = Lists.newArrayList();
290 //        for (SortingBatch child : frequencyChilds) {
291 //            SpeciesBatchFrequency target = SpeciesBatchFrequencys.newBenthosBatchFrequency();
292 //
293 //            batchHelper.entityToBatchFrequency(child, target);
294 //            results.add(target);
295 //        }
296 //        return Collections.unmodifiableList(results);
297     }
298 
299     @Override
300     public Multimap<Species, SpeciesBatchFrequency> getAllBenthosBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) {
301         return getAllSpeciesBatchFrequencyForBatch0(batchContainer);
302 //        Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create();
303 //        for (SpeciesBatch speciesBatch : batchContainer.getChildren()) {
304 //            getAllSpeciesBatchFrequencyForBatch(speciesBatch, result);
305 //        }
306 //        return result;
307     }
308 
309 //    protected void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch,
310 //                                                       Multimap<Species, SpeciesBatchFrequency> result) {
311 //
312 //        List<SpeciesBatchFrequency> speciesBatchFrequency = getAllBenthosBatchFrequency(batch.getIdAsInt());
313 //        result.putAll(batch.getSpecies(), speciesBatchFrequency);
314 //
315 //        if (!batch.isChildBatchsEmpty()) {
316 //            for (SpeciesBatch child : batch.getChildBatchs()) {
317 //                getAllSpeciesBatchFrequencyForBatch(child, result);
318 //            }
319 //        }
320 //    }
321 
322     @Override
323     public List<SpeciesBatchFrequency> saveBenthosBatchFrequency(Integer benthosBatchId,
324                                                                  List<SpeciesBatchFrequency> frequencies) {
325         return saveSpeciesBatchFrequency0(benthosBatchId, frequencies);
326 //        Preconditions.checkNotNull(benthosBatchId);
327 //        Preconditions.checkNotNull(frequencies);
328 //
329 //        // Check that all frequencies have the same length PMFM (before doing any db call)
330 //        String pmfmId = null;
331 //        for (SpeciesBatchFrequency source : frequencies) {
332 //
333 //            if (pmfmId == null) {
334 //                pmfmId = source.getLengthStepCaracteristic().getId();
335 //            } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) {
336 //                throw new DataIntegrityViolationException("Batch frequencies under one Speciesbatch must have all the same lengthStepCaracteristic");
337 //            }
338 //        }
339 //
340 //        CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(benthosBatchId);
341 //
342 //        if (catchBatch == null) {
343 //            return frequencies;
344 //        }
345 //
346 //        // Synchronization status
347 //        synchronizationStatusHelper.setDirty(catchBatch);
348 //
349 //        // Retrieve parent
350 //        SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, benthosBatchId);
351 //
352 //        // Remember child ids, to remove unchanged item (see at bottom in this method)
353 //        List<Integer> notUpdatedChildIds = Lists.newArrayList();
354 //        List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds(
355 //                parentBatch);
356 //        for (SortingBatch child : frequencyChilds) {
357 //            notUpdatedChildIds.add(child.getId());
358 //        }
359 //
360 //        short rankOrder = 0;
361 //        List<SortingBatch> batchsToUpdate = Lists.newArrayList();
362 //        for (SpeciesBatchFrequency source : frequencies) {
363 //            rankOrder++;
364 //
365 //            // Not existing batch
366 //            SortingBatch target;
367 //            if (source.getId() == null) {
368 //                target = SortingBatch.Factory.newInstance();
369 //
370 //                // Fill the sorting batch from the source
371 //                batchHelper.beanToEntity(source, target, parentBatch, rankOrder);
372 //
373 //                // Create the targeted batch, then update the source id
374 //                batchHelper.createSortingBatch(source, catchBatch, target);
375 //            }
376 //
377 //            // Existing batch
378 //            else {
379 //                target = batchHelper.getSortingBatchById(catchBatch, source.getIdAsInt());
380 //
381 //                // Fill the sorting batch from the source
382 //                batchHelper.beanToEntity(source, target, parentBatch, rankOrder);
383 //
384 //                // Add the batch into a list (will be update later, using this list)
385 //                batchsToUpdate.add(target);
386 //
387 //                notUpdatedChildIds.remove(target.getId());
388 //            }
389 //        }
390 //
391 //        // If some batchs need to be update, do it
392 //        if (CollectionUtils.isNotEmpty(batchsToUpdate)) {
393 //            batchHelper.updateSortingBatch(batchsToUpdate, catchBatch);
394 //        }
395 //
396 //        if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) {
397 //            for (Integer batchId : notUpdatedChildIds) {
398 //                batchHelper.removeWithChildren(batchId, catchBatch);
399 //            }
400 //        }
401 //
402 //        return Collections.unmodifiableList(frequencies);
403     }
404 
405     //------------------------------------------------------------------------//
406     //-- Internal methods                                                   --//
407     //------------------------------------------------------------------------//
408 
409 //    protected void beanToEntity(SpeciesBatch source,
410 //                                SortingBatch target,
411 //                                Integer parentBatchId,
412 //                                CatchBatch catchBatch,
413 //                                boolean computeRankOrder) {
414 //
415 //        Preconditions.checkNotNull(source.getFishingOperation());
416 //        Preconditions.checkNotNull(source.getFishingOperation().getId());
417 //
418 //        // If parent and root need to be set
419 //        if (target.getId() == null
420 //                || target.getRootBatch() == null
421 //                || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) {
422 //            batchHelper.setBenthosBatchParents(
423 //                    source.getSampleCategoryId(),
424 //                    source.getSampleCategoryValue(),
425 //                    target,
426 //                    parentBatchId,
427 //                    catchBatch);
428 //        }
429 //
430 //        batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder);
431 //    }
432 
433 }