fr.ird.observe.toolkit.maven.plugin.PersistenceRunner Maven / Gradle / Ivy
package fr.ird.observe.toolkit.maven.plugin;
/*-
* #%L
* ObServe Toolkit :: Maven plugin
* %%
* Copyright (C) 2017 - 2022 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 fr.ird.observe.dto.db.configuration.topia.ObserveDataSourceConfigurationTopiaH2;
import fr.ird.observe.entities.ToolkitTopiaApplicationContextSupport;
import fr.ird.observe.entities.TopiaApplicationContextFactory;
import fr.ird.observe.test.DatabaseName;
import io.ultreia.java4all.util.Version;
import io.ultreia.java4all.util.Zips;
import org.nuiton.topia.persistence.script.SqlScriptWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
/**
* Created on 24/11/2021.
*
* @author Tony Chemit - [email protected]
* @since 5.0.56
*/
public abstract class PersistenceRunner extends MojoRunnable {
protected Path sourceDirectory;
protected Version modelVersion;
protected Version previousModelVersion;
public void setSourceDirectory(Path sourceDirectory) {
this.sourceDirectory = sourceDirectory;
}
public void setModelVersion(Version modelVersion) {
this.modelVersion = modelVersion;
}
public void setPreviousModelVersion(Version previousModelVersion) {
this.previousModelVersion = previousModelVersion;
}
@Override
public void init() {
super.init();
Objects.requireNonNull(sourceDirectory);
Objects.requireNonNull(modelVersion);
}
public Version getModelVersion() {
return modelVersion;
}
public ObserveDataSourceConfigurationTopiaH2 createConfiguration() {
Path localDbFile = getTemporaryPath().resolve("db").resolve(modelVersion.toString()).resolve("H2");
return createConfiguration(localDbFile);
}
public ObserveDataSourceConfigurationTopiaH2 createConfiguration(String classifier) {
Path localDbFile = getTemporaryPath().resolve("db").resolve(modelVersion.toString()).resolve(classifier);
return createConfiguration(localDbFile);
}
public ObserveDataSourceConfigurationTopiaH2 createConfiguration(Path localDbFile) {
ObserveDataSourceConfigurationTopiaH2 configurationTopiaH2 = new ObserveDataSourceConfigurationTopiaH2();
configurationTopiaH2.setLabel("Script creation: " + localDbFile.toFile().getName());
configurationTopiaH2.setLogin("sa");
configurationTopiaH2.setPassword("sa".toCharArray());
configurationTopiaH2.setDirectory(localDbFile.toFile());
configurationTopiaH2.setDbName("obstuna");
configurationTopiaH2.setAutoMigrate(false);
configurationTopiaH2.setShowMigrationProgression(false);
configurationTopiaH2.setShowMigrationSql(false);
configurationTopiaH2.setModelVersion(modelVersion);
configurationTopiaH2.setTemporaryDirectory(getTemporaryPath());
return configurationTopiaH2;
}
protected ToolkitTopiaApplicationContextSupport> createTopiaApplicationContext(ObserveDataSourceConfigurationTopiaH2 configuration) {
return TopiaApplicationContextFactory.get().getOrCreateTopiaApplicationContext0(configuration);
}
protected Path explodeArchive(Path archiveFile) {
Path exploded = getTemporaryPath().resolve("exploded");
if (Files.notExists(exploded)) {
try {
Files.createDirectories(exploded);
try (InputStream inputStream = Files.newInputStream(archiveFile)) {
Zips.uncompress(inputStream, exploded.toFile());
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
return exploded;
}
protected void createForVersion(Version version, Path exploded, Path targetPath, DatabaseName... databaseNames) {
Path sourcePath = exploded.resolve(version.toString());
Path versionTargetPath = targetPath.resolve(version.toString());
try {
Files.createDirectories(versionTargetPath);
for (DatabaseName databaseName : databaseNames) {
generate(databaseName, sourcePath, versionTargetPath);
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private void generate(DatabaseName database, Path sourcePath, Path targetPath) throws IOException {
try (SqlScriptWriter scriptWriter = SqlScriptWriter.builder(targetPath.resolve(database.name() + ".sql.gz")).gzip().keepCommentLine().keepEmptyLine().build()) {
database.generate(sourcePath, scriptWriter);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy