001/*
002 *  Copyright 2020 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.core.migration.action.impl;
017
018import java.sql.Connection;
019import java.sql.SQLException;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.core.datasource.ConnectionHelper;
026import org.ametys.core.migration.MigrationException;
027import org.ametys.core.migration.action.data.ActionData;
028import org.ametys.core.migration.action.data.impl.SqlInitializationActionData;
029import org.ametys.core.migration.version.Version;
030import org.ametys.core.migration.version.impl.SqlVersion;
031import org.ametys.core.script.SQLScriptHelper;
032
033
034/**
035 * Initialization of an SQL table
036 * Similar to {@link SqlUpgradeAction} with a test about the existence of a table
037 */
038public class SqlInitializationAction extends SqlUpgradeAction
039{
040    @Override
041    public void doAction(ActionData actionData) throws MigrationException
042    {
043        if (!(actionData instanceof SqlInitializationActionData))
044        {
045            throw new MigrationException("SQL Initialization can only be created with a SQL action, this is not the case for action: " + actionData.toString());
046        }
047
048        SqlInitializationActionData sqlActionData = (SqlInitializationActionData) actionData;
049        Version version = actionData.getVersion();
050        
051        if (!(version instanceof SqlVersion))
052        {
053            throw new MigrationException("SQL Initialization can only be created with a SQL Version, this is not the case for action: " + actionData.toString());
054        }
055        
056        Connection connection = null;
057    
058        try
059        {
060            SqlVersion sqlVersion = (SqlVersion) version;
061            String datasourceId = sqlVersion.getDatasourceId();
062            connection = ConnectionHelper.getConnection(datasourceId);
063            // If no table configured, we consider it does not exists (maybe the script works with many tables or test it itself)
064            boolean tableExists = StringUtils.isNotBlank(sqlActionData.getTable()) ? SQLScriptHelper.tableExists(connection, sqlActionData.getTable()) : false;
065            if (tableExists)
066            {
067                // If the table exists, throw an exception, no initialization for this component, the admin will need to do a manual upgrade
068                throw new MigrationException("Impossible to initialize the version as the table '" + sqlActionData.getTable() + "' already exists. " + actionData.toString());
069            }
070            else
071            {
072                _upgrade(sqlActionData, sqlVersion);
073            }
074        }
075        catch (SQLException e)
076        {
077            throw new MigrationException("SQL exception when trying to upgrade: " + actionData.toString(), e);
078        }
079        finally
080        {
081            ConnectionHelper.cleanup(connection);
082        }
083    }
084    
085    @Override
086    public ActionData generateActionData(String versionNumber, Version version, String comment, String from, String type, String pluginName, Configuration configuration) throws MigrationException, ConfigurationException
087    {
088        return new SqlInitializationActionData(versionNumber, version, comment, from, type, pluginName, configuration);
089    }
090}