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.dao.technical.DatabaseSchemaDao;
31  import fr.ifremer.quadrige3.core.exception.DatabaseSchemaUpdateException;
32  import fr.ifremer.quadrige3.core.dao.technical.hibernate.DatabaseSchemaDaoImpl;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  import java.io.File;
37  
38  /**
39   * <p>DatabaseNewDbAction class.</p>
40   */
41  public class DatabaseNewDbAction {
42  	/* Logger */
43  	private static final Log log = LogFactory.getLog(DatabaseNewDbAction.class);
44  
45  	/**
46  	 * <p>run.</p>
47  	 */
48  	public void run() {
49  		QuadrigeConfiguration config = QuadrigeConfiguration.getInstance();
50  
51  		// Check output directory validity
52  		File outputDirectory = ActionUtils.checkAndGetOutputFile(true, this.getClass());
53  
54  		DatabaseSchemaDao databaseSchemaDao = new DatabaseSchemaDaoImpl(config);
55  
56  		try {
57  			// Create the database
58  			databaseSchemaDao.generateNewDb(outputDirectory, false);
59  
60  			// Update the DB schema
61  			databaseSchemaDao.updateSchema(outputDirectory);
62  		} catch (QuadrigeTechnicalException | DatabaseSchemaUpdateException e1) {
63  			log.error(e1.getMessage());
64  
65              // stop here
66              return;
67  		}
68  
69          // Set this new db directory (and JDBC URL) as default database
70  		// This allow to chain action. e.g. --new-db --import-ref
71  		config.setDbDirectory(outputDirectory);
72          config.setJdbcUrl(Daos.getJdbcUrl(outputDirectory, config.getDbName()));
73  	}
74  }