View Javadoc
1   package fr.ifremer.quadrige3.core.action;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Shared
6    * %%
7    * Copyright (C) 2017 - 2019 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
25  import fr.ifremer.quadrige3.core.dao.technical.Daos;
26  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
27  import fr.ifremer.quadrige3.core.service.DatabaseSchemaService;
28  import fr.ifremer.quadrige3.core.service.ServiceLocator;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.nuiton.version.Version;
32  
33  import java.io.File;
34  import java.io.IOException;
35  
36  /**
37   * @author peck7 on 06/06/2019.
38   */
39  public class DatabaseCreateSchemaAction {
40  
41      private static final Log LOG = LogFactory.getLog(DatabaseCreateSchemaAction.class);
42  
43      public void run() {
44  
45          QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
46          File outputFile = ActionUtils.checkAndGetOutputFile(false, this.getClass());
47  
48          if (LOG.isInfoEnabled()) {
49              LOG.info("Starting generation script file ...");
50          }
51          ActionUtils.logConnectionProperties();
52  
53          boolean isValidConnection = Daos.isValidConnectionProperties(config.getJdbcDriver(),
54                  config.getJdbcURL(),
55                  config.getJdbcUsername(),
56                  config.getJdbcPassword());
57  
58          if (!isValidConnection) {
59              LOG.warn("Connection error: could not generate script file.");
60              return;
61          }
62          DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
63  
64          try {
65              Version actualDbVersion = databaseSchemaService.getDbVersion();
66              if (actualDbVersion != null) {
67                  LOG.info("Database schema version is: " + actualDbVersion.toString());
68              }
69  
70              Version modelVersion = config.getVersion();
71              LOG.info("Model version is: " + modelVersion.toString());
72          } catch (QuadrigeTechnicalException e) {
73              LOG.error("Error while getting versions.", e);
74          }
75  
76          try {
77              LOG.info("Launching creation file generation...");
78              databaseSchemaService.createSchemaToFile(outputFile, false);
79              if (outputFile != null) {
80                  LOG.info(String.format("Database creation script file successfully generated at %s", outputFile));
81              }
82              else {
83                  LOG.info("Database creation script file successfully generated.");
84              }
85          } catch (QuadrigeTechnicalException | IOException e) {
86              LOG.error("Error while generating creation script file.", e);
87          }
88      }
89  }