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 fr.ifremer.quadrige3.core.dao.technical.Daos;
30  import fr.ifremer.quadrige3.core.service.DatabaseSchemaService;
31  import fr.ifremer.quadrige3.core.service.ServiceLocator;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.version.Version;
35  
36  import java.io.File;
37  import java.io.IOException;
38  
39  /**
40   * <p>DatabaseStatusAction class.</p>
41   */
42  public class DatabaseStatusAction {
43      /* Logger */
44      private static final Log log = LogFactory.getLog(DatabaseStatusAction.class);
45  
46      /**
47       * <p>run.</p>
48       */
49      public void run() {
50          QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
51          
52          if (log.isInfoEnabled()) {
53              log.info("Starting database status report...");
54          }
55          ActionUtils.logConnectionProperties();
56          
57          boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
58                  config.getJdbcURL(),
59                  config.getJdbcUsername(),
60                  config.getJdbcPassword()); 
61          
62          if (!isValidConnection) {
63              log.warn("Connection error : could not check database status.");
64              return;
65          }
66          DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
67  
68          // Check if database is well loaded
69          if (!databaseSchemaService.isDbLoaded()) {
70              log.warn("Database not start ! Could not check database status.");
71              return;
72          }
73  
74          try {
75              Version actualDbVersion = databaseSchemaService.getDbVersion();
76              if (actualDbVersion != null) {
77                  log.info("Database schema version is: " + actualDbVersion.toString());
78              }
79  
80              Version expectedDbVersion = databaseSchemaService.getApplicationVersion();
81              log.info("Database schema version AFTER an update should be: " + expectedDbVersion.toString());
82          } catch (QuadrigeTechnicalException e) {
83              log.error("Error while getting versions.", e);
84          }
85          
86          File outputFile = ActionUtils.checkAndGetOutputFile(false, this.getClass());
87  
88          try {
89              log.info("Launching status report generation...");
90              databaseSchemaService.generateStatusReport(outputFile);
91              if (outputFile != null) {
92                  log.info(String.format("Database status report successfully generated at %s", outputFile));
93              }
94              else {
95                  log.info("Database status report successfully generated.");
96              }
97          } catch (QuadrigeTechnicalException | IOException e) {
98              log.error("Error while writing database status report.", e);
99          }
100     }
101 
102 }