1 package fr.ifremer.tutti.persistence.entities.data;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import com.google.common.base.Function;
28 import com.google.common.base.Predicate;
29 import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
30 import fr.ifremer.tutti.persistence.entities.referential.Species;
31
32 import java.io.Serializable;
33 import java.util.Collection;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.TreeMap;
37
38
39
40
41 public class SpeciesBatchs extends AbstractSpeciesBatchs {
42
43 public static final Function<SpeciesBatch, Serializable> GET_SAMPLE_CATEGORY_VALUE = SpeciesBatch::getSampleCategoryValue;
44
45 public static SpeciesBatch newInstance(SpeciesBatch parent) {
46 SpeciesBatch speciesBatch = newSpeciesBatch();
47 speciesBatch.setBenthosBatch(parent.isBenthosBatch());
48 return speciesBatch;
49 }
50
51 public static Map<Integer, SpeciesBatch> getAllSpeciesBatchesById(BatchContainer<SpeciesBatch> rootSpeciesBatch) {
52 Map<Integer, SpeciesBatch> result = new TreeMap<>();
53
54 for (SpeciesBatch speciesBatch : rootSpeciesBatch.getChildren()) {
55 getAllspeciesBatchesById(speciesBatch, result);
56 }
57 return result;
58 }
59
60 private static void getAllspeciesBatchesById(SpeciesBatch speciesBatch, Map<Integer, SpeciesBatch> result) {
61 result.put(speciesBatch.getIdAsInt(), speciesBatch);
62 if (!speciesBatch.isChildBatchsEmpty()) {
63 for (SpeciesBatch batch : speciesBatch.getChildBatchs()) {
64 getAllspeciesBatchesById(batch, result);
65 }
66 }
67 }
68
69 public static Predicate<SpeciesBatch> newSpeciesAbleBatchCategoryPredicate(Integer cateogryId, Integer value) {
70 return new SpeciesAbleBatchCategoryPredicate(cateogryId, value);
71 }
72
73 public static SpeciesBatch createNewChild(SpeciesBatch parent) {
74
75 SpeciesBatch child = newInstance(parent);
76 child.setFishingOperation(parent.getFishingOperation());
77 child.setSpecies(parent.getSpecies());
78 child.setParentBatch(parent);
79 parent.addChildBatchs(child);
80 return child;
81
82 }
83
84 public static SpeciesBatch newBenthosBatch() {
85 SpeciesBatch speciesBatch = newSpeciesBatch();
86 speciesBatch.setBenthosBatch(true);
87 return speciesBatch;
88 }
89
90 public static void grabSpeciesChildBatchs(Collection<SpeciesBatch> childs, Set<Species> speciesSet) {
91
92 for (SpeciesBatch child : childs) {
93 speciesSet.add(child.getSpecies());
94 }
95
96 }
97
98 public static void grabSampleCategorieValuesChildBatchs(SpeciesBatch batch, Set<Integer> categoryIds) {
99
100 CaracteristicQualitativeValue sampleCategoryValue = (CaracteristicQualitativeValue) batch.getSampleCategoryValue();
101
102 categoryIds.add(sampleCategoryValue.getIdAsInt());
103 if (!batch.isChildBatchsEmpty()) {
104 for (SpeciesBatch child : batch.getChildBatchs()) {
105 grabSampleCategorieValuesChildBatchs(child, categoryIds);
106 }
107 }
108
109
110 }
111
112 public static class SpeciesAbleBatchCategoryPredicate implements Predicate<SpeciesBatch> {
113
114 private final Integer id;
115
116 private final Integer qualitativeValue;
117
118 public SpeciesAbleBatchCategoryPredicate(Integer id, Integer qualitativeValue) {
119 this.id = id;
120 this.qualitativeValue = qualitativeValue;
121 }
122
123 @Override
124 public boolean apply(SpeciesBatch input) {
125 return id.equals(input.getSampleCategoryId()) &&
126 input.getSampleCategoryValue() instanceof CaracteristicQualitativeValue &&
127 qualitativeValue.equals(((CaracteristicQualitativeValue) input.getSampleCategoryValue()).getIdAsInt());
128 }
129
130 }
131 }