1 package fr.ifremer.tutti.service.genericformat;
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.collect.ArrayListMultimap;
29 import com.google.common.collect.ImmutableList;
30 import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
31 import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
32 import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
33 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
34 import fr.ifremer.tutti.persistence.entities.data.IndividualObservationBatch;
35 import fr.ifremer.tutti.persistence.entities.data.MarineLitterBatch;
36 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
37 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
38 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchs;
39 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
40 import fr.ifremer.tutti.persistence.entities.referential.Species;
41 import fr.ifremer.tutti.persistence.model.OperationDataModel;
42 import org.apache.commons.collections4.CollectionUtils;
43 import org.apache.commons.collections4.MapUtils;
44
45 import java.io.Closeable;
46 import java.io.Serializable;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashSet;
50 import java.util.LinkedHashSet;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.Set;
54 import java.util.TreeMap;
55
56
57
58
59
60
61
62 public class GenericFormatImportOperationContext implements Closeable {
63
64 public static final Function<GenericFormatImportOperationContext, FishingOperation> TO_FISHING_OPERATION_FUNCTION = GenericFormatImportOperationContext::getFishingOperation;
65
66 private final FishingOperation fishingOperation;
67
68 private final CatchBatch catchBatch;
69
70 private final OperationDataModel existingFishingOperationData;
71
72 private final String fishingOperationLabel;
73
74 private final Collection<MarineLitterBatch> marineLitterBatches;
75
76 private final Map<Integer, AccidentalBatch> accidentalBatchesById;
77
78 private final Map<Integer, IndividualObservationBatch> individualObservationBatchesById;
79
80 private final Map<Integer, SpeciesBatch> vracSpeciesBatches;
81
82 private final Map<Integer, SpeciesBatch> horsVracSpeciesBatches;
83
84 private final ArrayListMultimap<Integer, SpeciesBatchFrequency> speciesFrequencies;
85
86 private final Map<Integer, SpeciesBatch> vracBenthosBatches;
87
88 private final Map<Integer, SpeciesBatch> horsVracBenthosBatches;
89
90
91
92
93 private final Map<Integer, Integer> batchesObjectIds;
94
95
96
97
98 private final Map<Integer, Integer> speciesBatchIds;
99
100 private final ArrayListMultimap<Integer, SpeciesBatchFrequency> benthosFrequencies;
101
102 private final CaracteristicMap gearUseFeatures;
103
104 private final CaracteristicMap vesselUseFeatures;
105
106 private final Set<String> checkErrors;
107
108 private boolean weightsDeleted;
109
110 public GenericFormatImportOperationContext(FishingOperation fishingOperation,
111 CatchBatch catchBatch,
112 OperationDataModel existingFishingOperationData,
113 String fishingOperationLabel) {
114
115 this.fishingOperation = fishingOperation;
116 this.catchBatch = catchBatch;
117 this.existingFishingOperationData = existingFishingOperationData;
118 this.fishingOperationLabel = fishingOperationLabel;
119 this.marineLitterBatches = new ArrayList<>();
120 this.accidentalBatchesById = new TreeMap<>();
121 this.individualObservationBatchesById = new TreeMap<>();
122 this.vracSpeciesBatches = new TreeMap<>();
123 this.horsVracSpeciesBatches = new TreeMap<>();
124 this.speciesFrequencies = ArrayListMultimap.create();
125 this.vracBenthosBatches = new TreeMap<>();
126 this.horsVracBenthosBatches = new TreeMap<>();
127 this.benthosFrequencies = ArrayListMultimap.create();
128 this.gearUseFeatures = new CaracteristicMap();
129 this.vesselUseFeatures = new CaracteristicMap();
130 this.checkErrors = new LinkedHashSet<>();
131 this.batchesObjectIds = new TreeMap<>();
132 this.speciesBatchIds = new TreeMap<>();
133
134 }
135
136 public boolean isOverride() {
137 return existingFishingOperationData != null;
138 }
139
140 public OperationDataModel getExistingFishingOperationData() {
141 return existingFishingOperationData;
142 }
143
144 public FishingOperation getFishingOperation() {
145 return fishingOperation;
146 }
147
148 public CatchBatch getCatchBatch() {
149 return catchBatch;
150 }
151
152 public String getFishingOperationLabel() {
153 return fishingOperationLabel;
154 }
155
156 public boolean withGearFeatures() {
157 return MapUtils.isNotEmpty(gearUseFeatures);
158 }
159
160 public boolean withVesselFeatures() {
161 return MapUtils.isNotEmpty(vesselUseFeatures);
162 }
163
164 public boolean withMarineLitterBatches() {
165 return CollectionUtils.isNotEmpty(marineLitterBatches);
166 }
167
168 public boolean withAccidentalBatches() {
169 return MapUtils.isNotEmpty(accidentalBatchesById);
170 }
171
172 public boolean withIndividualObservationBatches() {
173 return MapUtils.isNotEmpty(individualObservationBatchesById);
174 }
175
176 public boolean withSpeciesBatches(boolean vrac) {
177 return MapUtils.isNotEmpty(getSpeciesBatchMap(vrac));
178 }
179
180 public boolean withBenthosBatches(boolean vrac) {
181 return MapUtils.isNotEmpty(getBenthosBatchMap(vrac));
182 }
183
184 public AccidentalBatch getAccidentalBatchById(Integer accidentalBatchId) {
185 return accidentalBatchesById.get(accidentalBatchId);
186 }
187
188 public IndividualObservationBatch getIndividualObservationBatchById(Integer individualObservationBatchId) {
189 return individualObservationBatchesById.get(individualObservationBatchId);
190 }
191
192 public SpeciesBatch getSpeciesBatch(boolean vrac, Integer referenceTaxonId) {
193 return getSpeciesBatchMap(vrac).get(referenceTaxonId);
194 }
195
196 public SpeciesBatch getBenthosBatch(boolean vrac, Integer referenceTaxonId) {
197 return getBenthosBatchMap(vrac).get(referenceTaxonId);
198 }
199
200 public void addGearUseFeature(Caracteristic caracteristic, Serializable value) {
201 gearUseFeatures.put(caracteristic, value);
202 }
203
204 public void addVesselUseFeature(Caracteristic caracteristic, Serializable value) {
205 vesselUseFeatures.put(caracteristic, value);
206 }
207
208 public void addMarineLitterBatch(MarineLitterBatch marineLitterBatch) {
209 marineLitterBatches.add(marineLitterBatch);
210 }
211
212 public void addAccidentalBatch(Integer batchId, AccidentalBatch accidentalBatch) {
213 accidentalBatchesById.put(batchId, accidentalBatch);
214 }
215
216 public void addIndividualObservationBatch(Integer batchId, IndividualObservationBatch individualObservationBatch) {
217 individualObservationBatchesById.put(batchId, individualObservationBatch);
218 }
219
220 public void addSpeciesBatch(boolean vrac, SpeciesBatch speciesBatch) {
221 getSpeciesBatchMap(vrac).put(speciesBatch.getSpecies().getReferenceTaxonId(), speciesBatch);
222 }
223
224 public void addBenthosBatch(boolean vrac, SpeciesBatch benthosBatch) {
225 getBenthosBatchMap(vrac).put(benthosBatch.getSpecies().getReferenceTaxonId(), benthosBatch);
226 }
227
228 public void addSpeciesFrequency(SpeciesBatch batch, SpeciesBatchFrequency frequency) {
229 speciesFrequencies.put(batch.getIdAsInt(), frequency);
230 }
231
232 public void addBenthosFrequency(SpeciesBatch batch, SpeciesBatchFrequency frequency) {
233 benthosFrequencies.put(batch.getIdAsInt(), frequency);
234 }
235
236 public void addCheckErrors(Set<String> checkErrors) {
237 this.checkErrors.addAll(checkErrors);
238 }
239
240 public Collection<MarineLitterBatch> getMarineLitterBatches() {
241 return ImmutableList.copyOf(marineLitterBatches);
242 }
243
244 public Collection<AccidentalBatch> getAccidentalBatches() {
245 return ImmutableList.copyOf(accidentalBatchesById.values());
246 }
247
248 public ImmutableList<IndividualObservationBatch> getIndividualObservationBatches() {
249 return ImmutableList.copyOf(individualObservationBatchesById.values());
250 }
251
252 public int getNbSpeciesTaxon() {
253 Set<Species> speciesSet = new HashSet<>();
254 SpeciesBatchs.grabSpeciesChildBatchs(vracSpeciesBatches.values(), speciesSet);
255 SpeciesBatchs.grabSpeciesChildBatchs(horsVracSpeciesBatches.values(), speciesSet);
256 return speciesSet.size();
257 }
258
259 public int getNbBenthosTaxon() {
260 Set<Species> speciesSet = new HashSet<>();
261 SpeciesBatchs.grabSpeciesChildBatchs(vracBenthosBatches.values(), speciesSet);
262 SpeciesBatchs.grabSpeciesChildBatchs(horsVracBenthosBatches.values(), speciesSet);
263 return speciesSet.size();
264 }
265
266 public Collection<SpeciesBatch> getSpeciesBatches(boolean vrac) {
267 return ImmutableList.copyOf(getSpeciesBatchMap(vrac).values());
268 }
269
270 public Collection<SpeciesBatch> getBenthosBatches(boolean vrac) {
271 return ImmutableList.copyOf(getBenthosBatchMap(vrac).values());
272 }
273
274 public List<SpeciesBatchFrequency> getBenthosFrequencies(SpeciesBatch benthosBatch) {
275 return benthosFrequencies.get(benthosBatch.getIdAsInt());
276 }
277
278 public List<SpeciesBatchFrequency> getSpeciesFrequencies(SpeciesBatch speciesBatch) {
279 return speciesFrequencies.get(speciesBatch.getIdAsInt());
280 }
281
282 public CaracteristicMap getGearUseFeatures() {
283 return gearUseFeatures;
284 }
285
286 public CaracteristicMap getVesselUseFeatures() {
287 return vesselUseFeatures;
288 }
289
290 private Map<Integer, SpeciesBatch> getSpeciesBatchMap(boolean vrac) {
291 return vrac ? vracSpeciesBatches : horsVracSpeciesBatches;
292 }
293
294 private Map<Integer, SpeciesBatch> getBenthosBatchMap(boolean vrac) {
295 return vrac ? vracBenthosBatches : horsVracBenthosBatches;
296 }
297
298 public Set<String> getCheckErrors() {
299 return checkErrors;
300 }
301
302 @Override
303 public void close() {
304
305 gearUseFeatures.clear();
306 vesselUseFeatures.clear();
307 marineLitterBatches.clear();
308 individualObservationBatchesById.clear();
309 accidentalBatchesById.clear();
310 benthosFrequencies.clear();
311 vracBenthosBatches.clear();
312 horsVracBenthosBatches.clear();
313 speciesFrequencies.clear();
314 vracSpeciesBatches.clear();
315 horsVracSpeciesBatches.clear();
316 batchesObjectIds.clear();
317 speciesBatchIds.clear();
318
319 }
320
321 public void registerBatchObjectId(Integer batchId, Integer objectId) {
322 if (objectId != null) {
323 batchesObjectIds.put(batchId, objectId);
324 }
325 }
326
327 public Integer getBatchObjectId(Integer batchId) {
328 return batchesObjectIds.get(batchId);
329 }
330
331
332 public void registerPersistedSpeciesBatchId(Integer importBatchId, Integer persistBatchId) {
333 speciesBatchIds.put(importBatchId, persistBatchId);
334 }
335
336 public Integer getSpeciesBatchId(Integer importBatchId) {
337 return speciesBatchIds.get(importBatchId);
338 }
339
340 public void setWeightsDeleted(boolean weightsDeleted) {
341 this.weightsDeleted = weightsDeleted;
342 }
343
344 public boolean isWeightsDeleted() {
345 return weightsDeleted;
346 }
347 }
348