3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
19 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
20 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
21 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
22 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
23 import eu
.etaxonomy
.cdm
.model
.name
.RankClass
;
26 * Creates a new term if a term with the same given uuid does not exist yet
31 public class SingleTermUpdater
extends SchemaUpdaterStepBase
<SingleTermUpdater
> implements ITermUpdaterStep
{
32 @SuppressWarnings("unused")
33 private static final Logger logger
= Logger
.getLogger(SingleTermUpdater
.class);
36 * @Deprecated use {@link #NewInstance(String, TermType, UUID, String, String, String, String, UUID, UUID, boolean, UUID)} instead
39 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
){
40 return new SingleTermUpdater(stepName
, null, uuidTerm
, null, description
, label
, abbrev
, dtype
, uuidVocabulary
, uuidLanguage
, isOrdered
, uuidAfterTerm
);
43 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
){
44 return new SingleTermUpdater(stepName
, termType
, uuidTerm
, idInVocabulary
, description
, label
, abbrev
, dtype
, uuidVocabulary
, uuidLanguage
, isOrdered
, uuidAfterTerm
);
48 private UUID uuidTerm
;
49 private String description
;
51 private String abbrev
;
53 private UUID uuidVocabulary
;
54 private boolean isOrdered
;
55 private UUID uuidAfterTerm
;
56 private UUID uuidLanguage
;
57 private String reverseDescription
;
58 private String reverseLabel
;
59 private String reverseAbbrev
;
60 private RankClass rankClass
;
61 private TermType termType
;
62 private String idInVocabulary
;
63 private boolean symmetric
= false;
64 private boolean transitive
= false;
68 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
) {
70 this.termType
= termType
;
71 this.idInVocabulary
= idInVocabulary
;
73 this.description
= description
;
76 this.isOrdered
= isOrdered
;
77 this.uuidTerm
= uuidTerm
;
78 this.uuidVocabulary
= uuidVocabulary
;
79 this.uuidAfterTerm
= uuidAfterTerm
;
80 this.uuidLanguage
= uuidLanguage
;
84 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
85 String sqlCheckTermExists
= " SELECT count(*) as n FROM DefinedTermBase WHERE uuid = '" + uuidTerm
+ "'";
86 Long n
= (Long
)datasource
.getSingleValue(sqlCheckTermExists
);
88 monitor
.warning("Term already exists: " + label
+ "(" + uuidTerm
+ ")");
94 String sqlVocId
= " SELECT id FROM TermVocabulary WHERE uuid = '" + uuidVocabulary
+ "'";
95 ResultSet rs
= datasource
.executeQuery(sqlVocId
);
97 vocId
= rs
.getInt("id");
99 String warning
= "Vocabulary ( "+ uuidVocabulary
+" ) for term does not exist!";
100 monitor
.warning(warning
);
105 String sqlMaxId
= " SELECT max(id)+1 as maxId FROM DefinedTermBase";
106 rs
= datasource
.executeQuery(sqlMaxId
);
108 termId
= rs
.getInt("maxId");
110 String warning
= "No defined terms do exist yet. Can't update terms!";
111 monitor
.warning(warning
);
115 String id
= Integer
.toString(termId
);
116 String created
= getNowString();
117 String defaultColor
= "null";
118 String protectedTitleCache
= getBoolean(false, datasource
);
121 orderIndex
= getOrderIndex(datasource
, vocId
, monitor
);
125 String titleCache
= label
!= null ? label
: (abbrev
!= null ? abbrev
: description
);
126 String idInVocStr
= idInVocabulary
== null ?
"NULL" : "'" + idInVocabulary
+ "'";
127 String sqlInsertTerm
= " INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, termtype, idInVocabulary, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)" +
128 "VALUES ('" + dtype
+ "', " + id
+ ", '" + uuidTerm
+ "', '" + created
+ "', '" + termType
.getKey() + "', " + idInVocStr
+ ", " + protectedTitleCache
+ ", '" + titleCache
+ "', " + orderIndex
+ ", " + defaultColor
+ ", " + vocId
+ ")";
129 datasource
.executeUpdate(sqlInsertTerm
);
131 updateFeatureTerms(termId
, datasource
, monitor
);
132 updateRelationshipTerms(termId
, datasource
, monitor
);
133 updateRanks(termId
, datasource
, monitor
);
136 // INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)
137 // 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
138 // FROM DefinedTermBase ;
142 Integer langId
= getLanguageId(uuidLanguage
, datasource
, monitor
);
149 sqlMaxId
= " SELECT max(id)+1 as maxId FROM Representation";
150 rs
= datasource
.executeQuery(sqlMaxId
);
152 repId
= rs
.getInt("maxId");
154 String warning
= "No representations do exist yet. Can't update terms!";
155 monitor
.warning(warning
);
159 //standard representation
160 UUID uuidRepresentation
= UUID
.randomUUID();
161 String sqlInsertRepresentation
= " INSERT INTO Representation (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
162 "VALUES (" + repId
+ ", '" + created
+ "', '" + uuidRepresentation
+ "', " + nullSafeStr(description
) + ", " +nullSafeStr( label
) + ", " + nullSafeStr(abbrev
) + ", " + langId
+ ")";
164 datasource
.executeUpdate(sqlInsertRepresentation
);
166 String sqlInsertMN
= "INSERT INTO DefinedTermBase_Representation (DefinedTermBase_id, representations_id) " +
167 " VALUES ("+ termId
+"," +repId
+ " )";
169 datasource
.executeUpdate(sqlInsertMN
);
171 //reverse representation
172 if (hasReverseRepresentation()){
173 int reverseRepId
= repId
+ 1;
174 UUID uuidReverseRepresentation
= UUID
.randomUUID();
175 String sqlInsertReverseRepresentation
= " INSERT INTO Representation (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
176 "VALUES (" + reverseRepId
+ ", '" + created
+ "', '" + uuidReverseRepresentation
+ "', " + nullSafeStr(reverseDescription
) + ", " + nullSafeStr(reverseLabel
) + ", " + nullSafeStr(reverseAbbrev
) + ", " + langId
+ ")";
178 datasource
.executeUpdate(sqlInsertReverseRepresentation
);
180 String sqlReverseInsertMN
= "INSERT INTO RelationshipTermBase_inverseRepresentation (DefinedTermBase_id, inverserepresentations_id) " +
181 " VALUES ("+ termId
+"," +reverseRepId
+ " )";
183 datasource
.executeUpdate(sqlReverseInsertMN
);
189 private String
nullSafeStr(String str
) {
193 return "'" + str
+ "'";
197 private void updateFeatureTerms(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
198 if (dtype
.equals(Feature
.class.getSimpleName())){
199 String sqlUpdate
= "UPDATE DefinedTermBase SET " +
200 " supportscategoricaldata = " + getBoolean(false, datasource
) + ", " +
201 " supportscommontaxonname = " + getBoolean(false, datasource
) + ", " +
202 " supportsdistribution = " + getBoolean(false, datasource
) + ", " +
203 " supportsindividualassociation = " + getBoolean(false, datasource
) + ", " +
204 " supportsquantitativedata = " + getBoolean(false, datasource
) + ", " +
205 " supportstaxoninteraction = " + getBoolean(false, datasource
) + ", " +
206 " supportstextdata = " + getBoolean(true, datasource
) + " " +
207 " WHERE id = " + termId
;
208 datasource
.executeUpdate(sqlUpdate
);
212 private void updateRelationshipTerms(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
213 if (dtype
.contains("Relationship")){
214 String sqlUpdate
= "UPDATE DefinedTermBase SET " +
215 " symmetrical = " + getBoolean(symmetric
, datasource
) + ", " +
216 " transitive = " + getBoolean(transitive
, datasource
) + " " +
217 " WHERE id = " + termId
;
218 datasource
.executeUpdate(sqlUpdate
);
222 private void updateRanks(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
223 if (dtype
.equals(Rank
.class.getSimpleName())){
224 String sqlUpdate
= "UPDATE DefinedTermBase " +
225 " SET rankClass = '" + rankClass
.getKey() + "'" +
226 " WHERE id = " + termId
;
227 datasource
.executeUpdate(sqlUpdate
);
231 public SingleTermUpdater
setRankClass(RankClass rankClass
) {
232 this.rankClass
= rankClass
;
243 * @throws SQLException
245 private String
getOrderIndex(ICdmDataSource datasource
, int vocId
, IProgressMonitor monitor
) throws SQLException
{
247 Integer intOrderIndex
= null;
248 if (uuidAfterTerm
== null){
251 String sqlOrderIndex
= " SELECT orderindex FROM DefinedTermBase WHERE uuid = '"+uuidAfterTerm
+"' AND vocabulary_id = "+vocId
+"";
252 rs
= datasource
.executeQuery(sqlOrderIndex
);
254 intOrderIndex
= rs
.getInt("orderindex") + 1;
256 String sqlUpdateLowerTerms
= "UPDATE DefinedTermBase SET orderindex = orderindex + 1 WHERE vocabulary_id = " + vocId
+ " AND orderindex >= " + intOrderIndex
;
257 datasource
.executeUpdate(sqlUpdateLowerTerms
);
259 String warning
= "The previous term has not been found in vocabulary. Put term to the end";
260 monitor
.warning(warning
);
262 if (intOrderIndex
== null){
263 String sqlMaxOrderIndex
= " SELECT max(orderindex) FROM DefinedTermBase WHERE vocabulary_id = " + vocId
+ "";
264 intOrderIndex
= (Integer
)datasource
.getSingleValue(sqlMaxOrderIndex
);
265 if (intOrderIndex
!= null){
268 String warning
= "No term was found in vocabulary or vocabulary does not exist. Use order index '0'.";
269 monitor
.warning(warning
);
274 return intOrderIndex
.toString();
278 private boolean hasReverseRepresentation() {
279 return reverseLabel
!= null || reverseDescription
!= null || reverseAbbrev
!= null;
282 public SingleTermUpdater
setReverseRepresentation(String reverseDescription
, String reverseLabel
, String reverseAbbrev
) {
283 this.reverseLabel
= reverseLabel
;
284 this.reverseDescription
= reverseDescription
;
285 this.reverseAbbrev
= reverseAbbrev
;
289 public SingleTermUpdater
setSymmetricTransitiv(boolean symmetric
, boolean transitive
){
290 this.symmetric
= symmetric
;
291 this.transitive
= transitive
;