org.nuiton.topia.service.script.TopiaSqlScriptGeneratorServiceContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of topia-extension-script Show documentation
Show all versions of topia-extension-script Show documentation
ObServe ToPIA Script service module
package org.nuiton.topia.service.script;
/*-
* #%L
* ObServe Toolkit :: ToPIA Script service
* %%
* Copyright (C) 2017 - 2018 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 org.hibernate.boot.Metadata;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.nuiton.topia.persistence.HibernateAvailableSettings;
import org.nuiton.topia.persistence.TopiaApplicationContext;
import org.nuiton.topia.persistence.TopiaPersistenceContext;
import org.nuiton.topia.persistence.internal.AbstractTopiaPersistenceContext;
import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
import org.nuiton.topia.persistence.script.SqlScriptWriter;
import org.nuiton.topia.persistence.script.TopiaBlobsContainer;
import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
/**
* Created by tchemit on 13/05/2018.
*
* @author Tony Chemit - [email protected]
*/
public class TopiaSqlScriptGeneratorServiceContext implements Closeable {
private final TopiaSqlScriptGeneratorRequest request;
private final TopiaApplicationContext> sourceTopiaApplicationContext;
private final Set blobsContainerBuilders = new LinkedHashSet<>();
private TopiaPersistenceContext sourcePersistenceContext;
private TopiaMetadataModel topiaMetaModel;
private SqlScriptWriter writer;
public TopiaSqlScriptGeneratorServiceContext(TopiaSqlScriptGeneratorRequest request, TopiaApplicationContext sourceTopiaApplicationContext) {
this.request = request;
this.sourceTopiaApplicationContext = sourceTopiaApplicationContext;
}
public TopiaApplicationContext> getSourceTopiaApplicationContext() {
return sourceTopiaApplicationContext;
}
public void addSchemaExportScript(Class extends Dialect> dialectType, SchemaExport.Action action) throws IOException {
Path sqlScriptFile = Files.createTempFile("schemaExport_" + action.name() + "_", ".sql");
try {
Metadata hibernateMetadata = getSourcePersistenceContext().getHibernateSupport().getHibernateMetadata();
StandardServiceRegistry serviceRegistry = ((MetadataImplementor) hibernateMetadata).getMetadataBuildingOptions().getServiceRegistry();
Properties properties = new Properties();
properties.putAll(serviceRegistry.getService(ConfigurationService.class).getSettings());
properties.put(HibernateAvailableSettings.DIALECT, dialectType.getName());
BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
ssrBuilder.applySettings(properties);
StandardServiceRegistry registry = ssrBuilder.build();
try {
new SchemaExport()
.setOutputFile(sqlScriptFile.toFile().getAbsolutePath())
.setDelimiter(";")
.execute(EnumSet.of(TargetType.SCRIPT), action, hibernateMetadata, registry);
} finally {
StandardServiceRegistryBuilder.destroy(registry);
}
getWriter().writeScript(sqlScriptFile);
} finally {
if (Files.exists(sqlScriptFile)) {
Files.delete(sqlScriptFile);
}
}
}
public AbstractTopiaPersistenceContext getSourcePersistenceContext() {
if (sourcePersistenceContext == null) {
sourcePersistenceContext = sourceTopiaApplicationContext.newPersistenceContext();
}
return (AbstractTopiaPersistenceContext) sourcePersistenceContext;
}
@Override
public void close() throws IOException {
try {
if (sourcePersistenceContext != null) {
sourcePersistenceContext.close();
}
} finally {
if (writer != null) {
writer.close();
}
}
}
public SqlScriptWriter getWriter() {
if (writer == null) {
SqlScriptWriter.Builder writerBuilder = SqlScriptWriter.builder(request.getPath());
if (request.isGzip()) {
writerBuilder.gzip();
}
writer = writerBuilder.build();
}
return writer;
}
public Optional> getBlobsContainersBuilder() {
if (blobsContainerBuilders.isEmpty()) {
return Optional.empty();
}
return Optional.of(blobsContainerBuilders);
}
public void registerBlobsContainer(TopiaBlobsContainer.Builder builder) {
blobsContainerBuilders.add(builder);
}
public int getReadFetchSize() {
return request.getReadFetchSize();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy