1 package fr.ifremer.tutti.service.genericformat.importactions;
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.adagio.core.dao.referential.ObjectTypeCode;
28 import fr.ifremer.tutti.persistence.ProgressionModel;
29 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
30 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatch;
31 import fr.ifremer.tutti.persistence.entities.data.SpeciesBatchFrequency;
32 import fr.ifremer.tutti.service.genericformat.GenericFormatCsvFileResult;
33 import fr.ifremer.tutti.service.genericformat.GenericFormatImportContext;
34 import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
35 import fr.ifremer.tutti.service.genericformat.GenericFormatImportOperationContext;
36 import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
37 import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForCatch;
38 import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
39 import fr.ifremer.tutti.service.genericformat.csv.CatchRow;
40 import org.apache.commons.collections4.CollectionUtils;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.nuiton.csv.ImportRow;
44 import org.nuiton.csv.ImportRuntimeException;
45 import org.nuiton.jaxx.application.ApplicationTechnicalException;
46
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.List;
50
51 import static org.nuiton.i18n.I18n.t;
52
53
54
55
56
57
58
59 public class ImportCatchAction extends ImportActionSupport {
60
61
62 private static final Log log = LogFactory.getLog(ImportCatchAction.class);
63
64 private final GenericformatImportPersistenceHelper persistenceHelper;
65
66 public ImportCatchAction(GenericFormatImportContext importContext, GenericformatImportPersistenceHelper persistenceHelper) {
67 super(importContext);
68 this.persistenceHelper = persistenceHelper;
69 }
70
71 @Override
72 protected boolean canExecute() {
73 return importContext.isTechnicalFilesValid()
74 && importContext.getSurveyFileResult().isValid()
75 && importContext.getOperationFileResult().isValid()
76 && (importContext.getImportRequest().isImportSpecies() ||
77 importContext.getImportRequest().isImportBenthos());
78 }
79
80 @Override
81 protected void skipExecute() {
82
83 importContext.increments(t("tutti.service.genericFormat.skip.import.catches"));
84
85 if (!(importContext.getImportRequest().isImportSpecies() ||
86 importContext.getImportRequest().isImportBenthos())) {
87 GenericFormatCsvFileResult importFileResult = importContext.getCatchFileResult();
88 importFileResult.setSkipped(true);
89 }
90
91 }
92
93 @Override
94 protected void doExecute() {
95
96 if (log.isInfoEnabled()) {
97 log.info("Import catch.csv file.");
98 }
99 importContext.increments(t("tutti.service.genericFormat.import.catches"));
100 GenericFormatCsvFileResult importFileResult = importContext.getCatchFileResult();
101 try (CsvConsumerForCatch consumer = importContext.loadCatches(true)) {
102 for (ImportRow<CatchRow> row : consumer) {
103
104 GenericFormatImportOperationContext operationContext = consumer.validateRow(row, importContext);
105 if (operationContext != null) {
106 consumer.prepareRowForPersist(operationContext, row);
107 }
108
109 }
110 } catch (IOException e) {
111 throw new ApplicationTechnicalException("Could not close catch.csv file", e);
112 } catch (ImportRuntimeException e) {
113
114 importFileResult.addGlobalError(e.getMessage());
115
116 }
117
118 if (importContext.getImportRequest().isImportSpecies()) {
119 persistSpeciesBatches();
120 }
121
122 if (importContext.getImportRequest().isImportBenthos()) {
123 persistBenthosBatches();
124 }
125
126 }
127
128 public void persistSpeciesBatches() {
129
130 importContext.doActionOnCruiseContexts(new GenericFormatImportContext.CruiseContextAction() {
131
132 @Override
133 public void onCruise(GenericFormatImportCruiseContext cruiseContext, ProgressionModel progressionModel) {
134
135 for (GenericFormatImportOperationContext fishingOperationContext : cruiseContext) {
136
137
138
139
140
141
142
143
144
145 deleteSpeciesBatches(fishingOperationContext);
146
147 String cruiseStr = cruiseContext.getCruiseLabel();
148 String operationStr = fishingOperationContext.getFishingOperationLabel();
149
150 importContext.increments(t("tutti.service.genericFormat.persist.operation.speciesBatches", cruiseStr, operationStr));
151
152 if (fishingOperationContext.withSpeciesBatches(true)) {
153
154 Collection<SpeciesBatch> batches = fishingOperationContext.getSpeciesBatches(true);
155 if (log.isInfoEnabled()) {
156 log.info("Persist " + batches.size() + " VRAC root species batch(es) of " + operationStr + " for cruise: " + cruiseStr);
157 }
158 persistSpeciesBatches(fishingOperationContext, batches, null);
159
160 }
161
162 if (fishingOperationContext.withSpeciesBatches(false)) {
163
164 Collection<SpeciesBatch> batches = fishingOperationContext.getSpeciesBatches(false);
165 if (log.isInfoEnabled()) {
166 log.info("Persist " + batches.size() + " HORS VRAC root species batch(es) of " + operationStr + " for cruise: " + cruiseStr);
167 }
168 persistSpeciesBatches(fishingOperationContext, batches, null);
169
170 }
171
172 }
173
174 }
175
176 private void persistSpeciesBatches(GenericFormatImportOperationContext fishingOperationContext, Collection<SpeciesBatch> batches, Integer parentId) {
177
178 int rankOrder = 1;
179
180 for (SpeciesBatch batch : batches) {
181
182 batch.setRankOrder(rankOrder++);
183
184 Integer batchObjectId = fishingOperationContext.getBatchObjectId(batch.getIdAsInt());
185
186 List<SpeciesBatchFrequency> frequencies = fishingOperationContext.getSpeciesFrequencies(batch);
187
188
189 batch.setId((Integer) null);
190 SpeciesBatch createdBatch = persistenceHelper.createSpeciesBatch(batch, parentId);
191 Collection<AttachmentRow> attachmentRows = importContext.popAttachmentRows(ObjectTypeCode.BATCH, batchObjectId);
192 persistenceHelper.persistAttachments(createdBatch.getIdAsInt(), attachmentRows);
193
194
195 fishingOperationContext.registerPersistedSpeciesBatchId(batchObjectId, createdBatch.getIdAsInt());
196
197 if (CollectionUtils.isNotEmpty(frequencies)) {
198
199 Integer batchId = batch.getIdAsInt();
200 if (log.isInfoEnabled()) {
201 log.info("Persist " + frequencies.size() + " frequency(ies) (species batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + importContext.decorate(fishingOperationContext.getFishingOperation().getCruise()));
202 }
203 persistenceHelper.saveSpeciesBatchFrequency(batchId, frequencies);
204
205 }
206
207 if (!batch.isChildBatchsEmpty()) {
208
209 persistSpeciesBatches(fishingOperationContext, batch.getChildBatchs(), createdBatch.getIdAsInt());
210
211 }
212
213 }
214
215 }
216
217 private void deleteSpeciesBatches(GenericFormatImportOperationContext fishingOperationContext) {
218
219 FishingOperation fishingOperation = fishingOperationContext.getFishingOperation();
220 persistenceHelper.deleteSpeciesBatchForFishingOperation(fishingOperation.getIdAsInt());
221
222 }
223
224 });
225
226
227 }
228
229 public void persistBenthosBatches() {
230
231 importContext.doActionOnCruiseContexts(new GenericFormatImportContext.CruiseContextAction() {
232
233 @Override
234 public void onCruise(GenericFormatImportCruiseContext cruiseContext, ProgressionModel progressionModel) {
235
236 for (GenericFormatImportOperationContext fishingOperationContext : cruiseContext) {
237
238
239
240
241
242
243
244
245
246 deleteBenthosBatches(fishingOperationContext);
247
248 String cruiseStr = cruiseContext.getCruiseLabel();
249 String operationStr = fishingOperationContext.getFishingOperationLabel();
250
251 importContext.increments(t("tutti.service.genericFormat.persist.operation.benthosBatches", cruiseStr, operationStr));
252
253 if (fishingOperationContext.withBenthosBatches(true)) {
254
255 Collection<SpeciesBatch> batches = fishingOperationContext.getBenthosBatches(true);
256 if (log.isInfoEnabled()) {
257 log.info("Persist " + batches.size() + " VRAC benthos batch(es) of " + operationStr + " for cruise: " + cruiseStr);
258 }
259 persistBenthosBatches(fishingOperationContext, batches, null);
260
261 }
262
263 if (fishingOperationContext.withBenthosBatches(false)) {
264
265 Collection<SpeciesBatch> batches = fishingOperationContext.getBenthosBatches(false);
266 if (log.isInfoEnabled()) {
267 log.info("Persist " + batches.size() + " HORS VRAC benthos batch(es) of " + operationStr + " for cruise: " + cruiseContext.getCruiseLabel());
268 }
269 persistBenthosBatches(fishingOperationContext, batches, null);
270
271 }
272
273 }
274 }
275
276 private void persistBenthosBatches(GenericFormatImportOperationContext fishingOperationContext, Collection<SpeciesBatch> batches, Integer parentId) {
277
278 int rankOrder = 1;
279
280 for (SpeciesBatch batch : batches) {
281
282 batch.setRankOrder(rankOrder++);
283
284 Integer batchObjectId = fishingOperationContext.getBatchObjectId(batch.getIdAsInt());
285
286 List<SpeciesBatchFrequency> frequencies = fishingOperationContext.getBenthosFrequencies(batch);
287
288
289 batch.setId((Integer) null);
290
291 SpeciesBatch createdBatch = persistenceHelper.createBenthosBatch(batch, parentId);
292
293 Collection<AttachmentRow> attachmentRows = importContext.popAttachmentRows(ObjectTypeCode.BATCH, batchObjectId);
294 persistenceHelper.persistAttachments(createdBatch.getIdAsInt(), attachmentRows);
295
296
297 fishingOperationContext.registerPersistedSpeciesBatchId(batchObjectId, createdBatch.getIdAsInt());
298
299 if (CollectionUtils.isNotEmpty(frequencies)) {
300
301 Integer batchId = batch.getIdAsInt();
302 if (log.isInfoEnabled()) {
303 log.info("Persist " + frequencies.size() + " frequency(ies) (benthos batch: " + batchId + ") of " + fishingOperationContext.getFishingOperationLabel() + " for cruise: " + importContext.decorate(fishingOperationContext.getFishingOperation().getCruise()));
304 }
305 persistenceHelper.saveBenthosBatchFrequency(batchId, frequencies);
306
307 }
308
309 if (!batch.isChildBatchsEmpty()) {
310
311 persistBenthosBatches(fishingOperationContext, batch.getChildBatchs(), createdBatch.getIdAsInt());
312
313 }
314 }
315
316 }
317
318 private void deleteBenthosBatches(GenericFormatImportOperationContext fishingOperationContext) {
319
320 FishingOperation fishingOperation = fishingOperationContext.getFishingOperation();
321 persistenceHelper.deleteBenthosBatchForFishingOperation(fishingOperation.getIdAsInt());
322
323 }
324
325 });
326
327 }
328
329 }