1 package fr.ifremer.tutti.persistence.service.referential.synchro;
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 org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import java.sql.*;
31
32
33
34
35
36 public class ReplaceReferenceTaxonReferentialUpdateTaskImpl implements ReferentialUpdateTask {
37
38
39
40
41 private static final Log log = LogFactory.getLog(ReplaceReferenceTaxonReferentialUpdateTaskImpl.class);
42
43 public static final String GET_REFTAX_TO_REPLACE_QUERY = "SELECT object_id, external_code " +
44 "FROM transcribing_item ti " +
45 "JOIN transcribing_item_type tit " +
46 "ON ti.transcribing_item_type_fk = tit.id " +
47 "AND tit.label = 'TAXINOMIE-COMMUN.REFERENCE_HISTORY' " +
48 "WHERE (update_date IS NULL OR update_date > ?)";
49
50 public static final String REPLACE_REFTAX_IN_BATCH_QUERY = "UPDATE batch SET reference_taxon_fk = ? " +
51 "WHERE reference_taxon_fk = ?";
52
53 public static final String REPLACE_REFTAX_IN_SAMPLE_QUERY = "UPDATE sample SET reference_taxon_fk = ? " +
54 "WHERE reference_taxon_fk = ?";
55
56 @Override
57 public String getTable() {
58 return "TRANSCRIBING_ITEM";
59 }
60
61 @Override
62 public void update(Connection localConnection,
63 Timestamp lastUpdate) throws SQLException {
64
65
66 PreparedStatement preparedStatement = localConnection.prepareStatement(GET_REFTAX_TO_REPLACE_QUERY);
67 preparedStatement.setTimestamp(1, lastUpdate);
68 ResultSet reftaxToReplace = preparedStatement.executeQuery();
69
70 PreparedStatement replaceReftaxInBatchStatement = localConnection.prepareStatement(REPLACE_REFTAX_IN_BATCH_QUERY);
71 PreparedStatement replaceReftaxInSampleStatement = localConnection.prepareStatement(REPLACE_REFTAX_IN_SAMPLE_QUERY);
72
73 while (reftaxToReplace.next()) {
74
75 Integer newRefTax = reftaxToReplace.getInt(1);
76 Integer oldRefTax = reftaxToReplace.getInt(2);
77
78 if (log.isInfoEnabled()) {
79 log.info(String.format("[%s] Remplacement du taxon %s par le taxon %s", getTable(), oldRefTax, newRefTax));
80 }
81
82 replaceReftaxInBatchStatement.setInt(1, newRefTax);
83 replaceReftaxInBatchStatement.setInt(2, oldRefTax);
84 int batchUpdated = replaceReftaxInBatchStatement.executeUpdate();
85
86 if (log.isInfoEnabled()) {
87 log.info(String.format("[%s] %s batchs mis à jour", getTable(), batchUpdated));
88 }
89
90 replaceReftaxInSampleStatement.setInt(1, newRefTax);
91 replaceReftaxInSampleStatement.setInt(2, oldRefTax);
92 int sampleUpdated = replaceReftaxInSampleStatement.executeUpdate();
93
94 if (log.isInfoEnabled()) {
95 log.info(String.format("[%s] %s samples mis à jour", getTable(), sampleUpdated));
96 }
97 }
98
99 }
100 }