Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
1 90b75b52 Andreas Müller
/**
2
* Copyright (C) 2009 EDIT
3 16313054 Patric Plitzner
* European Distributed Institute of Taxonomy
4 90b75b52 Andreas Müller
* http://www.e-taxonomy.eu
5 16313054 Patric Plitzner
*
6 90b75b52 Andreas Müller
* 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 bd1d3bee Andreas Müller
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
18 90b75b52 Andreas Müller
import eu.etaxonomy.cdm.database.ICdmDataSource;
19 cb61174c Andreas Müller
import eu.etaxonomy.cdm.model.common.TermType;
20 55356795 Andreas Müller
import eu.etaxonomy.cdm.model.description.Feature;
21 cb61174c Andreas Müller
import eu.etaxonomy.cdm.model.name.Rank;
22
import eu.etaxonomy.cdm.model.name.RankClass;
23 90b75b52 Andreas Müller
24
/**
25 b8833305 Andreas Müller
 * Creates a new term if a term with the same given uuid does not exist yet
26 90b75b52 Andreas Müller
 * @author a.mueller
27 53db84af Andreas Müller
 * @since 10.09.2010
28 90b75b52 Andreas Müller
 *
29
 */
30 65fbcddb Andreas Müller
public class SingleTermUpdater extends SchemaUpdaterStepBase {
31 90b75b52 Andreas Müller
	@SuppressWarnings("unused")
32
	private static final Logger logger = Logger.getLogger(SingleTermUpdater.class);
33 16313054 Patric Plitzner
34 cb61174c Andreas Müller
	/**
35
	 * @Deprecated use {@link #NewInstance(String, TermType, UUID, String, String, String, String, UUID, UUID, boolean, UUID)} instead
36
	 */
37
	@Deprecated
38 55356795 Andreas Müller
	public static final SingleTermUpdater NewInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, String dtype, UUID uuidVocabulary, UUID uuidLanguage, boolean isOrdered, UUID uuidAfterTerm){
39 16313054 Patric Plitzner
		return new SingleTermUpdater(stepName, null, uuidTerm, null, description, label, abbrev, dtype, uuidVocabulary, uuidLanguage, isOrdered, uuidAfterTerm);
40 cb61174c Andreas Müller
	}
41
42
	public static final SingleTermUpdater NewInstance(String stepName, TermType termType, UUID uuidTerm, String idInVocabulary, String description,  String label, String abbrev, String dtype, UUID uuidVocabulary, UUID uuidLanguage, boolean isOrdered, UUID uuidAfterTerm){
43 16313054 Patric Plitzner
		return new SingleTermUpdater(stepName, termType, uuidTerm, idInVocabulary, description, label, abbrev, dtype, uuidVocabulary, uuidLanguage, isOrdered, uuidAfterTerm);
44 90b75b52 Andreas Müller
	}
45 cb61174c Andreas Müller
46 16313054 Patric Plitzner
47
	private final UUID uuidTerm ;
48
	private final String description;
49
	private final String label;
50
	private final String abbrev;
51
	private final String dtype;
52
	private final UUID uuidVocabulary;
53
	private final boolean isOrdered;
54
	private final UUID uuidAfterTerm;
55
	private final UUID uuidLanguage;
56 9fcf1181 Andreas Müller
	private String reverseDescription;
57
	private String reverseLabel;
58
	private String reverseAbbrev;
59 cb61174c Andreas Müller
	private RankClass rankClass;
60 16313054 Patric Plitzner
	private final TermType termType;
61
	private final String idInVocabulary;
62 e4ef4d83 Andreas Müller
	private boolean symmetric = false;
63 f966cf6d Andreas Müller
	private boolean transitive = false;
64 16313054 Patric Plitzner
65
66 90b75b52 Andreas Müller
67 cb61174c Andreas Müller
	private SingleTermUpdater(String stepName, TermType termType, UUID uuidTerm, String idInVocabulary, String description, String label, String abbrev, String dtype, UUID uuidVocabulary, UUID uuidLanguage, boolean isOrdered, UUID uuidAfterTerm) {
68 b50514ac Andreas Müller
		super(stepName);
69 cb61174c Andreas Müller
		this.termType = termType;
70
		this.idInVocabulary = idInVocabulary;
71 90b75b52 Andreas Müller
		this.abbrev = abbrev;
72
		this.description = description;
73
		this.dtype = dtype;
74
		this.label = label;
75 55356795 Andreas Müller
		this.isOrdered = isOrdered;
76 90b75b52 Andreas Müller
		this.uuidTerm = uuidTerm;
77
		this.uuidVocabulary = uuidVocabulary;
78 55356795 Andreas Müller
		this.uuidAfterTerm = uuidAfterTerm;
79
		this.uuidLanguage = uuidLanguage;
80 90b75b52 Andreas Müller
	}
81
82 ba35e2f8 Andreas Müller
    @Override
83
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
84
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
85
		String sqlCheckTermExists = " SELECT count(*) as n " +
86 7747019c Andreas Müller
 				" FROM " + caseType.transformTo("DefinedTermBase") +
87
 				" WHERE uuid = '" + uuidTerm + "'";
88 316d5d5d Andreas Müller
		Long n = (Long)datasource.getSingleValue(sqlCheckTermExists);
89
		if (n != 0){
90 ba35e2f8 Andreas Müller
		    String message ="Term already exists: " + label + "(" + uuidTerm + ")";
91
			monitor.warning(message);
92 e85167f1 Andreas Müller
			result.addWarning(message, (String)null, getStepName());
93 ba35e2f8 Andreas Müller
			return;
94 316d5d5d Andreas Müller
		}
95 16313054 Patric Plitzner
96 90b75b52 Andreas Müller
		//vocabulary id
97
		int vocId;
98 7747019c Andreas Müller
		String sqlVocId = " SELECT id " +
99
				" FROM  " + caseType.transformTo("TermVocabulary") +
100
				" WHERE uuid = '" + uuidVocabulary + "'";
101 90b75b52 Andreas Müller
		ResultSet rs = datasource.executeQuery(sqlVocId);
102
		if (rs.next()){
103
			vocId = rs.getInt("id");
104
		}else{
105 ba35e2f8 Andreas Müller
			String message = "Vocabulary ( "+ uuidVocabulary +" ) for term does not exist!";
106
			monitor.warning(message);
107
			result.addError(message, getStepName() + ", SingleTermUpdater.invoke");
108
            return;
109 90b75b52 Andreas Müller
		}
110 16313054 Patric Plitzner
111 55356795 Andreas Müller
		Integer termId;
112 7747019c Andreas Müller
		String sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("DefinedTermBase");
113 90b75b52 Andreas Müller
		rs = datasource.executeQuery(sqlMaxId);
114
		if (rs.next()){
115
			termId = rs.getInt("maxId");
116
		}else{
117 ba35e2f8 Andreas Müller
			String message = "No defined terms do exist yet. Can't update terms!";
118
			monitor.warning(message);
119
			result.addError(message, getStepName() + ", SingleTermUpdater.invoke");
120
            return;
121 90b75b52 Andreas Müller
		}
122 16313054 Patric Plitzner
123 90b75b52 Andreas Müller
		String id = Integer.toString(termId);
124 648a8e05 Andreas Müller
		String created = getNowString();
125 90b75b52 Andreas Müller
		String defaultColor = "null";
126 55356795 Andreas Müller
		String protectedTitleCache = getBoolean(false, datasource);
127
		String orderIndex;
128
		if (isOrdered){
129 7747019c Andreas Müller
			orderIndex = getOrderIndex(datasource, vocId, monitor, caseType);
130 55356795 Andreas Müller
		}else{
131
			orderIndex = "null";
132
		}
133 90b75b52 Andreas Müller
		String titleCache = label != null ? label : (abbrev != null ? abbrev : description );
134 cb61174c Andreas Müller
		String idInVocStr = idInVocabulary == null ? "NULL" : "'" + idInVocabulary + "'";
135 7747019c Andreas Müller
		String sqlInsertTerm = " INSERT INTO @@DefinedTermBase@@ (DTYPE, id, uuid, created, termtype, idInVocabulary, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)" +
136 16313054 Patric Plitzner
				"VALUES ('" + dtype + "', " + id + ", '" + uuidTerm + "', '" + created + "', '" + termType.getKey() + "', " + idInVocStr +  ", " + protectedTitleCache + ", '" + titleCache + "', " + orderIndex + ", " + defaultColor + ", " + vocId + ")";
137
138 7747019c Andreas Müller
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertTerm));
139 16313054 Patric Plitzner
140 7747019c Andreas Müller
		updateFeatureTerms(termId, datasource, monitor, caseType);
141
		updateRelationshipTerms(termId, datasource, monitor, caseType);
142
		updateRanks(termId, datasource, monitor, caseType);
143 16313054 Patric Plitzner
144 90b75b52 Andreas Müller
//
145 16313054 Patric Plitzner
//		INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)
146 90b75b52 Andreas Müller
//		SELECT 'ReferenceSystem' ,  (@defTermId := max(id)+1)  as maxId , '1bb67042-2814-4b09-9e76-c8c1e68aa281', '2010-06-01 10:15:00', b'0', 'Google Earth', null, null, @refSysVocId
147
//		FROM DefinedTermBase ;
148
//
149
150
		//language id
151 7747019c Andreas Müller
		Integer langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
152 6d4b8d16 Andreas Müller
		if (langId == null){
153 ba35e2f8 Andreas Müller
			String message = "LangId is null";
154 e85167f1 Andreas Müller
			result.addWarning(message);
155 ba35e2f8 Andreas Müller
		    return;
156 90b75b52 Andreas Müller
		}
157 16313054 Patric Plitzner
158 90b75b52 Andreas Müller
		//representation
159
		int repId;
160 7747019c Andreas Müller
		sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("Representation");
161 90b75b52 Andreas Müller
		rs = datasource.executeQuery(sqlMaxId);
162
		if (rs.next()){
163
			repId = rs.getInt("maxId");
164
		}else{
165 ba35e2f8 Andreas Müller
			String message = "No representations do exist yet. Can't update terms!";
166
			monitor.warning(message);
167
			result.addError(message, this, "invoke");
168
			return;
169 90b75b52 Andreas Müller
		}
170 16313054 Patric Plitzner
171 9fcf1181 Andreas Müller
		//standard representation
172 90b75b52 Andreas Müller
		UUID uuidRepresentation = UUID.randomUUID();
173 7747019c Andreas Müller
		String sqlInsertRepresentation = " INSERT INTO @@Representation@@ (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
174 16313054 Patric Plitzner
				"VALUES (" + repId + ", '" + created + "', '" + uuidRepresentation + "', " + nullSafeStr(description) +  ", " +nullSafeStr( label) +  ", " + nullSafeStr(abbrev) +  ", " + langId + ")";
175
176 7747019c Andreas Müller
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertRepresentation));
177 16313054 Patric Plitzner
178
		String sqlInsertMN = "INSERT INTO @@DefinedTermBase_Representation@@ (DefinedTermBase_id, representations_id) " +
179
				" VALUES ("+ termId +"," +repId+ " )";
180
181 7747019c Andreas Müller
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertMN));
182 16313054 Patric Plitzner
183 9fcf1181 Andreas Müller
		//reverse representation
184
		if (hasReverseRepresentation()){
185
			int reverseRepId = repId + 1;
186
			UUID uuidReverseRepresentation = UUID.randomUUID();
187 7747019c Andreas Müller
			String sqlInsertReverseRepresentation = " INSERT INTO @@Representation@@ (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
188 16313054 Patric Plitzner
					"VALUES (" + reverseRepId + ", '" + created + "', '" + uuidReverseRepresentation + "', " + nullSafeStr(reverseDescription) +  ", " + nullSafeStr(reverseLabel) +  ",  " + nullSafeStr(reverseAbbrev) +  ", " + langId + ")";
189
190 7747019c Andreas Müller
			datasource.executeUpdate(caseType.replaceTableNames(sqlInsertReverseRepresentation));
191 16313054 Patric Plitzner
192
			String sqlReverseInsertMN = "INSERT INTO @@RelationshipTermBase_inverseRepresentation@@ (DefinedTermBase_id, inverserepresentations_id) " +
193
					" VALUES ("+ termId +"," +reverseRepId+ " )";
194
195 7747019c Andreas Müller
			datasource.executeUpdate(caseType.replaceTableNames(sqlReverseInsertMN));
196 16313054 Patric Plitzner
		}
197
198 ba35e2f8 Andreas Müller
		return;
199 55356795 Andreas Müller
	}
200
201 2881a717 Andreas Müller
	private String nullSafeStr(String str) {
202
		if (str == null){
203
			return " NULL ";
204
		}else{
205
			return "'" + str + "'";
206
		}
207
	}
208
209 7747019c Andreas Müller
	private void updateFeatureTerms(Integer termId, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
210 55356795 Andreas Müller
		if (dtype.equals(Feature.class.getSimpleName())){
211 16313054 Patric Plitzner
			String sqlUpdate = "UPDATE  " + caseType.transformTo("DefinedTermBase") +
212
				" SET " +
213
				" supportscategoricaldata = " + getBoolean(false, datasource) + ", " +
214
				" supportscommontaxonname = " + getBoolean(false, datasource) + ", " +
215 55356795 Andreas Müller
				" supportsdistribution = " + getBoolean(false, datasource) + ", " +
216
				" supportsindividualassociation = " + getBoolean(false, datasource) + ", " +
217
				" supportsquantitativedata = " + getBoolean(false, datasource) + ", " +
218
				" supportstaxoninteraction = " + getBoolean(false, datasource) + ", " +
219
				" supportstextdata = " + getBoolean(true, datasource) +  " " +
220
				" WHERE id = " + termId;
221
			datasource.executeUpdate(sqlUpdate);
222
		}
223
	}
224
225 7747019c Andreas Müller
	private void updateRelationshipTerms(Integer termId, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
226 e4ef4d83 Andreas Müller
		if (dtype.contains("Relationship")){
227 16313054 Patric Plitzner
			String sqlUpdate = "UPDATE "  + caseType.transformTo("DefinedTermBase") +
228
				" SET " +
229
				" symmetrical = " + getBoolean(symmetric, datasource) + ", " +
230
				" transitive = " + getBoolean(transitive, datasource) + " " +
231 e4ef4d83 Andreas Müller
				" WHERE id = " + termId;
232
			datasource.executeUpdate(sqlUpdate);
233
		}
234
	}
235 16313054 Patric Plitzner
236 7747019c Andreas Müller
	private void updateRanks(Integer termId, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
237 cb61174c Andreas Müller
		if (dtype.equals(Rank.class.getSimpleName())){
238 16313054 Patric Plitzner
			String sqlUpdate = "UPDATE " + caseType.transformTo("DefinedTermBase") +
239
				" SET rankClass = '" + rankClass.getKey() + "'" +
240 cb61174c Andreas Müller
				" WHERE id = " + termId;
241
			datasource.executeUpdate(sqlUpdate);
242
		}
243
	}
244 16313054 Patric Plitzner
245 cb61174c Andreas Müller
	public SingleTermUpdater setRankClass(RankClass rankClass) {
246
		this.rankClass = rankClass;
247
		return this;
248
	}
249 55356795 Andreas Müller
250
251
252
	/**
253
	 * @param datasource
254
	 * @param vocId
255
	 * @param monitor
256 16313054 Patric Plitzner
	 * @param caseType
257 55356795 Andreas Müller
	 * @return
258
	 * @throws SQLException
259
	 */
260 7747019c Andreas Müller
	private String getOrderIndex(ICdmDataSource datasource, int vocId, IProgressMonitor monitor, CaseType caseType) throws SQLException {
261 55356795 Andreas Müller
		ResultSet rs;
262
		Integer intOrderIndex = null;
263 1d36aa54 Andreas Müller
		if (uuidAfterTerm == null){
264
			return "1";
265
		}
266 7747019c Andreas Müller
		String sqlOrderIndex = " SELECT orderindex FROM %s WHERE uuid = '%s' AND vocabulary_id = %d ";
267
		sqlOrderIndex = String.format(sqlOrderIndex, caseType.transformTo("DefinedTermBase"), uuidAfterTerm.toString(), vocId);
268 55356795 Andreas Müller
		rs = datasource.executeQuery(sqlOrderIndex);
269
		if (rs.next()){
270
			intOrderIndex = rs.getInt("orderindex") + 1;
271 16313054 Patric Plitzner
272 7747019c Andreas Müller
			String sqlUpdateLowerTerms = "UPDATE %s SET orderindex = orderindex + 1 WHERE vocabulary_id = %d AND orderindex >= %d ";
273 16313054 Patric Plitzner
			sqlUpdateLowerTerms = String.format(sqlUpdateLowerTerms, caseType.transformTo("DefinedTermBase"), vocId, intOrderIndex );
274 55356795 Andreas Müller
			datasource.executeUpdate(sqlUpdateLowerTerms);
275
		}else{
276
			String warning = "The previous term has not been found in vocabulary. Put term to the end";
277
			monitor.warning(warning);
278
		}
279
		if (intOrderIndex == null){
280 7747019c Andreas Müller
			String sqlMaxOrderIndex = " SELECT max(orderindex) FROM %s WHERE vocabulary_id = %d";
281
			sqlMaxOrderIndex = String.format(sqlMaxOrderIndex, caseType.transformTo("DefinedTermBase"), vocId);
282 55356795 Andreas Müller
			intOrderIndex = (Integer)datasource.getSingleValue(sqlMaxOrderIndex);
283
			if (intOrderIndex != null){
284
				intOrderIndex++;
285
			}else{
286
				String warning = "No term was found in vocabulary or vocabulary does not exist. Use order index '0'.";
287
				monitor.warning(warning);
288 f966cf6d Andreas Müller
				intOrderIndex =0;
289 55356795 Andreas Müller
			}
290
		}
291 16313054 Patric Plitzner
292 55356795 Andreas Müller
		return intOrderIndex.toString();
293 90b75b52 Andreas Müller
	}
294 9fcf1181 Andreas Müller
295 16313054 Patric Plitzner
296 9fcf1181 Andreas Müller
	private boolean hasReverseRepresentation() {
297
		return  reverseLabel != null ||  reverseDescription != null ||  reverseAbbrev != null;
298
	}
299
300
	public SingleTermUpdater setReverseRepresentation(String reverseDescription, String reverseLabel, String reverseAbbrev) {
301
		this.reverseLabel = reverseLabel;
302
		this.reverseDescription = reverseDescription;
303
		this.reverseAbbrev = reverseAbbrev;
304
		return this;
305
	}
306 16313054 Patric Plitzner
307 f966cf6d Andreas Müller
	public SingleTermUpdater setSymmetricTransitiv(boolean symmetric, boolean transitive){
308 e4ef4d83 Andreas Müller
		this.symmetric = symmetric;
309 f966cf6d Andreas Müller
		this.transitive = transitive;
310 e4ef4d83 Andreas Müller
		return this;
311
	}
312 9fcf1181 Andreas Müller
313 90b75b52 Andreas Müller
}