View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.meta;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 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  
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.quadrige3.synchro.meta.data.DataSynchroTables;
31  import fr.ifremer.quadrige3.synchro.service.data.DataSynchroContext;
32  import fr.ifremer.quadrige3.synchro.service.data.DataSynchroDatabaseConfiguration;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.i18n.I18n;
36  import org.springframework.beans.factory.InitializingBean;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.context.annotation.Lazy;
39  import org.springframework.jdbc.datasource.DataSourceUtils;
40  import org.springframework.stereotype.Component;
41  
42  import javax.sql.DataSource;
43  import java.sql.Connection;
44  
45  @Component("dataSynchroDatabaseMetadata")
46  @Lazy
47  public class DataSynchroDatabaseMetadata extends SynchroDatabaseMetadata implements InitializingBean {
48  
49      /** Logger. */
50      private static final Log log =
51              LogFactory.getLog(DataSynchroDatabaseMetadata.class);
52  
53      private DataSource dataSource;
54      
55      public DataSynchroDatabaseMetadata(Connection connection, SynchroDatabaseConfiguration config) {
56          super(connection, config);
57      }
58  
59      @Autowired
60      public DataSynchroDatabaseMetadata(DataSource dataSource, SynchroConfiguration config) {
61          super(getConnection(dataSource), createDatabaseConfiguration(config));
62          this.dataSource = dataSource;
63      }
64      
65  
66      @Override
67      public void afterPropertiesSet() throws Exception {
68  
69          // Prepare data tables
70          log.info(I18n.t("quadrige3.synchro.server.meta.data.load",
71                  config.getJdbcUrl(),
72                  config.getJdbcUser()));
73          prepare(DataSynchroTables.getImportTablesIncludes());
74          
75          // Close the connection
76          if (dataSource != null) {
77              DataSourceUtils.releaseConnection(getConnection(), dataSource);
78          }
79          
80          // Close (to free memory)
81          close();    
82       }
83  
84      /* -- internal methods -- */
85  
86      private static Connection getConnection(DataSource dataSource) {
87          return DataSourceUtils.getConnection(dataSource);
88      }
89      
90      public static DataSynchroDatabaseConfiguration createDatabaseConfiguration(SynchroConfiguration config) {
91          DataSynchroDatabaseConfiguration result = new DataSynchroDatabaseConfiguration(new DataSynchroContext(), config.getImportConnectionProperties(), false);
92          result.setFullMetadataEnable(false);
93          result.setReadOnly(true);
94  
95          return result;
96      }
97  }