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

Commits:

9 changed files:

Changes:

  • persistence/src/main/java/fr/ird/observe/entities/referentiel/seine/ObjectMaterialImpl.java
    1
    +package fr.ird.observe.entities.referentiel.seine;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe :: Persistence
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2017 IRD, Code Lutin, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + * 
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + * 
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +public class ObjectMaterialImpl extends ObjectMaterialAbstract {
    
    26
    +
    
    27
    +    private static final long serialVersionUID = 7003158139439834679L;
    
    28
    +
    
    29
    +    @Override
    
    30
    +    public ObjectMaterial getSimplifiedObjectMaterial() {
    
    31
    +        if (getStandardCode() != null) {
    
    32
    +            return this;
    
    33
    +        }
    
    34
    +        return getParent().getSimplifiedObjectMaterial();
    
    35
    +    }
    
    36
    +
    
    37
    +    @Override
    
    38
    +    public ObjectMaterial getSimplifiedParentObjectMaterial() {
    
    39
    +        return getParent()==null?null:getParent().getSimplifiedObjectMaterial();
    
    40
    +    }
    
    41
    +}
    
    42
    +

  • persistence/src/main/java/fr/ird/observe/entities/referentiel/seine/ObjectMaterialTopiaDao.java
    1
    +package fr.ird.observe.entities.referentiel.seine;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe :: Persistence
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2017 IRD, Code Lutin, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + * 
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + * 
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.common.collect.ImmutableMap;
    
    26
    +
    
    27
    +public class ObjectMaterialTopiaDao extends AbstractObjectMaterialTopiaDao<ObjectMaterial> {
    
    28
    +    public ImmutableMap<String, SimplifiedObjectTypeNode> createMaterialMapping() {
    
    29
    +        return SimplifiedObjectTypeNode.create(findAll());
    
    30
    +    }
    
    31
    +}

  • persistence/src/main/java/fr/ird/observe/entities/referentiel/seine/SimplifiedObjectTypeNode.java
    1
    +package fr.ird.observe.entities.referentiel.seine;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe :: Persistence
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2017 IRD, Code Lutin, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + * 
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + * 
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.common.collect.ImmutableList;
    
    26
    +import com.google.common.collect.ImmutableMap;
    
    27
    +import java.util.LinkedHashSet;
    
    28
    +import java.util.LinkedList;
    
    29
    +import java.util.List;
    
    30
    +import java.util.Objects;
    
    31
    +import java.util.Set;
    
    32
    +import java.util.TreeMap;
    
    33
    +import java.util.stream.Collectors;
    
    34
    +
    
    35
    +/**
    
    36
    + * Created by tchemit on 02/08/17.
    
    37
    + *
    
    38
    + * @author Tony Chemit - dev@tchemit.fr
    
    39
    + */
    
    40
    +public class SimplifiedObjectTypeNode {
    
    41
    +
    
    42
    +    static ImmutableMap<String, SimplifiedObjectTypeNode> create(List<ObjectMaterial> materials) {
    
    43
    +        TreeMap<String, SimplifiedObjectTypeNode> mappingBuilder = new TreeMap<>();
    
    44
    +
    
    45
    +        Set<ObjectMaterial> materialsDone = new LinkedHashSet<>();
    
    46
    +        Set<ObjectMaterial> materialsTodo = new LinkedHashSet<>(materials);
    
    47
    +
    
    48
    +        while (!materialsTodo.isEmpty()) {
    
    49
    +
    
    50
    +            Set<ObjectMaterial> currentRoundMaterials;
    
    51
    +            if (materialsDone.isEmpty()) {
    
    52
    +
    
    53
    +                // first round
    
    54
    +                currentRoundMaterials = materialsTodo.stream().filter(f -> f.getParent() == null).collect(Collectors.toSet());
    
    55
    +
    
    56
    +            } else {
    
    57
    +                currentRoundMaterials = materialsTodo.stream().filter(f -> materialsDone.contains(f.getParent())).collect(Collectors.toSet());
    
    58
    +            }
    
    59
    +
    
    60
    +            materialsDone.addAll(currentRoundMaterials);
    
    61
    +            materialsTodo.removeAll(currentRoundMaterials);
    
    62
    +
    
    63
    +            for (ObjectMaterial currentRoundMaterial : currentRoundMaterials) {
    
    64
    +
    
    65
    +                ObjectMaterial simplifiedObjectMaterial = currentRoundMaterial.getSimplifiedObjectMaterial();
    
    66
    +
    
    67
    +                SimplifiedObjectTypeNode node;
    
    68
    +                if (Objects.equals(currentRoundMaterial, simplifiedObjectMaterial)) {
    
    69
    +
    
    70
    +                    // new node
    
    71
    +                    ObjectMaterial simplifiedParentObjectMaterial = currentRoundMaterial.getSimplifiedParentObjectMaterial();
    
    72
    +                    SimplifiedObjectTypeNode parentNode = simplifiedParentObjectMaterial == null ? null : mappingBuilder.get(simplifiedParentObjectMaterial.getTopiaId());
    
    73
    +                    node = new SimplifiedObjectTypeNode(currentRoundMaterial, parentNode);
    
    74
    +
    
    75
    +                } else {
    
    76
    +
    
    77
    +                    // simplified object material is a parent, so must have been process in previous round
    
    78
    +                    Objects.requireNonNull(simplifiedObjectMaterial);
    
    79
    +
    
    80
    +                    node = mappingBuilder.get(simplifiedObjectMaterial.getTopiaId());
    
    81
    +                    Objects.requireNonNull(node);
    
    82
    +
    
    83
    +                }
    
    84
    +
    
    85
    +                mappingBuilder.put(currentRoundMaterial.getTopiaId(), node);
    
    86
    +
    
    87
    +            }
    
    88
    +        }
    
    89
    +        return ImmutableMap.copyOf(mappingBuilder);
    
    90
    +    }
    
    91
    +
    
    92
    +    private final int level;
    
    93
    +    private final String id;
    
    94
    +    private final String standardCode;
    
    95
    +    private final SimplifiedObjectTypeNode parent;
    
    96
    +
    
    97
    +    private SimplifiedObjectTypeNode(ObjectMaterial material, SimplifiedObjectTypeNode parent) {
    
    98
    +        this.id = material.getTopiaId();
    
    99
    +        this.standardCode = material.getStandardCode();
    
    100
    +        this.parent = parent;
    
    101
    +        this.level = (parent == null ? 0 : 1 + parent.getLevel());
    
    102
    +    }
    
    103
    +
    
    104
    +    public String getId() {
    
    105
    +        return id;
    
    106
    +    }
    
    107
    +
    
    108
    +    public String getStandardCode() {
    
    109
    +        return standardCode;
    
    110
    +    }
    
    111
    +
    
    112
    +    public SimplifiedObjectTypeNode getParent() {
    
    113
    +        return parent;
    
    114
    +    }
    
    115
    +
    
    116
    +    public int getLevel() {
    
    117
    +        return level;
    
    118
    +    }
    
    119
    +
    
    120
    +    public SimplifiedObjectTypeNode getSharedAncestor(SimplifiedObjectTypeNode other) {
    
    121
    +        ImmutableList<SimplifiedObjectTypeNode> pathsToRoot = getPathsToRoot(true);
    
    122
    +        ImmutableList<SimplifiedObjectTypeNode> otherPathsToRoot = other.getPathsToRoot(true);
    
    123
    +        List<SimplifiedObjectTypeNode> result = new LinkedList<>(pathsToRoot);
    
    124
    +        result.retainAll(otherPathsToRoot);
    
    125
    +
    
    126
    +        // get the most precise on common paths
    
    127
    +        return result.get(0);
    
    128
    +    }
    
    129
    +
    
    130
    +    @Override
    
    131
    +    public boolean equals(Object o) {
    
    132
    +        if (this == o) return true;
    
    133
    +        if (o == null || getClass() != o.getClass()) return false;
    
    134
    +        SimplifiedObjectTypeNode that = (SimplifiedObjectTypeNode) o;
    
    135
    +        return Objects.equals(id, that.id);
    
    136
    +    }
    
    137
    +
    
    138
    +    @Override
    
    139
    +    public int hashCode() {
    
    140
    +        return Objects.hash(id);
    
    141
    +    }
    
    142
    +
    
    143
    +    public ImmutableList<SimplifiedObjectTypeNode> getPathsToRoot(boolean includeMe) {
    
    144
    +        List<SimplifiedObjectTypeNode> result = new LinkedList<>();
    
    145
    +        if (includeMe) {
    
    146
    +            result.add(this);
    
    147
    +        }
    
    148
    +        if (parent != null) {
    
    149
    +            parent.getPathsToRoot0(result);
    
    150
    +        }
    
    151
    +        return ImmutableList.copyOf(result);
    
    152
    +    }
    
    153
    +
    
    154
    +    private void getPathsToRoot0(List<SimplifiedObjectTypeNode> result) {
    
    155
    +        result.add(this);
    
    156
    +        if (parent != null) {
    
    157
    +            parent.getPathsToRoot0(result);
    
    158
    +        }
    
    159
    +    }
    
    160
    +}

  • persistence/src/main/models/Observe.model
    ... ... @@ -658,6 +658,8 @@ childSelectionMandatory + {*:1} boolean | notNull
    658 658
     parent {*:0..1} referentiel.seine.ObjectMaterial
    
    659 659
     objectMaterialType {*:1} referentiel.seine.ObjectMaterialType | lazy=false
    
    660 660
     validation + {*:1} String
    
    661
    +getSimplifiedObjectMaterial() referentiel.seine.ObjectMaterial
    
    662
    +getSimplifiedParentObjectMaterial() referentiel.seine.ObjectMaterial
    
    661 663
     
    
    662 664
     referentiel.seine.ObjectMaterialType > referentiel.I18nReferentialEntity | entity
    
    663 665
     
    

  • persistence/src/main/resources/db/migration/6.902/02_drop_object_type-H2.sql
    ... ... @@ -20,4 +20,4 @@
    20 20
     -- #L%
    
    21 21
     ---
    
    22 22
     DROP TABLE observe_seine.ObjectType;
    
    23
    -DELETE FROM observe_common.LastUpdateDate WHERE type = 'fr.ird.observe.entities.referentiel.seine.ObjectType';
    \ No newline at end of file
    23
    +DELETE FROM observe_common.LastUpdateDate WHERE type = 'fr.ird.observe.entities.referentiel.seine.ObjectType';

  • services-topia/src/main/java/fr/ird/observe/services/topia/service/actions/consolidate/ConsolidateDataServiceTopia.java
    ... ... @@ -22,6 +22,7 @@ package fr.ird.observe.services.topia.service.actions.consolidate;
    22 22
      * #L%
    
    23 23
      */
    
    24 24
     
    
    25
    +import com.google.common.collect.ImmutableMap;
    
    25 26
     import com.google.common.collect.ImmutableSet;
    
    26 27
     import fr.ird.observe.common.constants.seine.DcpComputedValue;
    
    27 28
     import fr.ird.observe.common.constants.seine.NonTargetCatchComputedValueSource;
    
    ... ... @@ -33,6 +34,7 @@ import fr.ird.observe.entities.referentiel.LengthWeightParameter;
    33 34
     import fr.ird.observe.entities.referentiel.Sex;
    
    34 35
     import fr.ird.observe.entities.referentiel.SizeMeasureType;
    
    35 36
     import fr.ird.observe.entities.referentiel.Species;
    
    37
    +import fr.ird.observe.entities.referentiel.seine.SimplifiedObjectTypeNode;
    
    36 38
     import fr.ird.observe.entities.seine.ActivitySeine;
    
    37 39
     import fr.ird.observe.entities.seine.FloatingObject;
    
    38 40
     import fr.ird.observe.entities.seine.FloatingObjectPart;
    
    ... ... @@ -58,6 +60,8 @@ import fr.ird.observe.services.topia.service.ObserveServiceTopia;
    58 60
     import java.util.Collection;
    
    59 61
     import java.util.Locale;
    
    60 62
     import java.util.Optional;
    
    63
    +import java.util.Set;
    
    64
    +import java.util.stream.Collectors;
    
    61 65
     import org.apache.commons.collections4.CollectionUtils;
    
    62 66
     import org.apache.commons.logging.Log;
    
    63 67
     import org.apache.commons.logging.LogFactory;
    
    ... ... @@ -118,7 +122,10 @@ public class ConsolidateDataServiceTopia extends ObserveServiceTopia implements
    118 122
     
    
    119 123
             ObserveTopiaPersistenceContext persistenceContext = getTopiaPersistenceContext();
    
    120 124
     
    
    121
    -        ImmutableSet.Builder<ConsolidateActivitySeineDataResult> actititiesResultBuilder = new ImmutableSet.Builder<>();
    
    125
    +        ImmutableMap<String, SimplifiedObjectTypeNode> materialMapping = persistenceContext.getObjectMaterialDao().createMaterialMapping();
    
    126
    +        SimplifiedObjectTypeManager simplifiedObjectTypeManager = new SimplifiedObjectTypeManager(materialMapping);
    
    127
    +
    
    128
    +        ImmutableSet.Builder<ConsolidateActivitySeineDataResult> activitiesResultBuilder = new ImmutableSet.Builder<>();
    
    122 129
     
    
    123 130
             for (Route route : tripSeine.getRoute()) {
    
    124 131
     
    
    ... ... @@ -145,6 +152,7 @@ public class ConsolidateDataServiceTopia extends ObserveServiceTopia implements
    145 152
                     activityContext.nonTargetLengthMonitor = nonTargetSampleMonitor;
    
    146 153
                     activityContext.nonTargetCatchMonitor = nonTargetCatchMonitor;
    
    147 154
                     activityContext.dcpMonitor = dcpMonitor;
    
    155
    +                activityContext.simplifiedObjectTypeManager = simplifiedObjectTypeManager;
    
    148 156
     
    
    149 157
                     consolidateActivitySeine(activityContext);
    
    150 158
     
    
    ... ... @@ -156,7 +164,7 @@ public class ConsolidateDataServiceTopia extends ObserveServiceTopia implements
    156 164
                         if (log.isInfoEnabled()) {
    
    157 165
                             log.info("Found some modifications on activity: " + consolidateActivitySeineDataResult.getActivitySeineLabel());
    
    158 166
                         }
    
    159
    -                    actititiesResultBuilder.add(consolidateActivitySeineDataResult);
    
    167
    +                    activitiesResultBuilder.add(consolidateActivitySeineDataResult);
    
    160 168
     
    
    161 169
                     }
    
    162 170
     
    
    ... ... @@ -164,7 +172,7 @@ public class ConsolidateDataServiceTopia extends ObserveServiceTopia implements
    164 172
     
    
    165 173
             }
    
    166 174
     
    
    167
    -        ImmutableSet<ConsolidateActivitySeineDataResult> consolidateActivitySeineDataResults = actititiesResultBuilder.build();
    
    175
    +        ImmutableSet<ConsolidateActivitySeineDataResult> consolidateActivitySeineDataResults = activitiesResultBuilder.build();
    
    168 176
     
    
    169 177
             ConsolidateTripSeineDataResult consolidateTripSeineDataResult;
    
    170 178
     
    
    ... ... @@ -278,19 +286,15 @@ public class ConsolidateDataServiceTopia extends ObserveServiceTopia implements
    278 286
     
    
    279 287
             computeDcpBiodegradable(dcp);
    
    280 288
             computeDcpNonEntangling(dcp);
    
    281
    -        computeDcpSimplifiedObjectType(dcp);
    
    289
    +        computeDcpSimplifiedObjectType(dcp, activityContext.simplifiedObjectTypeManager);
    
    282 290
     
    
    283 291
             activityContext.flushDcp();
    
    284 292
     
    
    285 293
         }
    
    286 294
     
    
    287
    -    private void computeDcpSimplifiedObjectType(FloatingObject dcp) {
    
    288
    -        String value = "FOB";
    
    289
    -        if (dcp.isFloatingObjectPartNotEmpty()) {
    
    290
    -            for (FloatingObjectPart floatingObjectPart : dcp.getFloatingObjectPart()) {
    
    291
    -                //TODO
    
    292
    -            }
    
    293
    -        }
    
    295
    +    private void computeDcpSimplifiedObjectType(FloatingObject dcp, SimplifiedObjectTypeManager simplifiedObjectTypeManager) {
    
    296
    +        Set<String> objectMaterialIds = dcp.getFloatingObjectPart().stream().map(p -> p.getObjectMaterial().getTopiaId()).collect(Collectors.toSet());
    
    297
    +        String value = simplifiedObjectTypeManager.getStandardCode(objectMaterialIds);
    
    294 298
             dcp.setComputedSimplifiedObjectType(value);
    
    295 299
         }
    
    296 300
     
    

  • services-topia/src/main/java/fr/ird/observe/services/topia/service/actions/consolidate/ConsolidationActivitySeineDataContext.java
    ... ... @@ -72,6 +72,8 @@ class ConsolidationActivitySeineDataContext {
    72 72
     
    
    73 73
         ConsolidateActivitySeineDataResultBuilder resultBuilder;
    
    74 74
     
    
    75
    +    SimplifiedObjectTypeManager simplifiedObjectTypeManager;
    
    76
    +
    
    75 77
         public Ocean getOcean() {
    
    76 78
             return tripSeine.getOcean();
    
    77 79
         }
    

  • services-topia/src/main/java/fr/ird/observe/services/topia/service/actions/consolidate/SimplifiedObjectTypeManager.java
    1
    +package fr.ird.observe.services.topia.service.actions.consolidate;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe :: Services ToPIA Implementation
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2017 IRD, Code Lutin, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + * 
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + * 
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.common.collect.ImmutableMap;
    
    26
    +import fr.ird.observe.entities.referentiel.seine.SimplifiedObjectTypeNode;
    
    27
    +import java.util.Comparator;
    
    28
    +import java.util.Iterator;
    
    29
    +import java.util.LinkedList;
    
    30
    +import java.util.List;
    
    31
    +import java.util.Objects;
    
    32
    +import java.util.Set;
    
    33
    +
    
    34
    +/**
    
    35
    + * Created by tchemit on 02/08/17.
    
    36
    + *
    
    37
    + * @author Tony Chemit - dev@tchemit.fr
    
    38
    + * @since 7.0
    
    39
    + */
    
    40
    +class SimplifiedObjectTypeManager {
    
    41
    +
    
    42
    +
    
    43
    +    // For each object material, get his simplified object type node
    
    44
    +    private final ImmutableMap<String, SimplifiedObjectTypeNode> mapping;
    
    45
    +
    
    46
    +    SimplifiedObjectTypeManager(ImmutableMap<String, SimplifiedObjectTypeNode> mapping) {
    
    47
    +        this.mapping = mapping;
    
    48
    +    }
    
    49
    +
    
    50
    +    SimplifiedObjectTypeNode getNodeForStandardCode(String standardCode) {
    
    51
    +        return mapping.values().stream().filter(n -> n.getStandardCode().equals(standardCode)).findFirst().orElseThrow(IllegalArgumentException::new);
    
    52
    +    }
    
    53
    +
    
    54
    +    String getStandardCode(Set<String> objectMaterialIds) {
    
    55
    +        List<SimplifiedObjectTypeNode> availableNodes = new LinkedList<>();
    
    56
    +        for (String objectMaterialId : objectMaterialIds) {
    
    57
    +            availableNodes.add(mapping.get(objectMaterialId));
    
    58
    +        }
    
    59
    +        // get highest level first
    
    60
    +        availableNodes.sort(Comparator.comparing(SimplifiedObjectTypeNode::getLevel).reversed());
    
    61
    +
    
    62
    +        // remove from available nodes, all nodes in the path of other ones
    
    63
    +        List<SimplifiedObjectTypeNode> mainNodes = new LinkedList<>(availableNodes);
    
    64
    +
    
    65
    +        for (SimplifiedObjectTypeNode availableNode : availableNodes) {
    
    66
    +            if (mainNodes.contains(availableNode)) {
    
    67
    +                mainNodes.removeAll(availableNode.getPathsToRoot(false));
    
    68
    +            }
    
    69
    +        }
    
    70
    +
    
    71
    +        // now each node is on a different path, get the shared common ancestor
    
    72
    +        Iterator<SimplifiedObjectTypeNode> iterator = mainNodes.iterator();
    
    73
    +        SimplifiedObjectTypeNode result = iterator.next();
    
    74
    +        while (iterator.hasNext()) {
    
    75
    +            result = result.getSharedAncestor(iterator.next());
    
    76
    +        }
    
    77
    +        Objects.requireNonNull(result);
    
    78
    +        return result.getStandardCode();
    
    79
    +    }
    
    80
    +
    
    81
    +
    
    82
    +}

  • services-topia/src/test/java/fr/ird/observe/services/topia/service/actions/consolidate/SimplifiedObjectTypeManagerTest.java
    1
    +package fr.ird.observe.services.topia.service.actions.consolidate;
    
    2
    +
    
    3
    +/*-
    
    4
    + * #%L
    
    5
    + * ObServe :: Services ToPIA Implementation
    
    6
    + * %%
    
    7
    + * Copyright (C) 2008 - 2017 IRD, Code Lutin, Ultreia.io
    
    8
    + * %%
    
    9
    + * This program is free software: you can redistribute it and/or modify
    
    10
    + * it under the terms of the GNU General Public License as
    
    11
    + * published by the Free Software Foundation, either version 3 of the
    
    12
    + * License, or (at your option) any later version.
    
    13
    + * 
    
    14
    + * This program is distributed in the hope that it will be useful,
    
    15
    + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    16
    + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    
    17
    + * GNU General Public License for more details.
    
    18
    + * 
    
    19
    + * You should have received a copy of the GNU General Public
    
    20
    + * License along with this program.  If not, see
    
    21
    + * <http://www.gnu.org/licenses/gpl-3.0.html>.
    
    22
    + * #L%
    
    23
    + */
    
    24
    +
    
    25
    +import com.google.common.collect.ImmutableMap;
    
    26
    +import com.google.common.collect.ImmutableSet;
    
    27
    +import fr.ird.observe.entities.referentiel.seine.SimplifiedObjectTypeNode;
    
    28
    +import fr.ird.observe.persistence.ObserveTopiaPersistenceContext;
    
    29
    +import fr.ird.observe.services.topia.service.AbstractServiceTopiaTest;
    
    30
    +import fr.ird.observe.test.DatabaseName;
    
    31
    +import fr.ird.observe.test.spi.DatabaseNameConfiguration;
    
    32
    +import java.util.stream.Collectors;
    
    33
    +import org.junit.Assert;
    
    34
    +import org.junit.Before;
    
    35
    +import org.junit.Test;
    
    36
    +
    
    37
    +/**
    
    38
    + * Created by tchemit on 02/08/17.
    
    39
    + *
    
    40
    + * @author Tony Chemit - dev@tchemit.fr
    
    41
    + */
    
    42
    +@DatabaseNameConfiguration(DatabaseName.referentiel)
    
    43
    +public class SimplifiedObjectTypeManagerTest extends AbstractServiceTopiaTest {
    
    44
    +
    
    45
    +    private SimplifiedObjectTypeManager simplifiedObjectTypeManager;
    
    46
    +
    
    47
    +    @Before
    
    48
    +    public void setUp() throws Exception {
    
    49
    +        try (ObserveTopiaPersistenceContext persistenceContext = topiaTestMethodResource.newPersistenceContext()) {
    
    50
    +
    
    51
    +            ImmutableMap<String, SimplifiedObjectTypeNode> materialMapping = persistenceContext.getObjectMaterialDao().createMaterialMapping();
    
    52
    +            simplifiedObjectTypeManager = new SimplifiedObjectTypeManager(materialMapping);
    
    53
    +        }
    
    54
    +
    
    55
    +
    
    56
    +    }
    
    57
    +
    
    58
    +    @Test
    
    59
    +    public void getStandardCode() {
    
    60
    +
    
    61
    +        SimplifiedObjectTypeNode fob = simplifiedObjectTypeManager.getNodeForStandardCode("FOB");
    
    62
    +        SimplifiedObjectTypeNode fad = simplifiedObjectTypeManager.getNodeForStandardCode("FAD");
    
    63
    +        SimplifiedObjectTypeNode dfad = simplifiedObjectTypeManager.getNodeForStandardCode("DFAD");
    
    64
    +        SimplifiedObjectTypeNode log = simplifiedObjectTypeManager.getNodeForStandardCode("LOG");
    
    65
    +        SimplifiedObjectTypeNode alog = simplifiedObjectTypeManager.getNodeForStandardCode("ALOG");
    
    66
    +        SimplifiedObjectTypeNode nlog = simplifiedObjectTypeManager.getNodeForStandardCode("NLOG");
    
    67
    +        SimplifiedObjectTypeNode vnlog = simplifiedObjectTypeManager.getNodeForStandardCode("VNLOG");
    
    68
    +        SimplifiedObjectTypeNode falog = simplifiedObjectTypeManager.getNodeForStandardCode("FALOG");
    
    69
    +        SimplifiedObjectTypeNode halog = simplifiedObjectTypeManager.getNodeForStandardCode("HALOG");
    
    70
    +
    
    71
    +        assertStandardCode(fob, fob);
    
    72
    +        assertStandardCode(fad, fob, fad);
    
    73
    +        assertStandardCode(dfad, fad, dfad, dfad);
    
    74
    +        assertStandardCode(falog, log, alog, falog);
    
    75
    +        assertStandardCode(fob, dfad, log, nlog);
    
    76
    +        assertStandardCode(falog, alog, falog, falog);
    
    77
    +        assertStandardCode(alog, alog, falog, halog);
    
    78
    +        assertStandardCode(log, falog, vnlog);
    
    79
    +
    
    80
    +    }
    
    81
    +
    
    82
    +    private void assertStandardCode(SimplifiedObjectTypeNode expectedStandardCode, SimplifiedObjectTypeNode... objectMaterialIds) {
    
    83
    +        String actualStandardCode = simplifiedObjectTypeManager.getStandardCode(ImmutableSet.copyOf(objectMaterialIds).stream().map(SimplifiedObjectTypeNode::getId).collect(Collectors.toSet()));
    
    84
    +        Assert.assertEquals(expectedStandardCode.getStandardCode(), actualStandardCode);
    
    85
    +    }
    
    86
    +
    
    87
    +}