| ... |
... |
@@ -23,6 +23,7 @@ package fr.ird.observe.spi.migration.v9; |
|
23
|
23
|
*/
|
|
24
|
24
|
|
|
25
|
25
|
import com.google.auto.service.AutoService;
|
|
|
26
|
+import fr.ird.observe.spi.context.DataDtoEntityContext;
|
|
26
|
27
|
import fr.ird.observe.spi.migration.ByMajorMigrationVersionResource;
|
|
27
|
28
|
import io.ultreia.java4all.util.Version;
|
|
28
|
29
|
import io.ultreia.java4all.util.sql.SqlQuery;
|
| ... |
... |
@@ -32,9 +33,13 @@ import org.apache.logging.log4j.Logger; |
|
32
|
33
|
import org.nuiton.topia.service.migration.resources.MigrationVersionResource;
|
|
33
|
34
|
import org.nuiton.topia.service.migration.resources.MigrationVersionResourceExecutor;
|
|
34
|
35
|
|
|
|
36
|
+import java.sql.ResultSet;
|
|
|
37
|
+import java.sql.SQLException;
|
|
|
38
|
+import java.sql.Timestamp;
|
|
35
|
39
|
import java.util.List;
|
|
36
|
40
|
import java.util.Map;
|
|
37
|
41
|
import java.util.Set;
|
|
|
42
|
+import java.util.TreeMap;
|
|
38
|
43
|
|
|
39
|
44
|
/**
|
|
40
|
45
|
* Created on 19/01/2021.
|
| ... |
... |
@@ -124,6 +129,11 @@ public class DataSourceMigrationForVersion_9_0 extends ByMajorMigrationVersionRe |
|
124
|
129
|
|
|
125
|
130
|
addNewTable(executor, withIds, "03", "table-ps_observation_catch", "table-ps_observation_sample");
|
|
126
|
131
|
|
|
|
132
|
+ if (withIds) {
|
|
|
133
|
+ // See https://gitlab.com/ultreiaio/ird-observe/-/issues/2436
|
|
|
134
|
+ migrateSample(executor);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
127
|
137
|
executor.addScript("04_0", "adapt_table-ps_observation_activity");
|
|
128
|
138
|
executor.addScript("04_1", "adapt_table-ps_observation_set");
|
|
129
|
139
|
|
| ... |
... |
@@ -189,6 +199,307 @@ public class DataSourceMigrationForVersion_9_0 extends ByMajorMigrationVersionRe |
|
189
|
199
|
}
|
|
190
|
200
|
}
|
|
191
|
201
|
|
|
|
202
|
+ static class Sample {
|
|
|
203
|
+ String topiaId;
|
|
|
204
|
+ long topiaVersion;
|
|
|
205
|
+ Timestamp topiaCreateDate;
|
|
|
206
|
+ Timestamp lastUpdateDate;
|
|
|
207
|
+ String homeId;
|
|
|
208
|
+ String comment;
|
|
|
209
|
+ String set;
|
|
|
210
|
+
|
|
|
211
|
+ public Sample(ResultSet resultSet) throws SQLException {
|
|
|
212
|
+ this.topiaId = resultSet.getString(1);
|
|
|
213
|
+ this.topiaVersion = resultSet.getLong(2);
|
|
|
214
|
+ this.topiaCreateDate = resultSet.getTimestamp(3);
|
|
|
215
|
+ this.homeId = resultSet.getString(4);
|
|
|
216
|
+ this.comment = resultSet.getString(5);
|
|
|
217
|
+ this.set = resultSet.getString(6);
|
|
|
218
|
+ this.lastUpdateDate = resultSet.getTimestamp(7);
|
|
|
219
|
+ }
|
|
|
220
|
+
|
|
|
221
|
+ public void addHomeId(String homeId) {
|
|
|
222
|
+ if (homeId == null) {
|
|
|
223
|
+ return;
|
|
|
224
|
+ }
|
|
|
225
|
+ if (this.homeId == null) {
|
|
|
226
|
+ this.homeId = homeId;
|
|
|
227
|
+ return;
|
|
|
228
|
+ }
|
|
|
229
|
+ this.homeId += " - " + homeId;
|
|
|
230
|
+ }
|
|
|
231
|
+
|
|
|
232
|
+ public void addComment(String comment) {
|
|
|
233
|
+ if (comment == null) {
|
|
|
234
|
+ return;
|
|
|
235
|
+ }
|
|
|
236
|
+ if (this.comment == null) {
|
|
|
237
|
+ this.comment = comment;
|
|
|
238
|
+ return;
|
|
|
239
|
+ }
|
|
|
240
|
+ this.comment += " - " + comment;
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+
|
|
|
246
|
+ static class SampleMeasure {
|
|
|
247
|
+ String topiaId;
|
|
|
248
|
+ long topiaVersion;
|
|
|
249
|
+ Timestamp topiaCreateDate;
|
|
|
250
|
+ Timestamp lastUpdateDate;
|
|
|
251
|
+ String homeId;
|
|
|
252
|
+ Float length;
|
|
|
253
|
+ Boolean isLengthComputed;
|
|
|
254
|
+ String picturesReferences;
|
|
|
255
|
+ Float weight;
|
|
|
256
|
+ Boolean isWeightComputed;
|
|
|
257
|
+ int count;
|
|
|
258
|
+ int acquisitionMode;
|
|
|
259
|
+ String species;
|
|
|
260
|
+ String sample;
|
|
|
261
|
+ String sex;
|
|
|
262
|
+ String sizeMeasureType;
|
|
|
263
|
+ String weightMeasureType;
|
|
|
264
|
+ String tagNumber;
|
|
|
265
|
+ String speciesFate;
|
|
|
266
|
+ int sample_idx;
|
|
|
267
|
+ String lengthMeasureMethod;
|
|
|
268
|
+ String weightMeasureMethod;
|
|
|
269
|
+
|
|
|
270
|
+ public SampleMeasure(ResultSet resultSet) throws SQLException {
|
|
|
271
|
+ this.topiaId = resultSet.getString(1);
|
|
|
272
|
+ this.topiaVersion = resultSet.getLong(2);
|
|
|
273
|
+ this.topiaCreateDate = resultSet.getTimestamp(3);
|
|
|
274
|
+ this.homeId = resultSet.getString(4);
|
|
|
275
|
+ this.length = resultSet.getFloat(5);
|
|
|
276
|
+ this.isLengthComputed = resultSet.getBoolean(6);
|
|
|
277
|
+ this.picturesReferences = resultSet.getString(7);
|
|
|
278
|
+ this.weight = resultSet.getFloat(8);
|
|
|
279
|
+ this.isWeightComputed = resultSet.getBoolean(9);
|
|
|
280
|
+ this.count = resultSet.getInt(10);
|
|
|
281
|
+ this.acquisitionMode = resultSet.getInt(11);
|
|
|
282
|
+ this.species = resultSet.getString(12);
|
|
|
283
|
+ this.sample = resultSet.getString(13);
|
|
|
284
|
+ this.sex = resultSet.getString(14);
|
|
|
285
|
+ this.lastUpdateDate = resultSet.getTimestamp(15);
|
|
|
286
|
+ this.sizeMeasureType = resultSet.getString(16);
|
|
|
287
|
+ this.weightMeasureType = resultSet.getString(17);
|
|
|
288
|
+ this.tagNumber = resultSet.getString(18);
|
|
|
289
|
+ this.speciesFate = resultSet.getString(19);
|
|
|
290
|
+ this.sample_idx = resultSet.getInt(20);
|
|
|
291
|
+ this.lengthMeasureMethod = resultSet.getString(21);
|
|
|
292
|
+ this.weightMeasureMethod = resultSet.getString(22);
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ }
|
|
|
296
|
+
|
|
|
297
|
+ private void migrateSample(MigrationVersionResourceExecutor executor) {
|
|
|
298
|
+
|
|
|
299
|
+ Map<String, Sample> setToSampleMap = new TreeMap<>();
|
|
|
300
|
+ Map<String, String> sampleMapping = new TreeMap<>();
|
|
|
301
|
+
|
|
|
302
|
+ addSample(executor, setToSampleMap, sampleMapping, "SELECT REPLACE(topiaId, '.NonTargetSample', '.Sample'), topiaVersion, topiaCreateDate, homeId, substr(trim(comment), 0, 1024), set, lastUpdateDate FROM ps_observation.nontargetsample order by set, topiaId");
|
|
|
303
|
+
|
|
|
304
|
+ addSample(executor, setToSampleMap, sampleMapping, "SELECT REPLACE(topiaId, '.TargetSample', '.Sample'), topiaVersion, topiaCreateDate, homeId, substr(trim(comment), 0, 1024), set, lastUpdateDate FROM ps_observation.targetsample order by set, topiaId");
|
|
|
305
|
+
|
|
|
306
|
+ for (Sample sample : setToSampleMap.values()) {
|
|
|
307
|
+ executor.writeSql(String.format("INSERT INTO ps_observation.Sample(topiaId, topiaVersion, topiaCreateDate, homeId, comment, set, lastUpdateDate) VALUES('%s', %s, '%s'::timestamp, %s, %s, '%s', '%s'::timestamp);",
|
|
|
308
|
+ sample.topiaId,
|
|
|
309
|
+ sample.topiaVersion,
|
|
|
310
|
+ sample.topiaCreateDate,
|
|
|
311
|
+ DataDtoEntityContext.escapeString(sample.homeId),
|
|
|
312
|
+ DataDtoEntityContext.escapeComment(sample.comment),
|
|
|
313
|
+ sample.set,
|
|
|
314
|
+ sample.lastUpdateDate
|
|
|
315
|
+ ));
|
|
|
316
|
+ }
|
|
|
317
|
+
|
|
|
318
|
+ migrateSampleMeasure(executor, sampleMapping);
|
|
|
319
|
+ }
|
|
|
320
|
+
|
|
|
321
|
+ private void migrateSampleMeasure(MigrationVersionResourceExecutor executor, Map<String, String> sampleMapping) {
|
|
|
322
|
+ // nonTargetSampleMeasure
|
|
|
323
|
+ addSampleMeasure(executor, sampleMapping, "SELECT " +
|
|
|
324
|
+ "REPLACE(topiaId, '.NonTargetLength', '.SampleMeasure'), " +
|
|
|
325
|
+ "topiaVersion + 1, " +
|
|
|
326
|
+ "topiaCreateDate, " +
|
|
|
327
|
+ "homeId, " +
|
|
|
328
|
+ "length, " +
|
|
|
329
|
+ "isLengthComputed, " +
|
|
|
330
|
+ "picturesReferences, " +
|
|
|
331
|
+ "weight, " +
|
|
|
332
|
+ "isWeightComputed, " +
|
|
|
333
|
+ "count, " +
|
|
|
334
|
+ "acquisitionMode, " +
|
|
|
335
|
+ "species, " +
|
|
|
336
|
+ "REPLACE(nonTargetSample, '.NonTargetSample', '.Sample'), " +
|
|
|
337
|
+ "sex, " +
|
|
|
338
|
+ "lastUpdateDate, " +
|
|
|
339
|
+ "sizeMeasureType, " +
|
|
|
340
|
+ "weightMeasureType, " +
|
|
|
341
|
+ "tagNumber, " +
|
|
|
342
|
+ "speciesFate, " +
|
|
|
343
|
+ "nonTargetSample_idx, " +
|
|
|
344
|
+ "lengthMeasureMethod, " +
|
|
|
345
|
+ "weightMeasureMethod " +
|
|
|
346
|
+ "FROM ps_observation.NonTargetLength");
|
|
|
347
|
+
|
|
|
348
|
+ // discardedTargetSampleMeasure
|
|
|
349
|
+ addSampleMeasure(executor, sampleMapping, "SELECT " +
|
|
|
350
|
+ "REPLACE(tl.topiaId, '.TargetLength', '.SampleMeasure'), " +
|
|
|
351
|
+ "tl.topiaVersion + 1, " +
|
|
|
352
|
+ "tl.topiaCreateDate, " +
|
|
|
353
|
+ "tl.homeId, " +
|
|
|
354
|
+ "tl.length, " +
|
|
|
355
|
+ "tl.isLengthComputed, " +
|
|
|
356
|
+ "NULL, " +
|
|
|
357
|
+ "tl.weight, " +
|
|
|
358
|
+ "tl.isWeightComputed, " +
|
|
|
359
|
+ "tl.count, " +
|
|
|
360
|
+ "tl.acquisitionMode, " +
|
|
|
361
|
+ "tl.species, " +
|
|
|
362
|
+ "REPLACE(tl.targetSample, '.TargetSample', '.Sample'), " +
|
|
|
363
|
+ "'fr.ird.referential.common.Sex#1239832686121#0.0', " +
|
|
|
364
|
+ "tl.lastUpdateDate, " +
|
|
|
365
|
+ "tl.sizeMeasureType, " +
|
|
|
366
|
+ "tl.weightMeasureType, " +
|
|
|
367
|
+ "tl.tagNumber, " +
|
|
|
368
|
+ "'fr.ird.referential.ps.common.SpeciesFate#1239832683619#0.6250731662108877', " +
|
|
|
369
|
+ "-tl.targetsample_idx, " +
|
|
|
370
|
+ "tl.lengthMeasureMethod, " +
|
|
|
371
|
+ "tl.weightMeasureMethod " +
|
|
|
372
|
+ "FROM ps_observation.TargetSample ts " +
|
|
|
373
|
+ "INNER JOIN ps_observation.TargetLength tl ON (tl.targetSample=ts.topiaId) " +
|
|
|
374
|
+ "WHERE ts.discarded");
|
|
|
375
|
+
|
|
|
376
|
+ // notDiscardedTargetSampleMeasure
|
|
|
377
|
+ addSampleMeasure(executor, sampleMapping, "SELECT " +
|
|
|
378
|
+ "REPLACE(tl.topiaId, '.TargetLength', '.SampleMeasure'), " +
|
|
|
379
|
+ "tl.topiaVersion + 1, " +
|
|
|
380
|
+ "tl.topiaCreateDate, " +
|
|
|
381
|
+ "tl.homeId, " +
|
|
|
382
|
+ "tl.length, " +
|
|
|
383
|
+ "tl.isLengthComputed, " +
|
|
|
384
|
+ "NULL, " +
|
|
|
385
|
+ "tl.weight, " +
|
|
|
386
|
+ "tl.isWeightComputed, " +
|
|
|
387
|
+ "tl.count, " +
|
|
|
388
|
+ "tl.acquisitionMode, " +
|
|
|
389
|
+ "tl.species, " +
|
|
|
390
|
+ "REPLACE(tl.targetSample, '.TargetSample', '.Sample'), " +
|
|
|
391
|
+ "'fr.ird.referential.common.Sex#1239832686121#0.0', " +
|
|
|
392
|
+ "tl.lastUpdateDate, " +
|
|
|
393
|
+ "tl.sizeMeasureType, " +
|
|
|
394
|
+ "tl.weightMeasureType, " +
|
|
|
395
|
+ "tl.tagNumber, " +
|
|
|
396
|
+ "'fr.ird.referential.ps.common.SpeciesFate#1239832683619#0.5722739932065866', " +
|
|
|
397
|
+ "-tl.targetsample_idx, " +
|
|
|
398
|
+ "tl.lengthMeasureMethod, " +
|
|
|
399
|
+ "tl.weightMeasureMethod " +
|
|
|
400
|
+ "FROM ps_observation.TargetSample ts " +
|
|
|
401
|
+ "INNER JOIN ps_observation.TargetLength tl ON (tl.targetSample=ts.topiaId) " +
|
|
|
402
|
+ "WHERE NOT ts.discarded");
|
|
|
403
|
+ }
|
|
|
404
|
+
|
|
|
405
|
+ private void addSample(MigrationVersionResourceExecutor executor, Map<String, Sample> setToSampleMap, Map<String, String> sampleMapping, String query) {
|
|
|
406
|
+ List<Sample> targetSample = executor.findMultipleResult(SqlQuery.wrap(query, Sample::new));
|
|
|
407
|
+ for (Sample sample : targetSample) {
|
|
|
408
|
+ Sample existingSample = setToSampleMap.get(sample.set);
|
|
|
409
|
+ if (existingSample == null) {
|
|
|
410
|
+ // new sample
|
|
|
411
|
+ setToSampleMap.put(sample.set, sample);
|
|
|
412
|
+ } else {
|
|
|
413
|
+ // add to sample mapping
|
|
|
414
|
+ sampleMapping.put(sample.topiaId, existingSample.topiaId);
|
|
|
415
|
+ // update homeId
|
|
|
416
|
+ existingSample.addHomeId(sample.homeId);
|
|
|
417
|
+ // update comment
|
|
|
418
|
+ existingSample.addComment(sample.comment);
|
|
|
419
|
+ }
|
|
|
420
|
+ }
|
|
|
421
|
+ }
|
|
|
422
|
+
|
|
|
423
|
+ private void addSampleMeasure(MigrationVersionResourceExecutor executor, Map<String, String> sampleMapping, String query) {
|
|
|
424
|
+ List<SampleMeasure> sampleMeasureList = executor.findMultipleResult(SqlQuery.wrap(query, SampleMeasure::new));
|
|
|
425
|
+ for (SampleMeasure sampleMeasure : sampleMeasureList) {
|
|
|
426
|
+ String sampleId = sampleMapping.get(sampleMeasure.sample);
|
|
|
427
|
+ if (sampleId == null) {
|
|
|
428
|
+ sampleId = sampleMeasure.sample;
|
|
|
429
|
+ }
|
|
|
430
|
+ executor.writeSql(String.format("INSERT INTO ps_observation.SampleMeasure(" +
|
|
|
431
|
+ " topiaId," +
|
|
|
432
|
+ " topiaVersion," +
|
|
|
433
|
+ " topiaCreateDate," +
|
|
|
434
|
+ " homeId," +
|
|
|
435
|
+ " length," +
|
|
|
436
|
+ " isLengthComputed," +
|
|
|
437
|
+ " picturesReferences," +
|
|
|
438
|
+ " weight," +
|
|
|
439
|
+ " isWeightComputed," +
|
|
|
440
|
+ " count," +
|
|
|
441
|
+ " acquisitionMode," +
|
|
|
442
|
+ " species," +
|
|
|
443
|
+ " sample," +
|
|
|
444
|
+ " sex," +
|
|
|
445
|
+ " lastUpdateDate," +
|
|
|
446
|
+ " sizeMeasureType," +
|
|
|
447
|
+ " weightMeasureType," +
|
|
|
448
|
+ " tagNumber," +
|
|
|
449
|
+ " speciesFate," +
|
|
|
450
|
+ " sample_idx," +
|
|
|
451
|
+ " lengthMeasureMethod," +
|
|
|
452
|
+ " weightMeasureMethod)" +
|
|
|
453
|
+ " VALUES(" +
|
|
|
454
|
+ "'%s', " +
|
|
|
455
|
+ "%s, " +
|
|
|
456
|
+ "'%s'::timestamp, " +
|
|
|
457
|
+ "%s, " +
|
|
|
458
|
+ "%s, " +
|
|
|
459
|
+ "%s, " +
|
|
|
460
|
+ "%s, " +
|
|
|
461
|
+ "%s, " +
|
|
|
462
|
+ "%s, " +
|
|
|
463
|
+ "%s, " +
|
|
|
464
|
+ "%s, " +
|
|
|
465
|
+ "%s, " +
|
|
|
466
|
+ "'%s', " +
|
|
|
467
|
+ "%s, " +
|
|
|
468
|
+ "'%s'::timestamp, " +
|
|
|
469
|
+ "%s, " +
|
|
|
470
|
+ "%s, " +
|
|
|
471
|
+ "%s, " +
|
|
|
472
|
+ "%s, " +
|
|
|
473
|
+ "%s, " +
|
|
|
474
|
+ "%s, " +
|
|
|
475
|
+ "%s " +
|
|
|
476
|
+ ");",
|
|
|
477
|
+ sampleMeasure.topiaId,
|
|
|
478
|
+ sampleMeasure.topiaVersion,
|
|
|
479
|
+ sampleMeasure.topiaCreateDate,
|
|
|
480
|
+ DataDtoEntityContext.escapeString(sampleMeasure.homeId == null ? null : sampleMeasure.homeId.replaceAll("'", "")),
|
|
|
481
|
+ sampleMeasure.length,
|
|
|
482
|
+ sampleMeasure.isLengthComputed,
|
|
|
483
|
+ DataDtoEntityContext.escapeString(sampleMeasure.picturesReferences == null ? null : sampleMeasure.picturesReferences.replaceAll("'", "")),
|
|
|
484
|
+ sampleMeasure.weight,
|
|
|
485
|
+ sampleMeasure.isWeightComputed,
|
|
|
486
|
+ sampleMeasure.count,
|
|
|
487
|
+ sampleMeasure.acquisitionMode,
|
|
|
488
|
+ DataDtoEntityContext.escapeString(sampleMeasure.species),
|
|
|
489
|
+ sampleId,
|
|
|
490
|
+ DataDtoEntityContext.escapeString(sampleMeasure.sex),
|
|
|
491
|
+ sampleMeasure.lastUpdateDate,
|
|
|
492
|
+ DataDtoEntityContext.escapeString(sampleMeasure.sizeMeasureType),
|
|
|
493
|
+ DataDtoEntityContext.escapeString(sampleMeasure.weightMeasureType),
|
|
|
494
|
+ DataDtoEntityContext.escapeString(sampleMeasure.tagNumber),
|
|
|
495
|
+ DataDtoEntityContext.escapeString(sampleMeasure.speciesFate),
|
|
|
496
|
+ sampleMeasure.sample_idx,
|
|
|
497
|
+ DataDtoEntityContext.escapeString(sampleMeasure.lengthMeasureMethod),
|
|
|
498
|
+ DataDtoEntityContext.escapeString(sampleMeasure.weightMeasureMethod)
|
|
|
499
|
+ ));
|
|
|
500
|
+ }
|
|
|
501
|
+ }
|
|
|
502
|
+
|
|
192
|
503
|
@Override
|
|
193
|
504
|
public void generateFinalizeSqlScript(MigrationVersionResourceExecutor executor) {
|
|
194
|
505
|
migrateIdx(executor, "ps_observation", "catch", "set");
|