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.v41_47;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
18
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
19
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
20
|
import eu.etaxonomy.cdm.database.update.CaseType;
|
21
|
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
|
22
|
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
|
23
|
import eu.etaxonomy.cdm.strategy.cache.agent.PersonDefaultCacheStrategy;
|
24
|
|
25
|
/**
|
26
|
* Updates the field Person.initials from Person.firstname.
|
27
|
* @author a.mueller
|
28
|
* @since 21.05.2017
|
29
|
*
|
30
|
*/
|
31
|
public class InitialsUpdater extends SchemaUpdaterStepBase{
|
32
|
@SuppressWarnings("unused")
|
33
|
private static final Logger logger = Logger.getLogger(InitialsUpdater.class);
|
34
|
|
35
|
/**
|
36
|
* @return
|
37
|
*/
|
38
|
public static InitialsUpdater NewInstance() {
|
39
|
return new InitialsUpdater();
|
40
|
}
|
41
|
|
42
|
private static final String stepName = "Make Person initials from firstname";
|
43
|
|
44
|
/**
|
45
|
* @param stepName
|
46
|
*/
|
47
|
protected InitialsUpdater() {
|
48
|
super(stepName);
|
49
|
}
|
50
|
|
51
|
|
52
|
|
53
|
@Override
|
54
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
55
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
56
|
|
57
|
try {
|
58
|
PersonDefaultCacheStrategy formatter = PersonDefaultCacheStrategy.NewInstance();
|
59
|
|
60
|
String sqlCopyFirstname = "UPDATE AgentBase SET initials = firstname WHERE DTYPE='Person'"
|
61
|
+ " AND firstname IS NOT NULL AND initials IS NULL ";
|
62
|
|
63
|
String sqlRemoveFirstname = "UPDATE AgentBase SET firstname = %s, initials = %s WHERE id = %d";
|
64
|
|
65
|
String sqlSelectInitials = "SELECT id, firstname FROM AgentBase WHERE DTYPE='Person'"
|
66
|
+ " AND firstname IS NOT NULL AND initials = firstname ";
|
67
|
|
68
|
datasource.executeUpdate(sqlCopyFirstname);
|
69
|
|
70
|
ResultSet rs = datasource.executeQuery(sqlSelectInitials);
|
71
|
while (rs.next()){
|
72
|
handleSingle(datasource, formatter, sqlRemoveFirstname, rs, monitor, result);
|
73
|
}
|
74
|
} catch (Exception e) {
|
75
|
String message = e.getMessage();
|
76
|
monitor.warning(message, e);
|
77
|
result.addException(e, message, this, "invoke");
|
78
|
}
|
79
|
|
80
|
return;
|
81
|
}
|
82
|
|
83
|
|
84
|
|
85
|
/**
|
86
|
* @param datasource
|
87
|
* @param formatter
|
88
|
* @param sqlRemoveFirstname
|
89
|
* @param rs
|
90
|
* @param monitor
|
91
|
* @throws SQLException
|
92
|
*/
|
93
|
private void handleSingle(ICdmDataSource datasource, PersonDefaultCacheStrategy formatter,
|
94
|
String sqlRemoveFirstname, ResultSet rs, IProgressMonitor monitor, SchemaUpdateResult result) throws SQLException {
|
95
|
try {
|
96
|
Integer id = rs.getInt("id");
|
97
|
String firstname = rs.getString("firstname");
|
98
|
|
99
|
String initialsAllow = formatter.getInitialsFromGivenName(firstname, false);
|
100
|
String initialsForced = formatter.getInitialsFromGivenName(firstname, true);
|
101
|
|
102
|
String firstnameSql;
|
103
|
String initialsSql;
|
104
|
if (CdmUtils.equalsIgnoreWS(firstname, initialsForced)){
|
105
|
//firstname was initials
|
106
|
firstnameSql = " null ";
|
107
|
initialsSql = initialsForced;
|
108
|
}else if (CdmUtils.equalsIgnoreWS(firstname, initialsAllow)){
|
109
|
//first name has only abbreviations, but not all of them being 1-letter, keep everything
|
110
|
firstnameSql = " firstname ";
|
111
|
initialsSql = initialsAllow;
|
112
|
}else {
|
113
|
//first name has non abbreviated parts, keep it as it is and use initials forced as initials
|
114
|
firstnameSql = " firstname ";
|
115
|
initialsSql = initialsForced;
|
116
|
}
|
117
|
if (initialsSql!= null){
|
118
|
initialsSql = initialsSql.replace("'", "\\'");
|
119
|
}
|
120
|
//handle blank
|
121
|
if (StringUtils.isBlank(firstname)){
|
122
|
firstnameSql = " null ";
|
123
|
initialsSql = " null ";
|
124
|
}else {
|
125
|
initialsSql = "'" + initialsSql + "'";
|
126
|
}
|
127
|
|
128
|
String sql = String.format(sqlRemoveFirstname, firstnameSql, initialsSql, id);
|
129
|
//remove old relationship
|
130
|
datasource.executeUpdate(sql);
|
131
|
} catch (Exception e) {
|
132
|
String message = e.getMessage();
|
133
|
monitor.warning(message, e);
|
134
|
result.addException(e, message, this, "handleSingle");
|
135
|
}
|
136
|
}
|
137
|
|
138
|
}
|