1
|
/**
|
2
|
* Copyright (C) 2009 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.log4j.Logger;
|
16
|
|
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
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
24
|
|
25
|
|
26
|
/**
|
27
|
* Updates the CdmPreference NomenclaturalCode #3658
|
28
|
*
|
29
|
* @author a.mueller
|
30
|
* @since 13.10.2016
|
31
|
*/
|
32
|
public class NomenclaturalCodeUpdater extends SchemaUpdaterStepBase {
|
33
|
private static final Logger logger = Logger.getLogger(NomenclaturalCodeUpdater.class);
|
34
|
|
35
|
private static final String stepName = "Update nomenclatural code";
|
36
|
|
37
|
// **************************** STATIC METHODS ********************************/
|
38
|
|
39
|
public static final NomenclaturalCodeUpdater NewInstance(List<ISchemaUpdaterStep> stepList){
|
40
|
NomenclaturalCodeUpdater result = new NomenclaturalCodeUpdater(stepList);
|
41
|
return result;
|
42
|
}
|
43
|
|
44
|
private NomenclaturalCodeUpdater(List<ISchemaUpdaterStep> stepList) {
|
45
|
super(stepList, stepName);
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
50
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
51
|
|
52
|
String existsSql = " SELECT count(*) FROM @@CdmPreference@@ "
|
53
|
+ " WHERE key_predicate = 'model.name.NC' AND key_subject = '/'";
|
54
|
existsSql = caseType.replaceTableNames(existsSql);
|
55
|
if (((Number)datasource.getSingleValue(existsSql)).intValue() > 0 ){
|
56
|
return;
|
57
|
}
|
58
|
|
59
|
String sql = "SELECT count(DTYPE) as n, DTYPE FROM @@TaxonNameBase@@ "
|
60
|
+ " GROUP BY DTYPE ORDER BY count(DTYPE) DESC ";
|
61
|
sql = caseType.replaceTableNames(sql);
|
62
|
ResultSet rs = datasource.executeQuery(sql);
|
63
|
if (rs.next()){
|
64
|
String dtype = rs.getString(2);
|
65
|
NomenclaturalCode code = NomenclaturalCode.fromDtype(dtype);
|
66
|
|
67
|
if (code != null){
|
68
|
String insertSql = " INSERT INTO @@CdmPreference@@ (key_predicate, key_subject, value)"
|
69
|
+ " VALUES ('model.name.NC','/','eu.etaxonomy.cdm.model.name.NomenclaturalCode.%s')";
|
70
|
|
71
|
insertSql = String.format(insertSql, code.getUuid());
|
72
|
insertSql = caseType.replaceTableNames(insertSql);
|
73
|
datasource.executeUpdate(insertSql);
|
74
|
logger.warn("Nomenclatural code updated");
|
75
|
}
|
76
|
}
|
77
|
return;
|
78
|
}
|
79
|
|
80
|
}
|