Tony CHEMIT pushed to branch feature/issue-2843 at ultreiaio / ird-observe
Commits:
-
2c4fed94
by Tony Chemit at 2024-03-08T12:54:45+01:00
-
ae84f818
by Tony Chemit at 2024-03-08T12:54:52+01:00
-
6af33613
by Tony Chemit at 2024-03-08T12:54:58+01:00
4 changed files:
- core/persistence/consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/common/TripConsolidateEngine.java
- core/persistence/consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/logbook/ActivityConsolidateEngine.java
- core/persistence/consolidation/src/main/java/fr/ird/observe/consolidation/data/ps/observation/ActivityConsolidateEngine.java
- + toolkit/persistence/src/main/java/fr/ird/observe/spi/consolidation/ToolkitIdModificationsToSql.java
Changes:
| ... | ... | @@ -33,13 +33,19 @@ import fr.ird.observe.dto.referential.ReferentialLocale; |
| 33 | 33 | import fr.ird.observe.entities.ObserveTopiaDaoSupplier;
|
| 34 | 34 | import fr.ird.observe.entities.data.ps.common.Trip;
|
| 35 | 35 | import fr.ird.observe.entities.data.ps.localmarket.Batch;
|
| 36 | +import fr.ird.observe.entities.data.ps.logbook.SampleActivity;
|
|
| 36 | 37 | import fr.ird.observe.entities.referential.common.LengthLengthParameterCache;
|
| 37 | 38 | import fr.ird.observe.entities.referential.common.LengthWeightParameterCache;
|
| 39 | +import fr.ird.observe.spi.consolidation.ToolkitIdModificationsToSql;
|
|
| 38 | 40 | import fr.ird.observe.spi.service.ServiceContext;
|
| 39 | 41 | import io.ultreia.java4all.decoration.Decorator;
|
| 42 | +import io.ultreia.java4all.util.sql.SqlScript;
|
|
| 40 | 43 | import org.apache.logging.log4j.LogManager;
|
| 41 | 44 | import org.apache.logging.log4j.Logger;
|
| 45 | +import org.nuiton.topia.service.sql.model.TopiaEntitySqlModel;
|
|
| 42 | 46 | |
| 47 | +import java.io.IOException;
|
|
| 48 | +import java.nio.file.Path;
|
|
| 43 | 49 | import java.util.LinkedHashSet;
|
| 44 | 50 | import java.util.Optional;
|
| 45 | 51 | import java.util.Set;
|
| ... | ... | @@ -95,15 +101,38 @@ public class TripConsolidateEngine { |
| 95 | 101 | decoratorService.installDecorator(Trip.class, trip);
|
| 96 | 102 | String tripLabel = trip.toString();
|
| 97 | 103 | TripConsolidateResult result = new TripConsolidateResult(tripId, tripLabel, observationActivityConsolidateResults, logbookActivityConsolidateResults, logbookSampleActivityResults, localmarketBatchResults);
|
| 98 | - if (result.withModifications()) {
|
|
| 104 | + boolean withModifications = result.withModifications();
|
|
| 105 | + if (withModifications) {
|
|
| 99 | 106 | log.info(String.format("Found some modifications on trip: %s - %s", tripId, result.getTripLabel()));
|
| 100 | 107 | }
|
| 101 | 108 | if (result.withWarnings()) {
|
| 102 | 109 | log.info(String.format("Found some warnings on trip: %s - %s", tripId, result.getTripLabel()));
|
| 103 | 110 | }
|
| 111 | + // clear hibernate session (See https://gitlab.com/ultreiaio/ird-observe/-/issues/2843)
|
|
| 112 | + context.getTopiaPersistenceContext().getHibernateSupport().clearSession();
|
|
| 113 | + if (withModifications) {
|
|
| 114 | + // generate sql script
|
|
| 115 | + Path scriptPath = context.getTemporaryDirectoryRoot().resolve(String.format("TripConsolidateResult%d.sql", System.nanoTime()));
|
|
| 116 | + log.info("Produce TripConsolidateResult sql script to: {}", scriptPath);
|
|
| 117 | + SqlScript script = toSql(scriptPath, result, context.getTopiaApplicationContext().getModel());
|
|
| 118 | + // consume sql script
|
|
| 119 | + context.getTopiaPersistenceContext().executeSqlScript(script);
|
|
| 120 | + }
|
|
| 104 | 121 | return Optional.of(result);
|
| 105 | 122 | }
|
| 106 | 123 | |
| 124 | + public SqlScript toSql(Path scriptPath, TripConsolidateResult result, TopiaEntitySqlModel entitySqlModel) {
|
|
| 125 | + try (ToolkitIdModificationsToSql toolkitIdModificationsToSql = new ToolkitIdModificationsToSql(scriptPath, context.timestampNow(), entitySqlModel)) {
|
|
| 126 | + observationActivityConsolidateEngine.toSql(result.getActivityObservationResults(), toolkitIdModificationsToSql);
|
|
| 127 | + logbookActivityConsolidateEngine.toSql(result.getActivityLogbookResults(), toolkitIdModificationsToSql);
|
|
| 128 | + toolkitIdModificationsToSql.toSql(Batch.SPI, result.getLocalmarketBatchResults());
|
|
| 129 | + toolkitIdModificationsToSql.toSql(SampleActivity.SPI, result.getLogbookSampleActivityResults());
|
|
| 130 | + return toolkitIdModificationsToSql.build();
|
|
| 131 | + } catch (IOException e) {
|
|
| 132 | + throw new IllegalStateException("Could not produce sql script to " + scriptPath, e);
|
|
| 133 | + }
|
|
| 134 | + }
|
|
| 135 | + |
|
| 107 | 136 | private Set<ToolkitIdModifications> consolidateLogbookSampleActivities(Trip trip) {
|
| 108 | 137 | Set<ToolkitIdModifications> result = new LinkedHashSet<>();
|
| 109 | 138 | if (trip.isWellEmpty() || trip.isSampleEmpty()) {
|
| ... | ... | @@ -33,6 +33,7 @@ import fr.ird.observe.entities.data.ps.logbook.Activity; |
| 33 | 33 | import fr.ird.observe.entities.data.ps.logbook.FloatingObject;
|
| 34 | 34 | import fr.ird.observe.entities.data.ps.logbook.FloatingObjectPart;
|
| 35 | 35 | import fr.ird.observe.entities.data.ps.logbook.Route;
|
| 36 | +import fr.ird.observe.spi.consolidation.ToolkitIdModificationsToSql;
|
|
| 36 | 37 | import fr.ird.observe.spi.service.ServiceContext;
|
| 37 | 38 | import io.ultreia.java4all.decoration.Decorator;
|
| 38 | 39 | import io.ultreia.java4all.util.Dates;
|
| ... | ... | @@ -41,6 +42,7 @@ import org.apache.logging.log4j.Logger; |
| 41 | 42 | |
| 42 | 43 | import java.util.LinkedHashSet;
|
| 43 | 44 | import java.util.Optional;
|
| 45 | +import java.util.Set;
|
|
| 44 | 46 | |
| 45 | 47 | /**
|
| 46 | 48 | * Created on 12/03/2023.
|
| ... | ... | @@ -60,6 +62,12 @@ public class ActivityConsolidateEngine { |
| 60 | 62 | this.activityDecorator = context.getDecoratorService().getDecoratorByType(Activity.class, ActivityAware.CLASSIFIER_WITH_ROUTE);
|
| 61 | 63 | }
|
| 62 | 64 | |
| 65 | + public void toSql(Set<ActivityConsolidateResult> activityLogbookResults, ToolkitIdModificationsToSql toolkitIdModificationsToSql) {
|
|
| 66 | + for (ActivityConsolidateResult activityConsolidateResult : activityLogbookResults) {
|
|
| 67 | + toolkitIdModificationsToSql.toSql(FloatingObject.SPI, activityConsolidateResult.getFloatingObjectModifications());
|
|
| 68 | + }
|
|
| 69 | + }
|
|
| 70 | + |
|
| 63 | 71 | public Optional<ActivityConsolidateResult> consolidateActivity(Trip trip,
|
| 64 | 72 | Route route,
|
| 65 | 73 | Activity activity,
|
| ... | ... | @@ -142,5 +150,4 @@ public class ActivityConsolidateEngine { |
| 142 | 150 | }
|
| 143 | 151 | }
|
| 144 | 152 | }
|
| 145 | - |
|
| 146 | 153 | } |
| ... | ... | @@ -30,11 +30,15 @@ import fr.ird.observe.dto.data.ps.observation.FloatingObjectPartDto; |
| 30 | 30 | import fr.ird.observe.dto.referential.ReferentialLocale;
|
| 31 | 31 | import fr.ird.observe.entities.data.ps.common.Trip;
|
| 32 | 32 | import fr.ird.observe.entities.data.ps.observation.Activity;
|
| 33 | +import fr.ird.observe.entities.data.ps.observation.Catch;
|
|
| 33 | 34 | import fr.ird.observe.entities.data.ps.observation.FloatingObject;
|
| 34 | 35 | import fr.ird.observe.entities.data.ps.observation.FloatingObjectPart;
|
| 35 | 36 | import fr.ird.observe.entities.data.ps.observation.Route;
|
| 37 | +import fr.ird.observe.entities.data.ps.observation.SampleMeasure;
|
|
| 38 | +import fr.ird.observe.entities.data.ps.observation.Set;
|
|
| 36 | 39 | import fr.ird.observe.entities.referential.common.LengthLengthParameterCache;
|
| 37 | 40 | import fr.ird.observe.entities.referential.common.LengthWeightParameterCache;
|
| 41 | +import fr.ird.observe.spi.consolidation.ToolkitIdModificationsToSql;
|
|
| 38 | 42 | import fr.ird.observe.spi.service.ServiceContext;
|
| 39 | 43 | import io.ultreia.java4all.decoration.Decorator;
|
| 40 | 44 | import io.ultreia.java4all.util.Dates;
|
| ... | ... | @@ -70,6 +74,15 @@ public class ActivityConsolidateEngine { |
| 70 | 74 | this.activityDecorator = context.getDecoratorService().getDecoratorByType(Activity.class, ActivityAware.CLASSIFIER_WITH_ROUTE);
|
| 71 | 75 | }
|
| 72 | 76 | |
| 77 | + public void toSql(java.util.Set<ActivityConsolidateResult> activityObservationResults, ToolkitIdModificationsToSql toolkitIdModificationsToSql) {
|
|
| 78 | + for (ActivityConsolidateResult activityConsolidateResult : activityObservationResults) {
|
|
| 79 | + toolkitIdModificationsToSql.toSql(FloatingObject.SPI, activityConsolidateResult.getFloatingObjectModifications());
|
|
| 80 | + toolkitIdModificationsToSql.toSql(Set.SPI, activityConsolidateResult.getSetModifications());
|
|
| 81 | + toolkitIdModificationsToSql.toSql(Catch.SPI, activityConsolidateResult.getCatchModifications());
|
|
| 82 | + toolkitIdModificationsToSql.toSql(SampleMeasure.SPI, activityConsolidateResult.getSampleMeasureModifications());
|
|
| 83 | + }
|
|
| 84 | + }
|
|
| 85 | + |
|
| 73 | 86 | public Optional<ActivityConsolidateResult> consolidateActivity(Trip trip,
|
| 74 | 87 | Route route,
|
| 75 | 88 | Activity activity,
|
| ... | ... | @@ -163,5 +176,4 @@ public class ActivityConsolidateEngine { |
| 163 | 176 | }
|
| 164 | 177 | }
|
| 165 | 178 | }
|
| 166 | - |
|
| 167 | 179 | } |
| 1 | +package fr.ird.observe.spi.consolidation;
|
|
| 2 | + |
|
| 3 | +/*-
|
|
| 4 | + * #%L
|
|
| 5 | + * ObServe Toolkit :: Persistence
|
|
| 6 | + * %%
|
|
| 7 | + * Copyright (C) 2008 - 2024 IRD, 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 fr.ird.observe.dto.ToolkitIdModifications;
|
|
| 26 | +import fr.ird.observe.entities.Entity;
|
|
| 27 | +import fr.ird.observe.spi.SqlConversions;
|
|
| 28 | +import fr.ird.observe.spi.context.DtoEntityContext;
|
|
| 29 | +import io.ultreia.java4all.bean.monitor.JavaBeanPropertyModification;
|
|
| 30 | +import io.ultreia.java4all.util.sql.SqlScript;
|
|
| 31 | +import io.ultreia.java4all.util.sql.SqlScriptWriter;
|
|
| 32 | +import org.nuiton.topia.service.sql.model.TopiaEntitySqlModel;
|
|
| 33 | +import org.nuiton.topia.service.sql.model.TopiaEntitySqlTable;
|
|
| 34 | + |
|
| 35 | +import java.io.Closeable;
|
|
| 36 | +import java.io.IOException;
|
|
| 37 | +import java.nio.file.Path;
|
|
| 38 | +import java.sql.Timestamp;
|
|
| 39 | +import java.util.ArrayList;
|
|
| 40 | +import java.util.LinkedHashSet;
|
|
| 41 | +import java.util.List;
|
|
| 42 | +import java.util.Objects;
|
|
| 43 | +import java.util.Set;
|
|
| 44 | + |
|
| 45 | +/**
|
|
| 46 | + * To produce sql update code from some consolidation modifications.
|
|
| 47 | + * <p>
|
|
| 48 | + * Created at 04/03/2024.
|
|
| 49 | + *
|
|
| 50 | + * @author Tony Chemit - dev@tchemit.fr
|
|
| 51 | + * @since 9.3.0
|
|
| 52 | + */
|
|
| 53 | +public class ToolkitIdModificationsToSql implements Closeable {
|
|
| 54 | + |
|
| 55 | + public static final String UPDATE_SQL = "UPDATE %s SET %s WHERE topiaId = '%s';";
|
|
| 56 | + public static final String SET_SQL_CLAUSE = "%s = %s";
|
|
| 57 | + |
|
| 58 | + /**
|
|
| 59 | + * Where to write sql statements.
|
|
| 60 | + */
|
|
| 61 | + private final Path target;
|
|
| 62 | + /**
|
|
| 63 | + * Now timestamp to used in any sql statement that requires it.
|
|
| 64 | + */
|
|
| 65 | + private final Timestamp now;
|
|
| 66 | + /**
|
|
| 67 | + * Topia Entity sql model used to perform conversions while writing sql statements.
|
|
| 68 | + */
|
|
| 69 | + private final TopiaEntitySqlModel entitySqlModel;
|
|
| 70 | + /**
|
|
| 71 | + * Set of spi used to produce sql statements (we be used in method {@link #build()} to add extra sql statements
|
|
| 72 | + * to update {@code LastUpdateDate} table).
|
|
| 73 | + */
|
|
| 74 | + private final Set<DtoEntityContext<?, ?, ?, ?>> spiUsed;
|
|
| 75 | + /**
|
|
| 76 | + * Internal writer used to store sql statements.
|
|
| 77 | + */
|
|
| 78 | + private SqlScriptWriter writer;
|
|
| 79 | + |
|
| 80 | + public ToolkitIdModificationsToSql(Path target, Timestamp now, TopiaEntitySqlModel entitySqlModel) {
|
|
| 81 | + this.target = Objects.requireNonNull(target);
|
|
| 82 | + this.now = Objects.requireNonNull(now);
|
|
| 83 | + this.entitySqlModel = Objects.requireNonNull(entitySqlModel);
|
|
| 84 | + this.spiUsed = new LinkedHashSet<>();
|
|
| 85 | + }
|
|
| 86 | + |
|
| 87 | + |
|
| 88 | + public static String toSetClause(TopiaEntitySqlTable sqlModelDescriptorTable, String propertyName, Object value) {
|
|
| 89 | + String stringValue = SqlConversions.convertValue(sqlModelDescriptorTable, propertyName, value);
|
|
| 90 | + return String.format(SET_SQL_CLAUSE, propertyName, stringValue);
|
|
| 91 | + }
|
|
| 92 | + |
|
| 93 | + public SqlScript build() {
|
|
| 94 | + SqlScriptWriter writer = writer();
|
|
| 95 | + Set<String> lastUpdateDateStatements = new LinkedHashSet<>();
|
|
| 96 | + spiUsed.forEach(spi -> spi.getUpdateLastUpdateDateTableScript().generate(now).forEach(l -> lastUpdateDateStatements.add(l.trim())));
|
|
| 97 | + lastUpdateDateStatements.forEach(writer::writeSql);
|
|
| 98 | + return SqlScript.of(target);
|
|
| 99 | + }
|
|
| 100 | + |
|
| 101 | + public void toSql(DtoEntityContext<?, ?, ?, ?> spi, ToolkitIdModifications modifications) {
|
|
| 102 | + if (modifications == null) {
|
|
| 103 | + return;
|
|
| 104 | + }
|
|
| 105 | + TopiaEntitySqlTable sqlModelDescriptorTable = entitySqlModel.getDescriptor(spi.toEntityType()).getTable();
|
|
| 106 | + boolean used = toSql(sqlModelDescriptorTable, modifications);
|
|
| 107 | + if (used) {
|
|
| 108 | + spiUsed.add(spi);
|
|
| 109 | + }
|
|
| 110 | + }
|
|
| 111 | + |
|
| 112 | + public void toSql(DtoEntityContext<?, ?, ?, ?> spi, Set<ToolkitIdModifications> modificationsSet) {
|
|
| 113 | + if (modificationsSet == null) {
|
|
| 114 | + return;
|
|
| 115 | + }
|
|
| 116 | + TopiaEntitySqlTable sqlModelDescriptorTable = entitySqlModel.getDescriptor(spi.toEntityType()).getTable();
|
|
| 117 | + boolean used = false;
|
|
| 118 | + for (ToolkitIdModifications modifications : modificationsSet) {
|
|
| 119 | + used |= toSql(sqlModelDescriptorTable, modifications);
|
|
| 120 | + }
|
|
| 121 | + if (used) {
|
|
| 122 | + spiUsed.add(spi);
|
|
| 123 | + }
|
|
| 124 | + }
|
|
| 125 | + |
|
| 126 | + @Override
|
|
| 127 | + public void close() throws IOException {
|
|
| 128 | + if (writer != null) {
|
|
| 129 | + writer.close();
|
|
| 130 | + }
|
|
| 131 | + }
|
|
| 132 | + |
|
| 133 | + protected SqlScriptWriter writer() {
|
|
| 134 | + return writer == null ? writer = SqlScriptWriter.of(target) : writer;
|
|
| 135 | + }
|
|
| 136 | + |
|
| 137 | + protected boolean toSql(TopiaEntitySqlTable sqlModelDescriptorTable, ToolkitIdModifications modifications) {
|
|
| 138 | + if (modifications == null || !modifications.withModifications()) {
|
|
| 139 | + // only warning on this one
|
|
| 140 | + return false;
|
|
| 141 | + }
|
|
| 142 | + List<String> setClauses = new ArrayList<>(modifications.modificationsCount() + 2);
|
|
| 143 | + String schemaAndTableName = sqlModelDescriptorTable.getSchemaAndTableName();
|
|
| 144 | + for (JavaBeanPropertyModification modification : modifications.getModifications()) {
|
|
| 145 | + setClauses.add(toSetClause(sqlModelDescriptorTable, modification.getPropertyName(), modification.getNewValue()));
|
|
| 146 | + }
|
|
| 147 | + setClauses.add(String.format(SET_SQL_CLAUSE, Entity.PROPERTY_TOPIA_VERSION, Entity.PROPERTY_TOPIA_VERSION + " + 1"));
|
|
| 148 | + setClauses.add(toSetClause(sqlModelDescriptorTable, Entity.PROPERTY_LAST_UPDATE_DATE, now));
|
|
| 149 | + String sql = String.format(UPDATE_SQL, schemaAndTableName, String.join(", ", setClauses), modifications.getId());
|
|
| 150 | + writer().writeSql(sql);
|
|
| 151 | + return true;
|
|
| 152 | + }
|
|
| 153 | +} |