All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.topia.service.sql.batch.actions.CreateSchemaAction Maven / Gradle / Ivy

package org.nuiton.topia.service.sql.batch.actions;

/*
 * #%L
 * ObServe Toolkit :: ToPIA Extension
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.nuiton.topia.persistence.TopiaException;

/**
 * Created on 01/01/16.
 *
 * @author Tony Chemit - [email protected]
 * @since 3.0.1
 */
public class CreateSchemaAction extends AbstractSchemaAction {

    public static final String CREATE_SCHEMA_STATEMENT = "CREATE SCHEMA %s;\n";

    /** Logger */
    private static final Log log = LogFactory.getLog(CreateSchemaAction.class);

    public CreateSchemaAction(CreateSchemaRequest request, boolean showSql) {
        super(request, showSql);
    }

    protected String produceSql(Class dialectType, Path temporaryDirectory) throws IOException {

        try {

            Path sqlScriptFile = temporaryDirectory.resolve("replicateSchema_" + System.nanoTime() + ".sql");

            generateSqlInFile(dialectType, sqlScriptFile, SchemaExport.Action.CREATE);

            String sqlStatements = "";
            if (request.isAddSchema()) {
                for (String schemaName : getSchemaNames()) {
                    sqlStatements += String.format(CREATE_SCHEMA_STATEMENT, schemaName);
                }
            }

            sqlStatements += new String(Files.readAllBytes(sqlScriptFile));
            if (showSql) {
                if (log.isInfoEnabled()) {
                    log.info(sqlStatements);
                }
            }
            Files.delete(sqlScriptFile);
            return sqlStatements;

        } catch (HibernateException eee) {
            throw new TopiaException(String.format("Could not create schema for reason: %s", eee.getMessage()), eee);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy