001package fr.ifremer.adagio.core.action; 002 003/* 004 * #%L 005 * SIH-Adagio :: Shared 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2012 - 2014 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026import java.io.File; 027import java.io.IOException; 028 029import org.apache.commons.lang3.StringUtils; 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuiton.util.Version; 033 034import fr.ifremer.adagio.core.AdagioTechnicalException; 035import fr.ifremer.adagio.core.config.AdagioConfiguration; 036import fr.ifremer.adagio.core.dao.technical.DaoUtils; 037import fr.ifremer.adagio.core.service.DatabaseSchemaService; 038import fr.ifremer.adagio.core.service.ServiceLocator; 039 040public class DatabaseChangeLogAction { 041 /* Logger */ 042 private static final Log log = LogFactory.getLog(DatabaseChangeLogAction.class); 043 044 public void run() { 045 AdagioConfiguration config = AdagioConfiguration.getInstance(); 046 047 if (log.isInfoEnabled()) { 048 log.info("Starting change log file generation..."); 049 050 boolean isFileDatabase = DaoUtils.isFileDatabase(config.getJdbcDriver()); 051 if (isFileDatabase) { 052 log.info(String.format(" Database directory: %s", AdagioConfiguration.getInstance().getDbDirectory())); 053 } 054 log.info(String.format(" JDBC Driver: %s", AdagioConfiguration.getInstance().getJdbcDriver())); 055 log.info(String.format(" JDBC URL: %s", AdagioConfiguration.getInstance().getJdbcURL())); 056 log.info(String.format(" JDBC Username: %s", AdagioConfiguration.getInstance().getJdbcUsername())); 057 String jdbcCatalog = config.getJdbcCatalog(); 058 if (StringUtils.isNotBlank(jdbcCatalog)) { 059 log.info(String.format(" JDBC Catalog: %s", jdbcCatalog)); 060 } 061 String jdbcSchema = config.getJdbcSchema(); 062 if (StringUtils.isNotBlank(jdbcSchema)) { 063 log.info(String.format(" JDBC Schema: %s", jdbcSchema)); 064 } 065 } 066 067 boolean isValidConnection = DaoUtils.isValidConnectionProperties(AdagioConfiguration.getInstance().getJdbcDriver(), 068 AdagioConfiguration.getInstance().getJdbcURL(), 069 AdagioConfiguration.getInstance().getJdbcUsername(), 070 AdagioConfiguration.getInstance().getJdbcPassword()); 071 072 if (!isValidConnection) { 073 log.warn("Connection error: could not generate changelog file."); 074 return; 075 } 076 DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class); 077 078 // Check if database is well loaded 079 if (!databaseSchemaService.isDbLoaded()) { 080 log.warn("Database not start ! Could not generate changelog file."); 081 return; 082 } 083 084 try { 085 Version actualDbVersion = databaseSchemaService.getDbVersion(); 086 if (actualDbVersion != null) { 087 log.info("Database schema version is: " + actualDbVersion.toString()); 088 } 089 090 Version modelVersion = AdagioConfiguration.getInstance().getVersion(); 091 log.info("Model version is: " + modelVersion.toString()); 092 } catch (AdagioTechnicalException e) { 093 log.error("Error while getting versions.", e); 094 } 095 096 File outputFile = config.getLiquibaseOutputFile(); 097 098 try { 099 log.info("Launching changelog file generation..."); 100 databaseSchemaService.generateDiffChangeLog(outputFile); 101 if (outputFile != null) { 102 log.info(String.format("Database changelog file successfullly generated at %s", outputFile)); 103 } 104 else { 105 log.info("Database changelog file successfullly generated."); 106 } 107 } catch (AdagioTechnicalException e) { 108 log.error("Error while generating changelog file.", e); 109 } catch (IOException e) { 110 log.error("Error while generating changelog file.", e); 111 } 112 } 113}