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

Commits:

2 changed files:

Changes:

  • services-topia/src/main/java/fr/ird/observe/services/topia/service/actions/synchro/referential/legacy/UnidirectionalReferentialSynchronizeLocalServiceTopia.java
    ... ... @@ -23,12 +23,15 @@ package fr.ird.observe.services.topia.service.actions.synchro.referential.legacy
    23 23
      */
    
    24 24
     
    
    25 25
     import com.google.common.collect.ImmutableList;
    
    26
    +import com.google.common.collect.ImmutableSet;
    
    26 27
     import fr.ird.observe.entities.referentiel.ObserveReferentialEntity;
    
    27 28
     import fr.ird.observe.persistence.ObserveEntityEnum;
    
    28 29
     import fr.ird.observe.services.binder.BinderEngine;
    
    29 30
     import fr.ird.observe.services.binder.referential.ReferentialBinderSupport;
    
    30 31
     import fr.ird.observe.services.dto.reference.ReferentialReference;
    
    31 32
     import fr.ird.observe.services.dto.referential.*;
    
    33
    +import fr.ird.observe.services.dto.referential.seine.ObjectMaterialDto;
    
    34
    +import fr.ird.observe.services.dto.referential.seine.ObjectMaterialHelper;
    
    32 35
     import fr.ird.observe.services.service.LastUpdateDateService;
    
    33 36
     import fr.ird.observe.services.service.actions.synchro.referential.legacy.UnidirectionalReferentialSynchronizeLocalService;
    
    34 37
     import fr.ird.observe.services.service.actions.synchro.referential.legacy.UnidirectionalReferentialSynchronizeRequest;
    
    ... ... @@ -116,7 +119,14 @@ public class UnidirectionalReferentialSynchronizeLocalServiceTopia extends Obser
    116 119
             if (request.withReferentialToAdd()) {
    
    117 120
     
    
    118 121
                 InsertSqlStatementGenerator<R> sqlStatementGenerator = new InsertSqlStatementGenerator<>(metadataEntity, dtoType);
    
    119
    -            for (R referentialDto : request.getReferentialToAdd()) {
    
    122
    +            ImmutableSet<R> referentialToAdd = request.getReferentialToAdd();
    
    123
    +            if (ObjectMaterialDto.class.equals(dtoType)) {
    
    124
    +
    
    125
    +                // must reorder to be sure to insert parent before sons
    
    126
    +                referentialToAdd = (ImmutableSet)ObjectMaterialHelper.reorder((ImmutableSet)referentialToAdd);
    
    127
    +
    
    128
    +            }
    
    129
    +            for (R referentialDto : referentialToAdd) {
    
    120 130
                     ImmutableList<String> sql = sqlStatementGenerator.generateSql(referentialDto);
    
    121 131
                     result.addAll(sql);
    
    122 132
                 }
    

  • services/src/main/java/fr/ird/observe/services/dto/referential/seine/ObjectMaterialHelper.java
    1
    +package fr.ird.observe.services.dto.referential.seine;
    
    2
    +
    
    3
    +import com.google.common.collect.ArrayListMultimap;
    
    4
    +import com.google.common.collect.ImmutableSet;
    
    5
    +import com.google.common.collect.Multimap;
    
    6
    +import java.util.Collection;
    
    7
    +import java.util.LinkedHashSet;
    
    8
    +import java.util.Set;
    
    9
    +
    
    10
    +public class ObjectMaterialHelper extends GeneratedObjectMaterialHelper {
    
    11
    +
    
    12
    +    public static ImmutableSet<ObjectMaterialDto> reorder(ImmutableSet<ObjectMaterialDto> incoming) {
    
    13
    +
    
    14
    +        Set<ObjectMaterialDto> roots = new LinkedHashSet<>();
    
    15
    +        Multimap<String, ObjectMaterialDto> map = ArrayListMultimap.create();
    
    16
    +        for (ObjectMaterialDto objectMaterialDto : incoming) {
    
    17
    +            if (objectMaterialDto.getParent() == null) {
    
    18
    +                roots.add(objectMaterialDto);
    
    19
    +            } else {
    
    20
    +                map.put(objectMaterialDto.getParent().getId(), objectMaterialDto);
    
    21
    +            }
    
    22
    +        }
    
    23
    +        ImmutableSet.Builder<ObjectMaterialDto> result = ImmutableSet.builder();
    
    24
    +        for (ObjectMaterialDto root : roots) {
    
    25
    +            fill(root, result, map);
    
    26
    +        }
    
    27
    +        return result.build();
    
    28
    +    }
    
    29
    +
    
    30
    +    private static void fill(ObjectMaterialDto root, ImmutableSet.Builder<ObjectMaterialDto> result, Multimap<String, ObjectMaterialDto> map) {
    
    31
    +        result.add(root);
    
    32
    +        Collection<ObjectMaterialDto> objectMaterialDtos = map.get(root.getId());
    
    33
    +        for (ObjectMaterialDto objectMaterialDto : objectMaterialDtos) {
    
    34
    +            fill(objectMaterialDto, result, map);
    
    35
    +        }
    
    36
    +    }
    
    37
    +}