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.database.update.v515_518;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
18
|
|
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.model.description.Feature;
|
26
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
27
|
|
28
|
/**
|
29
|
* #9124
|
30
|
*
|
31
|
* This schema update step class is to correct an error introduced by the
|
32
|
* first implementation of class {@link RecommendedMeasurementUnitAdder}
|
33
|
* which added the recommendedMeasurmentUnit for altitudes to table
|
34
|
* DefinedTermBase_StatisticalMeasure and not to table DefinedTermBase_MeasurementUnit.
|
35
|
*
|
36
|
* @author a.mueller
|
37
|
* @since 24.09.2020
|
38
|
*/
|
39
|
public class AltitudeMeterCorrector extends SchemaUpdaterStepBase {
|
40
|
|
41
|
@SuppressWarnings("unused")
|
42
|
private static final Logger logger = LogManager.getLogger(AltitudeMeterCorrector.class);
|
43
|
|
44
|
private final UUID uuidFeature = Feature.uuidAltitude;
|
45
|
private final UUID uuidUnit = MeasurementUnit.uuidMeter;
|
46
|
private static final String stepName = "Correct altitude recommended measurement unit";
|
47
|
|
48
|
public static final AltitudeMeterCorrector NewInstance(List<ISchemaUpdaterStep> stepList){
|
49
|
AltitudeMeterCorrector result = new AltitudeMeterCorrector(stepList);
|
50
|
return result;
|
51
|
}
|
52
|
|
53
|
protected AltitudeMeterCorrector(List<ISchemaUpdaterStep> stepList) {
|
54
|
super(stepList, stepName);
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public List<ISchemaUpdaterStep> getInnerSteps() {
|
59
|
List<ISchemaUpdaterStep> result = new ArrayList<>();
|
60
|
return result;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
|
65
|
SchemaUpdateResult result) throws SQLException {
|
66
|
|
67
|
boolean includeAudit = true;
|
68
|
|
69
|
//remove incorrect entry
|
70
|
String sql = "SELECT MN.feature_id, MN.recommendedstatisticalmeasures_id "
|
71
|
+ " FROM @@DefinedTermBase_StatisticalMeasure@@ MN "
|
72
|
+ " INNER JOIN @@DefinedTermBase@@ alt ON alt.id = MN.feature_id "
|
73
|
+ " INNER JOIN @@DefinedTermBase@@ sm ON sm.id = MN.recommendedstatisticalmeasures_id "
|
74
|
+ " WHERE alt.uuid = '"+uuidFeature+"' AND sm.uuid = '"+uuidUnit+"'";
|
75
|
sql = caseType.replaceTableNames(sql);
|
76
|
ResultSet rs = datasource.executeQuery(caseType.replaceTableNames(sql));
|
77
|
while (rs.next()){
|
78
|
int featureId = rs.getInt("feature_id");
|
79
|
int unitId = rs.getInt("recommendedstatisticalmeasures_id");
|
80
|
sql = "DELETE FROM @@DefinedTermBase_StatisticalMeasure@@ "
|
81
|
+ " WHERE feature_id = "+featureId+" AND recommendedstatisticalmeasures_id = " + unitId;
|
82
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
83
|
|
84
|
sql = "SELECT count(*) "
|
85
|
+ "FROM @@DefinedTermBase_MeasurementUnit@@ "
|
86
|
+ " WHERE Feature_id = "+featureId+" AND recommendedMeasurementUnits_id = " + unitId;
|
87
|
Long count = (Long)datasource.getSingleValue(caseType.replaceTableNames(sql));
|
88
|
if (count > 0){
|
89
|
return;
|
90
|
}
|
91
|
|
92
|
//insert records
|
93
|
sql = "INSERT INTO @@DefinedTermBase_MeasurementUnit@@ (Feature_id, recommendedMeasurementUnits_id)"
|
94
|
+ " VALUES (" + featureId + "," + unitId + ")";
|
95
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
96
|
|
97
|
//audit
|
98
|
if (includeAudit){
|
99
|
sql = "DELETE FROM @@DefinedTermBase_StatisticalMeasure_AUD@@ "
|
100
|
+ " WHERE feature_id = "+featureId+" AND recommendedstatisticalmeasures_id = " + unitId;
|
101
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
102
|
Integer rev;
|
103
|
try {
|
104
|
sql = "SELECT MAX(REV) "
|
105
|
+ " FROM "+caseType.transformTo("DefinedTermBase_AUD")+" t "
|
106
|
+ " WHERE t.uuid = '"+uuidFeature+"'";
|
107
|
rev = (Integer)datasource.getSingleValue(sql);
|
108
|
} catch (Exception e) {
|
109
|
//TODO we could also create a new AUDIT event
|
110
|
result.addWarning("Revision number for adding measurement unit to feature could not be defined. Adding is not audited.", this, "");
|
111
|
includeAudit = false;
|
112
|
return;
|
113
|
}
|
114
|
sql = "INSERT INTO @@DefinedTermBase_MeasurementUnit_AUD@@ (REV, Feature_id, recommendedMeasurementUnits_id, REVTYPE)"
|
115
|
+ " VALUES ("+rev+"," + featureId + "," + unitId + ","+0+")";
|
116
|
datasource.executeUpdate(caseType.replaceTableNames(sql));
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
}
|