Project

General

Profile

Download (3.18 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2020 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.app.pesi.validate;
10

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

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

    
17
import eu.etaxonomy.cdm.common.CdmUtils;
18

    
19
/**
20
 * @author a.mueller
21
 * @since 06.01.2020
22
 */
23
public class PesiValidatorBase {
24

    
25
    private static final Logger logger = Logger.getLogger(PesiValidatorBase.class);
26

    
27
    protected boolean testTreeIndex(ResultSet destRS, String childIndexAttr, String parentIndexAttr, String id) throws SQLException {
28
        boolean result;
29
        int taxonStatusFk = destRS.getInt("TaxonStatusFk");
30
        String parentTaxonId = destRS.getString("parentTaxonFk");
31
        int rankFk = destRS.getInt("RankFk");
32
        if (taxonStatusFk == 2 || taxonStatusFk == 3|| taxonStatusFk == 4 || taxonStatusFk == 8 || rankFk <= 10){  //synonym; partial syn; pro parte syn; valueless; kingdom and higher
33
            result = isNull(childIndexAttr, destRS, id);
34
        }else{
35
            String childIndex = destRS.getString(childIndexAttr);
36
            String parentIndex = destRS.getString(parentIndexAttr);
37
            parentIndex = parentIndex == null? "#": parentIndex;
38
            result = equals("Tree index", childIndex, parentIndex + parentTaxonId + "#", id);
39
        }
40
        return result;
41
    }
42

    
43
    protected boolean isNull(String attrName, ResultSet destRS, String id) throws SQLException {
44
        Object value = destRS.getObject(attrName);
45
        if (value != null){
46
            String message = attrName + " was expected to be null but was: " + value.toString() + "; id = " + id;
47
            logger.warn(message);
48
            return false;
49
        }else{
50
            logger.info(attrName + " was null as expected; id = " + id);
51
            return true;
52
        }
53
    }
54

    
55
    protected boolean equals(String messageStart, String strSrc, String strDest, String id) {
56
        if (StringUtils.isBlank(strSrc)){
57
            strSrc = null;
58
        }else{
59
            strSrc = strSrc.trim();
60
        }
61
        //we do not trim strDest here because this should be done during import already. If not it should be shown here
62
        if (!CdmUtils.nullSafeEqual(strSrc, strDest)){
63
            int index = CdmUtils.diffIndex(strSrc, strDest);
64
            String message = id+ ": " + messageStart + " must be equal, but was not at "+index+".\n  Source:      "+  strSrc + "\n  Destination: " + strDest;
65
            logger.warn(message);
66
            return false;
67
        }else{
68
            logger.info(id+ ": " + messageStart + " were equal: " + strSrc);
69
            return true;
70
        }
71
    }
72

    
73
    protected Integer nullSafeInt(ResultSet rs, String columnName) throws SQLException {
74
        Object intObject = rs.getObject(columnName);
75
        if (intObject == null){
76
            return null;
77
        }else{
78
            return Integer.valueOf(intObject.toString());
79
        }
80
    }
81

    
82
    protected boolean isNotBlank(String str) {
83
        return StringUtils.isNotBlank(str);
84
    }
85

    
86
}
(3-3/3)