1 package fr.ifremer.tutti.service.genericformat.producer;
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 fr.ifremer.tutti.persistence.entities.CaracteristicMap;
28 import fr.ifremer.tutti.persistence.entities.data.AccidentalBatch;
29 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
30 import fr.ifremer.tutti.service.csv.CsvProducer;
31 import fr.ifremer.tutti.service.genericformat.GenericFormatExportOperationContext;
32 import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchModel;
33 import fr.ifremer.tutti.service.genericformat.csv.AccidentalCatchRow;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.apache.commons.collections4.MapUtils;
36
37 import java.io.Serializable;
38 import java.nio.file.Path;
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Map;
42
43
44
45
46
47
48
49 public class CsvProducerForAccidentalCatch extends CsvProducer<AccidentalCatchRow, AccidentalCatchModel> {
50
51 public CsvProducerForAccidentalCatch(Path file, AccidentalCatchModel model) {
52 super(file, model);
53 }
54
55 public List<AccidentalCatchRow> getDataToExport(GenericFormatExportOperationContext operationExportContext) {
56
57 List<AccidentalCatchRow> rows = new ArrayList<>();
58
59 List<AccidentalBatch> accidentalBatches = operationExportContext.getAccidentalBatches();
60
61 if (CollectionUtils.isNotEmpty(accidentalBatches)) {
62 for (AccidentalBatch accidentalBatch : accidentalBatches) {
63
64 addAccidentalBatch(operationExportContext, rows, accidentalBatch);
65
66 }
67
68 }
69
70 return rows;
71
72 }
73
74 protected void addAccidentalBatch(GenericFormatExportOperationContext operationExportContext, List<AccidentalCatchRow> rows, AccidentalBatch accidentalBatch) {
75
76 addCaracteristicRow(operationExportContext,
77 rows,
78 accidentalBatch,
79 operationExportContext.getDeadOrAliveCaracteristic(),
80 accidentalBatch.getDeadOrAlive());
81
82 addCaracteristicRow(operationExportContext,
83 rows,
84 accidentalBatch,
85 operationExportContext.getGenderCaracteristic(),
86 accidentalBatch.getGender());
87
88 addCaracteristicRow(operationExportContext,
89 rows,
90 accidentalBatch,
91 operationExportContext.getWeightMeasuredCaracteristic(),
92 accidentalBatch.getWeight());
93
94 if (accidentalBatch.getLengthStepCaracteristic() != null) {
95 addCaracteristicRow(operationExportContext,
96 rows,
97 accidentalBatch,
98 operationExportContext.getPmfmIdCaracteristic(),
99 accidentalBatch.getLengthStepCaracteristic().getIdAsInt());
100
101 addCaracteristicRow(operationExportContext,
102 rows,
103 accidentalBatch,
104 accidentalBatch.getLengthStepCaracteristic(),
105 accidentalBatch.getSize());
106 }
107
108 CaracteristicMap caracteristics = accidentalBatch.getCaracteristics();
109 if (MapUtils.isNotEmpty(caracteristics)) {
110 for (Map.Entry<Caracteristic, Serializable> entry : caracteristics.entrySet()) {
111 addCaracteristicRow(operationExportContext,
112 rows,
113 accidentalBatch,
114 entry.getKey(),
115 entry.getValue());
116 }
117 }
118
119 }
120
121 protected void addCaracteristicRow(GenericFormatExportOperationContext operationExportContext,
122 List<AccidentalCatchRow> rows,
123 AccidentalBatch accidentalBatch,
124 Caracteristic caracteristic,
125 Serializable caracteristicValue) {
126 if (caracteristicValue != null) {
127
128 AccidentalCatchRow row = new AccidentalCatchRow();
129 row.setCruise(operationExportContext.getCruise());
130 row.setFishingOperation(operationExportContext.getOperation());
131
132 row.setComment(accidentalBatch.getComment());
133 row.setSpecies(accidentalBatch.getSpecies());
134 row.setBatchId(accidentalBatch.getIdAsInt());
135
136 row.setCaracteristic(caracteristic);
137 row.setCaracteristicValue(caracteristicValue);
138 rows.add(row);
139
140 }
141 }
142
143 }