View Javadoc
1   package fr.ifremer.tutti.persistence.service.util.tree;
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.base.Preconditions;
26  import fr.ifremer.adagio.core.dao.data.batch.CatchBatch;
27  import fr.ifremer.adagio.core.dao.data.batch.SortingBatch;
28  import fr.ifremer.adagio.core.dao.referential.pmfm.PmfmId;
29  import fr.ifremer.adagio.core.dao.referential.pmfm.QualitativeValueId;
30  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
31  import org.springframework.dao.DataIntegrityViolationException;
32  
33  import java.io.Serializable;
34  import java.text.MessageFormat;
35  
36  /**
37   * Helper to build or navigauet into the batch tree.
38   *
39   * Created on 4/20/14.
40   *
41   * @author Tony Chemit - chemit@codelutin.com
42   * @since 3.5
43   */
44  public abstract class SpeciesBatchTreeHelperSupport extends BatchTreeHelperSupport {
45  
46      private final String prefix;
47  
48      private final QualitativeValueId sortingType;
49  
50      private final short rootBatchRankOrder;
51  
52      protected SpeciesBatchTreeHelperSupport(String prefix, QualitativeValueId sortingType, short rootBatchRankOrder) {
53          this.prefix = prefix;
54          this.sortingType = sortingType;
55          this.rootBatchRankOrder = rootBatchRankOrder;
56      }
57  
58  
59      public void setBatchParents(Integer sampleCategoryId,
60                                  Serializable sampleCategoryValue,
61                                  SortingBatch target,
62                                  Integer parentBatchId,
63                                  CatchBatch catchBatch) {
64  
65          Preconditions.checkNotNull(target);
66          Preconditions.checkNotNull(catchBatch);
67  
68          target.setRootBatch(catchBatch);
69  
70          SortingBatch parentBatch;
71          if (parentBatchId != null) {
72  
73              // Load existing parent and root
74              parentBatch = catchBatchDao.getSortingBatchById(catchBatch, parentBatchId);
75          } else {
76  
77              // Or retrieve parent batch, from pmfm id
78              // Retrieve category type
79              if (!sampleCategoryId.equals(PmfmId.SORTED_UNSORTED.getValue())) {
80                  throw new DataIntegrityViolationException(MessageFormat.format(
81                          "A species or benthos batch with no parent should have a sampleCategoryType {0} (PMFM.ID={1})",
82                          PmfmId.SORTED_UNSORTED.getValue(),
83                          sampleCategoryId));
84              }
85  
86              Integer qualitativeValueId = convertSampleCategoryValueIntoQualitativeId(sampleCategoryValue);
87  
88              if (QualitativeValueId.SORTED_VRAC.getValue().equals(qualitativeValueId)) {
89  
90                  // -- Vrac > Species > Alive itemized
91                  parentBatch = getVracAliveItemizedRootBatch(catchBatch);
92  
93                  if (parentBatch == null) {
94  
95                      // -- Vrac
96                      SortingBatch vracBatch = getVracBatch(catchBatch);
97  
98                      if (vracBatch == null) {
99                          vracBatch = getOrCreateVracBatch(catchBatch, null, null);
100                     }
101 
102                     // -- Vrac > Species
103                     SortingBatch vracSpeciesBatch = getVracRootBatch(vracBatch);
104 
105                     if (vracSpeciesBatch == null) {
106                         vracSpeciesBatch = getOrCreateVracRootBatch(catchBatch, vracBatch, null);
107                     }
108 
109                     // -- Vrac > Species > Alive itemized
110                     parentBatch = getOrCreateVracAliveItemizedRootBatch(catchBatch, vracSpeciesBatch);
111 
112                 }
113             } else if (QualitativeValueId.SORTED_HORS_VRAC.getValue().equals(qualitativeValueId)) {
114 
115                 // -- Hors Vrac > Species
116                 parentBatch = getHorsVracRootBatch(catchBatch);
117 
118                 if (parentBatch == null) {
119 
120                     // -- Hors Vrac
121                     SortingBatch horsVracBatch = getOrCreateHorsVracBatch(catchBatch);
122 
123                     // -- Hors Vrac > Species
124                     parentBatch = getOrCreateHorsVracRootBatch(catchBatch, horsVracBatch);
125                 }
126             } else {
127 
128                 // not possible
129                 throw new DataIntegrityViolationException("Should have Vrac / Hors Vrac qualitative value, but had: " + qualitativeValueId);
130             }
131 
132         }
133 
134         Preconditions.checkNotNull(parentBatch);
135         target.setParentBatch(parentBatch);
136     }
137 
138     //------------------------------------------------------------------------//
139     //-- Get CatchBatch navigation methods                                  --//
140     //------------------------------------------------------------------------//
141 
142     public SortingBatch getVracAliveItemizedRootBatch(CatchBatch batch) {
143         return getSortingBatch(batch,
144                                "Vrac > " + prefix + " > Alive Itemized",
145                                PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_VRAC.getValue(),
146                                SORTING_TYPE_ID, sortingType.getValue(),
147                                SORTING_TYPE2_ID, QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue());
148     }
149 
150     public SortingBatch getHorsVracRootBatch(CatchBatch batch) {
151         return getSortingBatch(batch,
152                                "Hors Vrac > " + prefix,
153                                PmfmId.SORTED_UNSORTED.getValue(), QualitativeValueId.SORTED_HORS_VRAC.getValue(),
154                                SORTING_TYPE_ID, sortingType.getValue());
155     }
156 
157 //    public SortingBatch getVracBatch(CatchBatch batch) {
158 //        return getSortingBatch(
159 //                batch,
160 //                "Vrac",
161 //                PmfmId.SORTED_UNSORTED.getValue(),
162 //                QualitativeValueId.SORTED_VRAC.getValue()
163 //        );
164 //    }
165 //
166 //    public SortingBatch getHorsVracBatch(CatchBatch batch) {
167 //        return getSortingBatch(
168 //                batch,
169 //                "Hors Vrac",
170 //                PmfmId.SORTED_UNSORTED.getValue(),
171 //                QualitativeValueId.SORTED_HORS_VRAC.getValue()
172 //        );
173 //    }
174 
175 //    public SortingBatch getRejectedBatch(CatchBatch batch) {
176 //        return getSortingBatch(
177 //                batch,
178 //                "Unsorted",
179 //                PmfmId.SORTED_UNSORTED.getValue(),
180 //                QualitativeValueId.UNSORTED.getValue()
181 //        );
182 //    }
183 
184     //------------------------------------------------------------------------//
185     //-- Get SortingBatch navigation methods                                --//
186     //------------------------------------------------------------------------//
187 
188     public SortingBatch getVracRootBatch(SortingBatch batch) {
189         return getSortingBatch(batch,
190                                "Vrac > " + prefix,
191                                SORTING_TYPE_ID,
192                                sortingType.getValue());
193     }
194 
195     public SortingBatch getVracAliveNotItemizedRootBatch(SortingBatch batch) {
196         return getSortingBatch(batch,
197                                "Vrac > " + prefix + " > Alive not itemized",
198                                SORTING_TYPE2_ID,
199                                QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue());
200     }
201 
202 //    public SortingBatch getBenthosVracAliveNotItemizedRootBatch(SortingBatch batch) {
203 //        return getSortingBatch(
204 //                batch,
205 //                "Vrac > Benthos > Alive not itemized",
206 //                SORTING_TYPE2_ID,
207 //                QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue()
208 //        );
209 //    }
210 
211     public SortingBatch getVracInertRootBatch(SortingBatch batch) {
212         return getSortingBatch(batch,
213                                "Vrac > " + prefix + " > Inert (not alive)",
214                                SORTING_TYPE2_ID,
215                                QualitativeValueId.SORTING_TYPE2_INERT.getValue());
216     }
217 
218 //    public SortingBatch getBenthosVracInertRootBatch(SortingBatch batch) {
219 //        return getSortingBatch(
220 //                batch,
221 //                "Vrac > Benthos > Inert (not alive)",
222 //                SORTING_TYPE2_ID,
223 //                QualitativeValueId.SORTING_TYPE2_INERT.getValue()
224 //        );
225 //    }
226 
227     public SortingBatch getVracAliveItemizedRootBatch(SortingBatch batch) {
228         return getSortingBatch(batch,
229                                "Vrac > " + prefix + "> Alive itemized",
230                                SORTING_TYPE2_ID,
231                                QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue());
232     }
233 
234 //    public SortingBatch getBenthosVracAliveItemizedRootBatch(SortingBatch batch) {
235 //        return getSortingBatch(
236 //                batch,
237 //                "Vrac > Benthos > Alive itemized",
238 //                SORTING_TYPE2_ID,
239 //                QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue()
240 //        );
241 //    }
242 
243     public SortingBatch getHorsVracRootBatch(SortingBatch batch) {
244         return getSortingBatch(batch,
245                                "Hors Vrac > " + prefix,
246                                SORTING_TYPE_ID,
247                                QualitativeValueId.SORTING_TYPE_SPECIES.getValue());
248     }
249 
250 //    public SortingBatch getBenthosHorsVracRootBatch(SortingBatch batch) {
251 //        return getSortingBatch(
252 //                batch,
253 //                "Hors Vrac > Benthos",
254 //                SORTING_TYPE_ID,
255 //                QualitativeValueId.SORTING_TYPE_BENTHOS.getValue()
256 //        );
257 //    }
258 
259     //------------------------------------------------------------------------//
260     //-- getOrCreate methods                                                --//
261     //------------------------------------------------------------------------//
262 
263 //    public SortingBatch getOrCreateVracBatch(CatchBatch batch, Float weight, Float weightBeforeSampling) {
264 //        return getOrCreate(batch,
265 //                           batch,
266 //                           "Vrac",
267 //                           PmfmId.SORTED_UNSORTED.getValue(),
268 //                           QualitativeValueId.SORTED_VRAC.getValue(),
269 //                           weight,
270 //                           weightBeforeSampling,
271 //                           (short) 1);
272 //    }
273 
274     public SortingBatch getOrCreateVracRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) {
275         return getOrCreate(
276                 target,
277                 batch,
278                 "Vrac > " + prefix,
279                 SORTING_TYPE_ID,
280                 sortingType.getValue(),
281                 totalWeight,
282                 rootBatchRankOrder
283         );
284     }
285 
286     public SortingBatch getOrCreateVracAliveNotItemizedRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) {
287         return getOrCreate(
288                 target,
289                 batch,
290                 "Vrac > " + prefix + " > Alive Not Itemized",
291                 SORTING_TYPE2_ID,
292                 QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(),
293                 totalWeight,
294                 (short) 1
295         );
296     }
297 
298     public SortingBatch getOrCreateVracInertRootBatch(CatchBatch target, SortingBatch batch, Float totalWeight) {
299         return getOrCreate(
300                 target,
301                 batch,
302                 "Vrac > " + prefix + " > Inert",
303                 SORTING_TYPE2_ID,
304                 QualitativeValueId.SORTING_TYPE2_INERT.getValue(),
305                 totalWeight,
306                 (short) 2
307         );
308     }
309 
310     public SortingBatch getOrCreateVracAliveItemizedRootBatch(CatchBatch target, SortingBatch batch) {
311         return getOrCreate(
312                 target,
313                 batch,
314                 "Vrac > " + prefix + " > Alive Itemized",
315                 SORTING_TYPE2_ID,
316                 QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(),
317                 (short) 3
318         );
319     }
320 
321 //    public SortingBatch getOrCreateBenthosVracRootBatch(CatchBatch target,
322 //                                                        SortingBatch batch,
323 //                                                        Float totalWeight) {
324 //        return getOrCreate(
325 //                target,
326 //                batch,
327 //                "Vrac > Benthos",
328 //                SORTING_TYPE_ID,
329 //                QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(),
330 //                totalWeight,
331 //                (short) 2
332 //        );
333 //    }
334 
335 //    public SortingBatch getOrCreateBenthosVracAliveNotItemizedRootBatch(CatchBatch target,
336 //                                                                        SortingBatch batch,
337 //                                                                        Float totalWeight) {
338 //        return getOrCreate(
339 //                target,
340 //                batch,
341 //                "Vrac > Benthos > Alive Not itemized",
342 //                SORTING_TYPE2_ID,
343 //                QualitativeValueId.SORTING_TYPE2_ALIVE_NOT_ITEMIZED.getValue(),
344 //                totalWeight,
345 //                (short) 1
346 //        );
347 //    }
348 
349 //    public SortingBatch getOrCreateBenthosVracInertRootBatch(CatchBatch target,
350 //                                                             SortingBatch batch,
351 //                                                             Float totalWeight) {
352 //        return getOrCreate(
353 //                target,
354 //                batch,
355 //                "Vrac > Benthos > Inert",
356 //                SORTING_TYPE2_ID,
357 //                QualitativeValueId.SORTING_TYPE2_INERT.getValue(),
358 //                totalWeight,
359 //                (short) 2
360 //        );
361 //    }
362 
363 //    public SortingBatch getOrCreateBenthosVracAliveItemizedRootBatch(CatchBatch target,
364 //                                                                     SortingBatch batch) {
365 //        return getOrCreate(
366 //                target,
367 //                batch,
368 //                "Vrac > Benthos > Alive Itemized",
369 //                SORTING_TYPE2_ID,
370 //                QualitativeValueId.SORTING_TYPE2_ALIVE_ITEMIZED.getValue(),
371 //                (short) 3
372 //        );
373 //    }
374 
375 //    public SortingBatch getOrCreateHorsVracBatch(CatchBatch batch) {
376 //        return getOrCreate(
377 //                batch,
378 //                batch,
379 //                "Hors Vrac",
380 //                PmfmId.SORTED_UNSORTED.getValue(),
381 //                QualitativeValueId.SORTED_HORS_VRAC.getValue(),
382 //                (short) 2
383 //        );
384 //    }
385 
386     public SortingBatch getOrCreateHorsVracRootBatch(CatchBatch target, SortingBatch batch) {
387         return getOrCreate(
388                 target,
389                 batch,
390                 "Hors Vrac > " + prefix,
391                 SORTING_TYPE_ID,
392                 sortingType.getValue(),
393                 rootBatchRankOrder
394         );
395     }
396 
397 //    public SortingBatch getOrCreateBenthosHorsVracRootBatch(CatchBatch target,
398 //                                                            SortingBatch batch) {
399 //        return getOrCreate(
400 //                target,
401 //                batch,
402 //                "Hors Vrac > Benthos",
403 //                SORTING_TYPE_ID,
404 //                QualitativeValueId.SORTING_TYPE_BENTHOS.getValue(),
405 //                (short) 2
406 //        );
407 //    }
408 
409 
410     private Integer convertSampleCategoryValueIntoQualitativeId(Serializable value) {
411         if (value == null) {
412             return null;
413         }
414         Integer qualitativeValueId = null;
415         if (value instanceof CaracteristicQualitativeValue) {
416             CaracteristicQualitativeValue cqValue = (CaracteristicQualitativeValue) value;
417             qualitativeValueId = cqValue.getIdAsInt();
418         } else if (value instanceof String) {
419             qualitativeValueId = Integer.valueOf((String) value);
420         }
421         return qualitativeValueId;
422     }
423 
424 }