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  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.quadrige3.synchro.meta.DatabaseColumns;
31  import fr.ifremer.quadrige3.synchro.meta.referential.ReferentialSynchroTables;
32  import fr.ifremer.quadrige3.synchro.meta.system.RuleSynchroTables;
33  import fr.ifremer.quadrige3.synchro.server.config.SynchroServerConfiguration;
34  import fr.ifremer.quadrige3.synchro.service.referential.ReferentialSynchroContext;
35  import fr.ifremer.quadrige3.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      private SynchroConfiguration synchroConfiguration;
62      private SynchroServerConfiguration synchroServerConfiguration;
63  
64      public ReferentialSynchroDatabaseMetadata(Connection connection, SynchroDatabaseConfiguration config) {
65          super(connection, config);
66      }
67  
68      @Autowired
69      public ReferentialSynchroDatabaseMetadata(DataSource dataSource,
70                                                SynchroConfiguration synchroConfiguration,
71                                                SynchroServerConfiguration synchroServerConfiguration) {
72          this(getConnection(dataSource), createDatabaseConfiguration(synchroConfiguration, synchroServerConfiguration));
73          this.dataSource = dataSource;
74          this.synchroConfiguration = synchroConfiguration;
75          this.synchroServerConfiguration = synchroServerConfiguration;
76      }
77  
78      @Override
79      public void afterPropertiesSet() {
80          // prepare referential tables
81          prepareTables();
82      }
83  
84      public void updateConfiguration() {
85          // Update configuration by copy
86          this.config.copy(createDatabaseConfiguration(synchroConfiguration, synchroServerConfiguration));
87          // Re-init the metadata
88          init(getConnection(dataSource));
89          // Prepare referential tables (only program/strategy/moratorium)
90          prepareTables();
91      }
92  
93      /* -- internal methods -- */
94  
95      protected void prepareTables() {
96  
97          log.info(I18n.t("quadrige3.synchro.server.meta.referential.load",
98              config.getJdbcUrl(),
99              config.getJdbcUser()));
100 
101         unloadTables();
102         Set<String> tableNames = Sets.newLinkedHashSet(ReferentialSynchroTables.getImportTablesIncludes());
103 
104         // + Rule tables (need by Q3-déchet)
105         if (synchroServerConfiguration.isEnableImportTablesRules()) {
106             tableNames.addAll(RuleSynchroTables.tableNames());
107         }
108 
109         prepare(tableNames);
110 
111         // Close (to free memory)
112         close();
113     }
114 
115 
116     @Override
117     public void close() {
118         try {
119             DataSourceUtils.releaseConnection(getConnection(), dataSource);
120         } catch (SQLException ignored) {
121         }
122 
123         super.close();
124     }
125 
126     /* -- internal methods -- */
127 
128     private static Connection getConnection(DataSource dataSource) {
129         return DataSourceUtils.getConnection(dataSource);
130     }
131 
132     private static ReferentialSynchroDatabaseConfiguration createDatabaseConfiguration(SynchroConfiguration config, SynchroServerConfiguration synchroServerConfiguration) {
133 
134         // Set program codes from
135         ReferentialSynchroContext context = new ReferentialSynchroContext();
136         context.setProgramCodes(synchroServerConfiguration.getSynchroProgramCodeIncludes());
137 
138         ReferentialSynchroDatabaseConfiguration result = new ReferentialSynchroDatabaseConfiguration(context, config.getImportConnectionProperties(), false);
139         // Need to have a valid selectMaxUpdate query (e.g. DELETED_ITEM_HISTORY)
140         result.setFullMetadataEnable(true);
141         result.setColumnUpdateDate(DatabaseColumns.UPDATE_DT.name().toLowerCase());
142         return result;
143     }
144 
145 }