1 |
33724181
|
Andreas Müller
|
/**
|
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;
|
10 |
|
|
|
11 |
|
|
import java.sql.ResultSet;
|
12 |
|
|
import java.sql.SQLException;
|
13 |
|
|
import java.util.List;
|
14 |
|
|
import java.util.UUID;
|
15 |
|
|
|
16 |
|
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.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 |
|
|
* Updates those representation parts not being passed as <code>null</code>.
|
25 |
|
|
*
|
26 |
|
|
* @author a.mueller
|
27 |
|
|
* @since 27.09.2011
|
28 |
|
|
*/
|
29 |
|
|
public class TermRepresentationUpdater
|
30 |
|
|
extends SchemaUpdaterStepBase {
|
31 |
|
|
|
32 |
|
|
@SuppressWarnings("unused")
|
33 |
|
|
private static final Logger logger = LogManager.getLogger(TermRepresentationUpdater.class);
|
34 |
|
|
|
35 |
|
|
public static final TermRepresentationUpdater NewInstance(List<ISchemaUpdaterStep> stepList,
|
36 |
|
|
String stepName, UUID uuidTerm, String description, String label, String abbrev,
|
37 |
|
|
UUID uuidLanguage){
|
38 |
|
|
return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
|
39 |
|
|
abbrev, uuidLanguage, false, false, false);
|
40 |
|
|
}
|
41 |
|
|
public static final TermRepresentationUpdater NewInstance(List<ISchemaUpdaterStep> stepList,
|
42 |
|
|
String stepName, UUID uuidTerm, String description, String label, String abbrev,
|
43 |
|
|
UUID uuidLanguage, boolean withIdInVoc){
|
44 |
|
|
return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
|
45 |
|
|
abbrev, uuidLanguage, false, false, withIdInVoc);
|
46 |
|
|
}
|
47 |
|
|
public static final TermRepresentationUpdater NewInstanceWithTitleCache(List<ISchemaUpdaterStep> stepList,
|
48 |
|
|
String stepName, UUID uuidTerm, String description, String label, String abbrev,
|
49 |
|
|
UUID uuidLanguage){
|
50 |
|
|
return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
|
51 |
|
|
abbrev, uuidLanguage, false, true, false);
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
public static final TermRepresentationUpdater NewInverseInstance(List<ISchemaUpdaterStep> stepList,
|
55 |
|
|
String stepName, UUID uuidTerm, String description, String label, String abbrev,
|
56 |
|
|
UUID uuidLanguage){
|
57 |
|
|
return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
|
58 |
|
|
abbrev, uuidLanguage, true, false, false);
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
private UUID uuidTerm ;
|
62 |
|
|
private String description;
|
63 |
|
|
private String label;
|
64 |
|
|
private String abbrev;
|
65 |
|
|
private UUID uuidLanguage;
|
66 |
|
|
private boolean isInverse = false;
|
67 |
|
|
private boolean includeTitleCache = false;
|
68 |
|
|
private boolean includeIdInVoc = false;
|
69 |
|
|
|
70 |
|
|
private TermRepresentationUpdater(List<ISchemaUpdaterStep> stepList, String stepName, UUID uuidTerm,
|
71 |
|
|
String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse,
|
72 |
|
|
boolean includeTitleCache, boolean includeIdInVoc) {
|
73 |
|
|
super(stepList, stepName);
|
74 |
|
|
this.abbrev = abbrev;
|
75 |
|
|
this.description = description;
|
76 |
|
|
this.label = label;
|
77 |
|
|
this.uuidTerm = uuidTerm;
|
78 |
|
|
this.uuidLanguage = uuidLanguage;
|
79 |
|
|
this.isInverse = isReverse;
|
80 |
|
|
this.includeTitleCache = includeTitleCache;
|
81 |
|
|
this.includeIdInVoc = includeIdInVoc;
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
@Override
|
85 |
|
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
86 |
|
|
CaseType caseType, SchemaUpdateResult result) throws SQLException{
|
87 |
|
|
|
88 |
|
|
String sqlCheckTermExists = " SELECT count(*) as n FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm + "'";
|
89 |
|
|
|
90 |
|
|
Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
|
91 |
|
|
if (n == 0){
|
92 |
|
|
String name = label != null ? label : abbrev != null ? abbrev : description;
|
93 |
|
|
String message = "Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")";
|
94 |
|
|
monitor.warning(message);
|
95 |
|
|
result.addWarning(message, this, "invoke");
|
96 |
|
|
return;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
//language id
|
100 |
|
|
Integer langId = null;
|
101 |
|
|
if (uuidLanguage != null){
|
102 |
|
|
langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
|
103 |
|
|
if (langId == null){
|
104 |
|
|
String message = "Language for language uuid (%s) could not be found. Term representations not updated.";
|
105 |
|
|
message = String.format(message, uuidLanguage.toString());
|
106 |
|
|
monitor.warning(message);
|
107 |
|
|
result.addWarning(message, this, "invoke");
|
108 |
|
|
return;
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
Integer repId = getRepresentationId(datasource, monitor, langId, caseType);
|
113 |
|
|
if (repId == null){
|
114 |
|
|
String message = "repId is null";
|
115 |
|
|
result.addWarning(message, this, "invoke");
|
116 |
|
|
return;
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
//standard representation
|
120 |
|
|
String sqlUpdateRepresentationFormat = " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
|
121 |
|
|
sqlUpdateRepresentationFormat = caseType.replaceTableNames(sqlUpdateRepresentationFormat);
|
122 |
|
|
if (description != null){
|
123 |
|
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
|
124 |
|
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
125 |
|
|
}
|
126 |
|
|
if (label != null){
|
127 |
|
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
|
128 |
|
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
129 |
|
|
}
|
130 |
|
|
if (abbrev != null){
|
131 |
|
|
String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
|
132 |
|
|
datasource.executeUpdate(sqlUpdateRepresentation);
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
if (includeTitleCache && label != null){
|
136 |
|
|
String sql = "UPDATE %s SET titleCache = '%s' WHERE uuid = '%s'";
|
137 |
|
|
sql = String.format(sql, caseType.transformTo("DefinedTermBase"), label, uuidTerm);
|
138 |
|
|
datasource.executeUpdate(sql);
|
139 |
|
|
}
|
140 |
|
|
if (includeIdInVoc && abbrev != null){
|
141 |
|
|
String sql = "UPDATE %s SET idInVocabulary = '%s' WHERE uuid = '%s'";
|
142 |
|
|
sql = String.format(sql, caseType.transformTo("DefinedTermBase"), abbrev, uuidTerm);
|
143 |
|
|
datasource.executeUpdate(sql);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
return;
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
private Integer getRepresentationId(ICdmDataSource datasource,
|
152 |
|
|
IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
|
153 |
|
|
//representation
|
154 |
|
|
|
155 |
|
|
String tableName = isInverse ? "DefinedTermBase_InverseRepresentation" : "DefinedTermBase_Representation" ;
|
156 |
|
|
String repIdFkCol = isInverse ? "inverserepresentations_id" : "representations_id";
|
157 |
|
|
String sqlId = " SELECT rep.id " +
|
158 |
|
|
" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " +
|
159 |
|
|
" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
|
160 |
|
|
" WHERE dtb.uuid = '%s' ";
|
161 |
|
|
tableName = caseType.transformTo(tableName);
|
162 |
|
|
sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
|
163 |
|
|
sqlId = caseType.replaceTableNames(sqlId);
|
164 |
|
|
if (uuidLanguage != null){
|
165 |
|
|
sqlId += " AND rep.language_id = " + langId;
|
166 |
|
|
}
|
167 |
|
|
ResultSet rs = datasource.executeQuery(sqlId);
|
168 |
|
|
Integer repId;
|
169 |
|
|
if (rs.next()){
|
170 |
|
|
repId = rs.getInt("id");
|
171 |
|
|
}else{
|
172 |
|
|
String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
|
173 |
|
|
warning = String.format(warning, uuidTerm.toString());
|
174 |
|
|
monitor.warning(warning);
|
175 |
|
|
repId = null;
|
176 |
|
|
}
|
177 |
|
|
return repId;
|
178 |
|
|
}
|
179 |
62ea9850
|
Andreas Müller
|
}
|