001package fr.ifremer.adagio.core.service; 002 003/* 004 * #%L 005 * SIH Allegro ObsDeb :: Core 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2013 - 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; 028import java.sql.Connection; 029import java.sql.SQLException; 030import java.sql.Statement; 031 032import javax.sql.DataSource; 033 034import org.apache.commons.lang3.StringUtils; 035import org.apache.commons.logging.Log; 036import org.apache.commons.logging.LogFactory; 037import org.nuiton.util.Version; 038import org.nuiton.util.VersionUtil; 039import org.springframework.beans.factory.annotation.Autowired; 040import org.springframework.jdbc.CannotGetJdbcConnectionException; 041import org.springframework.jdbc.datasource.DataSourceUtils; 042import org.springframework.stereotype.Service; 043 044import fr.ifremer.adagio.core.AdagioTechnicalException; 045import fr.ifremer.adagio.core.config.AdagioConfiguration; 046import fr.ifremer.adagio.core.dao.technical.DaoUtils; 047import fr.ifremer.adagio.core.dao.technical.DatabaseSchemaDao; 048import fr.ifremer.adagio.core.dao.technical.DatabaseSchemaUpdateException; 049import fr.ifremer.adagio.core.dao.technical.VersionNotFoundException; 050 051/** 052 * 053 * @author Lionel Touseau <lionel.touseau@e-is.pro> 054 */ 055@Service("databaseSchemaService") 056public class DatabaseSchemaServiceImpl implements DatabaseSchemaService { 057 058 /** Logger. */ 059 private static final Log log = 060 LogFactory.getLog(DatabaseSchemaServiceImpl.class); 061 062 @Autowired 063 private AdagioConfiguration config; 064 065 @Autowired 066 protected DatabaseSchemaDao databaseSchemaDao; 067 068 @Override 069 public Version getDbVersion() { 070 fr.ifremer.adagio.core.dao.technical.Version version; 071 try { 072 if (!isDbLoaded()) { 073 throw new VersionNotFoundException("db is not open"); 074 } 075 version = databaseSchemaDao.getSchemaVersion(); 076 } catch (VersionNotFoundException e) { 077 if (log.isWarnEnabled()) { 078 log.warn(e.getMessage()); 079 } 080 version = null; 081 } 082 return version == null ? null : VersionUtil.valueOf(version.toString()); 083 } 084 085 @Override 086 public Version getApplicationVersion() { 087 fr.ifremer.adagio.core.dao.technical.Version version = databaseSchemaDao.getSchemaVersionIfUpdate(); 088 return VersionUtil.valueOf(version.toString()); 089 } 090 091 @Override 092 public void updateSchema() { 093 try { 094 databaseSchemaDao.updateSchema(); 095 } catch (DatabaseSchemaUpdateException e) { 096 throw new AdagioTechnicalException(e.getCause()); 097 } 098 } 099 100 @Override 101 public boolean isDbLoaded() { 102 return databaseSchemaDao.isDbLoaded(); 103 } 104 105 @Override 106 public boolean isDbExists() { 107 return databaseSchemaDao.isDbExists(); 108 } 109 110 @Override 111 public void generateStatusReport(File outputFile) throws IOException { 112 if (outputFile == null || !outputFile.getParentFile().isDirectory() || !outputFile.canWrite()) { 113 log.error("Could not write intto the output file. Please make sure the given path is a valid path."); 114 return; 115 } 116 117 databaseSchemaDao.generateStatusReport(outputFile); 118 } 119 120 @Override 121 public void generateDiffReport(File outputFile) throws IOException { 122 if (outputFile == null || !outputFile.getParentFile().isDirectory() || !outputFile.canWrite()) { 123 log.error("Could not write intto the output file. Please make sure the given path is a valid path."); 124 return; 125 } 126 databaseSchemaDao.generateDiffReport(outputFile, config.getLiquibaseDiffTypes()); 127 } 128 129 @Override 130 public void generateDiffChangeLog(File outputFile) throws IOException { 131 if (outputFile == null || !outputFile.getParentFile().isDirectory() || !outputFile.canWrite()) { 132 log.error("Could not write intto the output file. Please make sure the given path is a valid path."); 133 return; 134 } 135 databaseSchemaDao.generateDiffChangeLog(outputFile, config.getLiquibaseDiffTypes()); 136 } 137}