Project

General

Profile

Download (4.91 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 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.v40_50;
10

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

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

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
20
import eu.etaxonomy.cdm.database.ICdmDataSource;
21
import eu.etaxonomy.cdm.database.update.CaseType;
22
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
24
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
25
import eu.etaxonomy.cdm.strategy.cache.agent.PersonDefaultCacheStrategy;
26

    
27
/**
28
 * Updates the field Person.initials from Person.firstname.
29
 * @author a.mueller
30
 * @since 21.05.2017
31
 *
32
 */
33
public class InitialsUpdater extends SchemaUpdaterStepBase{
34
    @SuppressWarnings("unused")
35
    private static final Logger logger = Logger.getLogger(InitialsUpdater.class);
36

    
37
    /**
38
     * @return
39
     */
40
    public static InitialsUpdater NewInstance(List<ISchemaUpdaterStep> stepList) {
41
        return new InitialsUpdater(stepList);
42
    }
43

    
44
    private static final String stepName = "Make Person initials from firstname";
45

    
46
    /**
47
     * @param stepName
48
     */
49
    protected InitialsUpdater(List<ISchemaUpdaterStep> stepList) {
50
        super(stepList, stepName);
51
    }
52

    
53

    
54

    
55
    @Override
56
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
57
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
58

    
59
        try {
60
            PersonDefaultCacheStrategy formatter = PersonDefaultCacheStrategy.NewInstance();
61

    
62
            String sqlCopyFirstname = "UPDATE AgentBase SET initials = firstname WHERE DTYPE='Person'"
63
                    + " AND firstname IS NOT NULL AND initials IS NULL ";
64

    
65
            String sqlRemoveFirstname = "UPDATE AgentBase SET firstname = %s, initials = %s WHERE id = %d";
66

    
67
            String sqlSelectInitials = "SELECT id, firstname FROM AgentBase WHERE DTYPE='Person'"
68
                    + " AND firstname IS NOT NULL AND initials = firstname ";
69

    
70
            datasource.executeUpdate(sqlCopyFirstname);
71

    
72
            ResultSet rs = datasource.executeQuery(sqlSelectInitials);
73
            while (rs.next()){
74
                handleSingle(datasource, formatter, sqlRemoveFirstname, rs, monitor, result);
75
            }
76
        } catch (Exception e) {
77
            String message = e.getMessage();
78
            monitor.warning(message, e);
79
            result.addException(e, message, this, "invoke");
80
        }
81

    
82
        return;
83
    }
84

    
85

    
86

    
87
    /**
88
     * @param datasource
89
     * @param formatter
90
     * @param sqlRemoveFirstname
91
     * @param rs
92
     * @param monitor
93
     * @throws SQLException
94
     */
95
    private void handleSingle(ICdmDataSource datasource, PersonDefaultCacheStrategy formatter,
96
            String sqlRemoveFirstname, ResultSet rs, IProgressMonitor monitor, SchemaUpdateResult result) throws SQLException {
97
        try {
98
            Integer id = rs.getInt("id");
99
            String firstname = rs.getString("firstname");
100

    
101
            String initialsAllow = formatter.getInitialsFromGivenName(firstname, false);
102
            String initialsForced = formatter.getInitialsFromGivenName(firstname, true);
103

    
104
            String firstnameSql;
105
            String initialsSql;
106
            if (CdmUtils.equalsIgnoreWS(firstname, initialsForced)){
107
                //firstname was initials
108
                firstnameSql = " null ";
109
                initialsSql = initialsForced;
110
            }else if (CdmUtils.equalsIgnoreWS(firstname, initialsAllow)){
111
                //first name has only abbreviations, but not all of them being 1-letter, keep everything
112
                firstnameSql = " firstname ";
113
                initialsSql = initialsAllow;
114
            }else {
115
                //first name has non abbreviated parts, keep it as it is and use initials forced as initials
116
                firstnameSql = " firstname ";
117
                initialsSql = initialsForced;
118
            }
119
            if (initialsSql!= null){
120
                initialsSql = initialsSql.replace("'", "\\'");
121
            }
122
            //handle blank
123
            if (StringUtils.isBlank(firstname)){
124
                firstnameSql = " null ";
125
                initialsSql = " null ";
126
            }else {
127
                initialsSql = "'" + initialsSql + "'";
128
            }
129

    
130
            String sql = String.format(sqlRemoveFirstname, firstnameSql, initialsSql, id);
131
            //remove old relationship
132
            datasource.executeUpdate(sql);
133
        } catch (Exception e) {
134
            String message = e.getMessage();
135
            monitor.warning(message, e);
136
            result.addException(e, message, this, "handleSingle");
137
        }
138
    }
139

    
140
}
(1-1/10)