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
.text
.DateFormat
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.joda
.time
.DateTime
;
19 import org
.joda
.time
.format
.DateTimeFormat
;
20 import org
.joda
.time
.format
.DateTimeFormatter
;
22 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
23 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
24 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
25 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
26 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
27 import eu
.etaxonomy
.cdm
.model
.name
.RankClass
;
30 * Creates a new term if a term with the same given uuid does not exist yet
35 public class SingleTermUpdater
extends SchemaUpdaterStepBase
<SingleTermUpdater
> implements ITermUpdaterStep
{
36 @SuppressWarnings("unused")
37 private static final Logger logger
= Logger
.getLogger(SingleTermUpdater
.class);
40 * @Deprecated use {@link #NewInstance(String, TermType, UUID, String, String, String, String, UUID, UUID, boolean, UUID)} instead
43 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
){
44 return new SingleTermUpdater(stepName
, null, uuidTerm
, null, description
, label
, abbrev
, dtype
, uuidVocabulary
, uuidLanguage
, isOrdered
, uuidAfterTerm
);
47 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
){
48 return new SingleTermUpdater(stepName
, termType
, uuidTerm
, idInVocabulary
, description
, label
, abbrev
, dtype
, uuidVocabulary
, uuidLanguage
, isOrdered
, uuidAfterTerm
);
52 private UUID uuidTerm
;
53 private String description
;
55 private String abbrev
;
57 private UUID uuidVocabulary
;
58 private boolean isOrdered
;
59 private UUID uuidAfterTerm
;
60 private UUID uuidLanguage
;
61 private String reverseDescription
;
62 private String reverseLabel
;
63 private String reverseAbbrev
;
64 private RankClass rankClass
;
65 private TermType termType
;
66 private String idInVocabulary
;
67 private boolean symmetric
= false;
68 private boolean transitive
= false;
72 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
) {
74 this.termType
= termType
;
75 this.idInVocabulary
= idInVocabulary
;
77 this.description
= description
;
80 this.isOrdered
= isOrdered
;
81 this.uuidTerm
= uuidTerm
;
82 this.uuidVocabulary
= uuidVocabulary
;
83 this.uuidAfterTerm
= uuidAfterTerm
;
84 this.uuidLanguage
= uuidLanguage
;
88 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
89 String sqlCheckTermExists
= " SELECT count(*) as n FROM DefinedTermBase WHERE uuid = '" + uuidTerm
+ "'";
90 Long n
= (Long
)datasource
.getSingleValue(sqlCheckTermExists
);
92 monitor
.warning("Term already exists: " + label
+ "(" + uuidTerm
+ ")");
98 String sqlVocId
= " SELECT id FROM TermVocabulary WHERE uuid = '" + uuidVocabulary
+ "'";
99 ResultSet rs
= datasource
.executeQuery(sqlVocId
);
101 vocId
= rs
.getInt("id");
103 String warning
= "Vocabulary ( "+ uuidVocabulary
+" ) for term does not exist!";
104 monitor
.warning(warning
);
109 String sqlMaxId
= " SELECT max(id)+1 as maxId FROM DefinedTermBase";
110 rs
= datasource
.executeQuery(sqlMaxId
);
112 termId
= rs
.getInt("maxId");
114 String warning
= "No defined terms do exist yet. Can't update terms!";
115 monitor
.warning(warning
);
119 String id
= Integer
.toString(termId
);
120 String created
= DateTime
.now().toString("YYYY-MM-dd HH:mm:ss");
121 String defaultColor
= "null";
122 String protectedTitleCache
= getBoolean(false, datasource
);
125 orderIndex
= getOrderIndex(datasource
, vocId
, monitor
);
129 String titleCache
= label
!= null ? label
: (abbrev
!= null ? abbrev
: description
);
130 String idInVocStr
= idInVocabulary
== null ?
"NULL" : "'" + idInVocabulary
+ "'";
131 String sqlInsertTerm
= " INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, termtype, idInVocabulary, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)" +
132 "VALUES ('" + dtype
+ "', " + id
+ ", '" + uuidTerm
+ "', '" + created
+ "', '" + termType
.getKey() + "', " + idInVocStr
+ ", " + protectedTitleCache
+ ", '" + titleCache
+ "', " + orderIndex
+ ", " + defaultColor
+ ", " + vocId
+ ")";
133 datasource
.executeUpdate(sqlInsertTerm
);
135 updateFeatureTerms(termId
, datasource
, monitor
);
136 updateRelationshipTerms(termId
, datasource
, monitor
);
137 updateRanks(termId
, datasource
, monitor
);
140 // INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)
141 // 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
142 // FROM DefinedTermBase ;
146 Integer langId
= getLanguageId(uuidLanguage
, datasource
, monitor
);
153 sqlMaxId
= " SELECT max(id)+1 as maxId FROM Representation";
154 rs
= datasource
.executeQuery(sqlMaxId
);
156 repId
= rs
.getInt("maxId");
158 String warning
= "No representations do exist yet. Can't update terms!";
159 monitor
.warning(warning
);
163 //standard representation
164 UUID uuidRepresentation
= UUID
.randomUUID();
165 String sqlInsertRepresentation
= " INSERT INTO Representation (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
166 "VALUES (" + repId
+ ", '" + created
+ "', '" + uuidRepresentation
+ "', '" + description
+ "', '" + label
+ "', '" + abbrev
+ "', " + langId
+ ")";
168 datasource
.executeUpdate(sqlInsertRepresentation
);
170 String sqlInsertMN
= "INSERT INTO DefinedTermBase_Representation (DefinedTermBase_id, representations_id) " +
171 " VALUES ("+ termId
+"," +repId
+ " )";
173 datasource
.executeUpdate(sqlInsertMN
);
175 //reverse representation
176 if (hasReverseRepresentation()){
177 int reverseRepId
= repId
+ 1;
178 UUID uuidReverseRepresentation
= UUID
.randomUUID();
179 String sqlInsertReverseRepresentation
= " INSERT INTO Representation (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
180 "VALUES (" + reverseRepId
+ ", '" + created
+ "', '" + uuidReverseRepresentation
+ "', '" + reverseDescription
+ "', '" + reverseLabel
+ "', '" + reverseAbbrev
+ "', " + langId
+ ")";
182 datasource
.executeUpdate(sqlInsertReverseRepresentation
);
184 String sqlReverseInsertMN
= "INSERT INTO RelationshipTermBase_inverseRepresentation (DefinedTermBase_id, inverserepresentations_id) " +
185 " VALUES ("+ termId
+"," +reverseRepId
+ " )";
187 datasource
.executeUpdate(sqlReverseInsertMN
);
194 private void updateFeatureTerms(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
195 if (dtype
.equals(Feature
.class.getSimpleName())){
196 String sqlUpdate
= "UPDATE DefinedTermBase SET " +
197 " supportscategoricaldata = " + getBoolean(false, datasource
) + ", " +
198 " supportscommontaxonname = " + getBoolean(false, datasource
) + ", " +
199 " supportsdistribution = " + getBoolean(false, datasource
) + ", " +
200 " supportsindividualassociation = " + getBoolean(false, datasource
) + ", " +
201 " supportsquantitativedata = " + getBoolean(false, datasource
) + ", " +
202 " supportstaxoninteraction = " + getBoolean(false, datasource
) + ", " +
203 " supportstextdata = " + getBoolean(true, datasource
) + " " +
204 " WHERE id = " + termId
;
205 datasource
.executeUpdate(sqlUpdate
);
209 private void updateRelationshipTerms(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
210 if (dtype
.contains("Relationship")){
211 String sqlUpdate
= "UPDATE DefinedTermBase SET " +
212 " symmetrical = " + getBoolean(symmetric
, datasource
) + ", " +
213 " transitive = " + getBoolean(transitive
, datasource
) + " " +
214 " WHERE id = " + termId
;
215 datasource
.executeUpdate(sqlUpdate
);
219 private void updateRanks(Integer termId
, ICdmDataSource datasource
, IProgressMonitor monitor
) throws SQLException
{
220 if (dtype
.equals(Rank
.class.getSimpleName())){
221 String sqlUpdate
= "UPDATE DefinedTermBase " +
222 " SET rankClass = '" + rankClass
.getKey() + "'" +
223 " WHERE id = " + termId
;
224 datasource
.executeUpdate(sqlUpdate
);
228 public SingleTermUpdater
setRankClass(RankClass rankClass
) {
229 this.rankClass
= rankClass
;
240 * @throws SQLException
242 private String
getOrderIndex(ICdmDataSource datasource
, int vocId
, IProgressMonitor monitor
) throws SQLException
{
244 Integer intOrderIndex
= null;
245 if (uuidAfterTerm
== null){
248 String sqlOrderIndex
= " SELECT orderindex FROM DefinedTermBase WHERE uuid = '"+uuidAfterTerm
+"' AND vocabulary_id = "+vocId
+"";
249 rs
= datasource
.executeQuery(sqlOrderIndex
);
251 intOrderIndex
= rs
.getInt("orderindex") + 1;
253 String sqlUpdateLowerTerms
= "UPDATE DefinedTermBase SET orderindex = orderindex + 1 WHERE vocabulary_id = " + vocId
+ " AND orderindex >= " + intOrderIndex
;
254 datasource
.executeUpdate(sqlUpdateLowerTerms
);
256 String warning
= "The previous term has not been found in vocabulary. Put term to the end";
257 monitor
.warning(warning
);
259 if (intOrderIndex
== null){
260 String sqlMaxOrderIndex
= " SELECT max(orderindex) FROM DefinedTermBase WHERE vocabulary_id = " + vocId
+ "";
261 intOrderIndex
= (Integer
)datasource
.getSingleValue(sqlMaxOrderIndex
);
262 if (intOrderIndex
!= null){
265 String warning
= "No term was found in vocabulary or vocabulary does not exist. Use order index '0'.";
266 monitor
.warning(warning
);
271 return intOrderIndex
.toString();
275 private boolean hasReverseRepresentation() {
276 return reverseLabel
!= null || reverseDescription
!= null || reverseAbbrev
!= null;
279 public SingleTermUpdater
setReverseRepresentation(String reverseDescription
, String reverseLabel
, String reverseAbbrev
) {
280 this.reverseLabel
= reverseLabel
;
281 this.reverseDescription
= reverseDescription
;
282 this.reverseAbbrev
= reverseAbbrev
;
286 public SingleTermUpdater
setSymmetricTransitiv(boolean symmetric
, boolean transitive
){
287 this.symmetric
= symmetric
;
288 this.transitive
= transitive
;
292 public static void main(String
[] args
){
293 String time
= DateTime
.now().toString("YYYY-MM-dd HH:mm:ss");
294 System
.out
.println(time
);