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

Commits:

1 changed file:

Changes:

  • persistence/src/main/java/fr/ird/observe/persistence/migration/DataSourceMigrationForVersion_6_0.java
    ... ... @@ -25,6 +25,7 @@ package fr.ird.observe.persistence.migration;
    25 25
     import com.google.common.base.Joiner;
    
    26 26
     import com.google.common.collect.ImmutableMap;
    
    27 27
     import org.apache.commons.lang3.StringUtils;
    
    28
    +import org.apache.commons.lang3.tuple.Pair;
    
    28 29
     import org.apache.commons.logging.Log;
    
    29 30
     import org.apache.commons.logging.LogFactory;
    
    30 31
     import org.nuiton.topia.persistence.support.TopiaSqlQuery;
    
    ... ... @@ -35,10 +36,9 @@ import java.sql.PreparedStatement;
    35 36
     import java.sql.ResultSet;
    
    36 37
     import java.sql.SQLException;
    
    37 38
     import java.util.HashSet;
    
    38
    -import java.util.Iterator;
    
    39
    -import java.util.LinkedHashSet;
    
    40 39
     import java.util.List;
    
    41 40
     import java.util.Set;
    
    41
    +import java.util.stream.Collectors;
    
    42 42
     
    
    43 43
     /**
    
    44 44
      * Created on 27/10/16.
    
    ... ... @@ -90,32 +90,79 @@ public class DataSourceMigrationForVersion_6_0 extends AbstractObserveMigrationC
    90 90
     
    
    91 91
         private void evol8391(TopiaSqlSupport topiaSqlSupport, List<String> queries) {
    
    92 92
     
    
    93
    -        ImmutableMap<String, String> missingCodesMapping = ImmutableMap.<String, String>builder()
    
    94
    -                .put("LT ?", "TL")
    
    95
    -                .put("LF", "FL")
    
    96
    -                .put("L1", "PDL")
    
    97
    -                .put("LC", "DL")
    
    98
    -                .put("LT", "TL")
    
    99
    -                .put("", "TL")
    
    93
    +        ImmutableMap<String, String> sqlMappingByCode =
    
    94
    +                ImmutableMap.<String, String>builder()
    
    95
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336', 2, '2015-06-05', '2016-10-18 11:51:54.099140','FL', 1, null, 'Fork Length', 'Fork Length', 'Fork Length', null, null, null, null, null, false);")
    
    96
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465999#0.707568018231541", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465999#0.707568018231541', 3, '2015-06-05', '2016-10-18 11:51:54.099140', 'CFL', 1, null, 'Curved Fork Length', 'Curved Fork Length', 'Curved Fork Length', null, null, null, null, null, false);")
    
    97
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466255#0.444246932631359", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466255#0.444246932631359', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PAL', 1, null, 'Pectoral-Anal Length', 'Pectoral-Anal Length', 'Pectoral-Anal Length', null, null, null, null, null, false);")
    
    98
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'TL', 1, null, 'Total Length', 'Total Length', 'Total Length', null, null, null, null, null, false);")
    
    99
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PD1', 1, null, 'Predorsal Length', 'Predorsal Length', 'Predorsal Length', null, null, null, null, null, false);")
    
    100
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467059#0.563144549960271", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467059#0.563144549960271', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'SL', 1, null, 'Standard Length', 'Standard Length', 'Standard Length', null, null, null, null, null, false);")
    
    101
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467326#0.537910396233201", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467326#0.537910396233201', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'LJFL', 1, null, 'Lower Jaw-Fork Length', 'Lower Jaw-Fork Length', 'Lower Jaw-Fork Length', null, null, null, null, null, false);")
    
    102
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467638#0.409153908025473", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467638#0.409153908025473', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CLJFL', 1, null, 'Curved Lower Jaw-Fork Length', 'Curved Lower Jaw-Fork Length', 'Curved Lower Jaw-Fork Length', null, null, null, null, null, false);")
    
    103
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467945#0.249704661779106", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467945#0.249704661779106', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'IDL', 1, null, 'Interdorsal Length', 'Interdorsal Length', 'Interdorsal Length', null, null, null, null, null, false);")
    
    104
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468283#0.152317869942635", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468283#0.152317869942635', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'EFL', 1, null, 'Eye-Fork Length', 'Eye-Fork Length', 'Eye-Fork Length', null, null, null, null, null, false);")
    
    105
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468646#0.710986209334806", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468646#0.710986209334806', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'DW', 1, null, 'Disk Width', 'Disk Width', 'Disk Width', null, null, null, null, null, false);")
    
    106
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928', 2, '2015-06-05', '2016-10-18 11:51:54.099140',  'DL', 1, null, 'Disk Length', 'Disk Length', 'Disk Length', null, null, null, null, null, false);")
    
    107
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469572#0.969587777974084", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469572#0.969587777974084', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'TW', 1, null, 'Total Weight', 'Total Weight', 'Total Weight', null, null, null, null, null, false);")
    
    108
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470528#0.813851526239887", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470528#0.813851526239887', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CTL', 1, null, 'Curved Total Length', 'Curved Total Length', 'Curved Total Length', null, null, null, null, null, false);")
    
    109
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470887#0.950930784922093", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470887#0.950930784922093', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'DML', 1, null, 'Dorsal Mantle Length', 'Dorsal Mantle Length', 'Dorsal Mantle Length', null, null, null, null, null, false);")
    
    110
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499471278#0.425988764036447", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499471278#0.425988764036447', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CDML', 1, null, 'Curved Dorsal Mantle Length', 'Curved Dorsal Mantle Length', 'Curved Dorsal Mantle Length', null, null, null, null, null, false);")
    
    111
    +                        .put("fr.ird.observe.entities.referentiel.SizeMeasureType#1433499472220#0.885759855154902", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499472220#0.885759855154902', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PFL', 1, null, 'Pectoral-Fork Length', 'Pectoral-Fork Length', 'Pectoral-Fork Length', null, null, null, null, null, false);")
    
    112
    +                        .build();
    
    113
    +
    
    114
    +        ImmutableMap<String, String> knownSizeMeasureTypeMapping = ImmutableMap.<String, String>builder()
    
    115
    +                .put("LT ?", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293")
    
    116
    +                .put("LF", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336")
    
    117
    +                .put("L1", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607")
    
    118
    +                .put("LC", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928")
    
    119
    +                .put("LT", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293")
    
    120
    +                .put("", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293")
    
    121
    +                .put("FL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336")
    
    122
    +                .put("CFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465999#0.707568018231541")
    
    123
    +                .put("PAL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466255#0.444246932631359")
    
    124
    +                .put("TL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293")
    
    125
    +                .put("PD1", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607")
    
    126
    +                .put("PDL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607")
    
    127
    +                .put("SL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467059#0.563144549960271")
    
    128
    +                .put("LJFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467326#0.537910396233201")
    
    129
    +                .put("CLJFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467638#0.409153908025473")
    
    130
    +                .put("IDL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467945#0.249704661779106")
    
    131
    +                .put("EFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468283#0.152317869942635")
    
    132
    +                .put("DW", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468646#0.710986209334806")
    
    133
    +                .put("DL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928")
    
    134
    +                .put("TW", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469572#0.969587777974084")
    
    135
    +                .put("CTL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470528#0.813851526239887")
    
    136
    +                .put("DML", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470887#0.950930784922093")
    
    137
    +                .put("CDML", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499471278#0.425988764036447")
    
    138
    +                .put("PFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499472220#0.885759855154902")
    
    100 139
                     .build();
    
    101
    -        List<String> lengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() {
    
    140
    +
    
    141
    +        ImmutableMap.Builder<String, String> existingSizeMeasureMappingBuilder = ImmutableMap.builder();
    
    142
    +
    
    143
    +        topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<Pair<String, String>>() {
    
    102 144
     
    
    103 145
                 @Override
    
    104 146
                 public PreparedStatement prepareQuery(Connection connection) throws SQLException {
    
    105
    -                return connection.prepareStatement("SELECT DISTINCT(lengthmeasuretype) FROM observe_common.species WHERE lengthmeasuretype IS NOT NULL");
    
    147
    +                // Attention à ce moment là ce référentiel est encore dans le schema longline
    
    148
    +                return connection.prepareStatement("SELECT code, topiaid FROM observe_longline.sizemeasuretype ");
    
    106 149
                 }
    
    107 150
     
    
    108 151
                 @Override
    
    109
    -            public String prepareResult(ResultSet set) throws SQLException {
    
    110
    -                return set.getString(1);
    
    152
    +            public Pair<String, String> prepareResult(ResultSet set) throws SQLException {
    
    153
    +                String code = set.getString(1);
    
    154
    +                String topiaId = set.getString(2);
    
    155
    +                return Pair.of(code, StringUtils.replace(topiaId, ".longline", ""));
    
    111 156
                 }
    
    112
    -        });
    
    113
    -        List<String> existingLengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() {
    
    157
    +        }).forEach(p -> existingSizeMeasureMappingBuilder.put(p.getKey(), p.getValue()));
    
    158
    +
    
    159
    +        ImmutableMap<String, String> existingSizeMeasureMapping = existingSizeMeasureMappingBuilder.build();
    
    160
    +
    
    161
    +        List<String> lengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() {
    
    114 162
     
    
    115 163
                 @Override
    
    116 164
                 public PreparedStatement prepareQuery(Connection connection) throws SQLException {
    
    117
    -                // Attention à ce moment là ce référentiel est encore dans le schema longline
    
    118
    -                return connection.prepareStatement("SELECT DISTINCT(code) FROM observe_longline.sizemeasuretype ");
    
    165
    +                return connection.prepareStatement("SELECT DISTINCT(lengthmeasuretype) FROM observe_common.species WHERE lengthmeasuretype IS NOT NULL");
    
    119 166
                 }
    
    120 167
     
    
    121 168
                 @Override
    
    ... ... @@ -124,115 +171,35 @@ public class DataSourceMigrationForVersion_6_0 extends AbstractObserveMigrationC
    124 171
                 }
    
    125 172
             });
    
    126 173
     
    
    127
    -        Set<String> missingLengthMeasureTypeCodes = new HashSet<>();
    
    128
    -        for (String lengthMeasureType : lengthMeasureTypes) {
    
    129
    -            lengthMeasureType = lengthMeasureType.trim();
    
    130
    -            String realCode = missingCodesMapping.get(lengthMeasureType);
    
    131
    -            if (realCode == null) {
    
    132
    -                realCode = lengthMeasureType;
    
    133
    -            }
    
    134
    -            missingLengthMeasureTypeCodes.add(realCode);
    
    135
    -        }
    
    136
    -        missingLengthMeasureTypeCodes.removeAll(existingLengthMeasureTypes);
    
    137
    -
    
    138
    -        ImmutableMap.Builder<String, String> extraTopiaIdsByCodeBuilder = ImmutableMap.builder();
    
    139
    -
    
    140
    -        if (!missingLengthMeasureTypeCodes.isEmpty()) {
    
    141
    -
    
    142
    -            // il manque des référentiels on va les ajouter
    
    143
    -            ImmutableMap<String, String> sqlMappingByCode =
    
    144
    -                    ImmutableMap.<String, String>builder()
    
    145
    -                            .put("FL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336', 2, '2015-06-05', '2016-10-18 11:51:54.099140','FL', 1, null, 'Fork Length', 'Fork Length', 'Fork Length', null, null, null, null, null, false);")
    
    146
    -                            .put("CFL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465999#0.707568018231541', 3, '2015-06-05', '2016-10-18 11:51:54.099140', 'CFL', 1, null, 'Curved Fork Length', 'Curved Fork Length', 'Curved Fork Length', null, null, null, null, null, false);")
    
    147
    -                            .put("PAL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466255#0.444246932631359', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PAL', 1, null, 'Pectoral-Anal Length', 'Pectoral-Anal Length', 'Pectoral-Anal Length', null, null, null, null, null, false);")
    
    148
    -                            .put("TL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'TL', 1, null, 'Total Length', 'Total Length', 'Total Length', null, null, null, null, null, false);")
    
    149
    -                            .put("PDL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PDL', 1, null, 'Predorsal Length', 'Predorsal Length', 'Predorsal Length', null, null, null, null, null, false);")
    
    150
    -                            .put("SL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467059#0.563144549960271', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'SL', 1, null, 'Standard Length', 'Standard Length', 'Standard Length', null, null, null, null, null, false);")
    
    151
    -                            .put("LJFL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467326#0.537910396233201', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'LJFL', 1, null, 'Lower Jaw-Fork Length', 'Lower Jaw-Fork Length', 'Lower Jaw-Fork Length', null, null, null, null, null, false);")
    
    152
    -                            .put("CLJFL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467638#0.409153908025473', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CLJFL', 1, null, 'Curved Lower Jaw-Fork Length', 'Curved Lower Jaw-Fork Length', 'Curved Lower Jaw-Fork Length', null, null, null, null, null, false);")
    
    153
    -                            .put("IDL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467945#0.249704661779106', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'IDL', 1, null, 'Interdorsal Length', 'Interdorsal Length', 'Interdorsal Length', null, null, null, null, null, false);")
    
    154
    -                            .put("EFL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468283#0.152317869942635', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'EFL', 1, null, 'Eye-Fork Length', 'Eye-Fork Length', 'Eye-Fork Length', null, null, null, null, null, false);")
    
    155
    -                            .put("DW", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468646#0.710986209334806', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'DW', 1, null, 'Disk Width', 'Disk Width', 'Disk Width', null, null, null, null, null, false);")
    
    156
    -                            .put("DL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928', 2, '2015-06-05', '2016-10-18 11:51:54.099140',  'DL', 1, null, 'Disk Length', 'Disk Length', 'Disk Length', null, null, null, null, null, false);")
    
    157
    -                            .put("TW", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469572#0.969587777974084', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'TW', 1, null, 'Total Weight', 'Total Weight', 'Total Weight', null, null, null, null, null, false);")
    
    158
    -                            .put("CTL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470528#0.813851526239887', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CTL', 1, null, 'Curved Total Length', 'Curved Total Length', 'Curved Total Length', null, null, null, null, null, false);")
    
    159
    -                            .put("DML", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470887#0.950930784922093', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'DML', 1, null, 'Dorsal Mantle Length', 'Dorsal Mantle Length', 'Dorsal Mantle Length', null, null, null, null, null, false);")
    
    160
    -                            .put("CDML", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499471278#0.425988764036447', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'CDML', 1, null, 'Curved Dorsal Mantle Length', 'Curved Dorsal Mantle Length', 'Curved Dorsal Mantle Length', null, null, null, null, null, false);")
    
    161
    -                            .put("PFL", "INSERT INTO observe_common.sizemeasuretype (topiaid, topiaversion, topiacreatedate, lastupdatedate, code, status, uri, label1, label2, label3, label4, label5, label6, label7, label8, needcomment) VALUES ('fr.ird.observe.entities.referentiel.SizeMeasureType#1433499472220#0.885759855154902', 2, '2015-06-05', '2016-10-18 11:51:54.099140', 'PFL', 1, null, 'Pectoral-Fork Length', 'Pectoral-Fork Length', 'Pectoral-Fork Length', null, null, null, null, null, false);")
    
    162
    -                            .build();
    
    163
    -
    
    164
    -            Iterator<String> iterator = missingLengthMeasureTypeCodes.iterator();
    
    165
    -            while (iterator.hasNext()) {
    
    166
    -                String code = iterator.next();
    
    167
    -                String sql = sqlMappingByCode.get(code);
    
    168
    -                if (sql != null) {
    
    169
    -                    queries.add(sql);
    
    170
    -                    iterator.remove();
    
    171
    -                    int i = sql.indexOf("'");
    
    172
    -                    String topiaId = sql.substring(i + 1);
    
    173
    -                    int j = topiaId.indexOf("'");
    
    174
    -                    topiaId = topiaId.substring(0, j);
    
    175
    -                    if (log.isInfoEnabled()) {
    
    176
    -                        log.info("Add " + code + "::" + topiaId);
    
    177
    -                    }
    
    178
    -                    extraTopiaIdsByCodeBuilder.put(code, topiaId);
    
    179
    -                }
    
    180
    -            }
    
    174
    +        Set<String> unkownCodes = new HashSet<>(lengthMeasureTypes);
    
    175
    +        unkownCodes.removeAll(knownSizeMeasureTypeMapping.keySet());
    
    176
    +        unkownCodes.removeAll(existingSizeMeasureMapping.keySet());
    
    181 177
     
    
    182
    -            if (!missingLengthMeasureTypeCodes.isEmpty()) {
    
    178
    +        if (!unkownCodes.isEmpty()) {
    
    183 179
     
    
    184
    -                // migration impossible, il manque encore des référentiels
    
    185
    -                // ce cas ne devrait jamais arrivé
    
    186
    -                throw new IllegalStateException("Les types de mesure de taille pour les codes suivants :\n\t" + Joiner.on("\n\t").join(missingLengthMeasureTypeCodes) + " sont requis pour la migration mais n'ont pas été trouvés en base.");
    
    187
    -            }
    
    180
    +            // migration impossible, il manque encore des référentiels
    
    181
    +            // ce cas ne devrait jamais arrivé
    
    182
    +            throw new IllegalStateException("Les types de mesure de taille pour les codes suivants :\n\t" + Joiner.on("\n\t").join(unkownCodes) + " sont requis pour la migration mais n'ont pas été trouvés en base.");
    
    188 183
     
    
    189 184
             }
    
    190 185
     
    
    191
    -        ImmutableMap<String, String> extraTopiaIdsByCode = extraTopiaIdsByCodeBuilder.build();
    
    186
    +        Set<String> missingSizeMeasureTypeIds = knownSizeMeasureTypeMapping.values().stream().filter(id -> !existingSizeMeasureMapping.values().contains(id)).collect(Collectors.toSet());
    
    192 187
     
    
    193
    -        Set<String> invalidCodes = new LinkedHashSet<>();
    
    194
    -        for (String lengthMeasureType : lengthMeasureTypes) {
    
    195
    -
    
    196
    -            lengthMeasureType = lengthMeasureType.trim();
    
    197
    -            if (lengthMeasureType.isEmpty()) {
    
    198
    -                continue;
    
    199
    -            }
    
    200
    -            if (missingCodesMapping.containsKey(lengthMeasureType)) {
    
    201
    -                lengthMeasureType = missingCodesMapping.get(lengthMeasureType);
    
    202
    -            }
    
    188
    +        for (String missingSizeMeasureTypeId : missingSizeMeasureTypeIds) {
    
    189
    +            queries.add(sqlMappingByCode.get(missingSizeMeasureTypeId));
    
    190
    +        }
    
    203 191
     
    
    204
    -            String finalLengthMeasureType = lengthMeasureType;
    
    205
    -            String sizeMeasureTypeId = extraTopiaIdsByCode.get(lengthMeasureType);
    
    206
    -            if (sizeMeasureTypeId == null) {
    
    207
    -                sizeMeasureTypeId = topiaSqlSupport.findSingleResult(new TopiaSqlQuery<String>() {
    
    208
    -
    
    209
    -                    @Override
    
    210
    -                    public PreparedStatement prepareQuery(Connection connection) throws SQLException {
    
    211
    -                        return connection.prepareStatement("SELECT topiaId FROM observe_longline.sizeMeasureType WHERE code = '" + finalLengthMeasureType + "'");
    
    212
    -                    }
    
    213
    -
    
    214
    -                    @Override
    
    215
    -                    public String prepareResult(ResultSet set) throws SQLException {
    
    216
    -                        return set.getString(1);
    
    217
    -                    }
    
    218
    -                });
    
    219
    -            }
    
    192
    +        ImmutableMap.Builder<String, String> allSizeMeasureTypeMappingBuilder = ImmutableMap.<String, String>builder().putAll(existingSizeMeasureMapping);
    
    193
    +        knownSizeMeasureTypeMapping.entrySet().stream().filter(e -> !existingSizeMeasureMapping.containsKey(e.getKey())).forEach(e -> allSizeMeasureTypeMappingBuilder.put(e.getKey(), e.getValue()));
    
    194
    +        ImmutableMap<String, String> allSizeMeasureTypeMapping = allSizeMeasureTypeMappingBuilder.build();
    
    220 195
     
    
    221
    -            if (sizeMeasureTypeId == null) {
    
    196
    +        for (String lengthMeasureType : lengthMeasureTypes) {
    
    222 197
     
    
    223
    -                if (log.isWarnEnabled()) {
    
    224
    -                    log.warn("Impossible de trouver la type de mesure de taille pour le code: " + lengthMeasureType);
    
    225
    -                }
    
    226
    -                invalidCodes.add(lengthMeasureType);
    
    227
    -            } else {
    
    228
    -                sizeMeasureTypeId = StringUtils.replace(sizeMeasureTypeId, ".longline", "");
    
    229
    -                queries.add(String.format("UPDATE observe_common.species SET sizeMeasureType = '%s' WHERE lengthMeasureType = '%s'", sizeMeasureTypeId, lengthMeasureType));
    
    230
    -            }
    
    231
    -        }
    
    198
    +            lengthMeasureType = lengthMeasureType.trim();
    
    232 199
     
    
    233
    -        if (!invalidCodes.isEmpty()) {
    
    200
    +            String sizeMeasureTypeId = allSizeMeasureTypeMapping.get(lengthMeasureType);
    
    201
    +            queries.add(String.format("UPDATE observe_common.species SET sizeMeasureType = '%s' WHERE lengthMeasureType = '%s'", sizeMeasureTypeId, lengthMeasureType));
    
    234 202
     
    
    235
    -            throw new IllegalStateException("Impossible de trouver la type de mesure de taille pour les codes suivants :\n\t" + Joiner.on("\n\t").join(invalidCodes));
    
    236 203
             }
    
    237 204
     
    
    238 205
         }