View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.meta;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.collect.Sets;
27  import fr.ifremer.common.synchro.config.SynchroConfiguration;
28  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
29  import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
30  import fr.ifremer.quadrige2.synchro.meta.DatabaseColumns;
31  import fr.ifremer.quadrige2.synchro.meta.referential.ReferentialSynchroTables;
32  import fr.ifremer.quadrige2.synchro.meta.system.RuleSynchroTables;
33  import fr.ifremer.quadrige2.synchro.server.config.SynchroServerConfiguration;
34  import fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroContext;
35  import fr.ifremer.quadrige2.synchro.service.referential.ReferentialSynchroDatabaseConfiguration;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.nuiton.i18n.I18n;
39  import org.springframework.beans.factory.InitializingBean;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.context.annotation.Lazy;
42  import org.springframework.jdbc.datasource.DataSourceUtils;
43  import org.springframework.stereotype.Component;
44  
45  import javax.sql.DataSource;
46  import java.sql.Connection;
47  import java.sql.SQLException;
48  import java.util.Set;
49  
50  @Component("referentialSynchroDatabaseMetadata")
51  @Lazy
52  public class ReferentialSynchroDatabaseMetadata extends SynchroDatabaseMetadata implements InitializingBean {
53  
54      /**
55       * Logger.
56       */
57      private static final Log log =
58              LogFactory.getLog(ReferentialSynchroDatabaseMetadata.class);
59  
60      private DataSource dataSource;
61  
62      private SynchroServerConfiguration synchroServerConfiguration;
63  
64      public ReferentialSynchroDatabaseMetadata(Connection connection, SynchroDatabaseConfiguration config) throws SQLException {
65          super(connection, config);
66      }
67  
68      @Autowired
69      public ReferentialSynchroDatabaseMetadata(DataSource dataSource,
70                                                SynchroConfiguration synchroConfiguration,
71                                                SynchroServerConfiguration synchroServerConfiguration) throws SQLException {
72          this(getConnection(dataSource), createDatabaseConfiguration(synchroConfiguration));
73          this.dataSource = dataSource;
74          this.synchroServerConfiguration = synchroServerConfiguration;
75      }
76  
77      @Override
78      public void afterPropertiesSet() throws Exception {
79          // prepare data tables
80          log.info(I18n.t("quadrige2.synchro.server.meta.referential.load",
81                  config.getJdbcUrl(),
82                  config.getJdbcUser()));
83  
84          Set<String> tableNames = Sets.newLinkedHashSet(ReferentialSynchroTables.getImportTablesIncludes());
85  
86          // + Rule tables (need by Q3-déchet)
87          if (synchroServerConfiguration.isEnableImportTablesRules()) {
88              tableNames.addAll(RuleSynchroTables.tableNames());
89          }
90  
91          prepare(tableNames);
92  
93          // Close the connection
94          if (dataSource != null) {
95              DataSourceUtils.releaseConnection(getConnection(), dataSource);
96          }
97  
98          // Close (to free memory)
99          close();
100     }
101 
102     /* -- internal methods -- */
103 
104     @Override
105     public void close() {
106         super.close();
107         this.dataSource = null;
108     }
109 
110     private static Connection getConnection(DataSource dataSource) {
111         return DataSourceUtils.getConnection(dataSource);
112     }
113 
114     private static ReferentialSynchroDatabaseConfiguration createDatabaseConfiguration(SynchroConfiguration config) {
115         ReferentialSynchroDatabaseConfiguration result = new ReferentialSynchroDatabaseConfiguration(new ReferentialSynchroContext(), config.getImportConnectionProperties(), false);
116         // Need to have a valid selectMaxUpdate query (e.g. DELETED_ITEM_HISTORY)
117         result.setFullMetadataEnable(true);
118         result.setColumnUpdateDate(DatabaseColumns.UPDATE_DT.name().toLowerCase());
119         return result;
120     }
121 
122 }