1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.database.update;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.UUID;
|
15
|
|
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
|
|
22
|
/**
|
23
|
* Class for updating term representations.
|
24
|
* @author a.mueller
|
25
|
* @date 27.09.2011
|
26
|
*
|
27
|
*/
|
28
|
public class TermRepresentationUpdater extends SchemaUpdaterStepBase<TermRepresentationUpdater> implements ITermUpdaterStep{
|
29
|
@SuppressWarnings("unused")
|
30
|
private static final Logger logger = Logger.getLogger(TermRepresentationUpdater.class);
|
31
|
|
32
|
public static final TermRepresentationUpdater NewInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){
|
33
|
return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false);
|
34
|
}
|
35
|
|
36
|
public static final TermRepresentationUpdater NewReverseInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){
|
37
|
return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true);
|
38
|
}
|
39
|
|
40
|
private UUID uuidTerm ;
|
41
|
private String description;
|
42
|
private String label;
|
43
|
private String abbrev;
|
44
|
private UUID uuidLanguage;
|
45
|
private boolean isReverse = false;
|
46
|
|
47
|
private TermRepresentationUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse) {
|
48
|
super(stepName);
|
49
|
this.abbrev = abbrev;
|
50
|
this.description = description;
|
51
|
this.label = label;
|
52
|
this.uuidTerm = uuidTerm;
|
53
|
this.uuidLanguage = uuidLanguage;
|
54
|
this.isReverse = isReverse;
|
55
|
}
|
56
|
|
57
|
|
58
|
|
59
|
public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{
|
60
|
|
61
|
String sqlCheckTermExists = " SELECT count(*) as n FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm + "'";
|
62
|
|
63
|
Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
|
64
|
if (n == 0){
|
65
|
String name = label != null ? label : abbrev != null ? abbrev : description;
|
66
|
monitor.warning("Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")");
|
67
|
return null;
|
68
|
}
|
69
|
|
70
|
//language id
|
71
|
Integer langId = null;
|
72
|
if (uuidLanguage != null){
|
73
|
langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
|
74
|
if (langId == null){
|
75
|
String warning = "Language for language uuid (%s) could not be found. Term representations not updated.";
|
76
|
warning = String.format(warning, uuidLanguage.toString());
|
77
|
monitor.warning(warning);
|
78
|
return null;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
Integer repId = getRepresentationId(datasource, monitor, langId, caseType);
|
83
|
if (repId == null){
|
84
|
return null;
|
85
|
}
|
86
|
|
87
|
//standard representation
|
88
|
String sqlUpdateRepresentationFormat = " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
|
89
|
sqlUpdateRepresentationFormat = caseType.replaceTableNames(sqlUpdateRepresentationFormat);
|
90
|
if (description != null){
|
91
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
|
92
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
93
|
}
|
94
|
if (label != null){
|
95
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
|
96
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
97
|
}
|
98
|
if (abbrev != null){
|
99
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
|
100
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
101
|
}
|
102
|
|
103
|
return repId;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* @param datasource
|
108
|
* @param monitor
|
109
|
* @param langId
|
110
|
* @param caseType
|
111
|
* @return
|
112
|
* @throws SQLException
|
113
|
*/
|
114
|
private Integer getRepresentationId(ICdmDataSource datasource,
|
115
|
IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
|
116
|
//representation
|
117
|
|
118
|
String tableName = isReverse ? "RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ;
|
119
|
String repIdFkCol = isReverse ? "inverserepresentations_id" : "representations_id";
|
120
|
String sqlId = " SELECT rep.id " +
|
121
|
" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " +
|
122
|
" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
|
123
|
" WHERE dtb.uuid = '%s' ";
|
124
|
tableName = caseType.transformTo(tableName);
|
125
|
sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
|
126
|
sqlId = caseType.replaceTableNames(sqlId);
|
127
|
if (uuidLanguage != null){
|
128
|
sqlId += " AND rep.language_id = " + langId;
|
129
|
}
|
130
|
ResultSet rs = datasource.executeQuery(sqlId);
|
131
|
Integer repId;
|
132
|
if (rs.next()){
|
133
|
repId = rs.getInt("id");
|
134
|
}else{
|
135
|
String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
|
136
|
warning = String.format(warning, uuidTerm.toString());
|
137
|
monitor.warning(warning);
|
138
|
repId = null;
|
139
|
}
|
140
|
return repId;
|
141
|
}
|
142
|
|
143
|
}
|