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
.commons
.lang
.StringUtils
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
20 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
21 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
22 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
29 public class VocabularyCreator
extends SchemaUpdaterStepBase
<VocabularyCreator
> implements ITermUpdaterStep
{
30 @SuppressWarnings("unused")
31 private static final Logger logger
= Logger
.getLogger(VocabularyCreator
.class);
33 // **************************** STATIC METHODS ********************************/
35 public static final VocabularyCreator
NewVocabularyInstance(UUID uuidVocabulary
, String description
, String label
, String abbrev
, boolean isOrdered
, Class
<?
> termclass
, TermType termType
){
36 String stepName
= makeStepName(label
);
37 return new VocabularyCreator(stepName
, uuidVocabulary
, description
, label
, abbrev
, isOrdered
, termclass
, termType
);
40 // *************************** VARIABLES *****************************************/
41 private UUID uuidVocabulary
;
42 private String description
;
44 private String abbrev
;
45 private boolean isOrdered
;
46 private Class
<?
> termClass
;
47 private TermType termType
;
49 // ***************************** CONSTRUCTOR ***************************************/
51 private VocabularyCreator(String stepName
, UUID uuidVocabulary
, String description
, String label
, String abbrev
, boolean isOrdered
, Class
<?
> termClass
, TermType termType
) {
53 this.uuidVocabulary
= uuidVocabulary
;
54 this.description
= description
;
57 this.isOrdered
= isOrdered
;
58 this.termClass
= termClass
;
59 this.termType
= termType
;
62 // ******************************* METHODS *************************************************/
64 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
{
67 String sqlCheckTermExists
= " SELECT count(*) as n FROM @@TermVocabulary@@ WHERE uuid = '" + uuidVocabulary
+ "'";
68 Long n
= (Long
)datasource
.getSingleValue(caseType
.replaceTableNames(sqlCheckTermExists
));
70 monitor
.warning("Vocabulary already exists: " + label
+ "(" + uuidVocabulary
+ ")");
77 String sqlMaxId
= " SELECT max(id)+1 as maxId FROM " + caseType
.transformTo("TermVocabulary");
78 rs
= datasource
.executeQuery(sqlMaxId
);
80 vocId
= rs
.getInt("maxId");
82 String warning
= "No vocabularies do exist yet. Can't create vocabulary!";
83 monitor
.warning(warning
);
88 String id
= Integer
.toString(vocId
);
89 String created
= getNowString();
92 dtype
= "OrderedTermVocabulary"; //TODO case required here?
94 dtype
= "TermVocabulary";
96 String titleCache
= (StringUtils
.isNotBlank(label
))? label
: ( (StringUtils
.isNotBlank(abbrev
))? abbrev
: description
);
97 String protectedTitleCache
= getBoolean(false, datasource
);
98 String termSourceUri
= termClass
.getCanonicalName();
99 String sqlInsertTerm
= " INSERT INTO @@TermVocabulary@@ (DTYPE, id, uuid, created, protectedtitlecache, titleCache, termsourceuri, termType)" +
100 "VALUES ('" + dtype
+ "', " + id
+ ", '" + uuidVocabulary
+ "', '" + created
+ "', " + protectedTitleCache
+ ", '" + titleCache
+ "', '" + termSourceUri
+ "', '" + termType
.getKey() + "')";
101 datasource
.executeUpdate(caseType
.replaceTableNames(sqlInsertTerm
));
105 String uuidLanguage
= Language
.uuidEnglish
.toString();
106 String sqlLangId
= " SELECT id FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidLanguage
+ "'";
107 rs
= datasource
.executeQuery(caseType
.replaceTableNames(sqlLangId
));
109 langId
= rs
.getInt("id");
111 String warning
= "Term for default language (English) not does not exist!";
112 monitor
.warning(warning
);
118 sqlMaxId
= " SELECT max(id)+1 as maxId FROM " + caseType
.transformTo("Representation");
119 rs
= datasource
.executeQuery(sqlMaxId
);
121 repId
= rs
.getInt("maxId");
123 String warning
= "No representations do exist yet. Can't update terms!";
124 monitor
.warning(warning
);
128 UUID uuidRepresentation
= UUID
.randomUUID();
129 String sqlInsertRepresentation
= " INSERT INTO @@Representation@@ (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
130 "VALUES (" + repId
+ ", '" + created
+ "', '" + uuidRepresentation
+ "', '" + description
+ "', '" + label
+ "'," + nullSafeString(abbrev
) + ", " + langId
+ ")";
132 datasource
.executeUpdate(caseType
.replaceTableNames(sqlInsertRepresentation
));
134 //Vocabulary_representation
135 String sqlInsertMN
= "INSERT INTO @@TermVocabulary_Representation@@ (TermVocabulary_id, representations_id) " +
136 " VALUES ("+ vocId
+"," +repId
+ " )";
138 datasource
.executeUpdate(caseType
.replaceTableNames(sqlInsertMN
));
145 private String
nullSafeString(String abbrev
) {
149 return "'" + abbrev
+ "'";
153 private static String
makeStepName(String label
) {
154 String stepName
= "Create new vocabulary '"+ label
+ "'";