Project

General

Profile

Download (5.52 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.database.update.v523_525;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import eu.etaxonomy.cdm.common.CdmUtils;
17
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
18
import eu.etaxonomy.cdm.database.ICdmDataSource;
19
import eu.etaxonomy.cdm.database.update.CaseType;
20
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
21
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
23

    
24
/**
25
 * #9664 #4311
26
 * @author a.mueller
27
 * @since 22.04.2021
28
 */
29
public class NomenclaturalTitleUpdater extends SchemaUpdaterStepBase {
30

    
31
    private static final String step = "Update collector title";
32

    
33
    public static NomenclaturalTitleUpdater NewInstance (List<ISchemaUpdaterStep> stepList){
34
        return new NomenclaturalTitleUpdater(stepList);
35
    }
36

    
37
    protected NomenclaturalTitleUpdater(List<ISchemaUpdaterStep> stepList) {
38
        super(stepList, step);
39
    }
40
    @Override
41
    public List<ISchemaUpdaterStep> getInnerSteps() {
42
        List<ISchemaUpdaterStep> result = new ArrayList<>();
43

    
44
        return result;
45
    }
46

    
47
    @Override
48
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
49
            SchemaUpdateResult result) throws SQLException {
50

    
51
        //set collectorCache = titleCache for all persons and teams
52
        String sql = "UPDATE @@AgentBase@@ "
53
                  + " SET nomenclaturalTitleCache = nomenclaturalTitle, nomenclaturalTitle = NULL "
54
                  + " WHERE DTYPE = 'Person' OR DTYPE = 'Team'";
55
        datasource.executeUpdate(caseType.replaceTableNames(sql));
56

    
57
        //for teams being used as nomenclatural authors
58
        sql = " SELECT ab.* FROM @@AgentBase@@ ab "
59
                + " WHERE id IN (SELECT combinationAuthorship_id FROM @@TaxonName@@) "
60
                + "    OR id IN (SELECT exCombinationAuthorship_id FROM @@TaxonName@@) "
61
                + "    OR id IN (SELECT basionymAuthorship_id FROM @@TaxonName@@) "
62
                + "    OR id IN (SELECT exBasionymAuthorship_id FROM @@TaxonName@@) ";
63
        ResultSet rs = datasource.executeQuery(caseType.replaceTableNames(sql));
64
        while (rs.next()){
65
            String dtype = rs.getString("DTYPE");
66
            int id = rs.getInt("id");
67
            if ("Team".equalsIgnoreCase(dtype)){
68

    
69
                //for each team member handle like persons below (NOTE: we even handle team members of teams with protectedNomTitleCache in this way as the members could potentially be used in future and potentially be correct for some reason)
70
                sql = "SELECT p.* FROM @@AgentBase_AgentBase@@ MN INNER JOIN @@AgentBase@@ p ON p.id = MN.teamMembers_id WHERE MN.team_ID = " + id;
71
                ResultSet rs2 = datasource.executeQuery(caseType.replaceTableNames(sql));
72
                while (rs2.next()){
73
                    handlePerson(rs2, datasource, caseType, result);
74
                }
75
                rs2.close();
76
            }else if ("Person".equalsIgnoreCase(dtype)){
77
                //for each person in gathering event
78
                handlePerson(rs, datasource, caseType, result);
79
            }
80
        }
81

    
82
        sql = "SELECT * FROM @@AgentBase@@ ab "
83
           + " WHERE DTYPE = 'Person' AND nomenclaturalTitle IS NULL AND nomenclaturalTitleCache <> titleCache ";
84
        rs = datasource.executeQuery(caseType.replaceTableNames(sql));
85
        while (rs.next()){
86
            if (isNomTitleMaybeRelevant(rs)){
87
                handlePerson(rs, datasource, caseType, result);
88
            }
89
        }
90
        //TODO check if nomtitle = titleCache und Punkt
91
    }
92

    
93
    private boolean isNomTitleMaybeRelevant(ResultSet rs) throws SQLException {
94
        String titleCache = rs.getString("titleCache");
95
        String nomenclaturalTitleCache = rs.getString("nomenclaturalTitleCache");
96
        String familyName = rs.getString("familyName");
97
        String givenName = rs.getString("givenName");
98

    
99
        boolean protectedTitleCache = rs.getBoolean("protectedTitleCache");
100
        if (isBlank(nomenclaturalTitleCache) || nomenclaturalTitleCache.contains(",")){
101
            return false;
102
        }
103
        //if titleCache is protected and they are more or less equal it is expected that the nomCache was created from titleCache not the other way round
104
        if (protectedTitleCache && normalizeCache(titleCache).equals(normalizeCache(nomenclaturalTitleCache))){
105
            return false;
106
        }
107
        nomenclaturalTitleCache = nomenclaturalTitleCache.trim();
108
        if (nomenclaturalTitleCache.matches(".*(\\s+)(\\s*[A-Z]\\.)+")){ //if it ends with an upper case initial consider it not to be a valid nom. title
109
            return false;
110
        }
111
        return true;
112
    }
113

    
114

    
115
    private String normalizeCache(String str) {
116
        return (CdmUtils.Nz(str).replace(",", "").replaceAll("\\s", "").replace(".", ""));
117
    }
118

    
119
    private void handlePerson(ResultSet rs, ICdmDataSource datasource, CaseType caseType, SchemaUpdateResult result) throws SQLException {
120
        //preliminary set protected cache for all Persons being in use as nom. author
121
        int id = rs.getInt("id");
122
        String sql = "UPDATE @@AgentBase@@ SET nomenclaturalTitle = nomenclaturalTitleCache WHERE id = " + id;
123
        datasource.executeUpdate(caseType.replaceTableNames(sql));
124
    }
125
}
(2-2/4)