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 com.google.common.base.Preconditions;
28 import fr.ifremer.adagio.core.dao.referential.ObjectTypeCode;
29 import fr.ifremer.tutti.persistence.entities.TuttiEntities;
30 import fr.ifremer.tutti.persistence.entities.data.CatchBatch;
31 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
32 import fr.ifremer.tutti.persistence.model.OperationDataModel;
33 import fr.ifremer.tutti.service.genericformat.GenericFormatImportContext;
34 import fr.ifremer.tutti.service.genericformat.GenericFormatImportCruiseContext;
35 import fr.ifremer.tutti.service.genericformat.GenericformatImportPersistenceHelper;
36 import fr.ifremer.tutti.service.genericformat.consumer.CsvConsumerForOperation;
37 import fr.ifremer.tutti.service.genericformat.csv.AttachmentRow;
38 import fr.ifremer.tutti.service.genericformat.csv.OperationRow;
39 import org.apache.commons.lang3.tuple.Pair;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.nuiton.csv.ImportRow;
43 import org.nuiton.csv.ImportRuntimeException;
44 import org.nuiton.jaxx.application.ApplicationTechnicalException;
45
46 import java.io.IOException;
47 import java.util.Collection;
48
49 import static org.nuiton.i18n.I18n.t;
50
51
52
53
54
55
56
57 public class ImportOperationAction extends ImportActionSupport {
58
59
60 private static final Log log = LogFactory.getLog(ImportOperationAction.class);
61
62 private final GenericformatImportPersistenceHelper persistenceHelper;
63
64 public ImportOperationAction(GenericFormatImportContext importContext, GenericformatImportPersistenceHelper persistenceHelper) {
65 super(importContext);
66 this.persistenceHelper = persistenceHelper;
67 }
68
69 @Override
70 protected boolean canExecute() {
71 return importContext.isTechnicalFilesValid() && importContext.getSurveyFileResult().isValid();
72 }
73
74 @Override
75 protected void skipExecute() {
76 importContext.increments(t("tutti.service.genericFormat.skip.import.operations"));
77 }
78
79 @Override
80 protected void doExecute() {
81
82 if (log.isInfoEnabled()) {
83 log.info("Import operation.csv file.");
84 }
85 importContext.increments(t("tutti.service.genericFormat.import.operations"));
86
87 boolean importSpecies = importContext.getImportRequest().isImportSpecies();
88 boolean importBenthos = importContext.getImportRequest().isImportBenthos();
89 boolean importMarineLitter = importContext.getImportRequest().isImportMarineLitter();
90
91 try (CsvConsumerForOperation consumer = importContext.loadOperations(true)) {
92 for (ImportRow<OperationRow> row : consumer) {
93
94 GenericFormatImportCruiseContext cruiseContext = consumer.validateRow(row, importContext);
95
96 if (cruiseContext != null) {
97
98 OperationRow bean = row.getBean();
99 FishingOperation fishingOperation = bean.getFishingOperation();
100 CatchBatch catchBatch = bean.getCatchBatch();
101
102 Collection<AttachmentRow> operationAttachmentRows = importContext.popAttachmentRows(ObjectTypeCode.OPERATION, bean.getFishingOperationObjectId());
103 Collection<AttachmentRow> catchAttachmentRows = importContext.popAttachmentRows(ObjectTypeCode.CATCH_BATCH, bean.getCatchObjectId());
104
105 OperationDataModel selectionFishingOperation = cruiseContext.getSelectedFishingOperation(fishingOperation);
106
107 if (selectionFishingOperation == null) {
108
109
110 skipOperation(cruiseContext, fishingOperation);
111
112 } else {
113
114
115
116 consumer.prepareRowForPersist(row, importSpecies, importBenthos, importMarineLitter);
117
118 processOperation(cruiseContext, fishingOperation, catchBatch, operationAttachmentRows, catchAttachmentRows);
119
120 }
121
122 }
123
124 }
125 } catch (IOException e) {
126 throw new ApplicationTechnicalException("Could not close operation.csv file", e);
127 } catch (ImportRuntimeException e) {
128
129 importContext.getOperationFileResult().addGlobalError(e.getMessage());
130
131 }
132
133 }
134
135 private void skipOperation(GenericFormatImportCruiseContext cruiseContext, FishingOperation fishingOperation) {
136
137 String cruiseStr = cruiseContext.getCruiseLabel();
138 String operationStr = importContext.decorate(fishingOperation);
139 importContext.increments(t("tutti.service.genericFormat.persist.skipNotSelected.operation", cruiseStr, operationStr));
140 cruiseContext.addSkippedFishingOperation(fishingOperation);
141
142 }
143
144 private void processOperation(GenericFormatImportCruiseContext cruiseContext, FishingOperation fishingOperation, CatchBatch catchBatch, Collection<AttachmentRow> operationAttachmentRows, Collection<AttachmentRow> catchAttachmentRows) {
145
146 String cruiseStr = cruiseContext.getCruiseLabel();
147 String operationStr = importContext.decorate(fishingOperation);
148 OperationDataModel existingOperationData = cruiseContext.getExistingFishingOperationData(fishingOperation);
149
150 Pair<FishingOperation, CatchBatch> savedFishingOperation;
151
152 if (existingOperationData == null) {
153
154
155 importContext.increments(t("tutti.service.genericFormat.persist.create.operation", cruiseStr, operationStr));
156 savedFishingOperation = addFishingOperation(fishingOperation, catchBatch, operationAttachmentRows, catchAttachmentRows);
157
158 } else {
159
160
161 fishingOperation.setId(existingOperationData.getId());
162 if (importContext.getImportRequest().isUpdateOperations()) {
163
164
165 importContext.increments(t("tutti.service.genericFormat.persist.update.operation", cruiseStr, operationStr));
166 savedFishingOperation = updateFishingOperation(fishingOperation, catchBatch, operationAttachmentRows, catchAttachmentRows);
167
168 } else {
169
170
171 importContext.increments(t("tutti.service.genericFormat.persist.skip.operation", cruiseStr, operationStr));
172 savedFishingOperation = Pair.of(fishingOperation, catchBatch);
173
174 }
175 }
176
177 importContext.addImportedFishingOperation(savedFishingOperation.getLeft(), savedFishingOperation.getRight());
178
179 }
180
181 private Pair<FishingOperation, CatchBatch> addFishingOperation(FishingOperation fishingOperation, CatchBatch catchBatch, Collection<AttachmentRow> operationAttachmentRows, Collection<AttachmentRow> catchAttachmentRows) {
182
183 String cruiseStr = importContext.decorate(fishingOperation);
184
185 boolean createCruise = TuttiEntities.isNew(fishingOperation);
186 Preconditions.checkState(createCruise, "In addFishingOperation method, can't update existing operation: " + cruiseStr);
187
188 if (log.isInfoEnabled()) {
189 log.info("Create operation: " + cruiseStr);
190 }
191
192 FishingOperation createdFishingOperation = persistenceHelper.createFishingOperation(fishingOperation);
193
194 catchBatch.setFishingOperation(createdFishingOperation);
195 CatchBatch createdCatchBatch = persistenceHelper.createCatchBatch(catchBatch);
196
197 boolean importAttachments = importContext.getImportRequest().isImportAttachments();
198
199 if (importAttachments) {
200
201 persistenceHelper.persistAttachments(createdFishingOperation.getIdAsInt(), operationAttachmentRows);
202 persistenceHelper.persistAttachments(createdCatchBatch.getIdAsInt(), catchAttachmentRows);
203
204 }
205
206 return Pair.of(createdFishingOperation, createdCatchBatch);
207
208 }
209
210 private Pair<FishingOperation, CatchBatch> updateFishingOperation(FishingOperation fishingOperation, CatchBatch catchBatch, Collection<AttachmentRow> operationAttachmentRows, Collection<AttachmentRow> catchAttachmentRows) {
211
212 String operationStr = importContext.decorate(fishingOperation);
213
214 boolean createFishingOperation = TuttiEntities.isNew(fishingOperation);
215 Preconditions.checkState(!createFishingOperation, "In updateFishingOperation method, can't create new operation: " + operationStr);
216 Preconditions.checkState(importContext.getImportRequest().isUpdateOperations(), "In updateFishingOperation method, must be allowed to update operation: " + operationStr);
217
218 if (log.isInfoEnabled()) {
219 log.info("Persist fishing Operation: " + operationStr);
220 }
221
222 FishingOperation updatedFishingOperation = persistenceHelper.saveFishingOperation(fishingOperation);
223 catchBatch.setFishingOperation(updatedFishingOperation);
224
225 CatchBatch updatedCatchBatch;
226
227 if (persistenceHelper.isWithCatchBatch(updatedFishingOperation.getIdAsInt())) {
228
229 CatchBatch existingCatchBatch = persistenceHelper.getExistingCatchBatch(updatedFishingOperation.getIdAsInt());
230 catchBatch.setId(existingCatchBatch.getId());
231
232 if (!importContext.getImportRequest().isImportSpecies()) {
233
234
235 catchBatch.setSpeciesTotalSortedWeight(existingCatchBatch.getSpeciesTotalSortedWeight());
236 catchBatch.setSpeciesTotalInertWeight(existingCatchBatch.getSpeciesTotalInertWeight());
237 catchBatch.setSpeciesTotalLivingNotItemizedWeight(existingCatchBatch.getSpeciesTotalLivingNotItemizedWeight());
238
239 }
240
241 if (!importContext.getImportRequest().isImportBenthos()) {
242
243
244 catchBatch.setBenthosTotalSortedWeight(existingCatchBatch.getBenthosTotalSortedWeight());
245 catchBatch.setBenthosTotalInertWeight(existingCatchBatch.getBenthosTotalInertWeight());
246 catchBatch.setBenthosTotalLivingNotItemizedWeight(existingCatchBatch.getBenthosTotalLivingNotItemizedWeight());
247
248 }
249
250 if (!importContext.getImportRequest().isImportMarineLitter()) {
251
252
253 catchBatch.setMarineLitterTotalWeight(existingCatchBatch.getMarineLitterTotalWeight());
254
255 }
256
257 if (log.isInfoEnabled()) {
258 log.info("Update catch batch (" + operationStr + "): " + existingCatchBatch.getId());
259 }
260 updatedCatchBatch = persistenceHelper.saveCatchBatch(catchBatch);
261
262 } else {
263
264 if (log.isInfoEnabled()) {
265 log.info("Create new catchBatch (" + operationStr + ")");
266 }
267 updatedCatchBatch = persistenceHelper.createCatchBatch(catchBatch);
268
269 }
270
271 boolean importAttachments = importContext.getImportRequest().isImportAttachments();
272
273 if (importAttachments) {
274
275
276 persistenceHelper.deleteAllAttachments(ObjectTypeCode.OPERATION, fishingOperation.getIdAsInt());
277 persistenceHelper.deleteAllAttachments(ObjectTypeCode.CATCH_BATCH, catchBatch.getIdAsInt());
278
279 persistenceHelper.persistAttachments(updatedFishingOperation.getIdAsInt(), operationAttachmentRows);
280 persistenceHelper.persistAttachments(updatedCatchBatch.getIdAsInt(), catchAttachmentRows);
281
282 }
283
284 return Pair.of(updatedFishingOperation, updatedCatchBatch);
285
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 }