Tony CHEMIT pushed to branch develop at ultreiaio / ird-observe Commits: cd4789de by Tony CHEMIT at 2017-03-29T16:08:46+02:00 Correction de la migration de base (See #752) - - - - - 1 changed file: - persistence/src/main/java/fr/ird/observe/persistence/migration/DataSourceMigrationForVersion_6_0.java Changes: ===================================== persistence/src/main/java/fr/ird/observe/persistence/migration/DataSourceMigrationForVersion_6_0.java ===================================== --- a/persistence/src/main/java/fr/ird/observe/persistence/migration/DataSourceMigrationForVersion_6_0.java +++ b/persistence/src/main/java/fr/ird/observe/persistence/migration/DataSourceMigrationForVersion_6_0.java @@ -25,6 +25,7 @@ package fr.ird.observe.persistence.migration; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.topia.persistence.support.TopiaSqlQuery; @@ -35,10 +36,9 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; /** * Created on 27/10/16. @@ -90,32 +90,79 @@ public class DataSourceMigrationForVersion_6_0 extends AbstractObserveMigrationC private void evol8391(TopiaSqlSupport topiaSqlSupport, List<String> queries) { - ImmutableMap<String, String> missingCodesMapping = ImmutableMap.<String, String>builder() - .put("LT ?", "TL") - .put("LF", "FL") - .put("L1", "PDL") - .put("LC", "DL") - .put("LT", "TL") - .put("", "TL") + ImmutableMap<String, String> sqlMappingByCode = + ImmutableMap.<String, String>builder() + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .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);") + .build(); + + ImmutableMap<String, String> knownSizeMeasureTypeMapping = ImmutableMap.<String, String>builder() + .put("LT ?", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293") + .put("LF", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336") + .put("L1", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607") + .put("LC", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928") + .put("LT", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293") + .put("", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293") + .put("FL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465700#0.0902433863375336") + .put("CFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499465999#0.707568018231541") + .put("PAL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466255#0.444246932631359") + .put("TL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466532#0.844473292818293") + .put("PD1", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607") + .put("PDL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499466774#0.529249255312607") + .put("SL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467059#0.563144549960271") + .put("LJFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467326#0.537910396233201") + .put("CLJFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467638#0.409153908025473") + .put("IDL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499467945#0.249704661779106") + .put("EFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468283#0.152317869942635") + .put("DW", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499468646#0.710986209334806") + .put("DL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469115#0.42996318358928") + .put("TW", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499469572#0.969587777974084") + .put("CTL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470528#0.813851526239887") + .put("DML", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499470887#0.950930784922093") + .put("CDML", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499471278#0.425988764036447") + .put("PFL", "fr.ird.observe.entities.referentiel.SizeMeasureType#1433499472220#0.885759855154902") .build(); - List<String> lengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() { + + ImmutableMap.Builder<String, String> existingSizeMeasureMappingBuilder = ImmutableMap.builder(); + + topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<Pair<String, String>>() { @Override public PreparedStatement prepareQuery(Connection connection) throws SQLException { - return connection.prepareStatement("SELECT DISTINCT(lengthmeasuretype) FROM observe_common.species WHERE lengthmeasuretype IS NOT NULL"); + // Attention à ce moment là ce référentiel est encore dans le schema longline + return connection.prepareStatement("SELECT code, topiaid FROM observe_longline.sizemeasuretype "); } @Override - public String prepareResult(ResultSet set) throws SQLException { - return set.getString(1); + public Pair<String, String> prepareResult(ResultSet set) throws SQLException { + String code = set.getString(1); + String topiaId = set.getString(2); + return Pair.of(code, StringUtils.replace(topiaId, ".longline", "")); } - }); - List<String> existingLengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() { + }).forEach(p -> existingSizeMeasureMappingBuilder.put(p.getKey(), p.getValue())); + + ImmutableMap<String, String> existingSizeMeasureMapping = existingSizeMeasureMappingBuilder.build(); + + List<String> lengthMeasureTypes = topiaSqlSupport.findMultipleResult(new TopiaSqlQuery<String>() { @Override public PreparedStatement prepareQuery(Connection connection) throws SQLException { - // Attention à ce moment là ce référentiel est encore dans le schema longline - return connection.prepareStatement("SELECT DISTINCT(code) FROM observe_longline.sizemeasuretype "); + return connection.prepareStatement("SELECT DISTINCT(lengthmeasuretype) FROM observe_common.species WHERE lengthmeasuretype IS NOT NULL"); } @Override @@ -124,115 +171,35 @@ public class DataSourceMigrationForVersion_6_0 extends AbstractObserveMigrationC } }); - Set<String> missingLengthMeasureTypeCodes = new HashSet<>(); - for (String lengthMeasureType : lengthMeasureTypes) { - lengthMeasureType = lengthMeasureType.trim(); - String realCode = missingCodesMapping.get(lengthMeasureType); - if (realCode == null) { - realCode = lengthMeasureType; - } - missingLengthMeasureTypeCodes.add(realCode); - } - missingLengthMeasureTypeCodes.removeAll(existingLengthMeasureTypes); - - ImmutableMap.Builder<String, String> extraTopiaIdsByCodeBuilder = ImmutableMap.builder(); - - if (!missingLengthMeasureTypeCodes.isEmpty()) { - - // il manque des référentiels on va les ajouter - ImmutableMap<String, String> sqlMappingByCode = - ImmutableMap.<String, String>builder() - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .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);") - .build(); - - Iterator<String> iterator = missingLengthMeasureTypeCodes.iterator(); - while (iterator.hasNext()) { - String code = iterator.next(); - String sql = sqlMappingByCode.get(code); - if (sql != null) { - queries.add(sql); - iterator.remove(); - int i = sql.indexOf("'"); - String topiaId = sql.substring(i + 1); - int j = topiaId.indexOf("'"); - topiaId = topiaId.substring(0, j); - if (log.isInfoEnabled()) { - log.info("Add " + code + "::" + topiaId); - } - extraTopiaIdsByCodeBuilder.put(code, topiaId); - } - } + Set<String> unkownCodes = new HashSet<>(lengthMeasureTypes); + unkownCodes.removeAll(knownSizeMeasureTypeMapping.keySet()); + unkownCodes.removeAll(existingSizeMeasureMapping.keySet()); - if (!missingLengthMeasureTypeCodes.isEmpty()) { + if (!unkownCodes.isEmpty()) { - // migration impossible, il manque encore des référentiels - // ce cas ne devrait jamais arrivé - 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."); - } + // migration impossible, il manque encore des référentiels + // ce cas ne devrait jamais arrivé + 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."); } - ImmutableMap<String, String> extraTopiaIdsByCode = extraTopiaIdsByCodeBuilder.build(); + Set<String> missingSizeMeasureTypeIds = knownSizeMeasureTypeMapping.values().stream().filter(id -> !existingSizeMeasureMapping.values().contains(id)).collect(Collectors.toSet()); - Set<String> invalidCodes = new LinkedHashSet<>(); - for (String lengthMeasureType : lengthMeasureTypes) { - - lengthMeasureType = lengthMeasureType.trim(); - if (lengthMeasureType.isEmpty()) { - continue; - } - if (missingCodesMapping.containsKey(lengthMeasureType)) { - lengthMeasureType = missingCodesMapping.get(lengthMeasureType); - } + for (String missingSizeMeasureTypeId : missingSizeMeasureTypeIds) { + queries.add(sqlMappingByCode.get(missingSizeMeasureTypeId)); + } - String finalLengthMeasureType = lengthMeasureType; - String sizeMeasureTypeId = extraTopiaIdsByCode.get(lengthMeasureType); - if (sizeMeasureTypeId == null) { - sizeMeasureTypeId = topiaSqlSupport.findSingleResult(new TopiaSqlQuery<String>() { - - @Override - public PreparedStatement prepareQuery(Connection connection) throws SQLException { - return connection.prepareStatement("SELECT topiaId FROM observe_longline.sizeMeasureType WHERE code = '" + finalLengthMeasureType + "'"); - } - - @Override - public String prepareResult(ResultSet set) throws SQLException { - return set.getString(1); - } - }); - } + ImmutableMap.Builder<String, String> allSizeMeasureTypeMappingBuilder = ImmutableMap.<String, String>builder().putAll(existingSizeMeasureMapping); + knownSizeMeasureTypeMapping.entrySet().stream().filter(e -> !existingSizeMeasureMapping.containsKey(e.getKey())).forEach(e -> allSizeMeasureTypeMappingBuilder.put(e.getKey(), e.getValue())); + ImmutableMap<String, String> allSizeMeasureTypeMapping = allSizeMeasureTypeMappingBuilder.build(); - if (sizeMeasureTypeId == null) { + for (String lengthMeasureType : lengthMeasureTypes) { - if (log.isWarnEnabled()) { - log.warn("Impossible de trouver la type de mesure de taille pour le code: " + lengthMeasureType); - } - invalidCodes.add(lengthMeasureType); - } else { - sizeMeasureTypeId = StringUtils.replace(sizeMeasureTypeId, ".longline", ""); - queries.add(String.format("UPDATE observe_common.species SET sizeMeasureType = '%s' WHERE lengthMeasureType = '%s'", sizeMeasureTypeId, lengthMeasureType)); - } - } + lengthMeasureType = lengthMeasureType.trim(); - if (!invalidCodes.isEmpty()) { + String sizeMeasureTypeId = allSizeMeasureTypeMapping.get(lengthMeasureType); + queries.add(String.format("UPDATE observe_common.species SET sizeMeasureType = '%s' WHERE lengthMeasureType = '%s'", sizeMeasureTypeId, lengthMeasureType)); - throw new IllegalStateException("Impossible de trouver la type de mesure de taille pour les codes suivants :\n\t" + Joiner.on("\n\t").join(invalidCodes)); } } View it on GitLab: https://gitlab.com/ultreiaio/ird-observe/commit/cd4789de14996a02785959f9f6be...
participants (1)
-
Tony CHEMIT