Tony CHEMIT pushed to branch develop-7.x at ultreiaio / ird-observe
Commits:
-
82296bfa
by Tony CHEMIT at 2018-09-12T14:04:00Z
4 changed files:
- services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/legacy/UnidirectionalReferentialSynchronizeLocalServiceLocal.java
- services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/ng/ReferentialSynchronizeSqlsRequestBuilder.java
- services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/DeleteSqlStatementGenerator.java
- services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/UpdateSqlStatementGenerator.java
Changes:
| ... | ... | @@ -26,11 +26,7 @@ import com.google.common.collect.ImmutableList; |
| 26 | 26 |
import com.google.common.collect.ImmutableSet;
|
| 27 | 27 |
import fr.ird.observe.dto.reference.ReferentialDtoReference;
|
| 28 | 28 |
import fr.ird.observe.dto.reference.ReferentialDtoReferenceSet;
|
| 29 |
-import fr.ird.observe.dto.referential.GearCaracteristicDto;
|
|
| 30 |
-import fr.ird.observe.dto.referential.GearDto;
|
|
| 31 |
-import fr.ird.observe.dto.referential.OceanDto;
|
|
| 32 | 29 |
import fr.ird.observe.dto.referential.ReferentialDto;
|
| 33 |
-import fr.ird.observe.dto.referential.SpeciesDto;
|
|
| 34 | 30 |
import fr.ird.observe.dto.referential.seine.ObjectMaterialDto;
|
| 35 | 31 |
import fr.ird.observe.dto.referential.seine.ObjectMaterialHelper;
|
| 36 | 32 |
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
|
| ... | ... | @@ -48,18 +44,19 @@ import fr.ird.observe.services.service.actions.synchro.referential.legacy.Unidir |
| 48 | 44 |
import fr.ird.observe.spi.DbModelHelper;
|
| 49 | 45 |
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
|
| 50 | 46 |
import fr.ird.observe.spi.context.ReferentialReferenceEntityContext;
|
| 51 |
-import java.util.LinkedHashSet;
|
|
| 52 |
-import java.util.List;
|
|
| 53 |
-import java.util.Map;
|
|
| 54 |
-import java.util.Set;
|
|
| 55 |
-import org.apache.logging.log4j.Logger;
|
|
| 56 | 47 |
import org.apache.logging.log4j.LogManager;
|
| 48 |
+import org.apache.logging.log4j.Logger;
|
|
| 57 | 49 |
import org.nuiton.topia.persistence.TopiaDao;
|
| 58 | 50 |
import org.nuiton.topia.persistence.TopiaEntity;
|
| 59 | 51 |
import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
|
| 60 | 52 |
import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
|
| 61 | 53 |
import org.nuiton.topia.persistence.support.TopiaSqlWork;
|
| 62 | 54 |
|
| 55 |
+import java.util.LinkedHashSet;
|
|
| 56 |
+import java.util.List;
|
|
| 57 |
+import java.util.Map;
|
|
| 58 |
+import java.util.Set;
|
|
| 59 |
+ |
|
| 63 | 60 |
/**
|
| 64 | 61 |
* Created on 27/06/16.
|
| 65 | 62 |
*
|
| ... | ... | @@ -163,30 +160,8 @@ public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends Obser |
| 163 | 160 |
|
| 164 | 161 |
DeleteSqlStatementGenerator sqlStatementGenerator = new DeleteSqlStatementGenerator(metadataEntity);
|
| 165 | 162 |
for (String id : request.getReferentialToRemove()) {
|
| 166 |
- if (SpeciesDto.class.equals(request.getReferentialName())) {
|
|
| 167 |
- |
|
| 168 |
- // also remove ocean_species association
|
|
| 169 |
- result.add(sqlStatementGenerator.generateAssociationSql("ocean_species", "species", id));
|
|
| 170 |
- |
|
| 171 |
- // also remove species_specieslist association
|
|
| 172 |
- result.add(sqlStatementGenerator.generateAssociationSql("species_specieslist", "species", id));
|
|
| 173 |
- }
|
|
| 174 |
- if (OceanDto.class.equals(request.getReferentialName())) {
|
|
| 175 |
- |
|
| 176 |
- // also remove ocean_species association
|
|
| 177 |
- result.add(sqlStatementGenerator.generateAssociationSql("ocean_species", "ocean", id));
|
|
| 178 |
- }
|
|
| 179 |
- if (GearDto.class.equals(request.getReferentialName())) {
|
|
| 180 |
- |
|
| 181 |
- // also remove gear_gearcaracteristic association
|
|
| 182 |
- result.add(sqlStatementGenerator.generateAssociationSql("gear_gearcaracteristic", "gear", id));
|
|
| 183 |
- }
|
|
| 184 |
- if (GearCaracteristicDto.class.equals(request.getReferentialName())) {
|
|
| 185 |
- |
|
| 186 |
- // also remove gear_gearcaracteristic association
|
|
| 187 |
- result.add(sqlStatementGenerator.generateAssociationSql("gear_gearcaracteristic", "gearcaracteristic", id));
|
|
| 188 |
- }
|
|
| 189 |
- result.add(sqlStatementGenerator.generateSql(id));
|
|
| 163 |
+ List<String> sql = sqlStatementGenerator.generateSql(id);
|
|
| 164 |
+ result.addAll(sql);
|
|
| 190 | 165 |
}
|
| 191 | 166 |
|
| 192 | 167 |
}
|
| ... | ... | @@ -24,12 +24,10 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.ng; |
| 24 | 24 |
|
| 25 | 25 |
import com.google.common.collect.ImmutableList;
|
| 26 | 26 |
import com.google.common.collect.ImmutableSet;
|
| 27 |
-import fr.ird.observe.dto.reference.ReferentialDtoReference;
|
|
| 28 |
-import fr.ird.observe.spi.DbModelHelper;
|
|
| 29 | 27 |
import fr.ird.observe.binder.referential.ReferentialEntityDtoBinderSupport;
|
| 30 |
-import fr.ird.observe.spi.DtoModelClasses;
|
|
| 31 |
-import fr.ird.observe.dto.referential.ReferentialLocale;
|
|
| 28 |
+import fr.ird.observe.dto.reference.ReferentialDtoReference;
|
|
| 32 | 29 |
import fr.ird.observe.dto.referential.ReferentialDto;
|
| 30 |
+import fr.ird.observe.dto.referential.ReferentialLocale;
|
|
| 33 | 31 |
import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
|
| 34 | 32 |
import fr.ird.observe.persistence.ObserveEntityEnum;
|
| 35 | 33 |
import fr.ird.observe.services.local.service.actions.synchro.referential.sql.DeleteSqlStatementGenerator;
|
| ... | ... | @@ -42,15 +40,18 @@ import fr.ird.observe.services.local.service.actions.synchro.referential.sql.Upd |
| 42 | 40 |
import fr.ird.observe.services.service.actions.synchro.referential.ng.ReferentialSynchronizeRequest;
|
| 43 | 41 |
import fr.ird.observe.services.service.actions.synchro.referential.ng.ReferentialSynchronizeSqlsRequest;
|
| 44 | 42 |
import fr.ird.observe.services.service.actions.synchro.referential.ng.task.ReferentialSynchronizeTask;
|
| 43 |
+import fr.ird.observe.spi.DbModelHelper;
|
|
| 44 |
+import fr.ird.observe.spi.DtoModelClasses;
|
|
| 45 | 45 |
import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
|
| 46 |
+import org.apache.logging.log4j.LogManager;
|
|
| 47 |
+import org.apache.logging.log4j.Logger;
|
|
| 48 |
+import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
|
|
| 49 |
+import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
|
|
| 50 |
+ |
|
| 46 | 51 |
import java.util.List;
|
| 47 | 52 |
import java.util.Optional;
|
| 48 | 53 |
import java.util.Set;
|
| 49 | 54 |
import java.util.stream.Collectors;
|
| 50 |
-import org.apache.logging.log4j.Logger;
|
|
| 51 |
-import org.apache.logging.log4j.LogManager;
|
|
| 52 |
-import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
|
|
| 53 |
-import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
|
|
| 54 | 55 |
|
| 55 | 56 |
/**
|
| 56 | 57 |
* Created on 14/08/16.
|
| ... | ... | @@ -134,7 +135,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder { |
| 134 | 135 |
if (log.isInfoEnabled()) {
|
| 135 | 136 |
log.info("Delete: " + deleteTasks.size());
|
| 136 | 137 |
}
|
| 137 |
- onDelete(referentialName, referenceType, deleteTasks, binder);
|
|
| 138 |
+ onDelete(referentialName, referenceType, deleteTasks);
|
|
| 138 | 139 |
|
| 139 | 140 |
Set<ReferentialSynchronizeTask<R>> desactivateTasks = request.getDesactivateTasks(referenceType);
|
| 140 | 141 |
if (log.isInfoEnabled()) {
|
| ... | ... | @@ -258,7 +259,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder { |
| 258 | 259 |
|
| 259 | 260 |
}
|
| 260 | 261 |
|
| 261 |
- private <D extends ReferentialDto, R extends ReferentialDtoReference<D, R>, E extends ObserveReferentialEntity> void onDelete(String referentialName, Class<R> type, Set<ReferentialSynchronizeTask<R>> tasks, ReferentialEntityDtoBinderSupport<D, E> binder) {
|
|
| 262 |
+ private <D extends ReferentialDto, R extends ReferentialDtoReference<D, R>, E extends ObserveReferentialEntity> void onDelete(String referentialName, Class<R> type, Set<ReferentialSynchronizeTask<R>> tasks) {
|
|
| 262 | 263 |
|
| 263 | 264 |
TopiaMetadataEntity metadata = metadataModel.getEntity(referentialName);
|
| 264 | 265 |
|
| ... | ... | @@ -271,16 +272,14 @@ public class ReferentialSynchronizeSqlsRequestBuilder { |
| 271 | 272 |
Optional<String> replaceReferentialId = task.getOptionalReplaceReferentialId();
|
| 272 | 273 |
if (replaceReferentialId.isPresent()) {
|
| 273 | 274 |
ImmutableList<String> sqls = replaceGenerator.generateSqls(referentialId, replaceReferentialId.get());
|
| 274 |
- for (String sql : sqls) {
|
|
| 275 |
- resultBuilder.addDeleteStatement(sql);
|
|
| 276 |
- }
|
|
| 275 |
+ sqls.forEach(resultBuilder::addDeleteStatement);
|
|
| 277 | 276 |
}
|
| 278 |
- String sql = deleteGenerator.generateSql(referentialId);
|
|
| 277 |
+ List<String> sql = deleteGenerator.generateSql(referentialId);
|
|
| 279 | 278 |
|
| 280 | 279 |
if (log.isInfoEnabled()) {
|
| 281 | 280 |
log.info("Delete referential: " + type.getName() + " / " + referentialId + " -- " + sql);
|
| 282 | 281 |
}
|
| 283 |
- resultBuilder.addDeleteStatement(sql);
|
|
| 282 |
+ sql.forEach(resultBuilder::addDeleteStatement);
|
|
| 284 | 283 |
}
|
| 285 | 284 |
|
| 286 | 285 |
}
|
| ... | ... | @@ -10,22 +10,30 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.sql; |
| 10 | 10 |
* it under the terms of the GNU General Public License as
|
| 11 | 11 |
* published by the Free Software Foundation, either version 3 of the
|
| 12 | 12 |
* License, or (at your option) any later version.
|
| 13 |
- *
|
|
| 13 |
+ *
|
|
| 14 | 14 |
* This program is distributed in the hope that it will be useful,
|
| 15 | 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 16 | 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 17 | 17 |
* GNU General Public License for more details.
|
| 18 |
- *
|
|
| 18 |
+ *
|
|
| 19 | 19 |
* You should have received a copy of the GNU General Public
|
| 20 | 20 |
* License along with this program. If not, see
|
| 21 | 21 |
* <http://www.gnu.org/licenses/gpl-3.0.html>.
|
| 22 | 22 |
* #L%
|
| 23 | 23 |
*/
|
| 24 | 24 |
|
| 25 |
-import org.apache.logging.log4j.Logger;
|
|
| 25 |
+import com.google.common.collect.ImmutableList;
|
|
| 26 |
+import fr.ird.observe.services.local.service.actions.synchro.referential.sql.UpdateSqlStatementGenerator.ManyToManyAssociationStruct;
|
|
| 26 | 27 |
import org.apache.logging.log4j.LogManager;
|
| 28 |
+import org.apache.logging.log4j.Logger;
|
|
| 27 | 29 |
import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
|
| 28 | 30 |
|
| 31 |
+import java.util.LinkedHashSet;
|
|
| 32 |
+import java.util.List;
|
|
| 33 |
+import java.util.Map;
|
|
| 34 |
+ |
|
| 35 |
+import static fr.ird.observe.services.local.service.actions.synchro.referential.sql.UpdateSqlStatementGenerator.MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT;
|
|
| 36 |
+ |
|
| 29 | 37 |
/**
|
| 30 | 38 |
* Pour générer une requète sql de suppression à partir d'un référentiel donné.
|
| 31 | 39 |
* <p>
|
| ... | ... | @@ -43,34 +51,48 @@ public class DeleteSqlStatementGenerator { |
| 43 | 51 |
|
| 44 | 52 |
private final String schemaName;
|
| 45 | 53 |
private final String tableName;
|
| 54 |
+ private final LinkedHashSet<ManyToManyAssociationStruct> manyToManyAssociations;
|
|
| 46 | 55 |
|
| 47 | 56 |
public DeleteSqlStatementGenerator(TopiaMetadataEntity metadataEntity) {
|
| 48 | 57 |
this.schemaName = metadataEntity.getDbSchemaName();
|
| 49 | 58 |
this.tableName = metadataEntity.getDbTableName();
|
| 59 |
+ Map<String, String> manyToManyAssociationsMap = metadataEntity.getManyToManyAssociations();
|
|
| 60 |
+ this.manyToManyAssociations = new LinkedHashSet<>();
|
|
| 61 |
+ for (Map.Entry<String, String> entry : manyToManyAssociationsMap.entrySet()) {
|
|
| 62 |
+ String propertyName = entry.getKey();
|
|
| 63 |
+ String dbColumnName = metadataEntity.getDbColumnName(propertyName);
|
|
| 64 |
+ String tableName = metadataEntity.getBdManyToManyAssociationTableName(propertyName);
|
|
| 65 |
+ ManyToManyAssociationStruct manyToManyAssociation = new ManyToManyAssociationStruct(propertyName, dbColumnName, tableName);
|
|
| 66 |
+ manyToManyAssociations.add(manyToManyAssociation);
|
|
| 67 |
+ }
|
|
| 50 | 68 |
}
|
| 51 | 69 |
|
| 52 |
- public String generateSql(String id) {
|
|
| 53 |
- |
|
| 54 |
- String result = String.format(DELETE_STATEMENT, schemaName, tableName, id);
|
|
| 55 |
- |
|
| 70 |
+ public List<String> generateSql(String id) {
|
|
| 71 |
+ ImmutableList.Builder<String> result = ImmutableList.builder();
|
|
| 72 |
+ String sql = String.format(DELETE_STATEMENT, schemaName, tableName, id);
|
|
| 56 | 73 |
if (log.isDebugEnabled()) {
|
| 57 |
- log.debug("sql: " + result);
|
|
| 74 |
+ log.debug("sql: " + sql);
|
|
| 58 | 75 |
}
|
| 59 |
- |
|
| 60 |
- return result;
|
|
| 61 |
- |
|
| 76 |
+ result.add(sql);
|
|
| 77 |
+ for (ManyToManyAssociationStruct manyToManyAssociation : manyToManyAssociations) {
|
|
| 78 |
+ generateManyToManyAssociationSql(id, manyToManyAssociation, result);
|
|
| 79 |
+ }
|
|
| 80 |
+ return result.build();
|
|
| 62 | 81 |
}
|
| 63 | 82 |
|
| 64 |
- public String generateAssociationSql(String tableName, String columnName, String id) {
|
|
| 83 |
+ private void generateManyToManyAssociationSql(String referentialDtoId, ManyToManyAssociationStruct manyToManyAssociation, ImmutableList.Builder<String> result) {
|
|
| 65 | 84 |
|
| 66 |
- String result = String.format(DELETE_ASSOCIATION_STATEMENT, schemaName, tableName, columnName, id);
|
|
| 85 |
+ String manyToManyAssociationTableName = manyToManyAssociation.tableName;
|
|
| 67 | 86 |
|
| 87 |
+ // On commence toujours par supprimer toutes les anciennes associations, elles seront ré-ajoutées juste après
|
|
| 88 |
+ String deleteSql = String.format(MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT,
|
|
| 89 |
+ schemaName,
|
|
| 90 |
+ manyToManyAssociationTableName,
|
|
| 91 |
+ tableName,
|
|
| 92 |
+ referentialDtoId);
|
|
| 93 |
+ result.add(deleteSql);
|
|
| 68 | 94 |
if (log.isDebugEnabled()) {
|
| 69 |
- log.debug("sql: " + result);
|
|
| 95 |
+ log.debug("sql: " + deleteSql);
|
|
| 70 | 96 |
}
|
| 71 |
- |
|
| 72 |
- return result;
|
|
| 73 |
- |
|
| 74 | 97 |
}
|
| 75 |
- |
|
| 76 | 98 |
}
|
| ... | ... | @@ -57,7 +57,7 @@ public class UpdateSqlStatementGenerator<D extends ReferentialDto> { |
| 57 | 57 |
|
| 58 | 58 |
private static final String UPDATE_STATEMENT = "UPDATE %s.%s SET %s WHERE topiaId ='%s';\n";
|
| 59 | 59 |
// We use a special prefix because those requests must be applied before all simple insert requests
|
| 60 |
- private static final String MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT = "$$_DELETE FROM %s.%s WHERE %s = '%s';\n";
|
|
| 60 |
+ static final String MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT = "$$_DELETE FROM %s.%s WHERE %s = '%s';\n";
|
|
| 61 | 61 |
|
| 62 | 62 |
private final Map<String, String> columnNames;
|
| 63 | 63 |
private final String schemaName;
|
| ... | ... | @@ -329,7 +329,7 @@ public class UpdateSqlStatementGenerator<D extends ReferentialDto> { |
| 329 | 329 |
/**
|
| 330 | 330 |
* Pour décrire une association nm.
|
| 331 | 331 |
*/
|
| 332 |
- private static class ManyToManyAssociationStruct {
|
|
| 332 |
+ static class ManyToManyAssociationStruct {
|
|
| 333 | 333 |
|
| 334 | 334 |
/**
|
| 335 | 335 |
* Le nom de la propriété dans l'objet.
|
| ... | ... | @@ -342,9 +342,9 @@ public class UpdateSqlStatementGenerator<D extends ReferentialDto> { |
| 342 | 342 |
/**
|
| 343 | 343 |
* Le nom de la table d'association.
|
| 344 | 344 |
*/
|
| 345 |
- private final String tableName;
|
|
| 345 |
+ final String tableName;
|
|
| 346 | 346 |
|
| 347 |
- private ManyToManyAssociationStruct(String propertyName, String dbColumnName, String tableName) {
|
|
| 347 |
+ ManyToManyAssociationStruct(String propertyName, String dbColumnName, String tableName) {
|
|
| 348 | 348 |
this.propertyName = propertyName;
|
| 349 | 349 |
this.dbColumnName = dbColumnName;
|
| 350 | 350 |
this.tableName = tableName;
|