Revision eed1ac3d
Added by Andreas Müller almost 6 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/update/TermRepresentationAdder.java | ||
---|---|---|
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; |
|
10 |
|
|
11 |
import java.sql.ResultSet; |
|
12 |
import java.sql.SQLException; |
|
13 |
import java.util.UUID; |
|
14 |
|
|
15 |
import org.apache.log4j.Logger; |
|
16 |
|
|
17 |
import eu.etaxonomy.cdm.common.CdmUtils; |
|
18 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
|
19 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
20 |
|
|
21 |
/** |
|
22 |
* Class for adding a term representations. |
|
23 |
* |
|
24 |
* Untested!! |
|
25 |
* |
|
26 |
* @author a.mueller |
|
27 |
* @since 11.05.2018 |
|
28 |
* |
|
29 |
*/ |
|
30 |
public class TermRepresentationAdder |
|
31 |
extends SchemaUpdaterStepBase { |
|
32 |
|
|
33 |
@SuppressWarnings("unused") |
|
34 |
private static final Logger logger = Logger.getLogger(TermRepresentationAdder.class); |
|
35 |
|
|
36 |
public static final TermRepresentationAdder NewInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){ |
|
37 |
return new TermRepresentationAdder(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false); |
|
38 |
} |
|
39 |
|
|
40 |
public static final TermRepresentationAdder NewReverseInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){ |
|
41 |
return new TermRepresentationAdder(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true); |
|
42 |
} |
|
43 |
|
|
44 |
private UUID uuidTerm ; |
|
45 |
private String description; |
|
46 |
private String label; |
|
47 |
private String abbrev; |
|
48 |
private UUID uuidLanguage; |
|
49 |
private boolean isReverse = false; |
|
50 |
|
|
51 |
private TermRepresentationAdder(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse) { |
|
52 |
super(stepName); |
|
53 |
this.abbrev = abbrev; |
|
54 |
this.description = description; |
|
55 |
this.label = label; |
|
56 |
this.uuidTerm = uuidTerm; |
|
57 |
this.uuidLanguage = uuidLanguage; |
|
58 |
this.isReverse = isReverse; |
|
59 |
} |
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
@Override |
|
64 |
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor, |
|
65 |
CaseType caseType, SchemaUpdateResult result) throws SQLException{ |
|
66 |
|
|
67 |
Integer termId; |
|
68 |
String sqlCheckTermExists = " SELECT id FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm + "'"; |
|
69 |
ResultSet rs = datasource.executeQuery(caseType.replaceTableNames(sqlCheckTermExists)); |
|
70 |
if (rs.next() == false){ |
|
71 |
String name = label != null ? label : abbrev != null ? abbrev : description; |
|
72 |
String message = "Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")"; |
|
73 |
monitor.warning(message); |
|
74 |
result.addError(message, this, "invoke"); |
|
75 |
return; |
|
76 |
}else{ |
|
77 |
termId = rs.getInt("id"); |
|
78 |
} |
|
79 |
|
|
80 |
//language id |
|
81 |
Integer langId = null; |
|
82 |
if (uuidLanguage != null){ |
|
83 |
langId = getLanguageId(uuidLanguage, datasource, monitor, caseType); |
|
84 |
if (langId == null){ |
|
85 |
String message = "Language for language uuid (%s) could not be found. Term representations not updated."; |
|
86 |
message = String.format(message, uuidLanguage.toString()); |
|
87 |
monitor.warning(message); |
|
88 |
result.addError(message, this, "invoke"); |
|
89 |
return; |
|
90 |
} |
|
91 |
} |
|
92 |
|
|
93 |
Integer repId = getRepresentationId(datasource, monitor, langId, caseType); |
|
94 |
if (repId != null){ |
|
95 |
String message = "Representation for language uuid (%s) already exists. Did not add term representation."; |
|
96 |
message = String.format(message, uuidLanguage.toString()); |
|
97 |
monitor.warning(message); |
|
98 |
result.addError(message, this, "invoke"); |
|
99 |
return; |
|
100 |
} |
|
101 |
|
|
102 |
String query = " SELECT max(id) id FROM @@Representation@@ "; |
|
103 |
query = caseType.replaceTableNames(query); |
|
104 |
repId = (Integer)datasource.getSingleValue(query) + 1; |
|
105 |
|
|
106 |
query = " INSERT INTO @@Representation@@(id, uuid, created, label, text, abbreviatedLabel, language_id) " |
|
107 |
+ " VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d ) "; |
|
108 |
//TODO created |
|
109 |
query = String.format(query, repId, UUID.randomUUID(), getNowString(), label, description, abbrev, langId); |
|
110 |
query = caseType.replaceTableNames(query); |
|
111 |
datasource.executeUpdate(query); |
|
112 |
|
|
113 |
String tableName = isReverse ? "TermBase_inverseRepresentation" : "DefinedTermBase_Representation" ; |
|
114 |
String repIdCol = isReverse ? "inverserepresentations_id" : "representations_id"; |
|
115 |
String termCol = isReverse ? "term_id" : "DefinedTermBase_id"; |
|
116 |
query = " INSERT INTO %s (%s, %s) " |
|
117 |
+ " VALUES (%d, %d) "; |
|
118 |
tableName = caseType.transformTo(tableName); |
|
119 |
query = String.format(query, tableName, termCol, repIdCol, termId, repId); |
|
120 |
datasource.executeQuery(query); |
|
121 |
|
|
122 |
return; |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* @param datasource |
|
127 |
* @param monitor |
|
128 |
* @param langId |
|
129 |
* @param caseType |
|
130 |
* @return |
|
131 |
* @throws SQLException |
|
132 |
*/ |
|
133 |
private Integer getRepresentationId(ICdmDataSource datasource, |
|
134 |
IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException { |
|
135 |
//representation |
|
136 |
|
|
137 |
String tableName = isReverse ? "RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ; |
|
138 |
String repIdFkCol = isReverse ? "inverserepresentations_id" : "representations_id"; |
|
139 |
String sqlId = " SELECT rep.id " + |
|
140 |
" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " + |
|
141 |
" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " + |
|
142 |
" WHERE dtb.uuid = '%s' "; |
|
143 |
tableName = caseType.transformTo(tableName); |
|
144 |
sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString()); |
|
145 |
sqlId = caseType.replaceTableNames(sqlId); |
|
146 |
if (uuidLanguage != null){ |
|
147 |
sqlId += " AND rep.language_id = " + langId; |
|
148 |
} |
|
149 |
ResultSet rs = datasource.executeQuery(sqlId); |
|
150 |
Integer repId; |
|
151 |
if (rs.next()){ |
|
152 |
repId = rs.getInt("id"); |
|
153 |
}else{ |
|
154 |
repId = null; |
|
155 |
} |
|
156 |
return repId; |
|
157 |
} |
|
158 |
|
|
159 |
} |
Also available in: Unified diff
add TermRepresentationAdder (not needed yet, untested) implemented for #7334