View Javadoc
1   package fr.ifremer.quadrige3.core.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
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.quadrige3.core.config.QuadrigeConfiguration;
28  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.version.Version;
32  
33  import fr.ifremer.quadrige3.core.dao.technical.Daos;
34  import fr.ifremer.quadrige3.core.service.DatabaseSchemaService;
35  import fr.ifremer.quadrige3.core.service.ServiceLocator;
36  
37  /**
38   * <p>DatabaseUpdateSchemaAction class.</p>
39   *
40   */
41  public class DatabaseUpdateSchemaAction {
42      /* Logger */
43      private static final Log log = LogFactory.getLog(DatabaseUpdateSchemaAction.class);
44  
45      /**
46       * <p>run.</p>
47       */
48      public void run() {
49          QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
50  
51          if (log.isInfoEnabled()) {
52              log.info("Starting database schema update...");
53          }
54          ActionUtils.logConnectionProperties();
55  
56          boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
57                  config.getJdbcURL(),
58                  config.getJdbcUsername(),
59                  config.getJdbcPassword());
60  
61          if (!isValidConnection) {
62              log.warn("Connection error: could not update the schema.");
63              return;
64          }
65          DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
66  
67          // Check if database is well loaded
68          if (!databaseSchemaService.isDbLoaded()) {
69              log.warn("Database not start ! Could not update the schema.");
70              return;
71          }
72  
73          // Getting the database version 
74          try {
75              Version actualDbVersion = databaseSchemaService.getDbVersion();
76              // result could be null, is DB is empty (mantis #21013)
77              if (actualDbVersion == null) {
78                  log.warn("Could not get database schema version");
79              }
80              else {
81                  log.info(String.format("Database schema version is [%s]", actualDbVersion));
82              }
83              
84          } catch (QuadrigeTechnicalException e) {
85              log.error("Error while getting database version.", e);
86          }
87  
88          // Getting the version after update
89          try {
90              Version expectedDbVersion = databaseSchemaService.getApplicationVersion();
91              if (expectedDbVersion == null) {
92                  log.warn("Unable to get the database schema version AFTER the update. Nothing to update !");
93                  return;
94              }
95              log.info(String.format("Database schema version AFTER the update should be [%s]", expectedDbVersion));            
96          } catch (QuadrigeTechnicalException e) {
97              log.error("Error while getting database version AFTER the update.", e);
98          }
99  
100         // Run the update process
101         try {
102             log.info("Launching update...");
103             databaseSchemaService.updateSchema();
104             log.info("Database schema successfully updated.");
105         } catch (QuadrigeTechnicalException e) {
106             log.error("Error while updating the database schema.", e);
107         }
108     }
109 }