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.dao.technical.Daos;
29  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
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  
38  /**
39   * <p>DatabaseGenerateChangeLogAction class.</p>
40   */
41  public class DatabaseGenerateChangeLogAction {
42      /* Logger */
43      private static final Log log = LogFactory.getLog(DatabaseGenerateChangeLogAction.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 change log file generation...");        
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 generate changelog file.");
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 generate changelog file.");
70              return;
71          }
72  
73          try {
74              Version actualDbVersion = databaseSchemaService.getDbVersion();
75              if (actualDbVersion != null) {
76                  log.info("Database schema version is: " + actualDbVersion.toString());
77              }
78  
79              Version modelVersion = config.getVersion();
80              log.info("Model version is: " + modelVersion.toString());
81          } catch (QuadrigeTechnicalException e) {
82              log.error("Error while getting versions.", e);
83          }
84  
85          File outputFile = ActionUtils.checkAndGetOutputFile(false, this.getClass());
86          
87          try {
88              log.info("Launching changelog file generation...");
89              databaseSchemaService.generateDiffChangeLog(outputFile);
90              if (outputFile != null) {
91                  log.info(String.format("Database changelog file successfully generated at %s", outputFile));
92              }
93              else {
94                  log.info("Database changelog file successfully generated.");
95              }
96          } catch (QuadrigeTechnicalException e) {
97              log.error("Error while generating changelog file.", e);
98          }
99      }
100 }