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 org.apache.commons.lang3.StringUtils;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuiton.util.Version;
030
031import fr.ifremer.adagio.core.AdagioTechnicalException;
032import fr.ifremer.adagio.core.config.AdagioConfiguration;
033import fr.ifremer.adagio.core.dao.technical.DaoUtils;
034import fr.ifremer.adagio.core.service.DatabaseSchemaService;
035import fr.ifremer.adagio.core.service.ServiceLocator;
036
037public class DatabaseUpdateAction {
038    /* Logger */
039    private static final Log log = LogFactory.getLog(DatabaseUpdateAction.class);
040
041    public void run() {
042        AdagioConfiguration config = AdagioConfiguration.getInstance();
043
044        if (log.isInfoEnabled()) {
045            log.info("Starting database schema update...");
046
047            boolean isFileDatabase = DaoUtils.isFileDatabase(config.getJdbcDriver());
048            if (isFileDatabase) {
049                log.info(String.format(" Database directory: %s", AdagioConfiguration.getInstance().getDbDirectory()));
050            }
051            log.info(String.format(" JDBC Driver: %s", AdagioConfiguration.getInstance().getJdbcDriver()));
052            log.info(String.format(" JDBC URL: %s", AdagioConfiguration.getInstance().getJdbcURL()));
053            log.info(String.format(" JDBC Username: %s", AdagioConfiguration.getInstance().getJdbcUsername()));
054            String jdbcCatalog = config.getJdbcCatalog();
055            if (StringUtils.isNotBlank(jdbcCatalog)) {
056                log.info(String.format(" JDBC Catalog: %s", jdbcCatalog));
057            }
058            String jdbcSchema = config.getJdbcSchema();
059            if (StringUtils.isNotBlank(jdbcSchema)) {
060                log.info(String.format(" JDBC Schema: %s", jdbcSchema));
061            }
062        }
063
064        boolean isValidConnection = DaoUtils.isValidConnectionProperties(AdagioConfiguration.getInstance().getJdbcDriver(),
065                AdagioConfiguration.getInstance().getJdbcURL(),
066                AdagioConfiguration.getInstance().getJdbcUsername(),
067                AdagioConfiguration.getInstance().getJdbcPassword());
068
069        if (!isValidConnection) {
070            log.warn("Connection error: could not update the schema.");
071            return;
072        }
073        DatabaseSchemaService databaseSchemaService = ServiceLocator.instance().getService("databaseSchemaService", DatabaseSchemaService.class);
074
075        // Check if database is well loaded
076        if (!databaseSchemaService.isDbLoaded()) {
077            log.warn("Database not start ! Could not update the schema.");
078            return;
079        }
080
081        try {
082            Version actualDbVersion = databaseSchemaService.getDbVersion();
083            // DB could be null, is DB is empty (mantis #21013)
084            if (actualDbVersion != null) {
085                log.info("Database schema version is: " + actualDbVersion.toString());
086            }
087
088            Version expectedDbVersion = databaseSchemaService.getApplicationVersion();
089            log.info("Database schema version AFTER the update should be: " + expectedDbVersion.toString());
090        } catch (AdagioTechnicalException e) {
091            log.error("Error while getting versions.", e);
092        }
093
094        try {
095            log.info("Launching update...");
096            databaseSchemaService.updateSchema();
097            log.info("Database schema successfullly updated.");
098        } catch (AdagioTechnicalException e) {
099            log.error("Error while updating the database schema.", e);
100        }
101    }
102}