Tony CHEMIT pushed to branch develop-7.x at ultreiaio / ird-observe

Commits:

15 changed files:

Changes:

  • persistence/src/main/java/fr/ird/observe/entities/EntityMap.java deleted
    1
    -/*
    
    2
    - * #%L
    
    3
    - * ObServe :: Persistence
    
    4
    - * %%
    
    5
    - * Copyright (C) 2008 - 2018 IRD, Code Lutin, Ultreia.io
    
    6
    - * %%
    
    7
    - * This program is free software: you can redistribute it and/or modify
    
    8
    - * it under the terms of the GNU General Public License as
    
    9
    - * published by the Free Software Foundation, either version 3 of the 
    
    10
    - * License, or (at your option) any later version.
    
    11
    - * 
    
    12
    - * This program is distributed in the hope that it will be useful,
    
    13
    - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    14
    - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    15
    - * GNU General Public License for more details.
    
    16
    - * 
    
    17
    - * You should have received a copy of the GNU General Public 
    
    18
    - * License along with this program.  If not, see
    
    19
    - * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    20
    - * #L%
    
    21
    - */
    
    22
    -package fr.ird.observe.entities;
    
    23
    -
    
    24
    -import org.nuiton.topia.persistence.TopiaEntity;
    
    25
    -
    
    26
    -import java.util.HashMap;
    
    27
    -import java.util.List;
    
    28
    -import java.util.Map;
    
    29
    -
    
    30
    -/**
    
    31
    - * A Dictionary of entities
    
    32
    - *
    
    33
    - * @author Tony Chemit - dev@tchemit.fr
    
    34
    - * @since 1.4
    
    35
    - */
    
    36
    -public class EntityMap extends HashMap<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> {
    
    37
    -    private static final long serialVersionUID = 1L;
    
    38
    -
    
    39
    -    public EntityMap() {
    
    40
    -    }
    
    41
    -
    
    42
    -    public EntityMap(Map<? extends Class<? extends TopiaEntity>, ? extends List<? extends TopiaEntity>> m) {
    
    43
    -        super(m);
    
    44
    -    }
    
    45
    -}

  • persistence/src/main/java/fr/ird/observe/persistence/ObserveTopiaApplicationContext.java
    ... ... @@ -10,12 +10,12 @@ package fr.ird.observe.persistence;
    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>.
    
    ... ... @@ -24,19 +24,22 @@ package fr.ird.observe.persistence;
    24 24
     
    
    25 25
     import com.google.common.collect.ImmutableSet;
    
    26 26
     import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
    
    27
    -import org.apache.logging.log4j.Logger;
    
    28 27
     import org.apache.logging.log4j.LogManager;
    
    28
    +import org.apache.logging.log4j.Logger;
    
    29 29
     import org.hibernate.HibernateException;
    
    30 30
     import org.hibernate.tool.hbm2ddl.SchemaExport;
    
    31 31
     import org.hibernate.tool.schema.TargetType;
    
    32
    +import org.nuiton.topia.persistence.TopiaEntity;
    
    32 33
     import org.nuiton.topia.persistence.TopiaException;
    
    33 34
     import org.nuiton.topia.persistence.internal.support.TopiaMetadataModelSupportImpl;
    
    35
    +import org.nuiton.topia.persistence.internal.support.TopiaUsageSupportImpl;
    
    34 36
     import org.nuiton.topia.persistence.jdbc.JdbcHelper;
    
    35 37
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    36 38
     import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    37 39
     import org.nuiton.topia.persistence.script.SqlScriptReader;
    
    38 40
     import org.nuiton.topia.persistence.script.TopiaSqlScript;
    
    39 41
     import org.nuiton.topia.persistence.support.TopiaMetadataModelSupport;
    
    42
    +import org.nuiton.topia.persistence.support.TopiaUsageSupport;
    
    40 43
     import org.nuiton.topia.service.migration.TopiaMigrationService;
    
    41 44
     import org.nuiton.topia.service.script.TopiaSqlScriptGeneratorService;
    
    42 45
     import org.nuiton.topia.service.script.table.TopiaSqlTable;
    
    ... ... @@ -45,13 +48,16 @@ import org.nuiton.topia.service.script.table.TopiaSqlTablesFactory;
    45 48
     
    
    46 49
     import java.util.EnumSet;
    
    47 50
     import java.util.LinkedHashSet;
    
    51
    +import java.util.List;
    
    52
    +import java.util.Map;
    
    48 53
     import java.util.Objects;
    
    49 54
     import java.util.Set;
    
    50 55
     import java.util.UUID;
    
    56
    +import java.util.function.Predicate;
    
    51 57
     import java.util.stream.Collectors;
    
    52 58
     import java.util.stream.Stream;
    
    53 59
     
    
    54
    -public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicationContext implements TopiaMetadataModelSupport {
    
    60
    +public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicationContext {
    
    55 61
     
    
    56 62
         static final String MIGRATION_SERVICE_NAME = "migration";
    
    57 63
         static final String SQL_SCRIPT_GENERATOR_BATCH_SERVICE_NAME = "sqlScriptGenerator";
    
    ... ... @@ -65,6 +71,7 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    65 71
         protected final String authenticationToken;
    
    66 72
         private final TopiaMetadataModelSupport topiaMetadataModelSupport;
    
    67 73
         private final TopiaSqlTablesFactory topiaSqlTablesFactory;
    
    74
    +    private final TopiaUsageSupport topiaUsageSupport;
    
    68 75
         /**
    
    69 76
          * Mise à {@code true} quand au moins une connection a été effectuée.
    
    70 77
          */
    
    ... ... @@ -78,6 +85,7 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    78 85
             this.authenticationToken = UUID.randomUUID().toString();
    
    79 86
             this.topiaMetadataModelSupport = new TopiaMetadataModelSupportImpl("fr.ird.observe.persistence", "Observe");
    
    80 87
             this.topiaSqlTablesFactory = new TopiaSqlTablesFactory(getMetadataModel(), this);
    
    88
    +        this.topiaUsageSupport = new TopiaUsageSupportImpl(topiaMetadataModelSupport, this);
    
    81 89
         }
    
    82 90
     
    
    83 91
         @Override
    
    ... ... @@ -108,6 +116,22 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    108 116
             return getServices(TopiaSqlScriptGeneratorService.class).get(SQL_SCRIPT_GENERATOR_BATCH_SERVICE_NAME);
    
    109 117
         }
    
    110 118
     
    
    119
    +    public Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> findReverseCompositions(ObserveTopiaPersistenceContext topiaPersistenceContext, TopiaEntity entity, Predicate<Class<? extends TopiaEntity>> entityTypeFilter) {
    
    120
    +        return topiaUsageSupport.findReverseCompositions(topiaPersistenceContext, entity, entityTypeFilter);
    
    121
    +    }
    
    122
    +
    
    123
    +    public Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> findReverseAssociations(ObserveTopiaPersistenceContext topiaPersistenceContext, TopiaEntity entity, Predicate<Class<? extends TopiaEntity>> entityTypeFilter) {
    
    124
    +        return topiaUsageSupport.findReverseAssociations(topiaPersistenceContext, entity, entityTypeFilter);
    
    125
    +    }
    
    126
    +
    
    127
    +    public Map<Class<? extends TopiaEntity>, Long> countReverseCompositions(ObserveTopiaPersistenceContext topiaPersistenceContext, TopiaEntity entity, Predicate<Class<? extends TopiaEntity>> entityTypeFilter) {
    
    128
    +        return topiaUsageSupport.countReverseCompositions(topiaPersistenceContext, entity, entityTypeFilter);
    
    129
    +    }
    
    130
    +
    
    131
    +    public Map<Class<? extends TopiaEntity>, Long> countReverseAssociations(ObserveTopiaPersistenceContext topiaPersistenceContext, TopiaEntity entity, Predicate<Class<? extends TopiaEntity>> entityTypeFilter) {
    
    132
    +        return topiaUsageSupport.countReverseAssociations(topiaPersistenceContext, entity, entityTypeFilter);
    
    133
    +    }
    
    134
    +
    
    111 135
         @Override
    
    112 136
         public void createSchema() {
    
    113 137
             try {
    
    ... ... @@ -206,7 +230,6 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    206 230
             return authenticationToken;
    
    207 231
         }
    
    208 232
     
    
    209
    -    @Override
    
    210 233
         public TopiaMetadataModel getMetadataModel() {
    
    211 234
             return topiaMetadataModelSupport.getMetadataModel();
    
    212 235
         }
    
    ... ... @@ -269,7 +292,7 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    269 292
                         basketTable.getFromClause(),
    
    270 293
                         basketTable.getWhereClauseAlias(),
    
    271 294
                         ImmutableSet.of(" INNER JOIN observe_longline.set set ON set.topiaId = activity.set",
    
    272
    -                            " INNER JOIN observe_longline.basket basket ON basket.set = set.topiaId"
    
    295
    +                                    " INNER JOIN observe_longline.basket basket ON basket.set = set.topiaId"
    
    273 296
                         ),
    
    274 297
                         null);
    
    275 298
     
    
    ... ... @@ -285,8 +308,8 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    285 308
                         branchlineTable.getFromClause(),
    
    286 309
                         branchlineTable.getWhereClauseAlias(),
    
    287 310
                         ImmutableSet.of(" INNER JOIN observe_longline.set set ON set.topiaId = activity.set",
    
    288
    -                            " INNER JOIN observe_longline.basket basket ON basket.set = set.topiaId",
    
    289
    -                            " INNER JOIN observe_longline.branchline branchline ON branchline.basket = basket.topiaId"
    
    311
    +                                    " INNER JOIN observe_longline.basket basket ON basket.set = set.topiaId",
    
    312
    +                                    " INNER JOIN observe_longline.branchline branchline ON branchline.basket = basket.topiaId"
    
    290 313
                         ),
    
    291 314
                         null);
    
    292 315
     
    
    ... ... @@ -312,6 +335,7 @@ public class ObserveTopiaApplicationContext extends AbstractObserveTopiaApplicat
    312 335
             return referentialTables = topiaSqlTablesFactory.newReplicateEntityTables(new TripReplicateTablesPredicate(), entityEnum);
    
    313 336
         }
    
    314 337
     
    
    338
    +
    
    315 339
         private static class TripReplicateTablesPredicate implements TopiaSqlTablesFactory.TopiaSqlTablesPredicate {
    
    316 340
     
    
    317 341
             protected final Set<String> sections = ImmutableSet.of(
    

  • pom.xml
    ... ... @@ -162,7 +162,7 @@
    162 162
         <lib.version.h2>1.4.196</lib.version.h2>
    
    163 163
         <!--<lib.version.nuiton.topia>3.6-SNAPSHOT</lib.version.nuiton.topia>-->
    
    164 164
         <!--<lib.version.java4all.eugene>3.0-alpha-21</lib.version.java4all.eugene>-->
    
    165
    -    <!--<lib.version.java4all.topia>1.1.8-SNAPSHOT</lib.version.java4all.topia>-->
    
    165
    +    <lib.version.java4all.topia>1.1.8-SNAPSHOT</lib.version.java4all.topia>
    
    166 166
         <!-- license header configuration -->
    
    167 167
         <license.licenseName>gpl_v3</license.licenseName>
    
    168 168
         <license.organizationName>IRD, Code Lutin, Ultreia.io</license.organizationName>
    

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/legacy/UnidirectionalReferentialSynchronizeLocalServiceLocal.java
    ... ... @@ -10,12 +10,12 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.legacy
    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>.
    
    ... ... @@ -44,8 +44,6 @@ import fr.ird.observe.services.service.actions.synchro.referential.legacy.Unidir
    44 44
     import fr.ird.observe.spi.DbModelHelper;
    
    45 45
     import fr.ird.observe.spi.context.ReferentialDtoEntityContext;
    
    46 46
     import fr.ird.observe.spi.context.ReferentialReferenceEntityContext;
    
    47
    -import org.apache.logging.log4j.LogManager;
    
    48
    -import org.apache.logging.log4j.Logger;
    
    49 47
     import org.nuiton.topia.persistence.TopiaDao;
    
    50 48
     import org.nuiton.topia.persistence.TopiaEntity;
    
    51 49
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    ... ... @@ -65,9 +63,6 @@ import java.util.Set;
    65 63
      */
    
    66 64
     public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends ObserveServiceLocal implements UnidirectionalReferentialSynchronizeLocalService {
    
    67 65
     
    
    68
    -    /** Logger. */
    
    69
    -    private static final Logger log = LogManager.getLogger(UnidirectionalReferentialSynchronizeLocalServiceLocal.class);
    
    70
    -
    
    71 66
         private LastUpdateDateService lastUpdateDateService;
    
    72 67
     
    
    73 68
         @Override
    
    ... ... @@ -79,9 +74,6 @@ public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends Obser
    79 74
         @Override
    
    80 75
         public <D extends ReferentialDto> ImmutableSet<String> filterIdsUsedInLocalSource(Class<D> dtoType, ImmutableSet<String> ids) {
    
    81 76
     
    
    82
    -        if (log.isTraceEnabled()) {
    
    83
    -            log.trace("filterIdsUsedInLocalSource(" + dtoType + ", " + ids + ")");
    
    84
    -        }
    
    85 77
             Class<? extends ObserveReferentialEntity> entityType = DbModelHelper.fromReferentialDto(dtoType).toEntityType();
    
    86 78
             Set<String> result = new LinkedHashSet<>();
    
    87 79
             for (String id : ids) {
    
    ... ... @@ -107,10 +99,6 @@ public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends Obser
    107 99
         @Override
    
    108 100
         public <D extends ReferentialDto> ImmutableSet<String> generateSqlRequests(UnidirectionalReferentialSynchronizeRequest<D> request) {
    
    109 101
     
    
    110
    -        if (log.isTraceEnabled()) {
    
    111
    -            log.trace("generateSqlRequests(" + request + ")");
    
    112
    -        }
    
    113
    -
    
    114 102
             Set<String> result = new LinkedHashSet<>();
    
    115 103
     
    
    116 104
             Class<D> dtoType = request.getReferentialName();
    
    ... ... @@ -122,7 +110,7 @@ public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends Obser
    122 110
             TopiaMetadataEntity metadataEntity = metadataModel.getEntity(referentielName);
    
    123 111
             if (request.withReferentialToAdd()) {
    
    124 112
     
    
    125
    -            InsertSqlStatementGenerator<D> sqlStatementGenerator = new InsertSqlStatementGenerator<>(metadataEntity, dtoType);
    
    113
    +            InsertSqlStatementGenerator<D> sqlStatementGenerator = new InsertSqlStatementGenerator<>(metadataModel, metadataEntity, dtoType);
    
    126 114
                 ImmutableSet<D> referentialToAdd = request.getReferentialToAdd();
    
    127 115
                 if (ObjectMaterialDto.class.equals(dtoType)) {
    
    128 116
     
    
    ... ... @@ -139,55 +127,40 @@ public class UnidirectionalReferentialSynchronizeLocalServiceLocal extends Obser
    139 127
     
    
    140 128
             if (request.withReferentialToUpdate()) {
    
    141 129
     
    
    142
    -            UpdateSqlStatementGenerator<D> sqlStatementGenerator = new UpdateSqlStatementGenerator<>(metadataEntity, dtoType);
    
    130
    +            UpdateSqlStatementGenerator<D> sqlStatementGenerator = new UpdateSqlStatementGenerator<>(metadataModel, metadataEntity, dtoType);
    
    143 131
                 for (D referentialDto : request.getReferentialToUpdate()) {
    
    144 132
                     ImmutableList<String> sql = sqlStatementGenerator.generateSql(referentialDto);
    
    145 133
                     result.addAll(sql);
    
    146 134
                 }
    
    147
    -
    
    148 135
             }
    
    149
    -
    
    150 136
             if (request.withReferentialToReplace()) {
    
    151 137
     
    
    152
    -            ReplaceSqlStatementGenerator sqlStatementGenerator = new ReplaceSqlStatementGenerator(metadataModel, referentielName);
    
    138
    +            ReplaceSqlStatementGenerator sqlStatementGenerator = new ReplaceSqlStatementGenerator(metadataModel, metadataEntity);
    
    153 139
                 for (Map.Entry<String, String> entry : request.getReferentialToReplace().entrySet()) {
    
    154 140
                     ImmutableList<String> sql = sqlStatementGenerator.generateSqls(entry.getKey(), entry.getValue());
    
    155 141
                     result.addAll(sql);
    
    156 142
                 }
    
    157
    -
    
    158 143
             }
    
    159 144
             if (request.withReferentialToRemove()) {
    
    160 145
     
    
    161
    -            DeleteSqlStatementGenerator sqlStatementGenerator = new DeleteSqlStatementGenerator(metadataEntity);
    
    146
    +            DeleteSqlStatementGenerator sqlStatementGenerator = new DeleteSqlStatementGenerator(metadataModel, metadataEntity);
    
    162 147
                 for (String id : request.getReferentialToRemove()) {
    
    163 148
                     List<String> sql = sqlStatementGenerator.generateSql(id);
    
    164 149
                     result.addAll(sql);
    
    165 150
                 }
    
    166
    -
    
    167 151
             }
    
    168
    -
    
    169
    -
    
    170 152
             return ImmutableSet.copyOf(result);
    
    171
    -
    
    172 153
         }
    
    173 154
     
    
    174 155
         @Override
    
    175 156
         public void applySqlRequests(ImmutableSet<String> sqlRequests) {
    
    176
    -
    
    177
    -        if (log.isTraceEnabled()) {
    
    178
    -            log.trace("applySqlRequests(" + sqlRequests + ")");
    
    179
    -        }
    
    180
    -
    
    181 157
             TopiaSqlWork applySqlWork = new ApplySqlRequestWork(sqlRequests);
    
    182 158
             getTopiaPersistenceContext().getSqlSupport().doSqlWork(applySqlWork);
    
    183
    -
    
    184 159
         }
    
    185 160
     
    
    186 161
         @Override
    
    187 162
         public void updateLastUpdateDates() {
    
    188
    -
    
    189 163
             lastUpdateDateService.updateReferentialLastUpdateDates();
    
    190
    -
    
    191 164
         }
    
    192 165
     
    
    193 166
         private <E extends ObserveReferentialEntity> int countUsage0(Class<E> entityType, String id) {
    

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/ng/ReferentialSynchronizeSqlsRequestBuilder.java
    ... ... @@ -10,12 +10,12 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.ng;
    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>.
    
    ... ... @@ -31,7 +31,7 @@ import fr.ird.observe.dto.referential.ReferentialLocale;
    31 31
     import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
    
    32 32
     import fr.ird.observe.persistence.ObserveEntityEnum;
    
    33 33
     import fr.ird.observe.services.local.service.actions.synchro.referential.sql.DeleteSqlStatementGenerator;
    
    34
    -import fr.ird.observe.services.local.service.actions.synchro.referential.sql.DesactivateSqlStatementGenerator;
    
    34
    +import fr.ird.observe.services.local.service.actions.synchro.referential.sql.DisableSqlStatementGenerator;
    
    35 35
     import fr.ird.observe.services.local.service.actions.synchro.referential.sql.InsertSqlStatementGenerator;
    
    36 36
     import fr.ird.observe.services.local.service.actions.synchro.referential.sql.InsertSqlWithCascadeStatementGenerator;
    
    37 37
     import fr.ird.observe.services.local.service.actions.synchro.referential.sql.ReplaceSqlStatementGenerator;
    
    ... ... @@ -153,7 +153,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    153 153
             List<E> insertEntities = loadEntities(entityType, tasks);
    
    154 154
             if (metadata.withEntities()) {
    
    155 155
     
    
    156
    -            InsertSqlWithCascadeStatementGenerator<D> insertGenerator = new InsertSqlWithCascadeStatementGenerator<D>(metadata, dtoType, request.getIdsOnlyExistingOnThisSide()) {
    
    156
    +            InsertSqlWithCascadeStatementGenerator<D> insertGenerator = new InsertSqlWithCascadeStatementGenerator<D>(metadataModel, metadata, dtoType, request.getIdsOnlyExistingOnThisSide()) {
    
    157 157
     
    
    158 158
                     @Override
    
    159 159
                     protected <DD extends ReferentialDto> ImmutableList<String> insertMissingReferential(Class<DD> referentialType, String id) {
    
    ... ... @@ -166,9 +166,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    166 166
                     D referential = binder.toDto(referentialLocale, entity);
    
    167 167
                     ImmutableList<String> sqls = insertGenerator.generateSql(referential);
    
    168 168
     
    
    169
    -                if (log.isInfoEnabled()) {
    
    170
    -                    log.info("Insert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    171
    -                }
    
    169
    +                log.info("Insert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    172 170
                     for (String sql : sqls) {
    
    173 171
                         resultBuilder.addInsertStatement(sql);
    
    174 172
                     }
    
    ... ... @@ -176,14 +174,12 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    176 174
     
    
    177 175
             } else {
    
    178 176
     
    
    179
    -            InsertSqlStatementGenerator<D> insertGenerator = new InsertSqlStatementGenerator<>(metadata, dtoType);
    
    177
    +            InsertSqlStatementGenerator<D> insertGenerator = new InsertSqlStatementGenerator<>(metadataModel, metadata, dtoType);
    
    180 178
                 for (E entity : insertEntities) {
    
    181 179
                     D referential = binder.toDto(referentialLocale, entity);
    
    182 180
                     ImmutableList<String> sqls = insertGenerator.generateSql(referential);
    
    183 181
     
    
    184
    -                if (log.isInfoEnabled()) {
    
    185
    -                    log.info("Insert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    186
    -                }
    
    182
    +                log.info("Insert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    187 183
                     for (String sql : sqls) {
    
    188 184
                         resultBuilder.addInsertStatement(sql);
    
    189 185
                     }
    
    ... ... @@ -201,7 +197,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    201 197
     
    
    202 198
             if (metadata.withEntities()) {
    
    203 199
     
    
    204
    -            UpdateSqlWithCascadeStatementGenerator<D> updateGenerator = new UpdateSqlWithCascadeStatementGenerator<D>(metadata, dtoType, request.getIdsOnlyExistingOnThisSide()) {
    
    200
    +            UpdateSqlWithCascadeStatementGenerator<D> updateGenerator = new UpdateSqlWithCascadeStatementGenerator<D>(metadataModel, metadata, dtoType, request.getIdsOnlyExistingOnThisSide()) {
    
    205 201
     
    
    206 202
                     @Override
    
    207 203
                     protected <DD extends ReferentialDto> ImmutableList<String> insertMissingReferential(Class<DD> referentialType, String id) {
    
    ... ... @@ -212,9 +208,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    212 208
                     D referential = binder.toDto(referentialLocale, entity);
    
    213 209
                     ImmutableList<String> sqls = updateGenerator.generateSql(referential);
    
    214 210
     
    
    215
    -                if (log.isInfoEnabled()) {
    
    216
    -                    log.info("Update referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    217
    -                }
    
    211
    +                log.info("Update referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    218 212
                     for (String sql : sqls) {
    
    219 213
                         resultBuilder.addUpdateStatement(sql);
    
    220 214
                     }
    
    ... ... @@ -222,14 +216,12 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    222 216
     
    
    223 217
             } else {
    
    224 218
     
    
    225
    -            UpdateSqlStatementGenerator<D> updateGenerator = new UpdateSqlStatementGenerator<>(metadata, dtoType);
    
    219
    +            UpdateSqlStatementGenerator<D> updateGenerator = new UpdateSqlStatementGenerator<>(metadataModel, metadata, dtoType);
    
    226 220
                 for (E entity : updateEntities) {
    
    227 221
                     D referential = binder.toDto(referentialLocale, entity);
    
    228 222
                     ImmutableList<String> sqls = updateGenerator.generateSql(referential);
    
    229 223
     
    
    230
    -                if (log.isInfoEnabled()) {
    
    231
    -                    log.info("Update referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    232
    -                }
    
    224
    +                log.info("Update referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    233 225
                     for (String sql : sqls) {
    
    234 226
                         resultBuilder.addUpdateStatement(sql);
    
    235 227
                     }
    
    ... ... @@ -244,15 +236,13 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    244 236
             TopiaMetadataEntity metadata = metadataModel.getEntity(referentialName);
    
    245 237
             Class<E> entityType = binder.getEntityType();
    
    246 238
     
    
    247
    -        UpdateSqlStatementGenerator<D> updateGenerator = new UpdateSqlStatementGenerator<>(metadata, binder.getDtoType());
    
    239
    +        UpdateSqlStatementGenerator<D> updateGenerator = new UpdateSqlStatementGenerator<>(metadataModel, metadata, binder.getDtoType());
    
    248 240
             List<E> updateEntities = loadEntities(entityType, tasks);
    
    249 241
             for (E entity : updateEntities) {
    
    250 242
                 D referential = binder.toDto(referentialLocale, entity);
    
    251 243
                 ImmutableList<String> sqls = updateGenerator.generateSql(referential);
    
    252 244
     
    
    253
    -            if (log.isInfoEnabled()) {
    
    254
    -                log.info("Revert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    255
    -            }
    
    245
    +            log.info("Revert referential: " + referenceType.getName() + " / " + referential.getId() + " -- " + sqls);
    
    256 246
                 for (String sql : sqls) {
    
    257 247
                     resultBuilder.addUpdateStatement(sql);
    
    258 248
                 }
    
    ... ... @@ -264,8 +254,8 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    264 254
     
    
    265 255
             TopiaMetadataEntity metadata = metadataModel.getEntity(referentialName);
    
    266 256
     
    
    267
    -        ReplaceSqlStatementGenerator replaceGenerator = new ReplaceSqlStatementGenerator(metadataModel, referentialName);
    
    268
    -        DeleteSqlStatementGenerator deleteGenerator = new DeleteSqlStatementGenerator(metadata);
    
    257
    +        ReplaceSqlStatementGenerator replaceGenerator = new ReplaceSqlStatementGenerator(metadataModel, metadata);
    
    258
    +        DeleteSqlStatementGenerator deleteGenerator = new DeleteSqlStatementGenerator(metadataModel, metadata);
    
    269 259
     
    
    270 260
             for (ReferentialSynchronizeTask<R> task : tasks) {
    
    271 261
     
    
    ... ... @@ -291,8 +281,8 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    291 281
     
    
    292 282
             TopiaMetadataEntity metadata = metadataModel.getEntity(referentialName);
    
    293 283
     
    
    294
    -        ReplaceSqlStatementGenerator replaceGenerator = new ReplaceSqlStatementGenerator(metadataModel, referentialName);
    
    295
    -        DesactivateSqlStatementGenerator desactivateGenerator = new DesactivateSqlStatementGenerator(metadata);
    
    284
    +        ReplaceSqlStatementGenerator replaceGenerator = new ReplaceSqlStatementGenerator(metadataModel, metadata);
    
    285
    +        DisableSqlStatementGenerator desactivateGenerator = new DisableSqlStatementGenerator(metadata);
    
    296 286
     
    
    297 287
             for (ReferentialSynchronizeTask<R> task : tasks) {
    
    298 288
                 String referentialId = task.getReferentialId();
    
    ... ... @@ -305,9 +295,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    305 295
                 }
    
    306 296
                 String sql = desactivateGenerator.generateSql(referentialId);
    
    307 297
     
    
    308
    -            if (log.isInfoEnabled()) {
    
    309
    -                log.info("Desactivate referential: " + type.getName() + " / " + referentialId + " -- " + sql);
    
    310
    -            }
    
    298
    +            log.info("Desactivate referential: " + type.getName() + " / " + referentialId + " -- " + sql);
    
    311 299
                 resultBuilder.addDesactivateStatement(sql);
    
    312 300
             }
    
    313 301
     
    
    ... ... @@ -320,7 +308,7 @@ public class ReferentialSynchronizeSqlsRequestBuilder {
    320 308
             String referentialName = ObserveEntityEnum.valueOf(entityType).name();
    
    321 309
             TopiaMetadataEntity metadata = metadataModel.getEntity(referentialName);
    
    322 310
     
    
    323
    -        InsertSqlStatementGenerator<D> insertGenerator = new InsertSqlStatementGenerator<>(metadata, dtoType);
    
    311
    +        InsertSqlStatementGenerator<D> insertGenerator = new InsertSqlStatementGenerator<>(metadataModel, metadata, dtoType);
    
    324 312
             D referential = service.loadEntityToReferentialDto(binder, id);
    
    325 313
     
    
    326 314
             return insertGenerator.generateSql(referential);
    

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/DeleteSqlStatementGenerator.java
    ... ... @@ -23,16 +23,14 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.sql;
    23 23
      */
    
    24 24
     
    
    25 25
     import com.google.common.collect.ImmutableList;
    
    26
    -import fr.ird.observe.services.local.service.actions.synchro.referential.sql.UpdateSqlStatementGenerator.ManyToManyAssociationStruct;
    
    27
    -import org.apache.logging.log4j.LogManager;
    
    28
    -import org.apache.logging.log4j.Logger;
    
    26
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    29 27
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    28
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    29
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    30 30
     
    
    31
    -import java.util.LinkedHashSet;
    
    32 31
     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;
    
    32
    +import java.util.Objects;
    
    33
    +import java.util.Set;
    
    36 34
     
    
    37 35
     /**
    
    38 36
      * Pour générer une requète sql de suppression à partir d'un référentiel donné.
    
    ... ... @@ -43,56 +41,23 @@ import static fr.ird.observe.services.local.service.actions.synchro.referential.
    43 41
      */
    
    44 42
     public class DeleteSqlStatementGenerator {
    
    45 43
     
    
    46
    -    /** Logger. */
    
    47
    -    private static final Logger log = LogManager.getLogger(DeleteSqlStatementGenerator.class);
    
    48
    -
    
    49
    -    private static final String DELETE_STATEMENT = "DELETE FROM %s.%s WHERE topiaId = '%s';\n";
    
    50
    -    private static final String DELETE_ASSOCIATION_STATEMENT = "DELETE FROM %s.%s WHERE %s = '%s';\n";
    
    51
    -
    
    52
    -    private final String schemaName;
    
    53
    -    private final String tableName;
    
    54
    -    private final LinkedHashSet<ManyToManyAssociationStruct> manyToManyAssociations;
    
    44
    +    private final Set<TopiaMetadataAssociation> associations;
    
    45
    +    private final TopiaMetadataEntity metadataEntity;
    
    55 46
     
    
    56
    -    public DeleteSqlStatementGenerator(TopiaMetadataEntity metadataEntity) {
    
    57
    -        this.schemaName = metadataEntity.getDbSchemaName();
    
    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
    -        }
    
    47
    +    public DeleteSqlStatementGenerator(TopiaMetadataModel topiaMetadataModel, TopiaMetadataEntity metadataEntity) {
    
    48
    +        this.metadataEntity = Objects.requireNonNull(metadataEntity);
    
    49
    +        this.associations = topiaMetadataModel.getReverseAssociations(metadataEntity);
    
    68 50
         }
    
    69 51
     
    
    70 52
         public List<String> generateSql(String id) {
    
    71 53
             ImmutableList.Builder<String> result = ImmutableList.builder();
    
    72
    -        String sql = String.format(DELETE_STATEMENT, schemaName, tableName, id);
    
    73
    -        if (log.isDebugEnabled()) {
    
    74
    -            log.debug("sql: " + sql);
    
    75
    -        }
    
    54
    +        String sql = TopiaSqlStatements.generateDeleteStatement(metadataEntity, id);
    
    76 55
             result.add(sql);
    
    77
    -        for (ManyToManyAssociationStruct manyToManyAssociation : manyToManyAssociations) {
    
    78
    -            generateManyToManyAssociationSql(id, manyToManyAssociation, result);
    
    56
    +        for (TopiaMetadataAssociation association : associations) {
    
    57
    +            String sql2 = TopiaSqlStatements.generateAssociationDeleteStatement(association, id);
    
    58
    +            result.add(TopiaSqlStatements.boxAssociationStatement(sql2));
    
    79 59
             }
    
    80 60
             return result.build();
    
    81 61
         }
    
    82 62
     
    
    83
    -    private void generateManyToManyAssociationSql(String referentialDtoId, ManyToManyAssociationStruct manyToManyAssociation, ImmutableList.Builder<String> result) {
    
    84
    -
    
    85
    -        String manyToManyAssociationTableName = manyToManyAssociation.tableName;
    
    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);
    
    94
    -        if (log.isDebugEnabled()) {
    
    95
    -            log.debug("sql: " + deleteSql);
    
    96
    -        }
    
    97
    -    }
    
    98 63
     }

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/DesactivateSqlStatementGenerator.javaservices-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/DisableSqlStatementGenerator.java
    ... ... @@ -10,21 +10,22 @@ 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;
    
    26
    -import org.apache.logging.log4j.LogManager;
    
    27 25
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    26
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    27
    +
    
    28
    +import java.util.Objects;
    
    28 29
     
    
    29 30
     /**
    
    30 31
      * Pour générer une requète sql de désactivation d'un référentiel donné.
    
    ... ... @@ -33,35 +34,16 @@ import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    33 34
      * @author Tony Chemit - dev@tchemit.fr
    
    34 35
      * @since 5.0
    
    35 36
      */
    
    36
    -public class DesactivateSqlStatementGenerator {
    
    37
    +public class DisableSqlStatementGenerator {
    
    37 38
     
    
    38
    -    /** Logger. */
    
    39
    -    private static final Logger log = LogManager.getLogger(DesactivateSqlStatementGenerator.class);
    
    39
    +    private final TopiaMetadataEntity metadataEntity;
    
    40 40
     
    
    41
    -    private static final String UPDATE_STATEMENT = "UPDATE %s.%s SET enabled = false, topiaVersion = topiaVersion + 1 WHERE topiaId ='%s';\n";
    
    42
    -
    
    43
    -    private final String schemaName;
    
    44
    -    private final String tableName;
    
    45
    -
    
    46
    -    public DesactivateSqlStatementGenerator(TopiaMetadataEntity metadataEntity) {
    
    47
    -        this.schemaName = metadataEntity.getDbSchemaName();
    
    48
    -        this.tableName = metadataEntity.getDbTableName();
    
    41
    +    public DisableSqlStatementGenerator(TopiaMetadataEntity metadataEntity) {
    
    42
    +        this.metadataEntity = Objects.requireNonNull(metadataEntity);
    
    49 43
         }
    
    50 44
     
    
    51 45
         public String generateSql(String sourceId) {
    
    52
    -
    
    53
    -        StringBuilder result = new StringBuilder();
    
    54
    -
    
    55
    -        String sql = String.format(UPDATE_STATEMENT, schemaName, tableName, sourceId);
    
    56
    -        result.append(sql);
    
    57
    -
    
    58
    -        if (log.isDebugEnabled()) {
    
    59
    -            log.debug("sql: " + sql);
    
    60
    -        }
    
    61
    -
    
    62
    -        return result.toString();
    
    63
    -
    
    46
    +        return TopiaSqlStatements.generateUpdateStatement(metadataEntity, sourceId, "enabled = false, topiaVersion = topiaVersion + 1");
    
    64 47
         }
    
    65 48
     
    
    66
    -
    
    67 49
     }

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/InsertSqlStatementGenerator.java
    ... ... @@ -10,12 +10,12 @@ 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>.
    
    ... ... @@ -25,13 +25,12 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.sql;
    25 25
     import com.google.common.collect.ImmutableList;
    
    26 26
     import fr.ird.observe.dto.reference.ReferentialDtoReference;
    
    27 27
     import fr.ird.observe.dto.referential.ReferentialDto;
    
    28
    -import fr.ird.observe.persistence.ObserveEntityEnum;
    
    29
    -import fr.ird.observe.spi.DbModelHelper;
    
    30
    -import org.apache.commons.collections4.CollectionUtils;
    
    31
    -import org.apache.logging.log4j.LogManager;
    
    32
    -import org.apache.logging.log4j.Logger;
    
    33 28
     import org.nuiton.topia.persistence.TopiaEntity;
    
    29
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    30
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataComposition;
    
    34 31
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    32
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    33
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    35 34
     import org.nuiton.util.beans.Binder;
    
    36 35
     import org.nuiton.util.beans.BinderFactory;
    
    37 36
     
    
    ... ... @@ -42,6 +41,7 @@ import java.util.LinkedHashSet;
    42 41
     import java.util.LinkedList;
    
    43 42
     import java.util.List;
    
    44 43
     import java.util.Map;
    
    44
    +import java.util.Objects;
    
    45 45
     import java.util.Set;
    
    46 46
     
    
    47 47
     /**
    
    ... ... @@ -53,49 +53,31 @@ import java.util.Set;
    53 53
      */
    
    54 54
     public class InsertSqlStatementGenerator<D extends ReferentialDto> {
    
    55 55
     
    
    56
    -    /** Logger. */
    
    57
    -    private static final Logger log = LogManager.getLogger(InsertSqlStatementGenerator.class);
    
    58
    -
    
    59
    -    private static final String INSERT_STATEMENT = "INSERT INTO %s.%s(%s) VALUES (%s);\n";
    
    60
    -    // We use a special prefix because those requests must be applied after all simple insert requests
    
    61
    -    public static final String MANY_TO_MANY_ASSOCIATION_INSERT_STATEMENT = "$$_INSERT INTO %s.%s(%s, %s) VALUES ('%s', '%s');\n";
    
    62
    -
    
    56
    +    private final TopiaMetadataEntity metadataEntity;
    
    57
    +    private final Set<TopiaMetadataComposition> compositions;
    
    58
    +    private final Set<TopiaMetadataAssociation> associations;
    
    63 59
         private final Set<String> columnNames;
    
    64
    -    private final String schemaName;
    
    65
    -    private final String tableName;
    
    66 60
         private final Binder<D, D> binder;
    
    67 61
         private final String[] simplePropertyNames;
    
    68
    -    private final String[] manyToOneAssociationNames;
    
    69
    -    private final Set<ManyToManyAssociationStruct> manyToManyAssociations;
    
    62
    +
    
    70 63
         private final Set<String> primitiveBooleanPropertyNames;
    
    71 64
         private final Set<String> primitiveIntegerPropertyNames;
    
    72 65
         private final Set<String> primitiveLongPropertyNames;
    
    73 66
         private final Set<String> primitiveFloatPropertyNames;
    
    74 67
     
    
    75
    -    public InsertSqlStatementGenerator(TopiaMetadataEntity metadataEntity, Class<D> dtoType) {
    
    76
    -        this.schemaName = metadataEntity.getDbSchemaName();
    
    77
    -        this.tableName = metadataEntity.getDbTableName();
    
    68
    +    public InsertSqlStatementGenerator(TopiaMetadataModel topiaMetadataModel, TopiaMetadataEntity metadataEntity, Class<D> dtoType) {
    
    69
    +        this.metadataEntity = Objects.requireNonNull(metadataEntity);
    
    70
    +        this.compositions = topiaMetadataModel.getCompositions(this.metadataEntity);
    
    71
    +        this.associations = topiaMetadataModel.getAssociations(this.metadataEntity);
    
    72
    +
    
    78 73
             Set<String> propertyNamesSet = metadataEntity.getProperties().keySet();
    
    79 74
             this.simplePropertyNames = propertyNamesSet.toArray(new String[0]);
    
    80 75
             this.primitiveBooleanPropertyNames = metadataEntity.getPrimitivePropertyNames("boolean");
    
    81 76
             this.primitiveIntegerPropertyNames = metadataEntity.getPrimitivePropertyNames("int");
    
    82 77
             this.primitiveLongPropertyNames = metadataEntity.getPrimitivePropertyNames("long");
    
    83 78
             this.primitiveFloatPropertyNames = metadataEntity.getPrimitivePropertyNames("float");
    
    84
    -        Set<String> manyToOneAssociationNamesSet = metadataEntity.getManyToOneAssociations().keySet();
    
    85
    -        this.manyToOneAssociationNames = manyToOneAssociationNamesSet.toArray(new String[0]);
    
    86
    -
    
    87
    -        Map<String, String> manyToManyAssociationsMap = metadataEntity.getManyToManyAssociations();
    
    88
    -        this.manyToManyAssociations = new LinkedHashSet<>();
    
    89
    -        for (Map.Entry<String, String> entry : manyToManyAssociationsMap.entrySet()) {
    
    90
    -            String propertyName = entry.getKey();
    
    91
    -            String dbColumnName = metadataEntity.getDbColumnName(propertyName);
    
    92
    -            String tableName = metadataEntity.getBdManyToManyAssociationTableName(propertyName);
    
    93
    -            String typeName = entry.getValue();
    
    94
    -            ObserveEntityEnum entityEnum = ObserveEntityEnum.valueOf(typeName);
    
    95
    -            Class<? extends ReferentialDto> referentialype = DbModelHelper.fromReferentialEntity(entityEnum).toDtoType();
    
    96
    -            ManyToManyAssociationStruct manyToManyAssociation = new ManyToManyAssociationStruct(propertyName, dbColumnName, tableName, referentialype);
    
    97
    -            manyToManyAssociations.add(manyToManyAssociation);
    
    98
    -        }
    
    79
    +
    
    80
    +        String[] manyToOneAssociationNames = compositions.stream().map(TopiaMetadataComposition::getTargetDbName).toArray(String[]::new);
    
    99 81
             this.columnNames = computeColumnNames(metadataEntity, simplePropertyNames, manyToOneAssociationNames);
    
    100 82
             this.binder = BinderFactory.newBinder(dtoType);
    
    101 83
         }
    
    ... ... @@ -151,76 +133,46 @@ public class InsertSqlStatementGenerator<D extends ReferentialDto> {
    151 133
                 addOtherTypeParameter(parameterValue, parameters);
    
    152 134
             }
    
    153 135
     
    
    154
    -        Map<String, Object> manyToOneParameters = binder.obtainProperties(referentialDto, true, true, manyToOneAssociationNames);
    
    155
    -        for (String manyToOneAssociationName : manyToOneAssociationNames) {
    
    156
    -            Object parameterValue = manyToOneParameters.get(manyToOneAssociationName);
    
    157
    -            if (parameterValue == null) {
    
    136
    +        for (TopiaMetadataComposition composition : compositions) {
    
    137
    +            Object compositionValue = binder.obtainSourceProperty(referentialDto, composition.getTargetPropertyName());
    
    138
    +            if (compositionValue == null) {
    
    158 139
                     addNullParameter(parameters);
    
    159 140
                     continue;
    
    160 141
                 }
    
    161
    -            if (parameterValue instanceof ReferentialDto) {
    
    162
    -                addReferentialDtoParameter((ReferentialDto) parameterValue, parameters, result);
    
    142
    +            if (compositionValue instanceof ReferentialDto) {
    
    143
    +                addReferentialDtoParameter((ReferentialDto) compositionValue, parameters, result);
    
    163 144
                     continue;
    
    164 145
                 }
    
    165
    -            if (parameterValue instanceof ReferentialDtoReference) {
    
    166
    -                addReferentialReferenceParameter((ReferentialDtoReference) parameterValue, parameters, result);
    
    146
    +            if (compositionValue instanceof ReferentialDtoReference) {
    
    147
    +                addReferentialReferenceParameter((ReferentialDtoReference) compositionValue, parameters, result);
    
    167 148
                 }
    
    168 149
             }
    
    169 150
     
    
    151
    +        String insertSql = TopiaSqlStatements.generateInsertStatement(metadataEntity,
    
    152
    +                                                                      String.join(",", columnNames),
    
    153
    +                                                                      String.join(",", parameters));
    
    154
    +        result.add(insertSql);
    
    170 155
     
    
    171
    -        result.add(String.format(INSERT_STATEMENT,
    
    172
    -                                 schemaName,
    
    173
    -                                 tableName,
    
    174
    -                                 String.join(",", columnNames),
    
    175
    -                                 String.join(",", parameters)));
    
    156
    +        String id = referentialDto.getId();
    
    157
    +        for (TopiaMetadataAssociation association : associations) {
    
    176 158
     
    
    177
    -        if (log.isDebugEnabled()) {
    
    178
    -            log.debug("sql: " + result);
    
    179
    -        }
    
    180
    -        for (ManyToManyAssociationStruct manyToManyAssociation : manyToManyAssociations) {
    
    181
    -            generateNmAssociationSql(referentialDto, manyToManyAssociation, result);
    
    159
    +            Collection<ReferentialDtoReference<?, ?>> associationValues = binder.obtainSourceProperty(referentialDto, association.getTargetPropertyName());
    
    160
    +            for (ReferentialDtoReference<?, ?> associationValue : associationValues) {
    
    161
    +                String insertAssocationSql = TopiaSqlStatements.generateAssociationInsertStatement(association, id, associationValue.getId());
    
    162
    +                result.add(TopiaSqlStatements.boxAssociationStatement(insertAssocationSql));
    
    163
    +            }
    
    182 164
             }
    
    183
    -
    
    184 165
             return result.build();
    
    185 166
     
    
    186 167
         }
    
    187 168
     
    
    188
    -    private void generateNmAssociationSql(D referentialDto, ManyToManyAssociationStruct manyToManyAssociationStruct, ImmutableList.Builder<String> result) {
    
    189
    -
    
    190
    -        Collection<ReferentialDtoReference<?, ?>> manyToManyAssociationValues = binder.obtainSourceProperty(referentialDto, manyToManyAssociationStruct.propertyName);
    
    191
    -        if (CollectionUtils.isNotEmpty(manyToManyAssociationValues)) {
    
    192
    -
    
    193
    -            String nmAssociationTableName = manyToManyAssociationStruct.tableName;
    
    194
    -            String nmAssociationDbColumnName = manyToManyAssociationStruct.dbColumnName;
    
    195
    -            String referentialDtoId = referentialDto.getId();
    
    196
    -
    
    197
    -            for (ReferentialDtoReference<?, ?> nmAssociationValue : manyToManyAssociationValues) {
    
    198
    -
    
    199
    -                addMnAssociation(nmAssociationTableName, nmAssociationDbColumnName, referentialDtoId, manyToManyAssociationStruct.type, nmAssociationValue.getId(), result);
    
    200
    -
    
    201
    -            }
    
    202
    -        }
    
    203
    -    }
    
    204
    -
    
    205
    -    <DD extends ReferentialDto> void addMnAssociation(String nmAssociationTableName,
    
    206
    -                                                      String nmAssociationDbColumnName,
    
    207
    -                                                      String referentialDtoId,
    
    208
    -                                                      Class<DD> associationType,
    
    209
    -                                                      String associationId,
    
    210
    -                                                      ImmutableList.Builder<String> builder) {
    
    211
    -
    
    212
    -        String sql = String.format(MANY_TO_MANY_ASSOCIATION_INSERT_STATEMENT,
    
    213
    -                                   schemaName,
    
    214
    -                                   nmAssociationTableName,
    
    215
    -                                   this.tableName,
    
    216
    -                                   nmAssociationDbColumnName,
    
    217
    -                                   referentialDtoId,
    
    218
    -                                   associationId);
    
    219
    -        if (log.isDebugEnabled()) {
    
    220
    -            log.debug("sql: " + sql);
    
    221
    -        }
    
    222
    -        builder.add(sql);
    
    223 169
     
    
    170
    +    <DD extends ReferentialDto, RR extends ReferentialDtoReference<DD, RR>> void addAssociation(TopiaMetadataAssociation association,
    
    171
    +                                                                                                String sourceId,
    
    172
    +                                                                                                RR associationValue,
    
    173
    +                                                                                                ImmutableList.Builder<String> builder) {
    
    174
    +        String insertAssocationSql = TopiaSqlStatements.generateAssociationInsertStatement(association, sourceId, associationValue.getId());
    
    175
    +        builder.add(TopiaSqlStatements.boxAssociationStatement(insertAssocationSql));
    
    224 176
         }
    
    225 177
     
    
    226 178
         private Set<String> computeColumnNames(TopiaMetadataEntity metadataEntity,
    
    ... ... @@ -288,32 +240,4 @@ public class InsertSqlStatementGenerator<D extends ReferentialDto> {
    288 240
             addStringParameter(parameter.getId(), parameters);
    
    289 241
         }
    
    290 242
     
    
    291
    -    /**
    
    292
    -     * Pour décrire une association nm.
    
    293
    -     */
    
    294
    -    private static class ManyToManyAssociationStruct {
    
    295
    -
    
    296
    -        /**
    
    297
    -         * Le nom de la propriété dans l'objet.
    
    298
    -         */
    
    299
    -        private final String propertyName;
    
    300
    -        /**
    
    301
    -         * Le nom de la colonne de l'association dans la table d'association.
    
    302
    -         */
    
    303
    -        private final String dbColumnName;
    
    304
    -        /**
    
    305
    -         * Le nom de la table d'association.
    
    306
    -         */
    
    307
    -        private final String tableName;
    
    308
    -
    
    309
    -        private final Class<? extends ReferentialDto> type;
    
    310
    -
    
    311
    -        private ManyToManyAssociationStruct(String propertyName, String dbColumnName, String tableName, Class<? extends ReferentialDto> type) {
    
    312
    -            this.propertyName = propertyName;
    
    313
    -            this.dbColumnName = dbColumnName;
    
    314
    -            this.tableName = tableName;
    
    315
    -            this.type = type;
    
    316
    -        }
    
    317
    -
    
    318
    -    }
    
    319 243
     }

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/InsertSqlWithCascadeStatementGenerator.java
    ... ... @@ -10,12 +10,12 @@ 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>.
    
    ... ... @@ -26,7 +26,9 @@ import com.google.common.collect.ImmutableList;
    26 26
     import com.google.common.collect.Multimap;
    
    27 27
     import fr.ird.observe.dto.reference.ReferentialDtoReference;
    
    28 28
     import fr.ird.observe.dto.referential.ReferentialDto;
    
    29
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    29 30
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    31
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    30 32
     
    
    31 33
     import java.util.List;
    
    32 34
     
    
    ... ... @@ -43,16 +45,19 @@ public abstract class InsertSqlWithCascadeStatementGenerator<D extends Referenti
    43 45
         private final InsertSqlStatementGenerator<D> delegateGenerator;
    
    44 46
         private final Multimap<Class<? extends ReferentialDtoReference>, String> idsOnlyExistingOnThisSide;
    
    45 47
     
    
    46
    -    protected InsertSqlWithCascadeStatementGenerator(TopiaMetadataEntity metadataEntity,
    
    48
    +    protected InsertSqlWithCascadeStatementGenerator(TopiaMetadataModel topiaMetadataModel, TopiaMetadataEntity metadataEntity,
    
    47 49
                                                          Class<D> dtoType,
    
    48 50
                                                          Multimap<Class<? extends ReferentialDtoReference>, String> idsOnlyExistingOnThisSide) {
    
    49 51
             this.idsOnlyExistingOnThisSide = idsOnlyExistingOnThisSide;
    
    50
    -        this.delegateGenerator = new InsertSqlStatementGenerator<D>(metadataEntity, dtoType) {
    
    52
    +        this.delegateGenerator = new InsertSqlStatementGenerator<D>(topiaMetadataModel, metadataEntity, dtoType) {
    
    51 53
     
    
    52 54
                 @Override
    
    53
    -            protected <DD extends ReferentialDto> void addMnAssociation(String nmAssociationTableName, String nmAssociationDbColumnName, String referentialDtoId, Class<DD> associationType, String associationId, ImmutableList.Builder<String> builder) {
    
    54
    -                super.addMnAssociation(nmAssociationTableName, nmAssociationDbColumnName, referentialDtoId, associationType, associationId, builder);
    
    55
    -                addMissingReferentialIfNecessary(associationType, associationId, builder);
    
    55
    +            <DD extends ReferentialDto, RR extends ReferentialDtoReference<DD, RR>> void addAssociation(TopiaMetadataAssociation association,
    
    56
    +                                                                                                        String sourceId,
    
    57
    +                                                                                                        RR associationValue,
    
    58
    +                                                                                                        ImmutableList.Builder<String> builder) {
    
    59
    +                super.addAssociation(association, sourceId, associationValue, builder);
    
    60
    +                addMissingReferentialIfNecessary(associationValue.getDtoType(), associationValue.getId(), builder);
    
    56 61
                 }
    
    57 62
     
    
    58 63
                 @Override
    

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/ReplaceSqlStatementGenerator.java
    ... ... @@ -10,12 +10,12 @@ 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>.
    
    ... ... @@ -23,17 +23,25 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.sql;
    23 23
      */
    
    24 24
     
    
    25 25
     import com.google.common.collect.ImmutableList;
    
    26
    +import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
    
    27
    +import fr.ird.observe.persistence.ObserveEntityEnum;
    
    26 28
     import org.apache.logging.log4j.LogManager;
    
    27 29
     import org.apache.logging.log4j.Logger;
    
    30
    +import org.nuiton.topia.persistence.TopiaEntity;
    
    31
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    32
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataComposition;
    
    28 33
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    29 34
     import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    35
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    30 36
     
    
    31
    -import java.util.LinkedHashSet;
    
    37
    +import java.util.Objects;
    
    32 38
     import java.util.Set;
    
    33
    -import java.util.stream.Collectors;
    
    34 39
     
    
    35 40
     /**
    
    36 41
      * Pour générer une requète sql de remplacement à partir d'un référentiel donné.
    
    42
    + * <p>
    
    43
    + * À noter que l'on ne traite pas ici les remplacements d'association sur des types référentiel.
    
    44
    + * <p>
    
    37 45
      * Created on 04/07/16.
    
    38 46
      *
    
    39 47
      * @author Tony Chemit - dev@tchemit.fr
    
    ... ... @@ -44,118 +52,36 @@ public class ReplaceSqlStatementGenerator {
    44 52
         /** Logger. */
    
    45 53
         private static final Logger log = LogManager.getLogger(ReplaceSqlStatementGenerator.class);
    
    46 54
     
    
    47
    -    private static final String MANY_TO_ONE_ASSOCIATION_UPDATE_STATEMENT = "UPDATE %s.%s SET %s = '%s', topiaVersion = topiaVersion + 1 WHERE %s = '%s';\n";
    
    48
    -    private static final String MANY_TO_MANY_ASSOCIATION_UPDATE_STATEMENT = "UPDATE %s.%s SET %s = '%s' WHERE %s = '%s';\n";
    
    49
    -
    
    50 55
         /**
    
    51 56
          * Informations pour remplacer dans une relation many-to-one.
    
    52 57
          */
    
    53
    -    private final Set<ReplacementStruct> manyToOneAssociationReplacements;
    
    58
    +    private final Set<TopiaMetadataComposition> compositions;
    
    54 59
         /**
    
    55 60
          * Informations pour remplacer dans une relation many-to-many.
    
    56 61
          */
    
    57
    -    private final Set<ReplacementStruct> manyToManyAssociationReplacements;
    
    62
    +    private final Set<TopiaMetadataAssociation> associations;
    
    58 63
     
    
    59
    -    public ReplaceSqlStatementGenerator(TopiaMetadataModel topiaMetadataModel, String referentialName) {
    
    60
    -        this.manyToOneAssociationReplacements = computeManyToOneAssociationReplacements(referentialName, topiaMetadataModel);
    
    61
    -        this.manyToManyAssociationReplacements = computeManyToManyAssociationReplacements(referentialName, topiaMetadataModel);
    
    64
    +    public ReplaceSqlStatementGenerator(TopiaMetadataModel topiaMetadataModel, TopiaMetadataEntity metadataEntity) {
    
    65
    +        this.compositions = Objects.requireNonNull(topiaMetadataModel).getReverseCompositions(Objects.requireNonNull(metadataEntity));
    
    66
    +        this.associations = topiaMetadataModel.getReverseAssociations(metadataEntity);
    
    62 67
         }
    
    63 68
     
    
    64 69
         public ImmutableList<String> generateSqls(String sourceId, String replacementId) {
    
    65
    -
    
    66 70
             ImmutableList.Builder<String> builder = ImmutableList.builder();
    
    67
    -
    
    68
    -        for (ReplacementStruct replacementStruct : manyToOneAssociationReplacements) {
    
    69
    -
    
    70
    -            String sql = generateSqlStatement(MANY_TO_ONE_ASSOCIATION_UPDATE_STATEMENT, sourceId, replacementId, replacementStruct);
    
    71
    +        for (TopiaMetadataComposition replacementStruct : compositions) {
    
    72
    +            String sql = TopiaSqlStatements.generateCompositionUpdateStatement(replacementStruct, sourceId, replacementId);
    
    71 73
                 builder.add(sql);
    
    72
    -
    
    73 74
             }
    
    74
    -
    
    75
    -        for (ReplacementStruct replacementStruct : manyToManyAssociationReplacements) {
    
    76
    -
    
    77
    -            String sql = generateSqlStatement(MANY_TO_MANY_ASSOCIATION_UPDATE_STATEMENT, sourceId, replacementId, replacementStruct);
    
    75
    +        for (TopiaMetadataAssociation replacementStruct : associations) {
    
    76
    +            Class<? extends TopiaEntity> entityType = ObserveEntityEnum.valueOf(replacementStruct.getOwner().getType()).getContract();
    
    77
    +            if (ObserveReferentialEntity.class.isAssignableFrom(entityType)) {
    
    78
    +                // do not update referentials associations (see https://gitlab.com/ultreiaio/ird-observe/issues/1065)
    
    79
    +                continue;
    
    80
    +            }
    
    81
    +            String sql = TopiaSqlStatements.generateAssociationUpdateStatement(replacementStruct, sourceId, replacementId);
    
    78 82
                 builder.add(sql);
    
    79 83
             }
    
    80
    -
    
    81 84
             return builder.build();
    
    82
    -
    
    83
    -    }
    
    84
    -
    
    85
    -    private Set<ReplacementStruct> computeManyToOneAssociationReplacements(String referentialName, TopiaMetadataModel topiaMetadataModel) {
    
    86
    -
    
    87
    -        Set<ReplacementStruct> result = new LinkedHashSet<>();
    
    88
    -
    
    89
    -        for (TopiaMetadataEntity metadataEntity : topiaMetadataModel) {
    
    90
    -
    
    91
    -            result.addAll(metadataEntity.getManyToOneAssociations().entrySet().stream()
    
    92
    -                                  .filter(entry -> entry.getValue().equals(referentialName))
    
    93
    -                                  .map(entry -> new ReplacementStruct(metadataEntity.getDbSchemaName(),
    
    94
    -                                                                      metadataEntity.getDbTableName(),
    
    95
    -                                                                      metadataEntity.getDbColumnName(entry.getKey())))
    
    96
    -                                  .collect(Collectors.toList()));
    
    97
    -
    
    98
    -        }
    
    99
    -
    
    100
    -        return result;
    
    101
    -
    
    102
    -    }
    
    103
    -
    
    104
    -    private Set<ReplacementStruct> computeManyToManyAssociationReplacements(String referentialName, TopiaMetadataModel topiaMetadataModel) {
    
    105
    -
    
    106
    -        Set<ReplacementStruct> result = new LinkedHashSet<>();
    
    107
    -
    
    108
    -        for (TopiaMetadataEntity metadataEntity : topiaMetadataModel) {
    
    109
    -
    
    110
    -            result.addAll(metadataEntity.getManyToManyAssociations().entrySet().stream()
    
    111
    -                                  .filter(entry -> entry.getValue().equals(referentialName))
    
    112
    -                                  .map(entry -> new ReplacementStruct(metadataEntity.getDbSchemaName(),
    
    113
    -                                                                      metadataEntity.getBdManyToManyAssociationTableName(entry.getKey()),
    
    114
    -                                                                      metadataEntity.getDbColumnName(entry.getKey())))
    
    115
    -                                  .collect(Collectors.toList()));
    
    116
    -
    
    117
    -        }
    
    118
    -
    
    119
    -        return result;
    
    120
    -
    
    121
    -    }
    
    122
    -
    
    123
    -    private String generateSqlStatement(String sqlPattern, String sourceId, String replacementId, ReplacementStruct replacementStruct) {
    
    124
    -
    
    125
    -        String sql = String.format(sqlPattern,
    
    126
    -                                   replacementStruct.schemaName,
    
    127
    -                                   replacementStruct.tableName,
    
    128
    -                                   replacementStruct.columnName,
    
    129
    -                                   replacementId,
    
    130
    -                                   replacementStruct.columnName,
    
    131
    -                                   sourceId);
    
    132
    -        if (log.isDebugEnabled()) {
    
    133
    -            log.debug("sql: " + sql);
    
    134
    -        }
    
    135
    -        return sql;
    
    136
    -
    
    137
    -    }
    
    138
    -
    
    139
    -    private static class ReplacementStruct {
    
    140
    -
    
    141
    -        private final String schemaName;
    
    142
    -        private final String tableName;
    
    143
    -        private final String columnName;
    
    144
    -
    
    145
    -        private ReplacementStruct(String schemaName, String tableName, String columnName) {
    
    146
    -            this.schemaName = schemaName;
    
    147
    -            this.tableName = tableName;
    
    148
    -            this.columnName = columnName;
    
    149
    -        }
    
    150
    -
    
    151
    -        @Override
    
    152
    -        public String toString() {
    
    153
    -            return "ReplacementStruct{" +
    
    154
    -                    "schemaName='" + schemaName + '\'' +
    
    155
    -                    ", tableName='" + tableName + '\'' +
    
    156
    -                    ", columnName='" + columnName + '\'' +
    
    157
    -                    '}';
    
    158
    -        }
    
    159 85
         }
    
    160 86
     
    
    161 87
     }

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/UpdateSqlStatementGenerator.java
    ... ... @@ -10,12 +10,12 @@ 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>.
    
    ... ... @@ -25,24 +25,23 @@ package fr.ird.observe.services.local.service.actions.synchro.referential.sql;
    25 25
     import com.google.common.collect.ImmutableList;
    
    26 26
     import fr.ird.observe.dto.reference.ReferentialDtoReference;
    
    27 27
     import fr.ird.observe.dto.referential.ReferentialDto;
    
    28
    -import org.apache.commons.collections4.CollectionUtils;
    
    29
    -import org.apache.logging.log4j.LogManager;
    
    30
    -import org.apache.logging.log4j.Logger;
    
    31 28
     import org.nuiton.topia.persistence.TopiaEntity;
    
    29
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    30
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataComposition;
    
    32 31
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    32
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    33
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    33 34
     import org.nuiton.util.beans.Binder;
    
    34 35
     import org.nuiton.util.beans.BinderFactory;
    
    35 36
     
    
    36 37
     import java.sql.Timestamp;
    
    37 38
     import java.util.Collection;
    
    38 39
     import java.util.Date;
    
    39
    -import java.util.LinkedHashSet;
    
    40 40
     import java.util.Map;
    
    41
    +import java.util.Objects;
    
    41 42
     import java.util.Set;
    
    42 43
     import java.util.TreeMap;
    
    43 44
     
    
    44
    -import static fr.ird.observe.services.local.service.actions.synchro.referential.sql.InsertSqlStatementGenerator.MANY_TO_MANY_ASSOCIATION_INSERT_STATEMENT;
    
    45
    -
    
    46 45
     /**
    
    47 46
      * Pour générer une requète sql de mise à jour à partir d'un référentiel donné.
    
    48 47
      * <p>
    
    ... ... @@ -52,49 +51,32 @@ import static fr.ird.observe.services.local.service.actions.synchro.referential.
    52 51
      */
    
    53 52
     public class UpdateSqlStatementGenerator<D extends ReferentialDto> {
    
    54 53
     
    
    55
    -    /** Logger. */
    
    56
    -    private static final Logger log = LogManager.getLogger(UpdateSqlStatementGenerator.class);
    
    57
    -
    
    58
    -    private static final String UPDATE_STATEMENT = "UPDATE %s.%s SET %s WHERE topiaId ='%s';\n";
    
    59
    -    // We use a special prefix because those requests must be applied before all simple insert requests
    
    60
    -    static final String MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT = "$$_DELETE FROM %s.%s WHERE %s = '%s';\n";
    
    61
    -
    
    62 54
         private final Map<String, String> columnNames;
    
    63
    -    private final String schemaName;
    
    64
    -    private final String tableName;
    
    65 55
         private final Binder<D, D> binder;
    
    66 56
         private final String[] simplePropertyNames;
    
    67
    -    private final String[] manyToOneAssociationNames;
    
    68
    -    private final Set<ManyToManyAssociationStruct> manyToManyAssociations;
    
    57
    +    private final TopiaMetadataEntity metadataEntity;
    
    58
    +    private final Set<TopiaMetadataComposition> compositions;
    
    59
    +    private final Set<TopiaMetadataAssociation> associations;
    
    69 60
         private final Set<String> primitiveBooleanPropertyNames;
    
    70 61
         private final Set<String> primitiveIntegerPropertyNames;
    
    71 62
         private final Set<String> primitiveLongPropertyNames;
    
    72 63
         private final Set<String> primitiveFloatPropertyNames;
    
    73 64
     
    
    74
    -    public UpdateSqlStatementGenerator(TopiaMetadataEntity metadataEntity, Class<D> dtoType) {
    
    75
    -        this.schemaName = metadataEntity.getDbSchemaName();
    
    76
    -        this.tableName = metadataEntity.getDbTableName();
    
    65
    +    public UpdateSqlStatementGenerator(TopiaMetadataModel topiaMetadataModel, TopiaMetadataEntity metadataEntity, Class<D> dtoType) {
    
    66
    +        this.metadataEntity = Objects.requireNonNull(metadataEntity);
    
    67
    +        this.compositions = topiaMetadataModel.getCompositions(metadataEntity);
    
    68
    +        this.associations = topiaMetadataModel.getAssociations(metadataEntity);
    
    77 69
     
    
    78
    -        Set<String> simplePropertyNamesSet = metadataEntity.getProperties().keySet();
    
    70
    +        Set<String> simplePropertyNamesSet = this.metadataEntity.getProperties().keySet();
    
    79 71
             this.simplePropertyNames = simplePropertyNamesSet.toArray(new String[0]);
    
    80
    -        this.primitiveBooleanPropertyNames = metadataEntity.getPrimitivePropertyNames("boolean");
    
    81
    -        this.primitiveIntegerPropertyNames = metadataEntity.getPrimitivePropertyNames("int");
    
    82
    -        this.primitiveLongPropertyNames = metadataEntity.getPrimitivePropertyNames("long");
    
    83
    -        this.primitiveFloatPropertyNames = metadataEntity.getPrimitivePropertyNames("float");
    
    84
    -
    
    85
    -        Set<String> manyToOneAssociationNamesSet = metadataEntity.getManyToOneAssociations().keySet();
    
    86
    -        this.manyToOneAssociationNames = manyToOneAssociationNamesSet.toArray(new String[0]);
    
    87
    -
    
    88
    -        Map<String, String> manyToManyAssociationsMap = metadataEntity.getManyToManyAssociations();
    
    89
    -        this.manyToManyAssociations = new LinkedHashSet<>();
    
    90
    -        for (Map.Entry<String, String> entry : manyToManyAssociationsMap.entrySet()) {
    
    91
    -            String propertyName = entry.getKey();
    
    92
    -            String dbColumnName = metadataEntity.getDbColumnName(propertyName);
    
    93
    -            String tableName = metadataEntity.getBdManyToManyAssociationTableName(propertyName);
    
    94
    -            ManyToManyAssociationStruct manyToManyAssociation = new ManyToManyAssociationStruct(propertyName, dbColumnName, tableName);
    
    95
    -            manyToManyAssociations.add(manyToManyAssociation);
    
    96
    -        }
    
    97
    -        this.columnNames = computeColumnNames(metadataEntity, simplePropertyNames, manyToOneAssociationNames);
    
    72
    +        this.primitiveBooleanPropertyNames = this.metadataEntity.getPrimitivePropertyNames("boolean");
    
    73
    +        this.primitiveIntegerPropertyNames = this.metadataEntity.getPrimitivePropertyNames("int");
    
    74
    +        this.primitiveLongPropertyNames = this.metadataEntity.getPrimitivePropertyNames("long");
    
    75
    +        this.primitiveFloatPropertyNames = this.metadataEntity.getPrimitivePropertyNames("float");
    
    76
    +
    
    77
    +        String[] manyToOneAssociationNames = compositions.stream().map(TopiaMetadataComposition::getTargetDbName).toArray(String[]::new);
    
    78
    +
    
    79
    +        this.columnNames = computeColumnNames(this.metadataEntity, simplePropertyNames, manyToOneAssociationNames);
    
    98 80
             this.binder = BinderFactory.newBinder(dtoType);
    
    99 81
         }
    
    100 82
     
    
    ... ... @@ -148,121 +130,54 @@ public class UpdateSqlStatementGenerator<D extends ReferentialDto> {
    148 130
                         addEnumParameter(columnName, (Enum) simplePropertyValue, parameters);
    
    149 131
                         continue;
    
    150 132
                     }
    
    151
    -
    
    152 133
                     addOtherTypeParameter(columnName, simplePropertyValue, parameters);
    
    153
    -
    
    154 134
                 }
    
    155
    -
    
    156 135
             }
    
    157 136
     
    
    158 137
             ImmutableList.Builder<String> result = ImmutableList.builder();
    
    159 138
     
    
    160
    -        if (manyToOneAssociationNames.length > 0) {
    
    161
    -
    
    162
    -            Map<String, Object> manyToOneAssociations = binder.obtainProperties(referentialDto, true, manyToOneAssociationNames);
    
    163
    -            for (Map.Entry<String, Object> entry : manyToOneAssociations.entrySet()) {
    
    164
    -
    
    165
    -                String manyToOneAssociationName = entry.getKey();
    
    166
    -                String columnName = columnNames.get(manyToOneAssociationName);
    
    167
    -                Object manyToOneAssociationValue = entry.getValue();
    
    168
    -
    
    169
    -                if (manyToOneAssociationValue == null) {
    
    170
    -                    addNullParameter(columnName, parameters);
    
    171
    -                    continue;
    
    172
    -                }
    
    173
    -                if (manyToOneAssociationValue instanceof ReferentialDto) {
    
    174
    -                    addReferentialDtoParameter(columnName, (ReferentialDto) manyToOneAssociationValue, parameters, result);
    
    175
    -                    continue;
    
    176
    -                }
    
    177
    -                if (manyToOneAssociationValue instanceof ReferentialDtoReference) {
    
    178
    -                    addReferentialReferenceParameter(columnName, (ReferentialDtoReference) manyToOneAssociationValue, parameters, result);
    
    179
    -                }
    
    180
    -
    
    139
    +        for (TopiaMetadataComposition composition : compositions) {
    
    140
    +            Object compositionValue = binder.obtainSourceProperty(referentialDto, composition.getTargetPropertyName());
    
    141
    +            String columnName = composition.getTargetDbName();
    
    142
    +            if (compositionValue == null) {
    
    143
    +                addNullParameter(columnName, parameters);
    
    144
    +                continue;
    
    145
    +            }
    
    146
    +            if (compositionValue instanceof ReferentialDto) {
    
    147
    +                addReferentialDtoParameter(columnName, (ReferentialDto) compositionValue, parameters, result);
    
    148
    +                continue;
    
    149
    +            }
    
    150
    +            if (compositionValue instanceof ReferentialDtoReference) {
    
    151
    +                addReferentialReferenceParameter(columnName, (ReferentialDtoReference) compositionValue, parameters, result);
    
    181 152
                 }
    
    182
    -
    
    183
    -        }
    
    184
    -
    
    185
    -
    
    186
    -        String sql = String.format(UPDATE_STATEMENT,
    
    187
    -                                   schemaName,
    
    188
    -                                   tableName,
    
    189
    -                                   parameters.substring(2),
    
    190
    -                                   referentialDto.getId());
    
    191
    -        result.add(sql);
    
    192
    -        if (log.isDebugEnabled()) {
    
    193
    -            log.debug("sql: " + sql);
    
    194
    -        }
    
    195
    -
    
    196
    -        for (ManyToManyAssociationStruct manyToManyAssociation : manyToManyAssociations) {
    
    197
    -            generateManyToManyAssociationSql(referentialDto, manyToManyAssociation, result);
    
    198
    -        }
    
    199
    -
    
    200
    -        return result.build();
    
    201
    -
    
    202
    -    }
    
    203
    -
    
    204
    -    private void generateManyToManyAssociationSql(D referentialDto, ManyToManyAssociationStruct manyToManyAssociation, ImmutableList.Builder<String> result) {
    
    205
    -
    
    206
    -        String referentialDtoId = referentialDto.getId();
    
    207
    -        String manyToManyAssociationTableName = manyToManyAssociation.tableName;
    
    208
    -
    
    209
    -        // On commence toujours par supprimer toutes les anciennes associations, elles seront ré-ajoutées juste après
    
    210
    -        String deleteSql = String.format(MANY_TO_MANY_ASSOCIATION_DELETE_STATEMENT,
    
    211
    -                                         schemaName,
    
    212
    -                                         manyToManyAssociationTableName,
    
    213
    -                                         tableName,
    
    214
    -                                         referentialDtoId);
    
    215
    -        result.add(deleteSql);
    
    216
    -        if (log.isDebugEnabled()) {
    
    217
    -            log.debug("sql: " + deleteSql);
    
    218 153
             }
    
    219 154
     
    
    220
    -        Collection<ReferentialDtoReference<?, ?>> manyToManyAssociationValues = binder.obtainSourceProperty(referentialDto, manyToManyAssociation.propertyName);
    
    221
    -        if (CollectionUtils.isNotEmpty(manyToManyAssociationValues)) {
    
    222
    -
    
    223
    -
    
    224
    -            String manyToManyAssociationDbColumnName = manyToManyAssociation.dbColumnName;
    
    155
    +        String updateSql = TopiaSqlStatements.generateUpdateStatement(metadataEntity, parameters.substring(2), referentialDto.getId());
    
    156
    +        result.add(updateSql);
    
    225 157
     
    
    158
    +        String id = referentialDto.getId();
    
    159
    +        for (TopiaMetadataAssociation association : associations) {
    
    160
    +            // On commence toujours par supprimer toutes les anciennes associations, elles seront ré-ajoutées juste après
    
    161
    +            String deleteSql = TopiaSqlStatements.generateAssociationDeleteStatement(association, id);
    
    162
    +            result.add(TopiaSqlStatements.boxAssociationStatement(deleteSql));
    
    226 163
     
    
    227
    -            for (ReferentialDtoReference<?, ?> manyToManyAssociationValue : manyToManyAssociationValues) {
    
    228
    -
    
    229
    -                addMnAssociation(
    
    230
    -                        manyToManyAssociationTableName,
    
    231
    -                        manyToManyAssociationDbColumnName,
    
    232
    -                        referentialDtoId,
    
    233
    -                        manyToManyAssociationValue.getDtoType(),
    
    234
    -                        manyToManyAssociationValue.getId(),
    
    235
    -                        result
    
    236
    -                );
    
    164
    +            Collection<ReferentialDtoReference<?, ?>> associationValues = binder.obtainSourceProperty(referentialDto, association.getTargetPropertyName());
    
    165
    +            for (ReferentialDtoReference associationValue : associationValues) {
    
    166
    +                addAssociation(association, id, associationValue, result);
    
    237 167
                 }
    
    238 168
             }
    
    239
    -
    
    169
    +        return result.build();
    
    240 170
         }
    
    241 171
     
    
    242
    -    protected <DD extends ReferentialDto> void addMnAssociation(String nmAssociationTableName,
    
    243
    -                                                                String nmAssociationDbColumnName,
    
    244
    -                                                                String referentialDtoId,
    
    245
    -                                                                Class<DD> associationType,
    
    246
    -                                                                String associationId,
    
    247
    -                                                                ImmutableList.Builder<String> builder) {
    
    248
    -
    
    249
    -        String sql = String.format(MANY_TO_MANY_ASSOCIATION_INSERT_STATEMENT,
    
    250
    -                                   schemaName,
    
    251
    -                                   nmAssociationTableName,
    
    252
    -                                   this.tableName,
    
    253
    -                                   nmAssociationDbColumnName,
    
    254
    -                                   referentialDtoId,
    
    255
    -                                   associationId);
    
    256
    -        if (log.isDebugEnabled()) {
    
    257
    -            log.debug("sql: " + sql);
    
    258
    -        }
    
    259
    -        builder.add(sql);
    
    260
    -
    
    172
    +    <DD extends ReferentialDto, RR extends ReferentialDtoReference<DD, RR>> void addAssociation(TopiaMetadataAssociation association,
    
    173
    +                                                                                                String sourceId,
    
    174
    +                                                                                                RR associationValue,
    
    175
    +                                                                                                ImmutableList.Builder<String> builder) {
    
    176
    +        String insertSql = TopiaSqlStatements.generateAssociationInsertStatement(association, sourceId, associationValue.getId());
    
    177
    +        builder.add(TopiaSqlStatements.boxAssociationStatement(insertSql));
    
    261 178
         }
    
    262 179
     
    
    263
    -    private Map<String, String> computeColumnNames(TopiaMetadataEntity metadataEntity,
    
    264
    -                                                   String[] simplePropertyNames,
    
    265
    -                                                   String[] compositionPropertyNames) {
    
    180
    +    private Map<String, String> computeColumnNames(TopiaMetadataEntity metadataEntity, String[] simplePropertyNames, String[] compositionPropertyNames) {
    
    266 181
             Map<String, String> columnNames = new TreeMap<>();
    
    267 182
     
    
    268 183
             for (String propertyName : simplePropertyNames) {
    
    ... ... @@ -326,29 +241,4 @@ public class UpdateSqlStatementGenerator<D extends ReferentialDto> {
    326 241
             addParameter0(columnName, "" + (parameter == null ? 0f : parameter), parameters);
    
    327 242
         }
    
    328 243
     
    
    329
    -    /**
    
    330
    -     * Pour décrire une association nm.
    
    331
    -     */
    
    332
    -    static class ManyToManyAssociationStruct {
    
    333
    -
    
    334
    -        /**
    
    335
    -         * Le nom de la propriété dans l'objet.
    
    336
    -         */
    
    337
    -        private final String propertyName;
    
    338
    -        /**
    
    339
    -         * Le nom de la colonne de l'association dans la table d'association.
    
    340
    -         */
    
    341
    -        private final String dbColumnName;
    
    342
    -        /**
    
    343
    -         * Le nom de la table d'association.
    
    344
    -         */
    
    345
    -        final String tableName;
    
    346
    -
    
    347
    -        ManyToManyAssociationStruct(String propertyName, String dbColumnName, String tableName) {
    
    348
    -            this.propertyName = propertyName;
    
    349
    -            this.dbColumnName = dbColumnName;
    
    350
    -            this.tableName = tableName;
    
    351
    -        }
    
    352
    -
    
    353
    -    }
    
    354 244
     }

  • services-local/src/main/java/fr/ird/observe/services/local/service/actions/synchro/referential/sql/UpdateSqlWithCascadeStatementGenerator.java
    ... ... @@ -10,12 +10,12 @@ 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>.
    
    ... ... @@ -26,7 +26,9 @@ import com.google.common.collect.ImmutableList;
    26 26
     import com.google.common.collect.Multimap;
    
    27 27
     import fr.ird.observe.dto.reference.ReferentialDtoReference;
    
    28 28
     import fr.ird.observe.dto.referential.ReferentialDto;
    
    29
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataAssociation;
    
    29 30
     import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    31
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    30 32
     
    
    31 33
     /**
    
    32 34
      * Pour générer une requète sql d'ajout à partir d'un référentiel donné et aussi tous les inserts manquants.
    
    ... ... @@ -41,17 +43,20 @@ public abstract class UpdateSqlWithCascadeStatementGenerator<D extends Referenti
    41 43
         private final UpdateSqlStatementGenerator<D> delegateGenerator;
    
    42 44
         private final Multimap<Class<? extends ReferentialDtoReference>, String> idsOnlyExistingOnThisSide;
    
    43 45
     
    
    44
    -    protected UpdateSqlWithCascadeStatementGenerator(TopiaMetadataEntity metadataEntity,
    
    46
    +    protected UpdateSqlWithCascadeStatementGenerator(TopiaMetadataModel topiaMetadataModel,
    
    47
    +                                                     TopiaMetadataEntity metadataEntity,
    
    45 48
                                                          Class<D> dtoType,
    
    46 49
                                                          Multimap<Class<? extends ReferentialDtoReference>, String> idsOnlyExistingOnThisSide) {
    
    47 50
             this.idsOnlyExistingOnThisSide = idsOnlyExistingOnThisSide;
    
    48
    -        this.delegateGenerator = new UpdateSqlStatementGenerator<D>(metadataEntity, dtoType) {
    
    51
    +        this.delegateGenerator = new UpdateSqlStatementGenerator<D>(topiaMetadataModel, metadataEntity, dtoType) {
    
    49 52
     
    
    50
    -            @Override
    
    51
    -            protected <DD extends ReferentialDto> void addMnAssociation(String nmAssociationTableName, String nmAssociationDbColumnName, String referentialDtoId, Class<DD> associationType, String associationId, ImmutableList.Builder<String> result) {
    
    52
    -                super.addMnAssociation(nmAssociationTableName, nmAssociationDbColumnName, referentialDtoId, associationType, associationId, result);
    
    53 53
     
    
    54
    -                addMissingReferentialIfNecessary(associationType, associationId, result);
    
    54
    +            <DD extends ReferentialDto, RR extends ReferentialDtoReference<DD, RR>> void addAssociation(TopiaMetadataAssociation association,
    
    55
    +                                                                                                        String sourceId,
    
    56
    +                                                                                                        RR associationValue,
    
    57
    +                                                                                                        ImmutableList.Builder<String> builder) {
    
    58
    +                super.addAssociation(association, sourceId, associationValue, builder);
    
    59
    +                addMissingReferentialIfNecessary(associationValue.getDtoType(), associationValue.getId(), builder);
    
    55 60
                 }
    
    56 61
     
    
    57 62
                 @Override
    

  • services-local/src/main/java/fr/ird/observe/services/local/service/referential/ReferentialServiceLocal.java
    ... ... @@ -28,6 +28,7 @@ import fr.ird.observe.binder.data.DataEntityReferenceBinderSupport;
    28 28
     import fr.ird.observe.binder.referential.ReferentialEntityDtoBinderSupport;
    
    29 29
     import fr.ird.observe.binder.referential.ReferentialEntityReferenceBinderSupport;
    
    30 30
     import fr.ird.observe.dto.IdDto;
    
    31
    +import fr.ird.observe.dto.IdHelper;
    
    31 32
     import fr.ird.observe.dto.data.DataDto;
    
    32 33
     import fr.ird.observe.dto.form.Form;
    
    33 34
     import fr.ird.observe.dto.form.FormDefinition;
    
    ... ... @@ -42,7 +43,6 @@ import fr.ird.observe.dto.referential.SpeciesDto;
    42 43
     import fr.ird.observe.dto.referential.SpeciesGroupDto;
    
    43 44
     import fr.ird.observe.dto.referential.seine.ObservedSystemReference;
    
    44 45
     import fr.ird.observe.dto.result.SaveResultDto;
    
    45
    -import fr.ird.observe.entities.EntityMap;
    
    46 46
     import fr.ird.observe.entities.ObserveDataEntity;
    
    47 47
     import fr.ird.observe.entities.longline.TripLongline;
    
    48 48
     import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
    
    ... ... @@ -75,6 +75,7 @@ import org.apache.logging.log4j.LogManager;
    75 75
     import org.apache.logging.log4j.Logger;
    
    76 76
     import org.nuiton.topia.persistence.TopiaDao;
    
    77 77
     import org.nuiton.topia.persistence.TopiaEntity;
    
    78
    +import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
    
    78 79
     import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
    
    79 80
     import org.nuiton.topia.persistence.script.SqlScriptConsumer;
    
    80 81
     import org.nuiton.topia.persistence.script.TopiaSqlScript;
    
    ... ... @@ -86,6 +87,7 @@ import java.util.Collections;
    86 87
     import java.util.Date;
    
    87 88
     import java.util.LinkedHashSet;
    
    88 89
     import java.util.List;
    
    90
    +import java.util.Map;
    
    89 91
     import java.util.Objects;
    
    90 92
     import java.util.Optional;
    
    91 93
     import java.util.Set;
    
    ... ... @@ -205,8 +207,8 @@ public class ReferentialServiceLocal extends ObserveServiceLocal implements Refe
    205 207
             ObserveEntityEnum entityEnum = ObserveEntityEnum.valueOf(entityType);
    
    206 208
     
    
    207 209
             TopiaMetadataModel metadataModel = serviceContext.getTopiaApplicationContext().getMetadataModel();
    
    208
    -
    
    209
    -        ReplaceSqlStatementGenerator generator = new ReplaceSqlStatementGenerator(metadataModel, entityEnum.name());
    
    210
    +        TopiaMetadataEntity metadataEntity = Objects.requireNonNull(metadataModel.getEntity(entityEnum.name()));
    
    211
    +        ReplaceSqlStatementGenerator generator = new ReplaceSqlStatementGenerator(metadataModel, metadataEntity);
    
    210 212
     
    
    211 213
             ImmutableList<String> sql = generator.generateSqls(idToReplace, replaceId);
    
    212 214
     
    
    ... ... @@ -218,16 +220,22 @@ public class ReferentialServiceLocal extends ObserveServiceLocal implements Refe
    218 220
         public <D extends ReferentialDto> ImmutableSetDtoMap<DtoReference> findAllUsages(D bean) {
    
    219 221
     
    
    220 222
             ReferentialDtoEntityContext<D, ?, ?> spi = DbModelHelper.fromReferentialDto(bean);
    
    221
    -        Class<ObserveReferentialEntity> entityType = spi.toEntityType();
    
    222
    -
    
    223 223
             ObserveReferentialEntity entity = loadEntity(spi.toDtoType(), bean.getId());
    
    224 224
     
    
    225
    -        TopiaDao<ObserveReferentialEntity> dao = getTopiaPersistenceContext().getDao(entityType);
    
    226
    -        EntityMap allUsages = new EntityMap(dao.findAllUsages(entity));
    
    225
    +        Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> compositions = serviceContext.getTopiaApplicationContext().findReverseCompositions(getTopiaPersistenceContext(), entity, e -> true);
    
    226
    +        Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> associations = serviceContext.getTopiaApplicationContext().findReverseAssociations(getTopiaPersistenceContext(), entity, IdHelper::isData);
    
    227 227
     
    
    228 228
             ImmutableSetDtoMap.Builder<DtoReference> result = ImmutableSetDtoMap.builder();
    
    229
    -        for (Class<? extends TopiaEntity> type : allUsages.keySet()) {
    
    230
    -            List<? extends TopiaEntity> entities = allUsages.get(type);
    
    229
    +
    
    230
    +        buildUsageResult(compositions, result);
    
    231
    +        buildUsageResult(associations, result);
    
    232
    +        return result.build();
    
    233
    +    }
    
    234
    +
    
    235
    +    private void buildUsageResult(Map<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> compositions, ImmutableSetDtoMap.Builder<DtoReference> result) {
    
    236
    +        for (Map.Entry<Class<? extends TopiaEntity>, List<? extends TopiaEntity>> entry : compositions.entrySet()) {
    
    237
    +            Class<? extends TopiaEntity> type = entry.getKey();
    
    238
    +            List<? extends TopiaEntity> entities = entry.getValue();
    
    231 239
                 if (ObserveReferentialEntity.class.isAssignableFrom(type)) {
    
    232 240
                     //noinspection unchecked
    
    233 241
                     addAllReferentialUsage(DbModelHelper.fromReferentialEntity((Class) type), (List) entities, result);
    
    ... ... @@ -236,7 +244,6 @@ public class ReferentialServiceLocal extends ObserveServiceLocal implements Refe
    236 244
                     adddAllDataUsage(DbModelHelper.fromDataEntity((Class) type), (List) entities, result);
    
    237 245
                 }
    
    238 246
             }
    
    239
    -        return result.build();
    
    240 247
         }
    
    241 248
     
    
    242 249
         private <D extends ReferentialDto, R extends ReferentialDtoReference<D, R>, E extends ObserveReferentialEntity> ReferentialDtoReferenceSet<R> getReferenceSet0(ReferentialReferenceEntityContext<D, R, E> spi, Date lastUpdateDate) {
    

  • services/pom.xml
    ... ... @@ -153,6 +153,10 @@
    153 153
           <groupId>junit</groupId>
    
    154 154
           <artifactId>junit</artifactId>
    
    155 155
         </dependency>
    
    156
    +    <dependency>
    
    157
    +      <groupId>io.ultreia.java4all.topia</groupId>
    
    158
    +      <artifactId>service-script</artifactId>
    
    159
    +    </dependency>
    
    156 160
     
    
    157 161
       </dependencies>
    
    158 162
     
    

  • services/src/main/java/fr/ird/observe/services/service/actions/synchro/referential/ng/ReferentialSynchronizeSqlsRequest.java
    ... ... @@ -25,6 +25,7 @@ package fr.ird.observe.services.service.actions.synchro.referential.ng;
    25 25
     import fr.ird.observe.dto.ObserveDto;
    
    26 26
     import org.apache.logging.log4j.LogManager;
    
    27 27
     import org.apache.logging.log4j.Logger;
    
    28
    +import org.nuiton.topia.service.script.TopiaSqlStatements;
    
    28 29
     
    
    29 30
     /**
    
    30 31
      * Created on 08/08/16.
    
    ... ... @@ -34,7 +35,6 @@ import org.apache.logging.log4j.Logger;
    34 35
      */
    
    35 36
     public class ReferentialSynchronizeSqlsRequest implements ObserveDto {
    
    36 37
     
    
    37
    -    /** Logger. */
    
    38 38
         private static final Logger log = LogManager.getLogger(ReferentialSynchronizeSqlsRequest.class);
    
    39 39
     
    
    40 40
         private final byte[] insertAssociationSqlCode;
    
    ... ... @@ -105,11 +105,9 @@ public class ReferentialSynchronizeSqlsRequest implements ObserveDto {
    105 105
             }
    
    106 106
     
    
    107 107
             public Builder addInsertStatement(String sql) {
    
    108
    -            if (log.isInfoEnabled()) {
    
    109
    -                log.info("Add add sql: " + sql);
    
    110
    -            }
    
    111
    -            if (sql.startsWith("$$_INSERT")) {
    
    112
    -                addAssociationTasksBuilder.append(sql.substring(3));
    
    108
    +            log.info("Add add sql: " + sql);
    
    109
    +            if (sql.startsWith(TopiaSqlStatements.ASSOCIATION_STATEMENT_PREFIX)) {
    
    110
    +                addAssociationTasksBuilder.append(TopiaSqlStatements.unboxAssociationStatement(sql));
    
    113 111
                 } else {
    
    114 112
                     addTasksBuilder.append(sql);
    
    115 113
                 }
    
    ... ... @@ -117,20 +115,15 @@ public class ReferentialSynchronizeSqlsRequest implements ObserveDto {
    117 115
             }
    
    118 116
     
    
    119 117
             public Builder addUpdateStatement(String sql) {
    
    120
    -            if (log.isInfoEnabled()) {
    
    121
    -                log.info("Add update sql: " + sql);
    
    122
    -            }
    
    118
    +            log.info("Add update sql: " + sql);
    
    123 119
                 updateTasksBuilder.append(sql);
    
    124 120
                 return this;
    
    125 121
             }
    
    126 122
     
    
    127 123
             public Builder addDeleteStatement(String sql) {
    
    128
    -
    
    129
    -            if (log.isInfoEnabled()) {
    
    130
    -                log.info("Add delete sql: " + sql);
    
    131
    -            }
    
    132
    -            if (sql.startsWith("$$_DELETE")) {
    
    133
    -                deleteAssociationTasksBuilder.append(sql.substring(3));
    
    124
    +            log.info("Add delete sql: " + sql);
    
    125
    +            if (sql.startsWith(TopiaSqlStatements.ASSOCIATION_STATEMENT_PREFIX)) {
    
    126
    +                deleteAssociationTasksBuilder.append(TopiaSqlStatements.unboxAssociationStatement(sql));
    
    134 127
                 } else {
    
    135 128
                     deleteTasksBuilder.append(sql);
    
    136 129
                 }
    
    ... ... @@ -138,10 +131,7 @@ public class ReferentialSynchronizeSqlsRequest implements ObserveDto {
    138 131
             }
    
    139 132
     
    
    140 133
             public Builder addDesactivateStatement(String sql) {
    
    141
    -
    
    142
    -            if (log.isInfoEnabled()) {
    
    143
    -                log.info("Add desactivate sql: " + sql);
    
    144
    -            }
    
    134
    +            log.info("Add desactivate sql: " + sql);
    
    145 135
                 desactivateTasksBuilder.append(sql);
    
    146 136
                 return this;
    
    147 137
             }