Project

General

Profile

Download (47 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.pesi.euromed;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.sql.Timestamp;
14

    
15
import org.apache.commons.lang.StringUtils;
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.app.common.CdmDestinations;
19
import eu.etaxonomy.cdm.app.pesi.PesiDestinations;
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.database.ICdmDataSource;
22
import eu.etaxonomy.cdm.io.common.Source;
23
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
24

    
25
/**
26
 * Tests the ERMS -> PESI pipeline by comparing the source DB with destination PESI DB.
27
 *
28
 * @author a.mueller
29
 * @since 01.09.2019
30
 */
31
public class PesiEuroMedValidator {
32

    
33
    private static final Logger logger = Logger.getLogger(PesiEuroMedValidator.class);
34

    
35
    private static final ICdmDataSource defaultSource = CdmDestinations.cdm_test_local_mysql_euromed();
36
    private static final Source defaultDestination = PesiDestinations.pesi_test_local_CDM_EM2PESI();
37
//    private static final Source defaultDestination = PesiDestinations.pesi_test_local_CDM_EM2PESI_2();
38

    
39
    private Source source = new Source(defaultSource);
40
    private Source destination = defaultDestination;
41

    
42
    private String origEuroMed = "OriginalDB = 'E+M' ";
43

    
44
    public void invoke(Source source, Source destination){
45
        logger.warn("Validate destination " +  destination.getDatabase());
46
        boolean success = true;
47
        try {
48
            this.source = source;
49
            this.destination = destination;
50
            success &= testReferences();
51
//            success &= testTaxa();
52
//            success &= testTaxonRelations();
53
//            success &= testDistributions();
54
//            success &= testNotes();
55
//            success &= testAdditionalTaxonSources();
56
        } catch (Exception e) {
57
            e.printStackTrace();
58
            success = false;
59
        }
60
        //TBC
61
        System.out.println("end validation " + (success? "":"NOT ") + "successful.");
62
    }
63

    
64
    private boolean testAdditionalTaxonSources() throws SQLException {
65
        System.out.println("Start validate additional taxon sources");
66
        boolean success = testAdditionalTaxonSourcesCount();
67
        if (success){
68
              success &= testSingleAdditionalTaxonSources(source.getUniqueInteger(countAddtionalTaxonSource));
69
        }
70
        return success;
71
    }
72

    
73
    private boolean testNotes() throws SQLException {
74
        System.out.println("Start validate notes");
75
        boolean success = testNotesCount();
76
        if (success){
77
              success &= testSingleNotes(source.getUniqueInteger("SELECT count(*) FROM notes "));
78
        }
79
        return success;
80
    }
81

    
82
    private boolean testDistributions() throws SQLException {
83
        System.out.println("Start validate distributions");
84
        boolean success = testDistributionCount();
85
        if (success){
86
              success &= testSingleDistributions(source.getUniqueInteger("SELECT count(*) FROM dr "));
87
        }
88
        return success;
89
    }
90

    
91
    private boolean testCommonNames() throws SQLException {
92
        System.out.println("Start validate common names");
93
        boolean success = testCommonNameCount();
94
        if (success){
95
            success &= testSingleCommonNames(source.getUniqueInteger("SELECT count(*) FROM vernaculars "));
96
        }
97
        return success;
98
    }
99

    
100
    int countSynonyms;
101
    int countIncludedIns;
102
    private boolean testTaxonRelations() {
103
        System.out.println("Start validate taxon relations");
104
        boolean success = testSynonymRelations();
105
        success &= testIncludedInRelations();
106
        success &= testTotalRelations();
107
        success &= testNameRelations();
108
        return success;
109
    }
110

    
111
    private boolean testTotalRelations() {
112
        if (!(countSynonyms < 0 || countIncludedIns < 0)){
113
            int countTotalSrc = countSynonyms + countIncludedIns;
114
            int countSrc = source.getUniqueInteger("SELECT count(*) FROM tu ");
115
            boolean success = equals("Taxrel count + 1 must be same as source taxon count ", countTotalSrc+1, countSrc, String.valueOf(-1));
116
            int countDest = destination.getUniqueInteger("SELECT count(*) FROM Taxon t WHERE t."+ origEuroMed);
117
            success &= equals("Taxrel count + 1 must be same as destination taxon count ", countTotalSrc+1, countDest, String.valueOf(-1));
118
            return success;
119
        }else{
120
            return false;
121
        }
122
    }
123

    
124
    private boolean testSynonymRelations() {
125

    
126
        int countSrc = source.getUniqueInteger(countSynonymRelation);
127
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM RelTaxon WHERE RelTaxonQualifierFk > 101");
128
        boolean success = equals("Synonym count ", countSrc, countDest, String.valueOf(-1));
129
//         update Match_RelStat set RelTaxon  =  102 where tu_unacceptreason like 'currently placed%'
130
//                 update Match_RelStat set RelTaxon   =  102 where tu_unacceptreason like 'currently held%'
131
//                 update Match_RelStat set RelTaxon   =  102 where tu_unacceptreason like 'sy%' or tu_unacceptreason like '%jun%syn%'
132
//                 update Match_RelStat set RelTaxon   =  102 where tu_unacceptreason = '(synonym)'
133
//                 update Match_RelStat set RelTaxon   =  102 where tu_unacceptreason = 'reverted genus transfer'
134
//                 update Match_RelStat set RelTaxon   =  103 where tu_unacceptreason like 'misapplied%'
135
//                 update Match_RelStat set RelTaxon   =  104 where tu_unacceptreason like 'part% synonym%'
136
//                 update Match_RelStat set RelTaxon   =  106 where tu_unacceptreason = 'heterotypic synonym' or tu_unacceptreason = 'subjective synonym'
137
//                 update Match_RelStat set RelTaxon   =  107 where tu_unacceptreason like '%homot%syn%' or tu_unacceptreason = 'objective synonym' synyonym
138
//                 update Match_RelStat set RelTaxon   =  107 where tu_unacceptreason like '%bas[iy][no]%ny%'
139
        if (success){
140
            //TODO test single synonym relations
141
//            success &= testSingleTaxonRelations(source.getUniqueInteger(countSynonymRelation));
142
        }
143
        countSynonyms = (countSrc == countDest)? countSrc : -1;
144
        return success;
145
    }
146

    
147
    private boolean testNameRelations() {
148
        //Name relations
149
        int countSrc = source.getUniqueInteger("SELECT count(*) FROM tu WHERE ("
150
               + " tu_unacceptreason like '%bas[iy][no]%ny%' OR tu_unacceptreason = 'original combination' "
151
               + " OR tu_unacceptreason = 'Subsequent combination' OR tu_unacceptreason like '%genus transfer%'  "
152
               + " OR tu_unacceptreason = 'genus change' "  //1
153
               + " OR tu_unacceptreason like '%homon%' "   // 2
154
               + " OR tu_unacceptreason like '%spell%' OR tu_unacceptreason like 'lapsus %' " //16
155

    
156
                 + ")");
157
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM RelTaxon WHERE RelTaxonQualifierFk <100 ");
158
        boolean success = equals("Taxon name relation count ", countSrc, countDest, String.valueOf(-1));
159
        if (success){
160
            //TODO test single name relation
161
//            success &= testSingleTaxonRelations(source.getUniqueInteger(countSynonymRelation));
162
        }
163
        return success;
164
    }
165

    
166
    private boolean testIncludedInRelations() {
167
        int countSrc = source.getUniqueInteger(countParentRelation);
168
        int  countDest = destination.getUniqueInteger("SELECT count(*) FROM RelTaxon WHERE RelTaxonQualifierFk = 101 ");
169
        boolean success = equals("Tax included in count ", countSrc, countDest, String.valueOf(-1));
170
        if (success){
171
            //TODO test single includedIn relations
172
//            success &= testSingleTaxonRelations(source.getUniqueInteger(countSynonymRelation));
173
        }
174
        countIncludedIns = (countSrc == countDest)? countSrc : -1;
175
        return success;
176
    }
177

    
178
    private boolean testTaxa() throws SQLException {
179
        System.out.println("Start validate taxa");
180
        boolean success = testTaxaCount();
181
        //FIXME
182
        if (!success){
183
            success &= testSingleTaxa(source.getUniqueInteger(countTaxon));
184
        }
185
        return success;
186
    }
187

    
188
    private boolean testReferences() throws SQLException {
189
        System.out.println("Start validate references");
190
        boolean success = testReferenceCount();
191
        if (success){
192
            success &= testSingleReferences();
193
        }
194
        return success;
195
    }
196

    
197
    private final String countAddtionalTaxonSource = "SELECT count(*) FROM tu_sources ts ";
198
    private boolean testAdditionalTaxonSourcesCount() {
199
        int countSrc = source.getUniqueInteger(countAddtionalTaxonSource);
200
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM AdditionalTaxonSource ");
201
        return equals("AdditionalTaxonSource count ", countSrc, countDest, String.valueOf(-1));
202
    }
203

    
204
    private boolean testNotesCount() {
205
        int countSrc = source.getUniqueInteger("SELECT count(*) FROM notes ");
206
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM Note "
207
                + " WHERE NOT (NoteCategoryFk = 4 AND LastAction IS NULL) AND NOT NoteCategoryFk IN (22,23,24) ");
208
        boolean result = equals("Notes count ", countSrc, countDest, String.valueOf(-1));
209

    
210
        countSrc = source.getUniqueInteger("SELECT count(*) FROM tu "
211
                + " WHERE (tu_marine IS NOT NULL OR tu_brackish IS NOT NULL OR tu_fresh IS NOT NULL OR tu_terrestrial IS NOT NULL) " );
212
        countDest = destination.getUniqueInteger("SELECT count(*) FROM Note "
213
                + " WHERE (NoteCategoryFk = 4 AND LastAction IS NULL) ");
214
        result &= equals("Notes ecology count ", countSrc, countDest, String.valueOf(-1));
215

    
216
        countSrc = source.getUniqueInteger("SELECT count(*) FROM links ");
217
        countDest = destination.getUniqueInteger("SELECT count(*) FROM Note "
218
                + " WHERE NoteCategoryFk IN (22,23,24) ");
219
        result &= equals("Notes link count ", countSrc, countDest, String.valueOf(-1));
220

    
221
        return result;
222
    }
223

    
224
    private boolean testDistributionCount() {
225
        int countSrc = source.getUniqueInteger("SELECT count(*) FROM dr ");
226
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM Occurrence ");
227
        return equals("Occurrence count ", countSrc, countDest, String.valueOf(-1));
228
    }
229

    
230
    private boolean testCommonNameCount() {
231
        int countSrc = source.getUniqueInteger("SELECT count(*) FROM vernaculars ");
232
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM CommonName ");
233
        return equals("CommonName count ", countSrc, countDest, String.valueOf(-1));
234
    }
235

    
236
    private final String countSynonymRelation = "SELECT count(*) FROM tu syn LEFT JOIN tu acc ON syn.tu_acctaxon = acc.id WHERE (syn.id <> acc.id AND syn.tu_acctaxon IS NOT NULL AND syn.id <> acc.tu_parent) ";
237
    private final String countParentRelation  = "SELECT count(*)-1 FROM tu syn LEFT JOIN tu acc ON syn.tu_acctaxon = acc.id WHERE (syn.id =  acc.id OR  syn.tu_acctaxon IS     NULL OR  syn.id =  acc.tu_parent) ";
238

    
239
    private final String countTaxon = "SELECT count(*) FROM tu ";
240
    private boolean testTaxaCount() {
241
         int countSrc = source.getUniqueInteger(countTaxon);
242
         int countDest = destination.getUniqueInteger("SELECT count(*) FROM Taxon ");
243
         boolean result = equals("Taxon count ", countSrc, countDest, String.valueOf(-1));
244

    
245
         //NomStatus
246
         countSrc = source.getUniqueInteger("SELECT count(*) FROM tu WHERE ("
247
               + " tu_unacceptreason like '%inval%' OR  tu_unacceptreason like '%not val%' "
248
               + " OR tu_unacceptreason like '%illeg%' OR tu_unacceptreason like '%nud%' "
249
               + " OR tu_unacceptreason like '%rej.%' OR tu_unacceptreason like '%superfl%' "
250
               + " OR tu_unacceptreason like '%Comb. nov%' OR tu_unacceptreason like '%New name%' "
251
               + " OR tu_unacceptreason = 'new combination'  "
252
               + " OR tu_status IN (3,5,6,7,8) )");
253
         countDest = destination.getUniqueInteger("SELECT count(*) FROM Taxon WHERE NameStatusFk IS NOT NULL ");
254
         result = equals("Taxon name status count ", countSrc, countDest, String.valueOf(-1));
255

    
256
         return result;
257
     }
258

    
259
    private boolean testSingleTaxa(int n) throws SQLException {
260
        boolean success = true;
261
        ResultSet srcRS = source.getResultSet(""
262
                + " SELECT t.*, tu1.tu_name tu1_name, r.rank_name, acc.tu_sp as acc_sp, st.status_name, "
263
                + "        type.tu_displayname typename, type.tu_authority typeauthor, "
264
                + "        fo.fossil_name, qs.qualitystatus_name "
265
                + " FROM tu t "
266
                + " LEFT JOIN tu as tu1 on t.tu_parent = tu1.id"
267
                + " LEFT JOIN (SELECT DISTINCT rank_id, rank_name FROM ranks WHERE NOT(rank_id = 40 AND rank_name = 'Subdivision' OR rank_id = 122 AND rank_name='Subsection')) as r ON t.tu_rank = r.rank_id "
268
                + " LEFT JOIN tu acc ON acc.id = t.tu_acctaxon "
269
                + " LEFT JOIN status st ON st.status_id = t.tu_status "
270
                + " LEFT JOIN tu type ON type.id = t.tu_typetaxon "
271
                + " LEFT JOIN fossil fo ON t.tu_fossil = fo.fossil_id "
272
                + " LEFT JOIN qualitystatus qs ON t.tu_qualitystatus = qs.id "
273
                + " ORDER BY CAST(t.id as nvarchar(20)) ");
274
        ResultSet destRS = destination.getResultSet("SELECT t.*, "
275
                + "     pt.GenusOrUninomial p_GenusOrUninomial, pt.InfraGenericEpithet p_InfraGenericEpithet, pt.SpecificEpithet p_SpecificEpithet, "
276
                + "     s.Name as sourceName, type.IdInSource typeSourceId, r.Rank "
277
                + " FROM Taxon t "
278
                + "    LEFT JOIN Taxon pt ON pt.TaxonId = t.ParentTaxonFk "
279
                + "    LEFT JOIN Taxon type ON type.TaxonId = t.TypeNameFk "
280
                + "    LEFT JOIN Rank r ON r.RankId = t.RankFk AND r.KingdomId = t.KingdomFk "
281
                + "    LEFT JOIN Source s ON s.SourceId = t.SourceFk "
282
                + " WHERE t."+ origEuroMed
283
                + " ORDER BY t.IdInSource");
284
        ResultSet srcRsLastAction = source.getResultSet(""
285
                + " SELECT t.id, s.sessiondate, a.action_name, s.ExpertName "
286
                + " FROM tu t "
287
                + "   LEFT OUTER JOIN tu_sessions MN ON t.id = MN.tu_id "
288
                + "   LEFT JOIN actions a ON a.id = MN.action_id "
289
                + "   LEFT JOIN sessions s ON s.id = MN.session_id  "
290
                + " ORDER BY CAST(t.id as nvarchar(20)), s.sessiondate DESC, a.id DESC ");
291
        int i = 0;
292
        while (srcRS.next() && destRS.next()){
293
            success &= testSingleTaxon(srcRS, destRS);
294
//TODO       success &= testLastAction(srcRsLastAction, destRS, String.valueOf(srcRS.getInt("id")), "Taxon");
295
            i++;
296
        }
297
        success &= equals("Taxon count for single compare", n, i, String.valueOf(-1));
298
        return success;
299
    }
300

    
301

    
302
    private boolean testSingleTaxon(ResultSet srcRS, ResultSet destRS) throws SQLException {
303
        String id = String.valueOf(srcRS.getInt("id"));
304
        boolean success = equals("Taxon ID", "tu_id: " + srcRS.getInt("id"), destRS.getString("IdInSource"), id);
305
        success &= equals("Taxon source", "ERMS export for PESI", destRS.getString("sourceName"), id);
306
//TODO some
307
        success &= compareKingdom("Taxon kingdom", srcRS, destRS, id);
308
        success &= equals("Taxon rank fk", srcRS.getString("tu_rank"), destRS.getString("RankFk"), id);
309
        success &= equals("Taxon rank cache", normalizeRank(srcRS.getString("rank_name"), srcRS, id), destRS.getString("Rank"), id);
310
        success &= compareNameParts(srcRS, destRS, id);
311

    
312
        success &= equals("Taxon websearchname", srcRS.getString("tu_displayname"), destRS.getString("WebSearchName"), id);
313
//        success &= equals("Taxon WebShowName", srcRS.getString("tu_displayname"), destRS.getString("WebShowName"), id);
314
        success &= equals("Taxon authority", srcRS.getString("tu_authority"), destRS.getString("AuthorString"), id);
315
//        success &= equals("Taxon FullName", srcFullName(srcRS), destRS.getString("FullName"), id);
316
        success &= isNull("NomRefString", destRS);
317
//        success &= equals("Taxon DisplayName", srcDisplayName(srcRS), destRS.getString("DisplayName"), id);  //according to SQL script same as FullName, no nom.ref. information attached
318

    
319
//TODO        success &= equals("Taxon NameStatusFk", toNameStatus(nullSafeInt(srcRS, "tu_status")),nullSafeInt( destRS,"NameStatusFk"), id);
320
//TODO        success &= equals("Taxon NameStatusCache", srcRS.getString("status_name"), destRS.getString("NameStatusCache"), id);
321

    
322
//TODO        success &= equals("Taxon TaxonStatusFk", nullSafeInt(srcRS, "tu_status"),nullSafeInt( destRS,"TaxonStatusFk"), id);
323
//TODO        success &= equals("Taxon TaxonStatusCache", srcRS.getString("status_name"), destRS.getString("TaxonStatusCache"), id);
324

    
325
        //TODO ParentTaxonFk
326
        Integer orgigTypeNameFk = nullSafeInt(srcRS, "tu_typetaxon");
327
        success &= equals("Taxon TypeNameFk", orgigTypeNameFk == null? null : "tu_id: " + orgigTypeNameFk, destRS.getString("typeSourceId"), id);
328
//TODO  success &= equals("Taxon TypeFullNameCache", CdmUtils.concat(" ", srcRS.getString("typename"), srcRS.getString("typeauthor")), destRS.getString("TypeFullNameCache"), id);
329
        success &= equals("Taxon QualityStatusFK", nullSafeInt(srcRS, "tu_qualitystatus"),nullSafeInt( destRS,"QualityStatusFk"), String.valueOf(id));
330
        success &= equals("Taxon QualityStatusCache", srcRS.getString("qualitystatus_name"), destRS.getString("QualityStatusCache"), id);
331
        //TODO TreeIndex
332
        success &= equals("Taxon FossilStatusFk", nullSafeInt(srcRS, "tu_fossil"),nullSafeInt( destRS,"FossilStatusFk"), String.valueOf(id));
333
        success &= equals("Taxon FossilStatusCache", srcRS.getString("fossil_name"), destRS.getString("FossilStatusCache"), id);
334
        success &= equals("Taxon GUID", srcRS.getString("GUID"), destRS.getString("GUID"), id);
335
        success &= equals("Taxon DerivedFromGuid", srcRS.getString("GUID"), destRS.getString("DerivedFromGuid"), id); //according to SQL script GUID and DerivedFromGuid are always the same, according to 2014DB this is even true for all databases
336
        success &= isNull("ExpertGUID", destRS);  //only relevant after merge
337
        success &= isNull("ExpertName", destRS);  //only relevant after merge
338
        success &= isNull("SpeciesExpertGUID", destRS);  //only relevant after merge
339
        success &= equals("Taxon cache citation", srcRS.getString("cache_citation"), destRS.getString("CacheCitation"), id);
340
        //LastAction(Date) handled in separate method
341
        success &= isNull("GUID2", destRS);  //only relevant after merge
342
        success &= isNull("DerivedFromGuid2", destRS);  //only relevant after merge
343
        return success;
344
    }
345

    
346
    boolean namePartsFirst = true;
347
    private boolean compareNameParts(ResultSet srcRS, ResultSet destRS, String id) throws SQLException {
348
        if (namePartsFirst){
349
            logger.warn("Validation of name parts not fully implemented (difficult). Currently validated via fullname");
350
            namePartsFirst = false;
351
        }
352
        int rankFk = srcRS.getInt("tu_rank");
353
        String genusOrUninomial = null;
354
        String infraGenericEpithet = null;
355
        String specificEpithet = null;
356
        String infraSpecificEpithet = null;
357
        if (rankFk <= 180){
358
            genusOrUninomial = srcRS.getString("tu_name");
359
        }else if (rankFk == 190){
360
            genusOrUninomial = srcRS.getString("tu1_name");
361
            infraGenericEpithet =  srcRS.getString("tu_name");
362
            //TODO does not work this way
363
//        }else if (rankFk == 220){
364
//            genusOrUninomial = destRS.getString("p_GenusOrUninomial");
365
//            infraGenericEpithet = destRS.getString("p_InfraGenericEpithet");
366
//            specificEpithet = srcRS.getString("tu_name");
367
        }else{
368
            //TODO exception
369
            return false;
370
        }
371
        boolean result = testEpis(destRS, genusOrUninomial, infraGenericEpithet,
372
                specificEpithet, infraSpecificEpithet, id);
373
        return result;
374
    }
375

    
376
    private boolean testEpis(ResultSet destRS, String genusOrUninomial, String infraGenericEpithet, String specificEpithet,
377
            String infraSpecificEpithet, String id) throws SQLException {
378
        boolean result = equals("Taxon genusOrUninomial", genusOrUninomial, destRS.getString("GenusOrUninomial"), id) ;
379
        result &= equals("Taxon infraGenericEpithet", infraGenericEpithet, destRS.getString("InfraGenericEpithet"), id) ;
380
        result &= equals("Taxon specificEpithet", specificEpithet, destRS.getString("SpecificEpithet"), id) ;
381
        result &= equals("Taxon infraSpecificEpithet", infraSpecificEpithet, destRS.getString("InfraSpecificEpithet"), id) ;
382
        return result;
383
    }
384

    
385
    private String normalizeRank(String string, ResultSet srcRS, String id) throws SQLException {
386
        String result = string
387
                .replace("Subforma", "Subform")
388
                .replace("Forma", "Form");
389
        int kingdomFk = Integer.valueOf(getSourceKingdomFk(srcRS, id));
390
        if (kingdomFk == 3 || kingdomFk == 4){
391
            result = result.replace("Subphylum", "Subdivision");
392
            result = result.replace("Phylum", "Division");
393
        }
394
        return result;
395
    }
396

    
397
    //see also ErmsTaxonImport.getExpectedTitleCache()
398
    private String srcFullName(ResultSet srcRs) throws SQLException {
399
        String result = null;
400
        String epi = srcRs.getString("tu_name");
401
        epi = " a" + epi;
402
        String display = srcRs.getString("tu_displayname");
403
        String sp = srcRs.getString("tu_sp");
404
        if (display.indexOf(epi) != display.lastIndexOf(epi) && !sp.startsWith("#2#")){ //homonym, animal
405
            result = srcRs.getString("tu_displayname").replaceFirst(epi+" ", CdmUtils.concat(" ", " "+epi, srcRs.getString("tu_authority")))+" ";
406
        }else{
407
            result = CdmUtils.concat(" ", srcRs.getString("tu_displayname"), srcRs.getString("tu_authority"));
408
        }
409
        return result;
410
    }
411

    
412
    private String srcDisplayName(ResultSet srcRs) throws SQLException {
413
        String result = null;
414
        String epi = srcRs.getString("tu_name");
415
        epi = " a" + epi;
416
        String display = "<i>"+srcRs.getString("tu_displayname")+"</i>";
417
        display = display.replace(" var. ", "</i> var. <i>").replace(" f. ", "</i> f. <i>");
418
        String sp = srcRs.getString("tu_sp");
419
        if (display.indexOf(epi) != display.lastIndexOf(epi) && !sp.startsWith("#2#")){ //homonym, animal
420
            result = display.replaceFirst(epi+" ", CdmUtils.concat(" ", " "+epi, srcRs.getString("tu_authority")))+" ";
421
        }else{
422
            result = CdmUtils.concat(" ", display, srcRs.getString("tu_authority"));
423
        }
424
        return result;
425
    }
426

    
427
    String lastLastActionId = "-1";
428
    private boolean testLastAction(ResultSet srcRsLastAction, ResultSet destRs, String id, String table) throws SQLException {
429
        try {
430
            boolean success = true;
431
            String srcId = null;
432
            while (srcRsLastAction.next()){
433
                srcId = String.valueOf(srcRsLastAction.getInt("id"));
434
                if (!lastLastActionId.equals(srcId)){
435
                    lastLastActionId = srcId;
436
                    break;
437
                }
438
            }
439
            if(!id.equals(srcId)){
440
                logger.warn("Last Action SourceIDs are not equal: id: " +id + ", la-id: " + srcId);
441
            }
442
            String destStr = destRs.getString("LastAction");
443
            success &= equals(table + " SpeciesExpertName", srcRsLastAction.getString("ExpertName"), destRs.getString("SpeciesExpertName"), id);  //mapping ExpertName => SpeciesExpertName according to SQL script
444
            success &= equals(table + " Last Action", srcRsLastAction.getString("action_name"), destStr == null? null : destStr, id);
445
            success &= equals(table + " Last Action Date", srcRsLastAction.getTimestamp("sessiondate"), destRs.getTimestamp("LastActionDate"), id);
446

    
447
            return success;
448
        } catch (Exception e) {
449
            e.printStackTrace();
450
            throw e;
451
        }
452
    }
453

    
454
    private boolean compareKingdom(String messageStart, ResultSet srcRS, ResultSet destRS, String id) throws SQLException {
455
        String srcKingdom = getSourceKingdomFk(srcRS, id);
456
        Integer intDest = nullSafeInt(destRS, "KingdomFk");
457
        if (intDest == null){
458
            logger.warn(id +": " + messageStart + " must never be null for destination. Biota needs to be 0, all the rest needs to have >0 int value.");
459
            return false;
460
        }else{
461
            return equals(messageStart, srcKingdom, String.valueOf(intDest), id);
462
        }
463
    }
464

    
465
    private String getSourceKingdomFk(ResultSet srcRS, String id) throws SQLException {
466
        String strSrc = srcRS.getString("acc_sp");
467
        if (strSrc == null){
468
            strSrc = srcRS.getString("tu_sp");
469
        }
470
        if (strSrc == null){
471
            if ("1".equals(id)){
472
                strSrc = "0";  //Biota
473
            }else if ("147415".equals(id)){
474
                strSrc = "6";  //Monera is synonym of Bacteria
475
            }else{
476
                strSrc = id;
477
            }
478
        }else{
479
            strSrc = strSrc.substring(1);
480
            strSrc = strSrc.substring(0, strSrc.indexOf("#"));
481
        }
482
        return strSrc;
483
    }
484

    
485
    private boolean testSingleTaxonRelations(int n) throws SQLException {
486
        boolean success = true;
487
        ResultSet srcRS = source.getResultSet(""
488
                + " SELECT t.* "
489
                + " FROM tu t "
490
                + " WHERE tu_acctaxon <> id "
491
                + " ORDER BY CAST(t.id as nvarchar(20)) ");
492
        ResultSet destRS = destination.getResultSet("SELECT rel.*, t1.IdInSource t1Id, t2.IdInSource t2Id "
493
                + " FROM RelTaxon rel "
494
                + "    LEFT JOIN Taxon t1 ON t1.TaxonId = rel.TaxonFk1 "
495
                + "    LEFT JOIN Taxon t2 ON t2.TaxonId = rel.TaxonFk2 "
496
                + " WHERE t1."+origEuroMed+" AND t2." + origEuroMed
497
                + " ORDER BY t1.IdInSource");
498
        int i = 0;
499
        while (srcRS.next() && destRS.next()){
500
            success &= testSingleTaxonRelation(srcRS, destRS);
501
            i++;
502
        }
503
        success &= equals("Taxon relation count for single compare", n, i, String.valueOf(-1));
504
        return success;
505
    }
506

    
507
    private boolean testSingleTaxonRelation(ResultSet srcRS, ResultSet destRS) throws SQLException {
508
        String id = String.valueOf(srcRS.getInt("id"));
509
        boolean success = equals("Taxon relation taxon1", "tu_id: " + srcRS.getInt("id"), destRS.getString("t1Id"), id);
510
        success &= equals("Taxon relation taxon2", "tu_id: " + srcRS.getInt("tu_acctaxon"), destRS.getString("t2Id"), id);
511
        success &= equals("Taxon relation qualifier fk", PesiTransformer.IS_SYNONYM_OF, destRS.getInt("RelTaxonQualifierFk"), id);
512
        success &= equals("Taxon relation qualifier cache", "is synonym of", destRS.getString("RelQualifierCache"), id);
513
        //TODO enable after next import
514
//        success &= isNull("notes", destRS);
515
        //complete if no further relations need to added
516
        return success;
517
    }
518

    
519
    private boolean testSingleAdditionalTaxonSources(int n) throws SQLException {
520
        boolean success = true;
521
        ResultSet srcRs = source.getResultSet("SELECT CAST(tu.id as nvarchar(20)) tuId, MN.*, s.*, su.sourceuse_name "
522
                + " FROM tu_sources MN INNER JOIN tu ON MN.tu_id = tu.id "
523
                + "    LEFT JOIN sources s ON s.id = MN.source_id "
524
                + "    LEFT JOIN sourceuses su ON MN.sourceuse_id = su.sourceuse_id "
525
                + " ORDER BY CAST(tu.id as nvarchar(20)), MN.sourceuse_id, s.id ");  //, no.note (not possible because ntext
526
        ResultSet destRs = destination.getResultSet("SELECT t.IdInSource, ats.*, s.*, su.* "
527
                + " FROM AdditionalTaxonSource ats INNER JOIN Taxon t ON t.TaxonId = ats.TaxonFk "
528
                + "    INNER JOIN Source s ON s.SourceId = ats.SourceFk "
529
                + "    LEFT JOIN SourceUse su ON su.SourceUseId = ats.SourceUseFk "
530
                + " WHERE t."+origEuroMed
531
                + " ORDER BY t.IdInSource, su.SourceUseId, s.RefIdInSource ");
532
        int count = 0;
533
        while (srcRs.next() && destRs.next()){
534
            success &= testSingleAdditionalTaxonSource(srcRs, destRs);
535
            count++;
536
        }
537
        success &= equals("Notes count differs", n, count, "-1");
538
        return success;
539
    }
540

    
541
    private boolean testSingleAdditionalTaxonSource(ResultSet srcRs, ResultSet destRs) throws SQLException {
542
        String id = String.valueOf(srcRs.getInt("tuId") + "-" + srcRs.getString("sourceuse_name"));
543
        boolean success = equals("Additional taxon source taxonID ", "tu_id: " + String.valueOf(srcRs.getInt("tuId")), destRs.getString("IdInSource"), id);
544
        success &= equals("Additional taxon source fk ", srcRs.getString("source_id"), destRs.getString("RefIdInSource"), id);  //currently we use the same id in ERMS and PESI
545
        success &= equals("Additional taxon source use fk ", srcRs.getString("sourceuse_id"), destRs.getString("SourceUseFk"), id);
546
        success &= equals("Additional taxon source use cache ", srcRs.getString("sourceuse_name"), destRs.getString("SourceUseCache"), id);
547
        //TODO some records are still truncated ~ >820 characters
548
        success &= equals("Additional taxon source name cache ", srcRs.getString("source_name"), destRs.getString("SourceNameCache"), id);
549
        success &= equals("Additional taxon source detail ", srcRs.getString("pagenr"), destRs.getString("SourceDetail"), id);
550
        //Complete
551
        return success;
552
    }
553

    
554
    private boolean testSingleNotes(int n) throws SQLException {
555
        boolean success = true;
556
        ResultSet srcRs = source.getResultSet("SELECT CAST(tu.id as nvarchar(20)) tuId, no.*, l.LanName "
557
                + " FROM notes no INNER JOIN tu ON no.tu_id = tu.id "
558
                + "    LEFT JOIN languages l ON l.LanID = no.lan_id "
559
                + " ORDER BY CAST(tu.id as nvarchar(20)), no.type, no.noteSortable ");  //, no.note (not possible because ntext
560
        ResultSet destRs = destination.getResultSet("SELECT t.IdInSource, no.*, cat.NoteCategory, l.Language "
561
                + " FROM Note no INNER JOIN Taxon t ON t.TaxonId = no.TaxonFk "
562
                + "    LEFT JOIN NoteCategory cat ON cat.NoteCategoryId = no.NoteCategoryFk "
563
                + "    LEFT JOIN Language l ON l.LanguageId = no.LanguageFk "
564
                + " WHERE t." + origEuroMed
565
                + "      AND NOT (NoteCategoryFk = 4 AND no.LastAction IS NULL) AND NOT NoteCategoryFk IN (22,23,24) "
566
                + " ORDER BY t.IdInSource, no.NoteCategoryCache, Note_1  ");
567
        int count = 0;
568
        ResultSet srcRsLastAction = source.getResultSet(""
569
                + " SELECT no.id, s.sessiondate, a.action_name, s.ExpertName "
570
                + " FROM notes no "
571
                + "   INNER JOIN tu ON tu.id = no.tu_id "
572
                + "   LEFT JOIN languages l ON l.LanID = no.lan_id"
573
                + "   LEFT JOIN notes_sessions MN ON no.id = MN.note_id "
574
                + "   LEFT JOIN actions a ON a.id = MN.action_id "
575
                + "   LEFT JOIN sessions s ON s.id = MN.session_id  "
576
                + " ORDER BY CAST(tu.id as nvarchar(20)), no.type, no.noteSortable, s.sessiondate DESC, a.id DESC ");
577

    
578
        while (srcRs.next() && destRs.next()){
579
            success &= testSingleNote(srcRs, destRs);
580
            success &= testLastAction(srcRsLastAction, destRs, String.valueOf(srcRs.getInt("id")), "Note");
581
            count++;
582
        }
583
        success &= equals("Notes count differs", n, count, "-1");
584
        return success;
585
    }
586

    
587
    private boolean testSingleNote(ResultSet srcRs, ResultSet destRs) throws SQLException {
588
        String id = String.valueOf(srcRs.getInt("tuId") + "-" + srcRs.getString("type"));
589
        boolean success = equals("Note taxonID ", "tu_id: " + String.valueOf(srcRs.getInt("tuId")), destRs.getString("IdInSource"), id);
590
        success &= equals("Note Note_1 ", srcRs.getString("note"), destRs.getString("Note_1"), id);
591
        success &= isNull("Note_2", destRs);
592
        success &= equals("Note category cache", normalizeNoteCatCache(srcRs.getString("type")), destRs.getString("NoteCategoryCache"), id);
593
        success &= equals("Note language ", srcRs.getString("LanName"), destRs.getString("Language"), id);
594
        success &= isNull("Region", destRs);
595
        success &= isNull("SpeciesExpertGUID", destRs);
596
        //SpeciesExpertName, LastAction, LastActionDate handled in separate method
597
        //complete
598
        return success;
599
    }
600

    
601
    private String normalizeNoteCatCache(String string) {
602
        return StringUtils.capitalize(string)
603
                .replace("Original Combination", "Original combination")
604
                .replace("Taxonomic remark", "Taxonomic Remark");
605
    }
606

    
607
    private boolean testSingleDistributions(int n) throws SQLException {
608
        boolean success = true;
609
        ResultSet srcRs = source.getResultSet("SELECT CAST(ISNULL(tu.tu_accfinal, tu.id) as nvarchar(20)) tuId,"
610
                + " gu.gazetteer_id, dr.*, gu.id guId, gu.gu_name "
611
                + " FROM dr INNER JOIN tu ON dr.tu_id = tu.id "
612
                + "    LEFT JOIN gu ON gu.id = dr.gu_id "
613
                + " ORDER BY CAST(ISNULL(tu.tu_accfinal, tu.id) as nvarchar(20)), gu.gazetteer_id, gu.gu_name, dr.noteSortable ");  //, dr.note (not possible because ntext
614
        ResultSet destRs = destination.getResultSet("SELECT t.IdInSource, a.AreaERMSGazetteerId, oc.*, a.AreaName "
615
                + " FROM Occurrence oc INNER JOIN Taxon t ON t.TaxonId = oc.TaxonFk "
616
                + "    LEFT JOIN Area a ON a.AreaId = oc.AreaFk "
617
                + " WHERE t." + origEuroMed
618
                + " ORDER BY t.IdInSource, a.AreaERMSGazetteerId, a.AreaName, oc.Notes ");
619
        ResultSet srcRsLastAction = source.getResultSet(""
620
                + " SELECT dr.id, s.sessiondate, a.action_name, s.ExpertName "
621
                + " FROM dr "
622
                + "   INNER JOIN tu ON tu.id = dr.tu_id "
623
                + "   LEFT JOIN gu ON gu.id = dr.gu_id "
624
                + "   LEFT JOIN dr_sessions MN ON dr.id = MN.dr_id "
625
                + "   LEFT JOIN actions a ON a.id = MN.action_id "
626
                + "   LEFT JOIN sessions s ON s.id = MN.session_id  "
627
                + " ORDER BY CAST(tu.id as nvarchar(20)), gu.gazetteer_id, gu.gu_name, s.sessiondate DESC, a.id DESC ");
628
        int count = 0;
629
        while (srcRs.next() && destRs.next()){
630
            success &= testSingleDistribution(srcRs, destRs);
631
            //there are >1000 duplicates in dr, therefore this creates lots of warnings
632
            success &= testLastAction(srcRsLastAction, destRs, String.valueOf(srcRs.getInt("id")), "Distribution");
633
            count++;
634
        }
635
        success &= equals("Distribution count differs", n, count, "-1");
636
        return success;
637
    }
638

    
639
    private boolean testSingleDistribution(ResultSet srcRs, ResultSet destRs) throws SQLException {
640
        String id = String.valueOf(srcRs.getInt("tuId") + "-" + srcRs.getString("gu_name"));
641
        boolean success = equals("Distribution taxonID ", "tu_id: " + String.valueOf(srcRs.getInt("tuId")), destRs.getString("IdInSource"), id);
642
        success &= equals("Distribution gazetteer_id ", srcRs.getString("gazetteer_id"), destRs.getString("AreaERMSGazetteerId"), id);
643
        success &= equals("Distribution area name ", srcRs.getString("gu_name"), destRs.getString("AreaName"), id);
644
        success &= equals("Distribution area name cache", srcRs.getString("gu_name"), destRs.getString("AreaNameCache"), id);
645
        success &= equals("Distribution OccurrenceStatusFk", 1, destRs.getInt("OccurrenceStatusFk"), id);
646
        success &= equals("Distribution OccurrenceStatusCache", "Present", destRs.getString("OccurrenceStatusCache"), id);
647
        //TODO see comments
648
        success &= isNull("SourceFk", destRs);  //sources should be moved to extra table only, check with script and PESI 2014 (=> has values for ERMS)
649
        success &= isNull("SourceCache", destRs);  //sources should be moved to extra table, check with script and PESI 2014 (=> has values for ERMS)
650
        success &= equals("Distribution notes ", srcRs.getString("note"), destRs.getString("Notes"), id);
651
        success &= isNull("SpeciesExpertGUID", destRs);  //SpeciesExpertGUID does not exist in ERMS
652
        //SpeciesExpertName,LastAction,LastActionDate handled in separate method
653
        return success;
654
    }
655

    
656
    private boolean testSingleCommonNames(int n) throws SQLException {
657
        boolean success = true;
658
        ResultSet srcRs = source.getResultSet("SELECT v.*, ISNULL([639_3],[639_2]) iso, l.LanName, tu.id tuId "
659
                + " FROM vernaculars v LEFT JOIN tu ON v.tu_id = tu.id LEFT JOIN languages l ON l.LanID = v.lan_id "
660
                + " ORDER BY CAST(tu.id as nvarchar(20)), ISNULL([639_3],[639_2]), v.vername, v.id ");
661
        ResultSet destRs = destination.getResultSet("SELECT cn.*, t.IdInSource, l.ISO639_2, l.ISO639_3 "
662
                + " FROM CommonName cn INNER JOIN Taxon t ON t.TaxonId = cn.TaxonFk LEFT JOIN Language l ON l.LanguageId = cn.LanguageFk "
663
                + " WHERE t." + origEuroMed
664
                + " ORDER BY t.IdInSource, ISNULL("+preferredISO639+", "+alternativeISO639+"), cn.CommonName, cn.LastActionDate ");  //sorting also lastActionDate results in a minimum of exact duplicate problems
665
        ResultSet srcRsLastAction = source.getResultSet(""
666
                + " SELECT v.id, s.sessiondate, a.action_name, s.ExpertName "
667
                + " FROM vernaculars v "
668
                + "   INNER JOIN tu ON tu.id = v.tu_id "
669
                + "   LEFT JOIN languages l ON l.LanID = v.lan_id"
670
                + "   LEFT JOIN vernaculars_sessions MN ON v.id = MN.vernacular_id "
671
                + "   LEFT JOIN actions a ON a.id = MN.action_id "
672
                + "   LEFT JOIN sessions s ON s.id = MN.session_id  "
673
                + " ORDER BY CAST(tu.id as nvarchar(20)), ISNULL([639_3],[639_2]), v.vername, v.id, s.sessiondate DESC, a.id DESC ");
674
        int count = 0;
675
        while (srcRs.next() && destRs.next()){
676
            success &= testSingleCommonName(srcRs, destRs);
677
            success &= testLastAction(srcRsLastAction, destRs, String.valueOf(srcRs.getInt("id")), "CommonName");
678
            count++;
679
        }
680
        success &= equals("Common name count differs", n, count, "-1");
681
        return success;
682
    }
683

    
684
    boolean prefer639_3 = true;
685
    String preferredISO639 = prefer639_3? "ISO639_3":"ISO639_2";
686
    String alternativeISO639 = prefer639_3? "ISO639_2":"ISO639_3";
687

    
688
    private boolean testSingleCommonName(ResultSet srcRs, ResultSet destRs) throws SQLException {
689
        String id = String.valueOf(srcRs.getInt("tuId") + "-" + srcRs.getString("lan_id"));
690
        boolean success = equals("Common name taxonID ", "tu_id: " + String.valueOf(srcRs.getInt("tuId")), destRs.getString("IdInSource"), id);
691
        success &= equals("CommonName name ", srcRs.getString("vername"), destRs.getString("CommonName"), id);
692
        success &= equals("Common name languageFk ", srcRs.getString("iso"), getLanguageIso(destRs), id);
693
        success = equals("CommonName LanguageCache ", normalizeLang(srcRs.getString("LanName")), destRs.getString("LanguageCache"), id);
694
        //TODO needed? success = equals("CommonName language code ", srcRs.getString("lan_id"), destRs.getString("LanguageFk"), id);
695
        success &= isNull("Region", destRs);  //region does not seem to exist in ERMS
696
        //TODO see comments
697
//        success &= isNull("SourceFk", destRs);  //sources should be moved to extra table, check with PESI 2014
698
//        success &= isNull("SourceNameCache", destRs);  //sources should be moved to extra table, check with PESI 2014
699
        success &= isNull("SpeciesExpertGUID", destRs);  //SpeciesExpertGUID does not exist in ERMS
700
        //SpeciesExpertName,LastAction,LastActionDate handled in separate method
701
        //complete
702
        return success;
703
    }
704

    
705
    private String normalizeLang(String string) {
706
        if ("Spanish".equals(string)){
707
            return "Spanish, Castillian";
708
        }else if ("Modern Greek (1453-)".equals(string)){
709
            return "Greek";
710
        }else if ("Malay (individual language)".equals(string)){
711
            return "Malay";
712
        }else if ("Swahili (individual language)".equals(string)){
713
            return "Swahili";
714
        }
715

    
716
        return string;
717
    }
718

    
719
    private String getLanguageIso(ResultSet destRs) throws SQLException {
720
        String result = destRs.getString(preferredISO639);
721
        if (result == null){
722
            result = destRs.getString(alternativeISO639);
723
        }
724
        return result;
725
    }
726

    
727
    private boolean testSingleReferences() throws SQLException {
728
        boolean success = true;
729
        ResultSet srcRS = source.getResultSet("SELECT r.* FROM Reference r ORDER BY r.id ");
730
        ResultSet destRS = destination.getResultSet("SELECT s.* FROM Source s "
731
                + " WHERE s." + origEuroMed
732
                + " ORDER BY s.RefIdInSource ");  // +1 for the source reference "erms" but this has no OriginalDB
733
        while (srcRS.next() && destRS.next()){
734
            success &= testSingleReference(srcRS, destRS);
735
        }
736
        return success;
737
    }
738

    
739
    private boolean testSingleReference(ResultSet srcRS, ResultSet destRS) throws SQLException {
740
        String id = String.valueOf(srcRS.getInt("id"));
741
        boolean success = equals("Reference ID ", srcRS.getInt("id"), destRS.getInt("RefIdInSource"), id);
742
        success &= isNull("IMIS_Id", destRS);  //for E+M no IMIS id exists
743
        success &= equals("Reference SourceCategoryFk ", convertSourceTypeFk(srcRS.getString("refType")), destRS.getInt("SourceCategoryFk"), id);
744
        success &= equals("Reference SourceCategoryCache ", convertSourceTypeCache(srcRS.getString("refType")), destRS.getString("SourceCategoryCache"), id);
745
        success &= equals("Reference name ", srcRS.getString("titleCache"), destRS.getString("Name"), id);
746
        success &= equals("Reference abstract ", srcRS.getString("referenceAbstract"), destRS.getString("Abstract"), id);
747
        success &= equals("Reference title ", srcRS.getString("title"), destRS.getString("Title"), id);
748
//        success &= equals("Reference author string ", srcRS.getString("source_author"), destRS.getString("AuthorString"), id);
749
//        success &= equals("Reference year ", normalizeYear(srcRS.getString("source_year")), destRS.getString("RefYear"), id);
750
        success &= equals("Reference NomRefCache ", srcRS.getString("abbrevTitleCache"), destRS.getString("NomRefCache"), id);
751
        //TODO DOI
752
//        success &= equals("Reference link ", srcRS.getString("source_link"), destRS.getString("Link"), id);
753
//        success &= equals("Reference note ", srcRS.getString("source_note"), destRS.getString("Notes"), id);
754
        //TODO see above
755
        //complete
756
        return success;
757
    }
758

    
759
    private Integer convertSourceTypeFk(String sourceType) {
760
        if (sourceType == null){
761
            return null;
762
        }else if ("d".equals(sourceType)){
763
            return 4;
764
        }else if ("e".equals(sourceType)){
765
            return 5;
766
        }else if ("p".equals(sourceType)){
767
            return 11;
768
        }else if ("i".equals(sourceType)){
769
            return 12;
770
        }
771
        return null;
772
    }
773
    private String convertSourceTypeCache(String sourceType) {
774
        if (sourceType == null){
775
            return null;
776
        }else if ("d".equals(sourceType)){
777
            return "database";
778
        }else if ("e".equals(sourceType)){
779
            return "informal reference";
780
        }else if ("p".equals(sourceType)){
781
            return "publication";
782
        }else if ("i".equals(sourceType)){
783
            //TODO
784
            return "i";
785
        }
786
        return null;
787
    }
788

    
789
    private boolean testReferenceCount() {
790
        int countSrc = source.getUniqueInteger("SELECT count(*) FROM reference ");
791
        int countDest = destination.getUniqueInteger("SELECT count(*) FROM Source s WHERE s."+ origEuroMed);  // +1 for the source reference "erms" but this has no OriginalDB
792
        boolean success = equals("Reference count ", countSrc, countDest, "-1");
793
        return success;
794
    }
795

    
796
    private String normalizeYear(String yearStr) {
797
        if (StringUtils.isBlank(yearStr)){
798
            return yearStr;
799
        }
800
        yearStr = yearStr.trim();
801
        if (yearStr.matches("\\d{4}-\\d{2}")){
802
            yearStr = yearStr.substring(0, 5)+yearStr.substring(0, 2)+yearStr.substring(5);
803
        }
804
        return yearStr;
805
    }
806

    
807
    private boolean isNull(String attrName, ResultSet destRS) throws SQLException {
808
        Object value = destRS.getObject(attrName);
809
        if (value != null){
810
            String message = attrName + " was expected to be null but was: " + value.toString();
811
            logger.warn(message);
812
            return false;
813
        }else{
814
            logger.info(attrName + " was null as expected");
815
            return true;
816
        }
817
    }
818

    
819
    private boolean equals(String messageStart, Timestamp srcDate, Timestamp destDate, String id) {
820
        if (!CdmUtils.nullSafeEqual(srcDate, destDate)){
821
            String message = id + ": " + messageStart + " must be equal, but was not.\n Source: "+  srcDate + "; Destination: " + destDate;
822
            logger.warn(message);
823
            return false;
824
        }else{
825
            logger.info(messageStart + " were equal: " + srcDate);
826
            return true;
827
        }
828
    }
829

    
830
    private boolean equals(String messageStart, Integer nSrc, Integer nDest, String id) {
831
        String strId = id.equals("-1")? "": (id+ ": ");
832
        if (!CdmUtils.nullSafeEqual(nSrc,nDest)){
833
            String message = strId+ messageStart + " must be equal, but was not.\n Source: "+  nSrc + "; Destination: " + nDest;
834
            logger.warn(message);
835
            return false;
836
        }else{
837
            logger.info(strId + messageStart + " were equal: " + nSrc);
838
            return true;
839
        }
840
    }
841

    
842
    private boolean equals(String messageStart, String strSrc, String strDest, String id) {
843
        if (StringUtils.isBlank(strSrc)){
844
            strSrc = null;
845
        }else{
846
            strSrc = strSrc.trim();
847
        }
848
        //we do not trim strDest here because this should be done during import already. If not it should be shown here
849
        if (!CdmUtils.nullSafeEqual(strSrc, strDest)){
850
            int index = CdmUtils.diffIndex(strSrc, strDest);
851
            String message = id+ ": " + messageStart + " must be equal, but was not at "+index+".\n  Source:      "+  strSrc + "\n  Destination: " + strDest;
852
            logger.warn(message);
853
            return false;
854
        }else{
855
            logger.info(id+ ": " + messageStart + " were equal: " + strSrc);
856
            return true;
857
        }
858
    }
859

    
860
    protected Integer nullSafeInt(ResultSet rs, String columnName) throws SQLException {
861
        Object intObject = rs.getObject(columnName);
862
        if (intObject == null){
863
            return null;
864
        }else{
865
            return Integer.valueOf(intObject.toString());
866
        }
867
    }
868

    
869
//** ************* MAIN ********************************************/
870

    
871

    
872

    
873
    public static void main(String[] args){
874
        PesiEuroMedValidator validator = new PesiEuroMedValidator();
875
        validator.invoke(new Source(defaultSource), defaultDestination);
876
        System.exit(0);
877
    }
878
}
(4-4/4)