1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.database.update;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.commons.lang.StringUtils;
|
17
|
import org.apache.log4j.Logger;
|
18
|
|
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;
|
23
|
|
24
|
/**
|
25
|
* @author a.mueller
|
26
|
* @date 10.09.2010
|
27
|
*
|
28
|
*/
|
29
|
public class VocabularyCreator extends SchemaUpdaterStepBase<VocabularyCreator> implements ITermUpdaterStep{
|
30
|
@SuppressWarnings("unused")
|
31
|
private static final Logger logger = Logger.getLogger(VocabularyCreator.class);
|
32
|
|
33
|
// **************************** STATIC METHODS ********************************/
|
34
|
|
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);
|
38
|
}
|
39
|
|
40
|
// *************************** VARIABLES *****************************************/
|
41
|
private UUID uuidVocabulary;
|
42
|
private String description;
|
43
|
private String label;
|
44
|
private String abbrev;
|
45
|
private boolean isOrdered;
|
46
|
private Class<?> termClass;
|
47
|
private TermType termType;
|
48
|
|
49
|
// ***************************** CONSTRUCTOR ***************************************/
|
50
|
|
51
|
private VocabularyCreator(String stepName, UUID uuidVocabulary, String description, String label, String abbrev, boolean isOrdered, Class<?> termClass, TermType termType) {
|
52
|
super(stepName);
|
53
|
this.uuidVocabulary = uuidVocabulary;
|
54
|
this.description = description;
|
55
|
this.abbrev = abbrev;
|
56
|
this.label = label;
|
57
|
this.isOrdered = isOrdered;
|
58
|
this.termClass = termClass;
|
59
|
this.termType = termType;
|
60
|
}
|
61
|
|
62
|
// ******************************* METHODS *************************************************/
|
63
|
|
64
|
public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{
|
65
|
ResultSet rs;
|
66
|
|
67
|
String sqlCheckTermExists = " SELECT count(*) as n FROM @@TermVocabulary@@ WHERE uuid = '" + uuidVocabulary + "'";
|
68
|
Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
|
69
|
if (n != 0){
|
70
|
monitor.warning("Vocabulary already exists: " + label + "(" + uuidVocabulary + ")");
|
71
|
return null;
|
72
|
}
|
73
|
|
74
|
|
75
|
//vocId
|
76
|
Integer vocId;
|
77
|
String sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("TermVocabulary");
|
78
|
rs = datasource.executeQuery(sqlMaxId);
|
79
|
if (rs.next()){
|
80
|
vocId = rs.getInt("maxId");
|
81
|
}else{
|
82
|
String warning = "No vocabularies do exist yet. Can't create vocabulary!";
|
83
|
monitor.warning(warning);
|
84
|
return null;
|
85
|
}
|
86
|
|
87
|
|
88
|
String id = Integer.toString(vocId);
|
89
|
String created = getNowString();
|
90
|
String dtype;
|
91
|
if (isOrdered){
|
92
|
dtype = "OrderedTermVocabulary"; //TODO case required here?
|
93
|
}else{
|
94
|
dtype = "TermVocabulary";
|
95
|
}
|
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));
|
102
|
|
103
|
//language id
|
104
|
int langId;
|
105
|
String uuidLanguage = Language.uuidEnglish.toString();
|
106
|
String sqlLangId = " SELECT id FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidLanguage + "'";
|
107
|
rs = datasource.executeQuery(caseType.replaceTableNames(sqlLangId));
|
108
|
if (rs.next()){
|
109
|
langId = rs.getInt("id");
|
110
|
}else{
|
111
|
String warning = "Term for default language (English) not does not exist!";
|
112
|
monitor.warning(warning);
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
//representation
|
117
|
int repId;
|
118
|
sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("Representation");
|
119
|
rs = datasource.executeQuery(sqlMaxId);
|
120
|
if (rs.next()){
|
121
|
repId = rs.getInt("maxId");
|
122
|
}else{
|
123
|
String warning = "No representations do exist yet. Can't update terms!";
|
124
|
monitor.warning(warning);
|
125
|
return null;
|
126
|
}
|
127
|
|
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 + ")";
|
131
|
|
132
|
datasource.executeUpdate(caseType.replaceTableNames(sqlInsertRepresentation));
|
133
|
|
134
|
//Vocabulary_representation
|
135
|
String sqlInsertMN = "INSERT INTO @@TermVocabulary_Representation@@ (TermVocabulary_id, representations_id) " +
|
136
|
" VALUES ("+ vocId +"," +repId+ " )";
|
137
|
|
138
|
datasource.executeUpdate(caseType.replaceTableNames(sqlInsertMN));
|
139
|
|
140
|
return vocId;
|
141
|
}
|
142
|
|
143
|
|
144
|
|
145
|
private String nullSafeString(String abbrev) {
|
146
|
if (abbrev == null){
|
147
|
return "NULL";
|
148
|
}else{
|
149
|
return "'" + abbrev + "'";
|
150
|
}
|
151
|
}
|
152
|
|
153
|
private static String makeStepName(String label) {
|
154
|
String stepName = "Create new vocabulary '"+ label + "'";
|
155
|
return stepName;
|
156
|
}
|
157
|
|
158
|
}
|