View Javadoc
1   package fr.ifremer.tutti.persistence.service.referential.synchro;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import java.sql.*;
31  
32  /**
33   * @author Kevin Morin (Code Lutin)
34   * @since 4.2
35   */
36  public class ReplaceReferenceTaxonReferentialUpdateTaskImpl implements ReferentialUpdateTask {
37  
38      /**
39       * Logger.
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 }