Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe
Commits:
-
a864fb66
by Tony Chemit at 2022-03-16T18:47:49+01:00
11 changed files:
- client/core/src/main/java/fr/ird/observe/client/datasource/api/ObserveSwingDataSource.java
- client/datasource/editor/api/src/main/java/fr/ird/observe/client/datasource/editor/api/wizard/DataSourceHelper.java
- client/datasource/editor/api/src/main/java/fr/ird/observe/client/datasource/editor/api/wizard/StorageBackupUILauncher.java
- client/datasource/editor/api/src/main/java/fr/ird/observe/client/datasource/editor/api/wizard/StorageUIModel.java
- client/datasource/editor/api/src/main/java/fr/ird/observe/client/datasource/editor/api/wizard/launchers/CreateLauncher.java
- + core/persistence/java/src/main/java/fr/ird/observe/entities/DataSourceCreateHelper.java
- core/persistence/java/src/main/java/fr/ird/observe/entities/ObserveTopiaApplicationContext.java
- core/services/client/src/main/java/fr/ird/observe/services/client/AnonymousServiceClient.java
- core/services/local/src/main/java/fr/ird/observe/services/local/service/AnonymousServiceLocalSupport.java
- server/core/src/main/filtered-resources/META-INF/mapping-api-client.wm
- server/core/src/main/java/fr/ird/observe/server/controller/AnonymousServiceRestApi.java
Changes:
| ... | ... | @@ -40,6 +40,7 @@ import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException; |
| 40 | 40 | import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
| 41 | 41 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 42 | 42 | import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
| 43 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 43 | 44 | import fr.ird.observe.dto.db.configuration.IncompatibleDataSourceCreateConfigurationException;
|
| 44 | 45 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 45 | 46 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfigurationAndConnection;
|
| ... | ... | @@ -241,23 +242,26 @@ public class ObserveSwingDataSource extends AbstractJavaBean implements ObserveS |
| 241 | 242 | fireOpened();
|
| 242 | 243 | }
|
| 243 | 244 | |
| 244 | - public void createFrom(ObserveSwingDataSource localSource)
|
|
| 245 | + public void create(DataSourceCreateConfigurationDto createDto)
|
|
| 245 | 246 | throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException,
|
| 246 | 247 | DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
| 247 | - DataSourceCreateConfigurationDto createDto = new DataSourceCreateConfigurationDto();
|
|
| 248 | - createDto.setLeaveOpenSource(true);
|
|
| 249 | - createDto.setImportReferentialDataSourceConfiguration(localSource.getConfiguration());
|
|
| 250 | - create(createDto);
|
|
| 248 | + checkIsNotOpen();
|
|
| 249 | + fireNewMessage(t("observe.ui.datasource.storage.message.creating", getLabel()));
|
|
| 250 | + fireOpening();
|
|
| 251 | + AnonymousService dataSourceService = getAnonymousService();
|
|
| 252 | + setConnection(dataSourceService.create(getConfiguration(), createDto));
|
|
| 253 | + fireNewMessage(t("observe.ui.datasource.storage.message.created", getLabel()));
|
|
| 254 | + fireOpened();
|
|
| 251 | 255 | }
|
| 252 | 256 | |
| 253 | - public void create(DataSourceCreateConfigurationDto createDto)
|
|
| 257 | + public void createAndImport(DataSourceImportConfigurationDto createDto)
|
|
| 254 | 258 | throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException,
|
| 255 | 259 | DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
| 256 | 260 | checkIsNotOpen();
|
| 257 | 261 | fireNewMessage(t("observe.ui.datasource.storage.message.creating", getLabel()));
|
| 258 | 262 | fireOpening();
|
| 259 | 263 | AnonymousService dataSourceService = getAnonymousService();
|
| 260 | - setConnection(dataSourceService.create(getConfiguration(), createDto));
|
|
| 264 | + setConnection(dataSourceService.createAndImport(getConfiguration(), createDto));
|
|
| 261 | 265 | fireNewMessage(t("observe.ui.datasource.storage.message.created", getLabel()));
|
| 262 | 266 | fireOpened();
|
| 263 | 267 | }
|
| ... | ... | @@ -259,6 +259,31 @@ public class DataSourceHelper { |
| 259 | 259 | throw new IllegalStateException("Can't come here");
|
| 260 | 260 | }
|
| 261 | 261 | }
|
| 262 | + if (model.getChooseDb().doImportData()) {
|
|
| 263 | + |
|
| 264 | + String dbLabel = I18n.n("observe.ui.datasource.storage.label.data.import.db");
|
|
| 265 | + switch (model.getChooseDb().getDataImportMode()) {
|
|
| 266 | + case IMPORT_EXTERNAL_DUMP:
|
|
| 267 | + File dumpFile = ObstunaAdminAction.CREATE.equals(model.getAdminAction()) ? model.getDataSourceModel().getDumpFile() : model.getDumpFile();
|
|
| 268 | + importReferenceConfig.setImportDatabase(TopiaSqlScript.of(dumpFile.toURI()));
|
|
| 269 | + break;
|
|
| 270 | + case IMPORT_REMOTE_STORAGE: {
|
|
| 271 | + // import referentiel from a remote db
|
|
| 272 | + ObserveDataSourceConfigurationTopiaPG dataSourceConfig = toPGStorageConfig(model.getDataSourceModel(), dbLabel);
|
|
| 273 | + dataSourceConfig.setCanMigrate(false);
|
|
| 274 | + importReferenceConfig.setImportDataConfiguration(dataSourceConfig, model.getSelectDataModel().getSelectedDataIds());
|
|
| 275 | + }
|
|
| 276 | + break;
|
|
| 277 | + case IMPORT_SERVER_STORAGE: {
|
|
| 278 | + // import referentiel from a server db
|
|
| 279 | + ObserveDataSourceConfigurationRest dataSourceConfig = toRestStorageConfig(model.getDataSourceModel(), dbLabel);
|
|
| 280 | + importReferenceConfig.setImportDataConfiguration(dataSourceConfig, model.getSelectDataModel().getSelectedDataIds());
|
|
| 281 | + break;
|
|
| 282 | + }
|
|
| 283 | + default:
|
|
| 284 | + throw new IllegalStateException("Can't come here");
|
|
| 285 | + }
|
|
| 286 | + }
|
|
| 262 | 287 | return importReferenceConfig;
|
| 263 | 288 | }
|
| 264 | 289 | |
| ... | ... | @@ -311,6 +336,7 @@ public class DataSourceHelper { |
| 311 | 336 | return importDataSource;
|
| 312 | 337 | }
|
| 313 | 338 | |
| 339 | + |
|
| 314 | 340 | static ObserveDataSourceConfigurationTopiaH2 toH2StorageConfig(StorageUIModel model, String label) {
|
| 315 | 341 | ObserveDataSourceConfigurationTopiaH2 h2Config = model.getLocalConfig().getConfiguration();
|
| 316 | 342 | return ObserveDataSourceConfigurationTopiaH2.create(
|
| ... | ... | @@ -33,6 +33,7 @@ import fr.ird.observe.dto.db.BabModelVersionException; |
| 33 | 33 | import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException;
|
| 34 | 34 | import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
| 35 | 35 | import fr.ird.observe.dto.db.configuration.DataSourceConnectMode;
|
| 36 | +import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
|
| 36 | 37 | import fr.ird.observe.dto.db.configuration.DataSourceCreateMode;
|
| 37 | 38 | import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
| 38 | 39 | import fr.ird.observe.dto.db.configuration.DataSourceInitMode;
|
| ... | ... | @@ -137,7 +138,10 @@ public class StorageBackupUILauncher extends StorageUILauncher { |
| 137 | 138 | try (ObserveSwingDataSource targetSource = dataSourcesManager.newTemporaryH2DataSource("Backup-" + backupFile.getName())) {
|
| 138 | 139 | |
| 139 | 140 | // create temporary source
|
| 140 | - targetSource.createFrom(incomingSource);
|
|
| 141 | + DataSourceCreateConfigurationDto createDto = new DataSourceCreateConfigurationDto();
|
|
| 142 | + createDto.setLeaveOpenSource(true);
|
|
| 143 | + createDto.setImportReferentialDataSourceConfiguration(incomingSource.getConfiguration());
|
|
| 144 | + targetSource.create(createDto);
|
|
| 141 | 145 | progressModel.increments();
|
| 142 | 146 | |
| 143 | 147 | // insert selected data
|
| ... | ... | @@ -671,7 +671,9 @@ public class StorageUIModel extends WizardModel<StorageStep> implements WithClie |
| 671 | 671 | }
|
| 672 | 672 | break;
|
| 673 | 673 | case CONFIG_REFERENTIAL:
|
| 674 | - |
|
| 674 | + if (chooseDb.getReferentielImportMode() == null) {
|
|
| 675 | + return false;
|
|
| 676 | + }
|
|
| 675 | 677 | switch (chooseDb.getReferentielImportMode()) {
|
| 676 | 678 | |
| 677 | 679 | case EMPTY:
|
| ... | ... | @@ -703,7 +705,9 @@ public class StorageUIModel extends WizardModel<StorageStep> implements WithClie |
| 703 | 705 | break;
|
| 704 | 706 | |
| 705 | 707 | case CONFIG_DATA:
|
| 706 | - |
|
| 708 | + if (chooseDb.getDataImportMode() == null) {
|
|
| 709 | + return false;
|
|
| 710 | + }
|
|
| 707 | 711 | switch (chooseDb.getDataImportMode()) {
|
| 708 | 712 | |
| 709 | 713 | case EMPTY:
|
| ... | ... | @@ -22,26 +22,28 @@ package fr.ird.observe.client.datasource.editor.api.wizard.launchers; |
| 22 | 22 | * #L%
|
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | +import com.google.common.collect.ArrayListMultimap;
|
|
| 26 | +import com.google.common.collect.Multimap;
|
|
| 25 | 27 | import fr.ird.observe.client.datasource.api.ObserveSwingDataSource;
|
| 26 | 28 | import fr.ird.observe.client.datasource.editor.api.wizard.DataSourceHelper;
|
| 27 | 29 | import fr.ird.observe.client.datasource.editor.api.wizard.ObstunaAdminAction;
|
| 28 | 30 | import fr.ird.observe.client.datasource.editor.api.wizard.StorageUI;
|
| 29 | 31 | import fr.ird.observe.client.datasource.editor.api.wizard.StorageUIModel;
|
| 30 | -import fr.ird.observe.dto.db.BabModelVersionException;
|
|
| 31 | -import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException;
|
|
| 32 | -import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
|
| 33 | 32 | import fr.ird.observe.dto.db.configuration.DataSourceConnectMode;
|
| 34 | 33 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 35 | -import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
|
| 36 | -import fr.ird.observe.dto.db.configuration.IncompatibleDataSourceCreateConfigurationException;
|
|
| 34 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 37 | 35 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 38 | 36 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceInformation;
|
| 39 | -import fr.ird.observe.navigation.tree.selection.SelectionTreeModel;
|
|
| 37 | +import io.ultreia.java4all.util.Version;
|
|
| 40 | 38 | import org.apache.logging.log4j.LogManager;
|
| 41 | 39 | import org.apache.logging.log4j.Logger;
|
| 42 | 40 | import org.nuiton.jaxx.runtime.JAXXContext;
|
| 41 | +import org.nuiton.topia.persistence.script.TopiaSqlScript;
|
|
| 42 | +import org.nuiton.topia.service.sql.request.CreateDatabaseRequest;
|
|
| 43 | 43 | |
| 44 | 44 | import java.awt.Window;
|
| 45 | +import java.util.Collection;
|
|
| 46 | +import java.util.Map;
|
|
| 45 | 47 | import java.util.Objects;
|
| 46 | 48 | import java.util.Set;
|
| 47 | 49 | |
| ... | ... | @@ -54,6 +56,7 @@ import java.util.Set; |
| 54 | 56 | public class CreateLauncher extends RemoteUILauncher {
|
| 55 | 57 | private static final Logger log = LogManager.getLogger(CreateLauncher.class);
|
| 56 | 58 | DataSourceCreateConfigurationDto createConfigurationDto;
|
| 59 | + DataSourceImportConfigurationDto importConfiguration;
|
|
| 57 | 60 | |
| 58 | 61 | public CreateLauncher(JAXXContext context, Window frame, DataSourceConnectMode connectMode) {
|
| 59 | 62 | super(ObstunaAdminAction.CREATE, context, frame, connectMode);
|
| ... | ... | @@ -67,26 +70,82 @@ public class CreateLauncher extends RemoteUILauncher { |
| 67 | 70 | //FIXME This logic should be done in StorageModel not here, like this!
|
| 68 | 71 | model.getChooseDb().setCanMigrate(true);
|
| 69 | 72 | model.getRemoteConfig().setCanCreateDatabase(true);
|
| 73 | + model.getServerConfig().setCanCreateDatabase(true);
|
|
| 70 | 74 | super.init(ui);
|
| 71 | 75 | }
|
| 72 | 76 | |
| 73 | 77 | @Override
|
| 74 | - protected ObserveSwingDataSource initTask(StorageUIModel model) throws Exception {
|
|
| 78 | + protected ObserveSwingDataSource initTask(StorageUIModel model) {
|
|
| 75 | 79 | ObserveSwingDataSource dataSource = getDataSource(model);
|
| 76 | 80 | createConfigurationDto = DataSourceHelper.toImportReferentielSourceConfig(model);
|
| 81 | + boolean importReferential = createConfigurationDto.isImportReferential();
|
|
| 82 | + boolean importData = createConfigurationDto.isImportData();
|
|
| 83 | + if (!importReferential && !importData) {
|
|
| 84 | + importConfiguration = null;
|
|
| 85 | + return dataSource;
|
|
| 86 | + }
|
|
| 87 | + Version version = dataSource.getConfiguration().getModelVersion();
|
|
| 88 | + importConfiguration = new DataSourceImportConfigurationDto();
|
|
| 89 | + importConfiguration.setMigrate(createConfigurationDto.isMigrate());
|
|
| 90 | + |
|
| 77 | 91 | ObserveDataSourceConfiguration importReferentialConfig = createConfigurationDto.getImportReferentialDataSourceConfiguration();
|
| 92 | + ObserveDataSourceConfiguration importDataConfig = createConfigurationDto.getImportDataDataSourceConfiguration();
|
|
| 93 | + boolean importStandaloneReferential = importReferential;
|
|
| 94 | + |
|
| 78 | 95 | if (importReferentialConfig != null) {
|
| 79 | - log.info(String.format("Use referentiel import data source %s", importReferentialConfig.getLabel()));
|
|
| 80 | - checkImportDbVersion(model, importReferentialConfig);
|
|
| 96 | + log.info(String.format("Will use referentiel import data source %s", importReferentialConfig.getLabel()));
|
|
| 81 | 97 | }
|
| 82 | - // data import
|
|
| 83 | - ObserveSwingDataSource importDataConfig = DataSourceHelper.toImportDataSourceConfig(model);
|
|
| 84 | 98 | if (importDataConfig != null) {
|
| 85 | - log.info(String.format("Use data import data source %s", importDataConfig.getLabel()));
|
|
| 86 | - checkImportDbVersion(model, importDataConfig.getConfiguration());
|
|
| 87 | - SelectionTreeModel dataModel = model.getSelectDataModel();
|
|
| 88 | - Set<String> importDataIds = dataModel.getSelectedDataIds();
|
|
| 89 | - createConfigurationDto.setImportDataConfiguration(importDataConfig.getConfiguration(), importDataIds);
|
|
| 99 | + log.info(String.format("Will use data import data source %s", importDataConfig.getLabel()));
|
|
| 100 | + importStandaloneReferential = importReferentialConfig != null && !importReferentialConfig.equals(importDataConfig);
|
|
| 101 | + }
|
|
| 102 | + |
|
| 103 | + boolean referentialImported = false;
|
|
| 104 | + if (importStandaloneReferential) {
|
|
| 105 | + try (ObserveSwingDataSource importDataSource = Objects.requireNonNull(ui.getModel().getDataSourcesManager().newDataSource(importReferentialConfig))) {
|
|
| 106 | + log.info(String.format("Prepare standalone referential dump from %s", importDataSource.getLabel()));
|
|
| 107 | + ObserveSwingDataSource.doOpenSource(importDataSource);
|
|
| 108 | + model.checkImportDbVersion(importDataSource);
|
|
| 109 | + CreateDatabaseRequest request = CreateDatabaseRequest.builder(true, version).addStandaloneTables().build();
|
|
| 110 | + TopiaSqlScript createSqlScript = importDataSource.getDataSourceService().produceCreateSqlScript(request);
|
|
| 111 | + importConfiguration.setReferentialDump(createSqlScript);
|
|
| 112 | + }
|
|
| 113 | + referentialImported = true;
|
|
| 114 | + }
|
|
| 115 | + |
|
| 116 | + if (importData) {
|
|
| 117 | + CreateDatabaseRequest.Builder requestBuilder = CreateDatabaseRequest.builder(true, version);
|
|
| 118 | + if (!referentialImported) {
|
|
| 119 | + log.info("Add referential to data dump");
|
|
| 120 | + requestBuilder.addStandaloneTables();
|
|
| 121 | + }
|
|
| 122 | + Set<String> importDataIds = createConfigurationDto.getImportDataIds();
|
|
| 123 | + Multimap<String, String> idsByType = ArrayListMultimap.create();
|
|
| 124 | + for (String importDataId : importDataIds) {
|
|
| 125 | + //FIXME
|
|
| 126 | + String className = importDataId.contains(".ll.") ? "fr.ird.observe.entities.data.ll.common.Trip" : "fr.ird.observe.entities.data.ps.common.Trip";
|
|
| 127 | + idsByType.put(className, importDataId);
|
|
| 128 | + }
|
|
| 129 | + for (Map.Entry<String, Collection<String>> entry : idsByType.asMap().entrySet()) {
|
|
| 130 | + String type = entry.getKey();
|
|
| 131 | + Set<String> dataIds = Set.copyOf(entry.getValue());
|
|
| 132 | + log.info(String.format("Add %d data of type: %s", dataIds.size(), type));
|
|
| 133 | + requestBuilder.dataIdsToAdd(type, dataIds);
|
|
| 134 | + }
|
|
| 135 | + try (ObserveSwingDataSource importDataSource = Objects.requireNonNull(ui.getModel().getDataSourcesManager().newDataSource(importDataConfig))) {
|
|
| 136 | + log.info(String.format("Prepare data dump from %s", importDataSource.getLabel()));
|
|
| 137 | + ObserveSwingDataSource.doOpenSource(importDataSource);
|
|
| 138 | + model.checkImportDbVersion(importDataSource);
|
|
| 139 | + CreateDatabaseRequest request = requestBuilder.build();
|
|
| 140 | + TopiaSqlScript dump = importDataSource.getDataSourceService().produceCreateSqlScript(request);
|
|
| 141 | + if (referentialImported) {
|
|
| 142 | + // already a referential dump, so just data dump
|
|
| 143 | + importConfiguration.setDataDump(dump);
|
|
| 144 | + } else {
|
|
| 145 | + // referential dump not existing, will use this one
|
|
| 146 | + importConfiguration.setReferentialDump(dump);
|
|
| 147 | + }
|
|
| 148 | + }
|
|
| 90 | 149 | }
|
| 91 | 150 | return dataSource;
|
| 92 | 151 | }
|
| ... | ... | @@ -95,7 +154,11 @@ public class CreateLauncher extends RemoteUILauncher { |
| 95 | 154 | protected void execute(ObserveSwingDataSource dataSource, ObserveDataSourceInformation dataSourceInformation) throws Exception {
|
| 96 | 155 | log.info("Create db...");
|
| 97 | 156 | try {
|
| 98 | - dataSource.create(createConfigurationDto);
|
|
| 157 | + if (importConfiguration != null) {
|
|
| 158 | + dataSource.createAndImport(importConfiguration);
|
|
| 159 | + } else {
|
|
| 160 | + dataSource.create(createConfigurationDto);
|
|
| 161 | + }
|
|
| 99 | 162 | log.info(String.format("Open [%s] and create it.", dataSource.getLabel()));
|
| 100 | 163 | } finally {
|
| 101 | 164 | if (dataSource.isOpen()) {
|
| ... | ... | @@ -104,12 +167,5 @@ public class CreateLauncher extends RemoteUILauncher { |
| 104 | 167 | }
|
| 105 | 168 | }
|
| 106 | 169 | |
| 107 | - void checkImportDbVersion(StorageUIModel model, ObserveDataSourceConfiguration dataSourceConfig) throws DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException, BabModelVersionException, IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException {
|
|
| 108 | - try (ObserveSwingDataSource importDataSource = Objects.requireNonNull(model.getDataSourcesManager().newDataSource(dataSourceConfig))) {
|
|
| 109 | - importDataSource.open();
|
|
| 110 | - model.checkImportDbVersion(importDataSource);
|
|
| 111 | - }
|
|
| 112 | - }
|
|
| 113 | - |
|
| 114 | 170 | }
|
| 115 | 171 |
| 1 | +package fr.ird.observe.entities;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Core :: Persistence :: Java
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2022 IRD, Code Lutin, Ultreia.io
|
|
| 8 | + * %%
|
|
| 9 | + * This program is free software: you can redistribute it and/or modify
|
|
| 10 | + * it under the terms of the GNU General Public License as
|
|
| 11 | + * published by the Free Software Foundation, either version 3 of the
|
|
| 12 | + * License, or (at your option) any later version.
|
|
| 13 | + *
|
|
| 14 | + * This program is distributed in the hope that it will be useful,
|
|
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 17 | + * GNU General Public License for more details.
|
|
| 18 | + *
|
|
| 19 | + * You should have received a copy of the GNU General Public
|
|
| 20 | + * License along with this program. If not, see
|
|
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
| 22 | + * #L%
|
|
| 23 | + */
|
|
| 24 | + |
|
| 25 | +import com.google.common.collect.ArrayListMultimap;
|
|
| 26 | +import com.google.common.collect.Multimap;
|
|
| 27 | +import fr.ird.observe.dto.db.BabModelVersionException;
|
|
| 28 | +import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException;
|
|
| 29 | +import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
|
| 30 | +import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
|
| 31 | +import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
|
| 32 | +import fr.ird.observe.dto.db.configuration.topia.ObserveDataSourceConfigurationTopiaH2;
|
|
| 33 | +import io.ultreia.java4all.util.Version;
|
|
| 34 | +import org.apache.logging.log4j.LogManager;
|
|
| 35 | +import org.apache.logging.log4j.Logger;
|
|
| 36 | +import org.nuiton.topia.persistence.TopiaEntity;
|
|
| 37 | +import org.nuiton.topia.persistence.TopiaIdFactory;
|
|
| 38 | +import org.nuiton.topia.persistence.script.TopiaSqlScript;
|
|
| 39 | +import org.nuiton.topia.service.migration.TopiaMigrationService;
|
|
| 40 | +import org.nuiton.topia.service.sql.request.CreateDatabaseRequest;
|
|
| 41 | + |
|
| 42 | +import java.io.File;
|
|
| 43 | +import java.io.IOException;
|
|
| 44 | +import java.nio.file.Files;
|
|
| 45 | +import java.nio.file.Path;
|
|
| 46 | +import java.util.Collection;
|
|
| 47 | +import java.util.Map;
|
|
| 48 | +import java.util.Set;
|
|
| 49 | + |
|
| 50 | +/**
|
|
| 51 | + * Created on 16/03/2022.
|
|
| 52 | + *
|
|
| 53 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 54 | + * @since 6.0.7
|
|
| 55 | + */
|
|
| 56 | +public class DataSourceCreateHelper {
|
|
| 57 | + |
|
| 58 | + private static final Logger log = LogManager.getLogger(DataSourceCreateHelper.class);
|
|
| 59 | + |
|
| 60 | + public static void create(ToolkitTopiaApplicationContextSupport<?> applicationContext, ObserveDataSourceConfiguration dataSourceConfiguration, DataSourceCreateConfigurationDto dataSourceCreateConfiguration) throws BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
|
| 61 | + log.debug(String.format("Create topia application context: %s", applicationContext));
|
|
| 62 | + Path temporaryDirectory = dataSourceConfiguration.getTemporaryDirectory();
|
|
| 63 | + if (dataSourceCreateConfiguration.isImportDatabase()) {
|
|
| 64 | + log.info("Create new database from a script.");
|
|
| 65 | + TopiaSqlScript importDatabase = dataSourceCreateConfiguration.getImportDatabase();
|
|
| 66 | + boolean migrate = dataSourceCreateConfiguration.isMigrate();
|
|
| 67 | + TopiaMigrationService migrationService = (TopiaMigrationService) applicationContext.getMigrationService();
|
|
| 68 | + if (dataSourceConfiguration.isLocal()) {
|
|
| 69 | + applicationContext.executeSqlStatements(importDatabase);
|
|
| 70 | + if (migrate) {
|
|
| 71 | + migrationService.createSchemaIfNotExist();
|
|
| 72 | + migrationService.runSchemaMigration();
|
|
| 73 | + }
|
|
| 74 | + } else { // base postgres
|
|
| 75 | + // do import in temporary h2 database
|
|
| 76 | + File tmpDir;
|
|
| 77 | + try {
|
|
| 78 | + tmpDir = Files.createTempDirectory(temporaryDirectory, "obstuna").toFile();
|
|
| 79 | + } catch (IOException e) {
|
|
| 80 | + throw new IllegalStateException("could not create temporary directory ", e);
|
|
| 81 | + }
|
|
| 82 | + ObserveDataSourceConfigurationTopiaH2 temporaryConfiguration = applicationContext.createTemporaryConfiguration(tmpDir, dataSourceConfiguration.getModelVersion());
|
|
| 83 | + ObserveTopiaApplicationContext temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(temporaryConfiguration, false);
|
|
| 84 | + temporaryTopiaApplicationContext.executeSqlStatements(importDatabase);
|
|
| 85 | + if (migrate) {
|
|
| 86 | + temporaryTopiaApplicationContext.getMigrationService().createSchemaIfNotExist();
|
|
| 87 | + temporaryTopiaApplicationContext.getMigrationService().runSchemaMigration();
|
|
| 88 | + }
|
|
| 89 | + CreateDatabaseRequest request = CreateDatabaseRequest.builder(true, dataSourceConfiguration.getModelVersion()).addSchema().addStandaloneTables().addAllData().build();
|
|
| 90 | + TopiaSqlScript dump = applicationContext.getSqlService().consume(request);
|
|
| 91 | + applicationContext.executeSqlStatements(dump);
|
|
| 92 | + migrationService.createSchemaIfNotExist();
|
|
| 93 | + migrationService.runSchemaMigration();
|
|
| 94 | + temporaryTopiaApplicationContext.close();
|
|
| 95 | + File databaseFile = temporaryConfiguration.getDatabaseFile();
|
|
| 96 | + if (!databaseFile.delete()) {
|
|
| 97 | + throw new IllegalStateException("could not delete " + databaseFile);
|
|
| 98 | + }
|
|
| 99 | + }
|
|
| 100 | + } else {
|
|
| 101 | + boolean importReferential = dataSourceCreateConfiguration.isImportReferential();
|
|
| 102 | + boolean importData = dataSourceCreateConfiguration.isImportData();
|
|
| 103 | + // si le referentiel n'est pas importé on aliment la table lastUpdateDate
|
|
| 104 | + if (!importReferential) {
|
|
| 105 | + applicationContext.insertLastUpdateDate();
|
|
| 106 | + }
|
|
| 107 | + boolean importStandaloneReferential = importReferential;
|
|
| 108 | + if (importReferential && importData) {
|
|
| 109 | + // Si on est sur la même source de données pour les deux imports, on fait tout sur la même base temporaire
|
|
| 110 | + ObserveDataSourceConfiguration importReferentialDataSourceConfiguration = dataSourceCreateConfiguration.getImportReferentialDataSourceConfiguration();
|
|
| 111 | + ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportDataDataSourceConfiguration();
|
|
| 112 | + importStandaloneReferential = !importReferentialDataSourceConfiguration.equals(importDataSourceConfiguration);
|
|
| 113 | + }
|
|
| 114 | + boolean referentialImported = false;
|
|
| 115 | + if (importStandaloneReferential) {
|
|
| 116 | + log.info("Import referential.");
|
|
| 117 | + ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportReferentialDataSourceConfiguration();
|
|
| 118 | + if (importDataSourceConfiguration.getTemporaryDirectory() == null) {
|
|
| 119 | + importDataSourceConfiguration.setTemporaryDirectory(temporaryDirectory);
|
|
| 120 | + }
|
|
| 121 | + Version dbVersion = Version.valueOf(applicationContext.getModelVersion());
|
|
| 122 | + CreateDatabaseRequest request = CreateDatabaseRequest.builder(!dataSourceConfiguration.isLocal(), dbVersion)
|
|
| 123 | + .addStandaloneTables().build();
|
|
| 124 | + TopiaSqlScript referentialDump;
|
|
| 125 | + ObserveTopiaApplicationContext temporaryTopiaApplicationContext = null;
|
|
| 126 | + try {
|
|
| 127 | + temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(importDataSourceConfiguration, false);
|
|
| 128 | + referentialDump = temporaryTopiaApplicationContext.getSqlService().consume(request);
|
|
| 129 | + applicationContext.executeSqlStatements(referentialDump);
|
|
| 130 | + } finally {
|
|
| 131 | + if (!dataSourceCreateConfiguration.isLeaveOpenSource() && temporaryTopiaApplicationContext != null) {
|
|
| 132 | + temporaryTopiaApplicationContext.close();
|
|
| 133 | + }
|
|
| 134 | + }
|
|
| 135 | + referentialImported = true;
|
|
| 136 | + }
|
|
| 137 | + |
|
| 138 | + if (importData) {
|
|
| 139 | + ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportDataDataSourceConfiguration();
|
|
| 140 | + if (importDataSourceConfiguration.getTemporaryDirectory() == null) {
|
|
| 141 | + importDataSourceConfiguration.setTemporaryDirectory(temporaryDirectory);
|
|
| 142 | + }
|
|
| 143 | + Version dbVersion = Version.valueOf(applicationContext.getModelVersion());
|
|
| 144 | + CreateDatabaseRequest.Builder requestBuilder = CreateDatabaseRequest.builder(!dataSourceConfiguration.isLocal(), dbVersion);
|
|
| 145 | + if (!referentialImported) {
|
|
| 146 | + log.info("Get referential.");
|
|
| 147 | + requestBuilder.addStandaloneTables();
|
|
| 148 | + }
|
|
| 149 | + |
|
| 150 | + // Récupération du dump qui contient les données
|
|
| 151 | + Set<String> importDataIds = dataSourceCreateConfiguration.getImportDataIds();
|
|
| 152 | + TopiaIdFactory idFactory = applicationContext.getTopiaIdFactory();
|
|
| 153 | + Multimap<String, String> idsByType = ArrayListMultimap.create();
|
|
| 154 | + for (String importDataId : importDataIds) {
|
|
| 155 | + Class<TopiaEntity> className = idFactory.getClassName(importDataId);
|
|
| 156 | + idsByType.put(className.getName(), importDataId);
|
|
| 157 | + }
|
|
| 158 | + log.info(String.format("Get data: %s", importDataIds));
|
|
| 159 | + for (Map.Entry<String, Collection<String>> entry : idsByType.asMap().entrySet()) {
|
|
| 160 | + requestBuilder.dataIdsToAdd(entry.getKey(), Set.copyOf(entry.getValue()));
|
|
| 161 | + }
|
|
| 162 | + TopiaSqlScript dataDump;
|
|
| 163 | + ObserveTopiaApplicationContext temporaryTopiaApplicationContext = null;
|
|
| 164 | + CreateDatabaseRequest request = requestBuilder.build();
|
|
| 165 | + try {
|
|
| 166 | + temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(importDataSourceConfiguration, false);
|
|
| 167 | + dataDump = temporaryTopiaApplicationContext.getSqlService().consume(request);
|
|
| 168 | + } finally {
|
|
| 169 | + if (!dataSourceCreateConfiguration.isLeaveOpenSource() && temporaryTopiaApplicationContext != null) {
|
|
| 170 | + temporaryTopiaApplicationContext.close();
|
|
| 171 | + }
|
|
| 172 | + }
|
|
| 173 | + log.info(String.format("Import data%s", request.isAddStandaloneTables() ? " and referential." : "."));
|
|
| 174 | + applicationContext.executeSqlStatements(dataDump);
|
|
| 175 | + }
|
|
| 176 | + }
|
|
| 177 | + }
|
|
| 178 | + |
|
| 179 | +} |
| ... | ... | @@ -22,14 +22,12 @@ package fr.ird.observe.entities; |
| 22 | 22 | * #L%
|
| 23 | 23 | */
|
| 24 | 24 | |
| 25 | -import com.google.common.collect.ArrayListMultimap;
|
|
| 26 | -import com.google.common.collect.Multimap;
|
|
| 27 | 25 | import fr.ird.observe.dto.db.BabModelVersionException;
|
| 28 | 26 | import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException;
|
| 29 | 27 | import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
| 30 | 28 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 29 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 31 | 30 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 32 | -import fr.ird.observe.dto.db.configuration.topia.ObserveDataSourceConfigurationTopiaH2;
|
|
| 33 | 31 | import fr.ird.observe.dto.referential.ReferentialDto;
|
| 34 | 32 | import fr.ird.observe.entities.data.DataGroupByEntity;
|
| 35 | 33 | import fr.ird.observe.spi.module.BusinessProject;
|
| ... | ... | @@ -37,16 +35,12 @@ import fr.ird.observe.spi.referential.differential.DifferentialMetaModel; |
| 37 | 35 | import fr.ird.observe.spi.referential.synchro.OneSideSqlResultBuilder;
|
| 38 | 36 | import fr.ird.observe.spi.script.ToolkitEntitySqlScriptModel;
|
| 39 | 37 | import fr.ird.observe.spi.service.ServiceContext;
|
| 40 | -import io.ultreia.java4all.util.Version;
|
|
| 41 | 38 | import org.apache.logging.log4j.LogManager;
|
| 42 | 39 | import org.apache.logging.log4j.Logger;
|
| 43 | 40 | import org.nuiton.topia.persistence.TagValues;
|
| 44 | 41 | import org.nuiton.topia.persistence.TopiaConfigurationExtension;
|
| 45 | 42 | import org.nuiton.topia.persistence.TopiaEntity;
|
| 46 | -import org.nuiton.topia.persistence.TopiaIdFactory;
|
|
| 47 | 43 | import org.nuiton.topia.persistence.TopiaPersistenceContextConstructorParameterSupport;
|
| 48 | -import org.nuiton.topia.persistence.script.TopiaSqlScript;
|
|
| 49 | -import org.nuiton.topia.service.migration.TopiaMigrationService;
|
|
| 50 | 44 | import org.nuiton.topia.service.sql.blob.TopiaEntitySqlBlobModel;
|
| 51 | 45 | import org.nuiton.topia.service.sql.metadata.TopiaMetadataModel;
|
| 52 | 46 | import org.nuiton.topia.service.sql.metadata.TopiaMetadataModelPaths;
|
| ... | ... | @@ -54,19 +48,11 @@ import org.nuiton.topia.service.sql.model.TopiaEntitySqlModel; |
| 54 | 48 | import org.nuiton.topia.service.sql.plan.copy.TopiaEntitySqlCopyPlanModel;
|
| 55 | 49 | import org.nuiton.topia.service.sql.plan.delete.TopiaEntitySqlDeletePlanModel;
|
| 56 | 50 | import org.nuiton.topia.service.sql.plan.replicate.TopiaEntitySqlReplicatePlanModel;
|
| 57 | -import org.nuiton.topia.service.sql.request.CreateDatabaseRequest;
|
|
| 58 | 51 | import org.nuiton.topia.service.sql.usage.TopiaEntitySqlUsageModel;
|
| 59 | 52 | |
| 60 | -import java.io.File;
|
|
| 61 | -import java.io.IOException;
|
|
| 62 | -import java.nio.file.Files;
|
|
| 63 | -import java.nio.file.Path;
|
|
| 64 | 53 | import java.time.Duration;
|
| 65 | -import java.util.Collection;
|
|
| 66 | 54 | import java.util.Date;
|
| 67 | -import java.util.Map;
|
|
| 68 | 55 | import java.util.Set;
|
| 69 | -import java.util.UUID;
|
|
| 70 | 56 | import java.util.function.Consumer;
|
| 71 | 57 | |
| 72 | 58 | public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicationContext {
|
| ... | ... | @@ -187,131 +173,16 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat |
| 187 | 173 | return ObserveTopiaEntitySqlModelResource.get().getScriptModel();
|
| 188 | 174 | }
|
| 189 | 175 | |
| 176 | + @Deprecated
|
|
| 190 | 177 | public void create(ObserveDataSourceConfiguration dataSourceConfiguration, DataSourceCreateConfigurationDto dataSourceCreateConfiguration) throws BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
| 191 | - log.debug(String.format("Create topia application context: %s", this));
|
|
| 192 | - if (dataSourceCreateConfiguration.isImportDatabase()) {
|
|
| 193 | - log.info("Create new database from a script.");
|
|
| 194 | - TopiaSqlScript importDatabase = dataSourceCreateConfiguration.getImportDatabase();
|
|
| 195 | - boolean migrate = dataSourceCreateConfiguration.isMigrate();
|
|
| 196 | - TopiaMigrationService migrationService = getMigrationService();
|
|
| 197 | - if (dataSourceConfiguration.isLocal()) {
|
|
| 198 | - executeSqlStatements(importDatabase);
|
|
| 199 | - if (migrate) {
|
|
| 200 | - migrationService.createSchemaIfNotExist();
|
|
| 201 | - migrationService.runSchemaMigration();
|
|
| 202 | - }
|
|
| 203 | - } else { // base postgres
|
|
| 204 | - // do import in temporary h2 database
|
|
| 205 | - File tmpDir;
|
|
| 206 | - try {
|
|
| 207 | - Path temporaryDirectory = dataSourceConfiguration.getTemporaryDirectory();
|
|
| 208 | - tmpDir = Files.createTempDirectory(temporaryDirectory, "obstuna").toFile();
|
|
| 209 | - } catch (IOException e) {
|
|
| 210 | - throw new IllegalStateException("could not create temporary directory ", e);
|
|
| 211 | - }
|
|
| 212 | - ObserveDataSourceConfigurationTopiaH2 temporaryConfiguration = createTemporaryConfiguration(tmpDir, dataSourceConfiguration.getModelVersion());
|
|
| 213 | - ObserveTopiaApplicationContext temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(temporaryConfiguration, false);
|
|
| 214 | - temporaryTopiaApplicationContext.executeSqlStatements(importDatabase);
|
|
| 215 | - if (migrate) {
|
|
| 216 | - temporaryTopiaApplicationContext.getMigrationService().createSchemaIfNotExist();
|
|
| 217 | - temporaryTopiaApplicationContext.getMigrationService().runSchemaMigration();
|
|
| 218 | - }
|
|
| 219 | - CreateDatabaseRequest request = CreateDatabaseRequest.builder(true, dataSourceConfiguration.getModelVersion()).addSchema().addStandaloneTables().addAllData().build();
|
|
| 220 | - TopiaSqlScript dump = getSqlService().consume(request);
|
|
| 221 | - executeSqlStatements(dump);
|
|
| 222 | - migrationService.createSchemaIfNotExist();
|
|
| 223 | - migrationService.runSchemaMigration();
|
|
| 224 | - temporaryTopiaApplicationContext.close();
|
|
| 225 | - File databaseFile = temporaryConfiguration.getDatabaseFile();
|
|
| 226 | - if (!databaseFile.delete()) {
|
|
| 227 | - throw new IllegalStateException("could not delete " + databaseFile);
|
|
| 228 | - }
|
|
| 229 | - }
|
|
| 230 | - } else {
|
|
| 231 | - boolean importReferential = dataSourceCreateConfiguration.isImportReferential();
|
|
| 232 | - boolean importData = dataSourceCreateConfiguration.isImportData();
|
|
| 233 | - // si le referentiel n'est pas importé on aliment la table lastUpdateDate
|
|
| 234 | - if (!importReferential) {
|
|
| 235 | - insertLastUpdateDate();
|
|
| 236 | - }
|
|
| 237 | - boolean importStandaloneReferential = importReferential;
|
|
| 238 | - if (importReferential && importData) {
|
|
| 239 | - // Si on est sur la même source de données pour les deux imports, on fait tout sur la même base temporaire
|
|
| 240 | - ObserveDataSourceConfiguration importReferentialDataSourceConfiguration = dataSourceCreateConfiguration.getImportReferentialDataSourceConfiguration();
|
|
| 241 | - ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportDataDataSourceConfiguration();
|
|
| 242 | - importStandaloneReferential = !importReferentialDataSourceConfiguration.equals(importDataSourceConfiguration);
|
|
| 243 | - }
|
|
| 244 | - boolean referentialImported = false;
|
|
| 245 | - if (importStandaloneReferential) {
|
|
| 246 | - log.info("Import referential.");
|
|
| 247 | - ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportReferentialDataSourceConfiguration();
|
|
| 248 | - Version dbVersion = Version.valueOf(getModelVersion());
|
|
| 249 | - CreateDatabaseRequest request = CreateDatabaseRequest.builder(!dataSourceConfiguration.isLocal(), dbVersion)
|
|
| 250 | - .addStandaloneTables().build();
|
|
| 251 | - TopiaSqlScript referentialDump;
|
|
| 252 | - ObserveTopiaApplicationContext temporaryTopiaApplicationContext = null;
|
|
| 253 | - try {
|
|
| 254 | - temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(importDataSourceConfiguration, false);
|
|
| 255 | -// ObserveDataSourceConnection importDataSourceConnection = serviceContext().newService(importDataSourceConfiguration, AnonymousService.class).open(importDataSourceConfiguration);
|
|
| 256 | -// DataSourceService dumpProducerService = serviceContext().newService(importDataSourceConnection, DataSourceService.class);
|
|
| 257 | - referentialDump = temporaryTopiaApplicationContext.getSqlService().consume(request);
|
|
| 258 | - executeSqlStatements(referentialDump);
|
|
| 259 | - } finally {
|
|
| 260 | - if (!dataSourceCreateConfiguration.isLeaveOpenSource() && temporaryTopiaApplicationContext != null) {
|
|
| 261 | - temporaryTopiaApplicationContext.close();
|
|
| 262 | - }
|
|
| 263 | - }
|
|
| 264 | - referentialImported = true;
|
|
| 265 | - }
|
|
| 266 | - |
|
| 267 | - if (importData) {
|
|
| 268 | - ObserveDataSourceConfiguration importDataSourceConfiguration = dataSourceCreateConfiguration.getImportDataDataSourceConfiguration();
|
|
| 269 | - Version dbVersion = Version.valueOf(getModelVersion());
|
|
| 270 | - CreateDatabaseRequest.Builder requestBuilder = CreateDatabaseRequest.builder(!dataSourceConfiguration.isLocal(), dbVersion);
|
|
| 271 | - if (!referentialImported) {
|
|
| 272 | - log.info("Get referential.");
|
|
| 273 | - requestBuilder.addStandaloneTables();
|
|
| 274 | - }
|
|
| 275 | - |
|
| 276 | - // Récupération du dump qui contient les données
|
|
| 277 | - Set<String> importDataIds = dataSourceCreateConfiguration.getImportDataIds();
|
|
| 278 | - TopiaIdFactory idFactory = getTopiaIdFactory();
|
|
| 279 | - Multimap<String, String> idsByType = ArrayListMultimap.create();
|
|
| 280 | - for (String importDataId : importDataIds) {
|
|
| 281 | - Class<TopiaEntity> className = idFactory.getClassName(importDataId);
|
|
| 282 | - idsByType.put(className.getName(), importDataId);
|
|
| 283 | - }
|
|
| 284 | - log.info(String.format("Get data: %s", importDataIds));
|
|
| 285 | - for (Map.Entry<String, Collection<String>> entry : idsByType.asMap().entrySet()) {
|
|
| 286 | - requestBuilder.dataIdsToAdd(entry.getKey(), Set.copyOf(entry.getValue()));
|
|
| 287 | - }
|
|
| 288 | - TopiaSqlScript dataDump;
|
|
| 289 | - ObserveTopiaApplicationContext temporaryTopiaApplicationContext = null;
|
|
| 290 | - CreateDatabaseRequest request = requestBuilder.build();
|
|
| 291 | - try {
|
|
| 292 | - temporaryTopiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(importDataSourceConfiguration, false);
|
|
| 293 | -// ObserveDataSourceConnection importDataSourceConnection = serviceContext().newService(importDataSourceConfiguration, AnonymousService.class).open(importDataSourceConfiguration);
|
|
| 294 | -// DataSourceService dumpProducerService = serviceContext().newService(importDataSourceConnection, DataSourceService.class);
|
|
| 295 | -// dataDump = dumpProducerService.produceCreateSqlScript(request);
|
|
| 296 | - dataDump = temporaryTopiaApplicationContext.getSqlService().consume(request);
|
|
| 297 | - } finally {
|
|
| 298 | - if (!dataSourceCreateConfiguration.isLeaveOpenSource() && temporaryTopiaApplicationContext != null) {
|
|
| 299 | - temporaryTopiaApplicationContext.close();
|
|
| 300 | -// serviceContext().newService(importDataSourceConfiguration, DataSourceService.class).close();
|
|
| 301 | - }
|
|
| 302 | - }
|
|
| 303 | - log.info(String.format("Import data%s", request.isAddStandaloneTables() ? " and referential." : "."));
|
|
| 304 | - executeSqlStatements(dataDump);
|
|
| 305 | - }
|
|
| 306 | - }
|
|
| 178 | + DataSourceCreateHelper.create(this, dataSourceConfiguration, dataSourceCreateConfiguration);
|
|
| 307 | 179 | }
|
| 308 | 180 | |
| 309 | - private ObserveDataSourceConfigurationTopiaH2 createTemporaryConfiguration(File tmpDirectory, Version version) {
|
|
| 310 | - File dbDirectory = new File(tmpDirectory, "obstuna" + UUID.randomUUID());
|
|
| 311 | - ObserveDataSourceConfigurationTopiaH2 result = ObserveDataSourceConfigurationTopiaH2.create(
|
|
| 312 | - "obtunaTmp", dbDirectory, "obstuna", "sa", "sa".toCharArray(), true, true, version);
|
|
| 313 | - result.setTemporaryDirectory(tmpDirectory.toPath());
|
|
| 314 | - return result;
|
|
| 181 | + public void createAndImport(DataSourceImportConfigurationDto dataSourceCreateConfiguration) throws BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
|
| 182 | + log.debug(String.format("Create topia application context: %s", this));
|
|
| 183 | + executeSqlStatements(dataSourceCreateConfiguration.getReferentialDump());
|
|
| 184 | + if (dataSourceCreateConfiguration.isImportData()) {
|
|
| 185 | + executeSqlStatements(dataSourceCreateConfiguration.getDataDump());
|
|
| 186 | + }
|
|
| 315 | 187 | }
|
| 316 | - |
|
| 317 | 188 | } |
| ... | ... | @@ -27,6 +27,7 @@ import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException; |
| 27 | 27 | import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
| 28 | 28 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 29 | 29 | import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
| 30 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 30 | 31 | import fr.ird.observe.dto.db.configuration.IncompatibleDataSourceCreateConfigurationException;
|
| 31 | 32 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 32 | 33 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConnection;
|
| ... | ... | @@ -44,6 +45,16 @@ public class AnonymousServiceClient extends GeneratedAnonymousServiceClient { |
| 44 | 45 | return dataSourceConnection;
|
| 45 | 46 | }
|
| 46 | 47 | |
| 48 | + @Override
|
|
| 49 | + public ObserveDataSourceConnection createAndImport(ObserveDataSourceConfiguration config, DataSourceImportConfigurationDto createConfiguration) throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException, BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
|
| 50 | + ObserveServiceInitializer initializer = getServiceContext().getInitializer();
|
|
| 51 | + initializer.setConfiguration(config);
|
|
| 52 | + ObserveDataSourceConnection dataSourceConnection = super.createAndImport(config, createConfiguration);
|
|
| 53 | + initializer.setConnection(dataSourceConnection);
|
|
| 54 | + initializer.setConfiguration(null);
|
|
| 55 | + return dataSourceConnection;
|
|
| 56 | + }
|
|
| 57 | + |
|
| 47 | 58 | @Override
|
| 48 | 59 | public ObserveDataSourceConnection open(ObserveDataSourceConfiguration config) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
| 49 | 60 | ObserveServiceInitializer initializer = getServiceContext().getInitializer();
|
| ... | ... | @@ -30,6 +30,7 @@ import fr.ird.observe.dto.db.DatabaseNotFoundException; |
| 30 | 30 | import fr.ird.observe.dto.db.DatabaseVersionIsTooHighException;
|
| 31 | 31 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 32 | 32 | import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
| 33 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 33 | 34 | import fr.ird.observe.dto.db.configuration.IncompatibleDataSourceCreateConfigurationException;
|
| 34 | 35 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 35 | 36 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConnection;
|
| ... | ... | @@ -124,6 +125,22 @@ public class AnonymousServiceLocalSupport extends ObserveServiceLocal implements |
| 124 | 125 | return config.toConnection(topiaApplicationContext.getAuthenticationToken(), dataSourceInformation);
|
| 125 | 126 | }
|
| 126 | 127 | |
| 128 | + @Override
|
|
| 129 | + public ObserveDataSourceConnection createAndImport(ObserveDataSourceConfiguration config, DataSourceImportConfigurationDto createConfiguration) throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException, BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
|
| 130 | + createConfiguration.validateConfiguration(getApplicationLocale());
|
|
| 131 | + setTemporaryDirectory(config);
|
|
| 132 | + boolean initSchema = true;
|
|
| 133 | + ObserveTopiaApplicationContext topiaApplicationContext = ObserveTopiaApplicationContextFactory.createTopiaApplicationContext(config, initSchema);
|
|
| 134 | + try {
|
|
| 135 | + topiaApplicationContext.createAndImport(createConfiguration);
|
|
| 136 | + } catch (Exception e) {
|
|
| 137 | + close(config, topiaApplicationContext);
|
|
| 138 | + throw e;
|
|
| 139 | + }
|
|
| 140 | + ObserveDataSourceInformation dataSourceInformation = getDataSourceInformation(config, topiaApplicationContext.getConfiguration(), false);
|
|
| 141 | + return config.toConnection(topiaApplicationContext.getAuthenticationToken(), dataSourceInformation);
|
|
| 142 | + }
|
|
| 143 | + |
|
| 127 | 144 | @Override
|
| 128 | 145 | public ObserveDataSourceConnection open(ObserveDataSourceConfiguration config) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
| 129 | 146 | setTemporaryDirectory(config);
|
| ... | ... | @@ -26,6 +26,7 @@ GET /AnonymousService/applySecurity Anon |
| 26 | 26 | GET /AnonymousService/checkCanConnect AnonymousServiceRestApi.checkCanConnect
|
| 27 | 27 | GET /AnonymousService/checkCanConnectOrBeEmpty AnonymousServiceRestApi.checkCanConnectOrBeEmpty
|
| 28 | 28 | GET /AnonymousService/create AnonymousServiceRestApi.create
|
| 29 | +POST /AnonymousService/createAndImport AnonymousServiceRestApi.createAndImport
|
|
| 29 | 30 | GET /AnonymousService/generateHomeId AnonymousServiceRestApi.generateHomeId
|
| 30 | 31 | GET /AnonymousService/getModelVersion AnonymousServiceRestApi.getModelVersion
|
| 31 | 32 | GET /AnonymousService/getServerVersion AnonymousServiceRestApi.getServerVersion
|
| ... | ... | @@ -28,6 +28,7 @@ import fr.ird.observe.dto.db.DatabaseConnexionNotAuthorizedException; |
| 28 | 28 | import fr.ird.observe.dto.db.DatabaseNotFoundException;
|
| 29 | 29 | import fr.ird.observe.dto.db.configuration.DataSourceCreateConfigurationDto;
|
| 30 | 30 | import fr.ird.observe.dto.db.configuration.DataSourceCreateWithNoReferentialImportException;
|
| 31 | +import fr.ird.observe.dto.db.configuration.DataSourceImportConfigurationDto;
|
|
| 31 | 32 | import fr.ird.observe.dto.db.configuration.IncompatibleDataSourceCreateConfigurationException;
|
| 32 | 33 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConfiguration;
|
| 33 | 34 | import fr.ird.observe.dto.db.configuration.ObserveDataSourceConnection;
|
| ... | ... | @@ -49,6 +50,12 @@ public class AnonymousServiceRestApi extends GeneratedAnonymousServiceRestApi { |
| 49 | 50 | |
| 50 | 51 | private static final Logger log = LogManager.getLogger(AnonymousServiceRestApi.class);
|
| 51 | 52 | |
| 53 | + @Override
|
|
| 54 | + public ObserveDataSourceInformation checkCanConnectOrBeEmpty(ObserveDataSourceConfiguration config) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
|
| 55 | + ObserveWebUserSession session = getRequestContext().getConfigurationSession(config);
|
|
| 56 | + return getService().checkCanConnectOrBeEmpty(session.getConfiguration());
|
|
| 57 | + }
|
|
| 58 | + |
|
| 52 | 59 | @Override
|
| 53 | 60 | public ObserveDataSourceInformation checkCanConnect(ObserveDataSourceConfiguration config) throws DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException, BabModelVersionException {
|
| 54 | 61 | ObserveWebUserSession session = getRequestContext().getConfigurationSession(config);
|
| ... | ... | @@ -65,6 +72,14 @@ public class AnonymousServiceRestApi extends GeneratedAnonymousServiceRestApi { |
| 65 | 72 | return authenticatedSession.toRestConnection(config);
|
| 66 | 73 | }
|
| 67 | 74 | |
| 75 | + @Override
|
|
| 76 | + public ObserveDataSourceConnection createAndImport(ObserveDataSourceConfiguration config, DataSourceImportConfigurationDto createConfiguration) throws IncompatibleDataSourceCreateConfigurationException, DataSourceCreateWithNoReferentialImportException, BabModelVersionException, DatabaseConnexionNotAuthorizedException, DatabaseNotFoundException {
|
|
| 77 | + ObserveWebUserSession configurationSession = getRequestContext().getConfigurationSession(config);
|
|
| 78 | + ObserveDataSourceConnection connection = getService().createAndImport(configurationSession.getConfiguration(), createConfiguration);
|
|
| 79 | + ObserveWebUserSession authenticatedSession = getSecurityApplicationContext().registerAuthenticatedSession(configurationSession, connection);
|
|
| 80 | + return authenticatedSession.toRestConnection(config);
|
|
| 81 | + }
|
|
| 82 | + |
|
| 68 | 83 | @Override
|
| 69 | 84 | public ObserveDataSourceConnection open(ObserveDataSourceConfiguration config) throws DatabaseNotFoundException, DatabaseConnexionNotAuthorizedException, BabModelVersionException {
|
| 70 | 85 | ObserveWebUserSession configurationSession = getRequestContext().getConfigurationSession(config);
|