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.adagio.core.dao.data.batch.CatchBatch;
27 import fr.ifremer.tutti.persistence.InvalidBatchModelException;
28 import fr.ifremer.tutti.persistence.entities.data.BatchContainer;
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.Species;
35 import fr.ifremer.tutti.persistence.service.util.BatchPersistenceHelper;
36 import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelper;
37 import fr.ifremer.tutti.persistence.service.util.tree.SpeciesBatchTreeHelperSupport;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.springframework.stereotype.Service;
41
42 import javax.annotation.Resource;
43 import java.util.Collection;
44 import java.util.List;
45
46 /**
47 * Default implementation of {@link SpeciesBatchPersistenceService}.
48 *
49 * @author Tony Chemit - chemit@codelutin.com
50 * @since 1.2
51 */
52 @Service("speciesBatchPersistenceService")
53 public class SpeciesBatchPersistenceServiceImpl extends SpeciesBatchPersistenceServiceSupport
54 implements SpeciesBatchPersistenceService {
55
56 /** Logger. */
57 private static final Log log =
58 LogFactory.getLog(SpeciesBatchPersistenceServiceImpl.class);
59
60 @Resource(name = "speciesBatchTreeHelper")
61 protected SpeciesBatchTreeHelper batchTreeHelper;
62
63 public SpeciesBatchPersistenceServiceImpl() {
64 super("Species", SpeciesBatchs::newSpeciesBatch, SpeciesBatchFrequencys::newSpeciesBatchFrequency);
65 }
66
67 //------------------------------------------------------------------------//
68 //-- SpeciesBatch methods --//
69 //------------------------------------------------------------------------//
70
71 @Override
72 protected SpeciesBatchTreeHelperSupport getBatchTreeHelper() {
73 return batchTreeHelper;
74 }
75
76 @Override
77 protected void validate(BatchPersistenceHelper batchHelper, SampleCategoryModel sampleCategoryModel, BatchContainer<SpeciesBatch> result) {
78 batchHelper.validateSpecies(sampleCategoryModel, result);
79 }
80
81 @Override
82 public BatchContainer<SpeciesBatch> getRootSpeciesBatch(Integer fishingOperationId, boolean validateTree) throws InvalidBatchModelException {
83
84 return getRootSpeciesBatch0(fishingOperationId, validateTree);
85 //
86 // Preconditions.checkNotNull(fishingOperationId);
87 //
88 // DateFormat df = new SimpleDateFormat("dd/MM/yyy");
89 //
90 // CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false);
91 //
92 // // -- Vrac > Species > Alive Itemized
93 // SortingBatch vracSpeciesBatch = batchTreeHelper.getSpeciesVracAliveItemizedRootBatch(catchBatch);
94 //
95 // // container of speciesBatch is arbitraty put on vrac type (there is
96 // // no common ancestor for all species batch).
97 // BatchContainer<SpeciesBatch> result = new BatchContainer<>();
98 //
99 // SampleCategoryModel sampleCategoryModel = getSampleCategoryModel();
100 //
101 // if (vracSpeciesBatch != null) {
102 //
103 // result.setId(vracSpeciesBatch.getId());
104 //
105 // for (Batch batch : vracSpeciesBatch.getChildBatchs()) {
106 // SortingBatch source = (SortingBatch) batch;
107 // ReferenceTaxon referenceTaxon = source.getReferenceTaxon();
108 // Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + batch.getId());
109 // if (log.isTraceEnabled()) {
110 // log.trace("Loading CatchBatch Vrac > Species > Alive Itemized > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")");
111 // }
112 // Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId());
113 // if (species == null) {
114 // FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId);
115 // String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate());
116 // throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId()));
117 // }
118 // SpeciesBatch target = SpeciesBatchs.newSpeciesBatch();
119 // target.setSpecies(species);
120 //
121 // batchHelper.entityToBean(sampleCategoryModel, source, target);
122 // result.addChildren(target);
123 //
124 // if (log.isDebugEnabled()) {
125 // log.debug("Loaded CatchBatch Vrac > Species > Alive Itemized > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId());
126 // }
127 // }
128 // }
129 //
130 // // -- Hors Vrac > Species
131 // SortingBatch horsVracSpeciesBatch = batchTreeHelper.getSpeciesHorsVracRootBatch(catchBatch);
132 //
133 // if (horsVracSpeciesBatch != null) {
134 // for (Batch batch : horsVracSpeciesBatch.getChildBatchs()) {
135 // SortingBatch source = (SortingBatch) batch;
136 // ReferenceTaxon referenceTaxon = source.getReferenceTaxon();
137 // Preconditions.checkNotNull(referenceTaxon, "Can't have a rootSpeciesBatch with a null taxon, but was for " + source.getId());
138 // if (log.isTraceEnabled()) {
139 // log.trace("Loading CatchBatch Hors Vrac > Species > " + referenceTaxon.getId() + " - " + " (batch:" + source.getId() + ")");
140 // }
141 // Species species = speciesService.getSpeciesByReferenceTaxonId(referenceTaxon.getId());
142 // if (species == null) {
143 // FishingOperation fishingOperation = fishingOperationPersistenceService.getFishingOperation(fishingOperationId);
144 // String fishingOperationName = fishingOperation.getStationNumber() + " - " + fishingOperation.getFishingOperationNumber() + " - " + df.format(fishingOperation.getGearShootingStartDate());
145 // throw new InvalidBatchModelException(t("tutti.persistence.speciesBatch.validation.unkonwn.taxon", fishingOperationName, source.getId(), referenceTaxon.getId()));
146 // }
147 // SpeciesBatch target = SpeciesBatchs.newSpeciesBatch();
148 // target.setSpecies(species);
149 // batchHelper.entityToBean(sampleCategoryModel, source, target);
150 // result.addChildren(target);
151 // if (log.isDebugEnabled()) {
152 // log.debug("Loaded CatchBatch Hors Vrac > Species > " + target.getSpecies().getReferenceTaxonId() + ": " + target.getId());
153 // }
154 // }
155 // }
156 //
157 // if (validateTree) {
158 //
159 // // validate with given sample category model
160 // batchHelper.validateSpecies(sampleCategoryModel, result);
161 // }
162 //
163 // return result;
164 }
165
166 @Override
167 public SpeciesBatch createSpeciesBatch(SpeciesBatch bean, Integer parentBatchId, boolean computeRankOrder) {
168
169 return createSpeciesBatch0(bean, parentBatchId, computeRankOrder);
170
171 // Preconditions.checkNotNull(bean);
172 // Preconditions.checkArgument(TuttiEntities.isNew(bean));
173 // Preconditions.checkNotNull(bean.getSpecies());
174 // Preconditions.checkNotNull(bean.getSpecies().getId());
175 // Preconditions.checkNotNull(bean.getFishingOperation());
176 // Preconditions.checkNotNull(bean.getFishingOperation().getId());
177 //
178 // CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(bean.getFishingOperation().getIdAsInt(), false);
179 //
180 // return createSpeciesBatch(bean, parentBatchId, catchBatch, computeRankOrder);
181 }
182
183 @Override
184 public Collection<SpeciesBatch> createSpeciesBatches(Integer fishingOperationId, Collection<SpeciesBatch> beans) {
185
186 return createSpeciesBatches0(fishingOperationId, beans);
187
188 // Preconditions.checkNotNull(beans);
189 // Preconditions.checkNotNull(fishingOperationId);
190 //
191 // CatchBatch catchBatch = batchHelper.getRootCatchBatchByFishingOperationId(fishingOperationId, false);
192 //
193 // Collection<SpeciesBatch> result = new ArrayList<>();
194 // for (SpeciesBatch bean : beans) {
195 //
196 // SpeciesBatch created = createSpeciesBatch(bean, null, catchBatch, true);
197 // result.add(created);
198 //
199 // }
200 // return result;
201
202 }
203
204
205 protected SpeciesBatch createSpeciesBatch(SpeciesBatch bean, Integer parentBatchId, CatchBatch catchBatch, boolean computeRankOrder) {
206 return createSpeciesBatch0(bean, parentBatchId, catchBatch, computeRankOrder);
207 // Preconditions.checkNotNull(bean);
208 // Preconditions.checkArgument(TuttiEntities.isNew(bean));
209 // Preconditions.checkNotNull(bean.getSpecies());
210 // Preconditions.checkNotNull(bean.getSpecies().getId());
211 // Preconditions.checkNotNull(bean.getFishingOperation());
212 // Preconditions.checkNotNull(bean.getFishingOperation().getId());
213 //
214 // SortingBatch batch = SortingBatch.Factory.newInstance();
215 // beanToEntity(bean, batch, parentBatchId, catchBatch, computeRankOrder);
216 // bean = batchHelper.createSortingBatch(bean, catchBatch, batch);
217 //
218 // return bean;
219 }
220
221 @Override
222 public SpeciesBatch saveSpeciesBatch(SpeciesBatch bean) {
223 return saveSpeciesBatch0(bean);
224 // Preconditions.checkNotNull(bean);
225 // Preconditions.checkArgument(!TuttiEntities.isNew(bean));
226 //
227 // Integer batchId = bean.getIdAsInt();
228 //
229 // CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(batchId);
230 // SortingBatch batch = batchHelper.getSortingBatchById(catchBatch, batchId);
231 // Integer parentBatchId = null;
232 // if (bean.getParentBatch() != null) {
233 // parentBatchId = bean.getParentBatch().getIdAsInt();
234 // }
235 // beanToEntity(bean, batch, parentBatchId, catchBatch, true);
236 // batchHelper.updateSortingBatch(batch, catchBatch);
237 //
238 // return bean;
239 }
240
241 @Override
242 public void deleteSpeciesBatch(Integer id) {
243 deleteSpeciesBatch0(id);
244 // Preconditions.checkNotNull(id);
245 // batchHelper.deleteBatch(id);
246 }
247
248 @Override
249 public void deleteSpeciesSubBatch(Integer id) {
250 deleteSpeciesSubBatch0(id);
251 // Preconditions.checkNotNull(id);
252 // batchHelper.deleteSpeciesSubBatch(id);
253 }
254
255 @Override
256 public void changeSpeciesBatchSpecies(Integer id, Species species) {
257 changeSpeciesBatchSpecies0(id, species);
258 // Preconditions.checkNotNull(id);
259 // Preconditions.checkNotNull(species);
260 // Preconditions.checkNotNull(species.getReferenceTaxonId());
261 // batchHelper.changeBatchSpecies(id, species);
262 }
263
264 @Override
265 public List<SpeciesBatch> getAllSpeciesBatchToConfirm(Integer fishingOperationId) throws InvalidBatchModelException {
266 return getAllSpeciesBatchToConfirm0(fishingOperationId);
267 // List<SpeciesBatch> batchesToConfirm = new ArrayList<>();
268 //
269 // BatchContainer<SpeciesBatch> rootSpeciesBatch = getRootSpeciesBatch(fishingOperationId, false);
270 // for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) {
271 // findSpeciesBatchesToConfirm(speciesBatch, batchesToConfirm);
272 // }
273 //
274 // return batchesToConfirm;
275 }
276
277 // protected void findSpeciesBatchesToConfirm(SpeciesBatch speciesBatch, List<SpeciesBatch> batchesToConfirm) {
278 // if (speciesBatch.isSpeciesToConfirm()) {
279 // batchesToConfirm.add(speciesBatch);
280 //
281 // } else if (!speciesBatch.isChildBatchsEmpty()) {
282 // for (SpeciesBatch batch : speciesBatch.getChildBatchs()) {
283 // findSpeciesBatchesToConfirm(batch, batchesToConfirm);
284 // }
285 // }
286 // }
287
288 //------------------------------------------------------------------------//
289 //-- SpeciesBatchFrequency methods --//
290 //------------------------------------------------------------------------//
291
292 @Override
293 public List<SpeciesBatchFrequency> getAllSpeciesBatchFrequency(Integer speciesBatchId) {
294 return getAllSpeciesBatchFrequency0(speciesBatchId);
295 // Preconditions.checkNotNull(speciesBatchId);
296 //
297 // List<SortingBatch> frequencyChilds =
298 // batchHelper.getFrequencies(speciesBatchId);
299 // List<SpeciesBatchFrequency> results = Lists.newArrayList();
300 // for (SortingBatch child : frequencyChilds) {
301 // SpeciesBatchFrequency target =
302 // SpeciesBatchFrequencys.newSpeciesBatchFrequency();
303 //
304 // batchHelper.entityToBatchFrequency(child, target);
305 // results.add(target);
306 // }
307 // return Collections.unmodifiableList(results);
308 }
309
310 @Override
311 public Multimap<Species, SpeciesBatchFrequency> getAllSpeciesBatchFrequencyForBatch(BatchContainer<SpeciesBatch> batchContainer) {
312 return getAllSpeciesBatchFrequencyForBatch0(batchContainer);
313 // Multimap<Species, SpeciesBatchFrequency> result = ArrayListMultimap.create();
314 // for (SpeciesBatch speciesBatch : batchContainer.getChildren()) {
315 // getAllSpeciesBatchFrequencyForBatch(speciesBatch, result);
316 // }
317 // return result;
318 }
319
320 // protected void getAllSpeciesBatchFrequencyForBatch(SpeciesBatch batch,
321 // Multimap<Species, SpeciesBatchFrequency> result) {
322 //
323 // List<SpeciesBatchFrequency> speciesBatchFrequency = getAllSpeciesBatchFrequency(batch.getIdAsInt());
324 // result.putAll(batch.getSpecies(), speciesBatchFrequency);
325 //
326 // if (!batch.isChildBatchsEmpty()) {
327 // for (SpeciesBatch child : batch.getChildBatchs()) {
328 // getAllSpeciesBatchFrequencyForBatch(child, result);
329 // }
330 // }
331 // }
332
333 @Override
334 public List<SpeciesBatchFrequency> saveSpeciesBatchFrequency(Integer speciesBatchId, List<SpeciesBatchFrequency> frequencies) {
335 return saveSpeciesBatchFrequency0(speciesBatchId, frequencies);
336 // Preconditions.checkNotNull(speciesBatchId);
337 // Preconditions.checkNotNull(frequencies);
338 //
339 // List<SpeciesBatchFrequency> notNullFrequencies = Lists.newArrayList();
340 //
341 // // Check that all frequencies have the same length PMFM (before doing any db call)
342 // // and remove null frequencies
343 // String pmfmId = null;
344 // for (SpeciesBatchFrequency source : frequencies) {
345 //
346 // if (source.getLengthStepCaracteristic() != null) {
347 // if (pmfmId == null) {
348 // pmfmId = source.getLengthStepCaracteristic().getId();
349 // } else if (!pmfmId.equals(source.getLengthStepCaracteristic().getId())) {
350 // throw new DataIntegrityViolationException("Batch frequencies under one SpeciesBatch must have all the same lengthStepCaracteristic");
351 // }
352 // notNullFrequencies.add(source);
353 // }
354 // }
355 //
356 // CatchBatch catchBatch = batchHelper.getRootCatchBatchByBatchId(speciesBatchId);
357 //
358 // if (catchBatch == null) {
359 // return notNullFrequencies;
360 // }
361 //
362 // // Synchronization status
363 // synchronizationStatusHelper.setDirty(catchBatch);
364 //
365 // // Retrieve parent
366 // SortingBatch parentBatch = batchHelper.getSortingBatchById(catchBatch, speciesBatchId);
367 //
368 // // Remember child ids, to remove unchanged item (see at bottom in this method)
369 // List<Integer> notUpdatedChildIds = Lists.newArrayList();
370 // List<SortingBatch> frequencyChilds = batchHelper.getFrequencyChilds(parentBatch);
371 // notUpdatedChildIds.addAll(frequencyChilds.stream().map(SortingBatch::getId).collect(Collectors.toList()));
372 //
373 // short rankOrder = 0;
374 // List<SortingBatch> batchsToUpdate = Lists.newArrayList();
375 // for (SpeciesBatchFrequency source : notNullFrequencies) {
376 // rankOrder++;
377 //
378 // SortingBatch target;
379 // if (source.getId() == null) {
380 //
381 // // Not existing batch
382 // target = SortingBatch.Factory.newInstance();
383 //
384 // // Fill the sorting batch from the source
385 // batchHelper.beanToEntity(source, target, parentBatch, rankOrder);
386 //
387 // // Create the targeted batch, then update the source id
388 // batchHelper.createSortingBatch(source, catchBatch, target);
389 //
390 // // push back id of created sortingBatch
391 // source.setId(target.getId());
392 //
393 // if (log.isDebugEnabled()) {
394 // log.debug("Create frequency sortingBatch(" + rankOrder + "): " + target.getId());
395 // }
396 // } else {
397 //
398 // // Existing batch
399 // target = batchHelper.loadSortingBatch(source.getIdAsInt(), catchBatch);
400 //
401 // // Fill the sorting batch from the source
402 // batchHelper.beanToEntity(source, target, parentBatch, rankOrder);
403 //
404 // // Add the batch into a list (will be update later, using this list)
405 // batchsToUpdate.add(target);
406 //
407 // // Remove id from id to remove
408 // notUpdatedChildIds.remove(target.getId());
409 //
410 // if (log.isDebugEnabled()) {
411 // log.debug("Update frequency sortingBatch(" + rankOrder + "): " + target.getId());
412 // }
413 // }
414 // }
415 //
416 // if (CollectionUtils.isNotEmpty(batchsToUpdate)) {
417 //
418 // // update some batchs
419 // batchHelper.updateSortingBatch(batchsToUpdate, catchBatch);
420 // }
421 //
422 // if (CollectionUtils.isNotEmpty(notUpdatedChildIds)) {
423 //
424 // // Remove obsolete frequencies
425 // for (Integer batchId : notUpdatedChildIds) {
426 //
427 // if (log.isDebugEnabled()) {
428 // log.debug("Remove obsolete frequency sortingBatch: " + batchId);
429 // }
430 // batchHelper.removeWithChildren(batchId, catchBatch);
431 // }
432 // }
433 //
434 // return Collections.unmodifiableList(notNullFrequencies);
435 }
436
437 //------------------------------------------------------------------------//
438 //-- Internal methods --//
439 //------------------------------------------------------------------------//
440
441 // protected void beanToEntity(SpeciesBatch source,
442 // SortingBatch target,
443 // Integer parentBatchId,
444 // CatchBatch catchBatch,
445 // boolean computeRankOrder) {
446 //
447 // Preconditions.checkNotNull(source.getFishingOperation());
448 // Preconditions.checkNotNull(source.getFishingOperation().getId());
449 //
450 // // If parent and root need to be set
451 // if (target.getId() == null
452 // || target.getRootBatch() == null
453 // || (target.getParentBatch() != null && !target.getParentBatch().getId().equals(parentBatchId))) {
454 //
455 // batchHelper.setSpeciesBatchParents(
456 // source.getSampleCategoryId(),
457 // source.getSampleCategoryValue(),
458 // target,
459 // parentBatchId,
460 // catchBatch
461 // );
462 // }
463 //
464 // batchHelper.beanToEntity(parentBatchId, source, target, computeRankOrder);
465 //
466 // }
467
468 }