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.database.update.v505_508;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
17
|
|
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.ISchemaUpdaterStep;
|
22
|
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
|
23
|
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
|
24
|
import eu.etaxonomy.cdm.database.update.TreeIndexUpdater;
|
25
|
|
26
|
/**
|
27
|
* @author a.mueller
|
28
|
* @since 20.06.2019
|
29
|
*/
|
30
|
public class FeatureTreeMover extends SchemaUpdaterStepBase {
|
31
|
|
32
|
@SuppressWarnings("unused")
|
33
|
private static final Logger logger = LogManager.getLogger(FeatureTreeMover.class);
|
34
|
|
35
|
private static final String stepName = "Move FeatureTree to TermCollection";
|
36
|
|
37
|
|
38
|
public static final FeatureTreeMover NewInstance(List<ISchemaUpdaterStep> stepList){
|
39
|
FeatureTreeMover result = new FeatureTreeMover(stepList);
|
40
|
|
41
|
return result;
|
42
|
}
|
43
|
|
44
|
protected FeatureTreeMover(List<ISchemaUpdaterStep> stepList) {
|
45
|
super(stepList, stepName);
|
46
|
}
|
47
|
|
48
|
|
49
|
@Override
|
50
|
public List<ISchemaUpdaterStep> getInnerSteps() {
|
51
|
List<ISchemaUpdaterStep> result = new ArrayList<>();
|
52
|
|
53
|
// update tree index for feature node
|
54
|
//note: it could also be enough to only replace the first index entry by graph_id as only the graph_id changed
|
55
|
String stepName = "Update TermNode treeindex";
|
56
|
String tableName = "TermRelation";
|
57
|
String treeIdColumnName = "graph_id";
|
58
|
TreeIndexUpdater.NewInstance(result, stepName, tableName,
|
59
|
treeIdColumnName, "treeIndex", false); // see comment for TaxonTree
|
60
|
|
61
|
return result;
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
|
66
|
SchemaUpdateResult result) throws SQLException {
|
67
|
|
68
|
int maxIdTermVoc = getMaxIdTermVoc(datasource, monitor, caseType, result);
|
69
|
List<Integer> featureTreeIds = getFeatureTreeIds(datasource, monitor, caseType, result);
|
70
|
for (Integer featureTreeId : featureTreeIds){
|
71
|
maxIdTermVoc = moveFeatureTree(featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result);
|
72
|
}
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* @param featureTreeId
|
77
|
* @param maxIdTermVoc
|
78
|
* @param datasource
|
79
|
* @param monitor
|
80
|
* @param caseType
|
81
|
* @param result
|
82
|
* @return
|
83
|
* @throws SQLException
|
84
|
*/
|
85
|
private int moveFeatureTree(Integer featureTreeId, int maxIdTermVoc, ICdmDataSource datasource,
|
86
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
87
|
|
88
|
maxIdTermVoc++;
|
89
|
String attributes = "termType, created, updated, uuid, lsid_authority, lsid_lsid, lsid_namespace, lsid_object, lsid_revision, "
|
90
|
+ "protectedTitleCache, titleCache, createdBy_id, updatedBy_id, root_id, allowDuplicates";
|
91
|
String sql = "INSERT INTO @@TermCollection@@ (DTYPE,"+attributes+",id, isFlat, orderRelevant) "
|
92
|
+ " SELECT 'TermTree', "+attributes+","+ maxIdTermVoc +",0,1 FROM @@FeatureTree@@ WHERE id="+featureTreeId;
|
93
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
94
|
|
95
|
//AUD
|
96
|
attributes = attributes + ",REV,REVTYPE";
|
97
|
sql = "INSERT INTO @@TermCollection_AUD@@ (DTYPE,"+attributes+",id, isFlat, orderRelevant) "
|
98
|
+ " SELECT 'TermTree', "+attributes+","+ maxIdTermVoc +",0,1 FROM @@FeatureTree_AUD@@ WHERE id="+featureTreeId;
|
99
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
100
|
|
101
|
updateSupplement("Annotation", "annotations_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
102
|
updateSupplement("Credit", "credits_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, true);
|
103
|
updateSupplement("Extension", "extensions_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
104
|
updateSupplement("Identifier", "identifiers_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, true);
|
105
|
updateSupplement("Marker", "markers_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
106
|
updateSupplement("OriginalSourceBase", "sources_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
107
|
updateSupplement("Representation", "representations_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
108
|
updateSupplement("RightsInfo", "rights_id", featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result, false);
|
109
|
|
110
|
updateDescriptiveSystem(featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result);
|
111
|
updateTermNode(featureTreeId, maxIdTermVoc, datasource, monitor, caseType, result);
|
112
|
|
113
|
// xx treeIndex update;
|
114
|
|
115
|
|
116
|
|
117
|
return maxIdTermVoc;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* @param featureTreeId
|
122
|
* @param maxIdTermVoc
|
123
|
* @param datasource
|
124
|
* @param monitor
|
125
|
* @param caseType
|
126
|
* @param result
|
127
|
* @throws SQLException
|
128
|
*/
|
129
|
private void updateTermNode(Integer featureTreeId, int maxIdTermVoc, ICdmDataSource datasource,
|
130
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
131
|
String update = "UPDATE @@TermRelation@@ "
|
132
|
+ " SET graph_id = " + maxIdTermVoc
|
133
|
+ " WHERE featureTree_id =" + featureTreeId;
|
134
|
|
135
|
datasource.executeUpdate(caseType.replaceTableNames(update));
|
136
|
|
137
|
update = "UPDATE @@TermRelation_AUD@@ "
|
138
|
+ " SET graph_id = " + maxIdTermVoc
|
139
|
+ " WHERE featureTree_id =" + featureTreeId;
|
140
|
|
141
|
datasource.executeUpdate(caseType.replaceTableNames(update));
|
142
|
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* @param featureTreeId
|
147
|
* @param maxIdTermVoc
|
148
|
* @param datasource
|
149
|
* @param monitor
|
150
|
* @param caseType
|
151
|
* @param result
|
152
|
* @throws SQLException
|
153
|
*/
|
154
|
private void updateDescriptiveSystem(Integer featureTreeId, int maxIdTermVoc, ICdmDataSource datasource,
|
155
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
156
|
String update = "UPDATE @@DescriptiveDataSet@@ "
|
157
|
+ " SET descriptiveSystem_id = " + maxIdTermVoc
|
158
|
+ " WHERE descriptiveSystemOld_id =" + featureTreeId;
|
159
|
|
160
|
datasource.executeUpdate(caseType.replaceTableNames(update));
|
161
|
|
162
|
update = "UPDATE @@DescriptiveDataSet_AUD@@ "
|
163
|
+ " SET descriptiveSystem_id = " + maxIdTermVoc
|
164
|
+ " WHERE descriptiveSystemOld_id =" + featureTreeId;
|
165
|
|
166
|
datasource.executeUpdate(caseType.replaceTableNames(update));
|
167
|
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* @param string
|
172
|
* @param featureTreeId
|
173
|
* @param maxIdTermVoc
|
174
|
* @param datasource
|
175
|
* @param monitor
|
176
|
* @param caseType
|
177
|
* @param result
|
178
|
* @param withSortIndex
|
179
|
* @throws SQLException
|
180
|
*/
|
181
|
private void updateSupplement(String supplement, String attr, Integer featureTreeId, int maxIdTermVoc, ICdmDataSource datasource,
|
182
|
IProgressMonitor monitor, CaseType caseType, SchemaUpdateResult result, boolean withSortIndex) throws SQLException {
|
183
|
|
184
|
if(withSortIndex){
|
185
|
attr = attr + ",sortIndex";
|
186
|
}
|
187
|
String sql = "INSERT INTO @@TermCollection_"+supplement+"@@ (TermCollection_id, "+attr+") "
|
188
|
+ " SELECT FeatureTree_id, "+attr+" FROM @@FeatureTree_"+supplement+"@@ WHERE FeatureTree_id="+featureTreeId;
|
189
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
190
|
|
191
|
sql = "INSERT INTO @@TermCollection_"+supplement+"_AUD@@ (TermCollection_id, "+attr+",REV,REVTYPE) "
|
192
|
+ " SELECT FeatureTree_id, "+attr+",REV,REVTYPE FROM @@FeatureTree_"+supplement+"_AUD@@ WHERE FeatureTree_id="+featureTreeId;
|
193
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
194
|
|
195
|
|
196
|
}
|
197
|
|
198
|
private List<Integer> getFeatureTreeIds(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
|
199
|
SchemaUpdateResult updateResult) throws SQLException {
|
200
|
|
201
|
List<Integer> result = new ArrayList<>();
|
202
|
String sql = "SELECT DISTINCT id FROM " +caseType.transformTo("FeatureTree") + " ORDER BY id";
|
203
|
ResultSet rs = datasource.executeQuery(sql);
|
204
|
while (rs.next()){
|
205
|
Integer id = rs.getInt("id");
|
206
|
result.add(id);
|
207
|
}
|
208
|
|
209
|
sql = "SELECT DISTINCT id FROM " +caseType.transformTo("FeatureTree_AUD") + " ORDER BY id";
|
210
|
rs = datasource.executeQuery(sql);
|
211
|
while (rs.next()){
|
212
|
Integer id = rs.getInt("id");
|
213
|
if (!result.contains(id)){
|
214
|
result.add(id);
|
215
|
}
|
216
|
}
|
217
|
|
218
|
return result;
|
219
|
}
|
220
|
|
221
|
|
222
|
private int getMaxIdTermVoc(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
|
223
|
SchemaUpdateResult result) throws NumberFormatException, SQLException {
|
224
|
String sql = "SELECT max(id) FROM " +caseType.transformTo("TermCollection");
|
225
|
int maxId = Integer.valueOf(datasource.getSingleValue(sql).toString());
|
226
|
sql = "SELECT max(id) FROM " +caseType.transformTo("TermCollection_AUD");
|
227
|
int maxIdAud = Integer.valueOf(datasource.getSingleValue(sql).toString());
|
228
|
return Math.max(maxId, maxIdAud);
|
229
|
}
|
230
|
|
231
|
}
|