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
|
import org.joda.time.DateTime;
|
19
|
|
20
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
21
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
22
|
import eu.etaxonomy.cdm.model.common.Language;
|
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){
|
36
|
String stepName = makeStepName(label);
|
37
|
return new VocabularyCreator(stepName, uuidVocabulary, description, label, abbrev, isOrdered, termclass);
|
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
|
|
48
|
// ***************************** CONSTRUCTOR ***************************************/
|
49
|
|
50
|
private VocabularyCreator(String stepName, UUID uuidVocabulary, String description, String label, String abbrev, boolean isOrdered, Class termClass) {
|
51
|
super(stepName);
|
52
|
this.uuidVocabulary = uuidVocabulary;
|
53
|
this.description = description;
|
54
|
this.abbrev = abbrev;
|
55
|
this.label = label;
|
56
|
this.isOrdered = isOrdered;
|
57
|
this.termClass = termClass;
|
58
|
}
|
59
|
|
60
|
// ******************************* METHODS *************************************************/
|
61
|
|
62
|
public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException{
|
63
|
ResultSet rs;
|
64
|
|
65
|
String sqlCheckTermExists = " SELECT count(*) as n FROM TermVocabulary WHERE uuid = '" + uuidVocabulary + "'";
|
66
|
Long n = (Long)datasource.getSingleValue(sqlCheckTermExists);
|
67
|
if (n != 0){
|
68
|
monitor.warning("Vocabulary already exists: " + label + "(" + uuidVocabulary + ")");
|
69
|
return null;
|
70
|
}
|
71
|
|
72
|
|
73
|
//vocId
|
74
|
Integer vocId;
|
75
|
String sqlMaxId = " SELECT max(id)+1 as maxId FROM TermVocabulary";
|
76
|
rs = datasource.executeQuery(sqlMaxId);
|
77
|
if (rs.next()){
|
78
|
vocId = rs.getInt("maxId");
|
79
|
}else{
|
80
|
String warning = "No vocabularies do exist yet. Can't create vocabulary!";
|
81
|
monitor.warning(warning);
|
82
|
return null;
|
83
|
}
|
84
|
|
85
|
|
86
|
String id = Integer.toString(vocId);
|
87
|
String created = getNowString();
|
88
|
String dtype;
|
89
|
if (isOrdered){
|
90
|
dtype = "OrderedTermVocabulary";
|
91
|
}else{
|
92
|
dtype = "TermVocabulary";
|
93
|
}
|
94
|
String titleCache = (StringUtils.isNotBlank(label))? label : ( (StringUtils.isNotBlank(abbrev))? abbrev : description );
|
95
|
String protectedTitleCache = getBoolean(false, datasource);
|
96
|
String termSourceUri = termClass.getCanonicalName();
|
97
|
String sqlInsertTerm = " INSERT INTO TermVocabulary (DTYPE, id, uuid, created, protectedtitlecache, titleCache, termsourceuri)" +
|
98
|
"VALUES ('" + dtype + "', " + id + ", '" + uuidVocabulary + "', '" + created + "', " + protectedTitleCache + ", '" + titleCache + "', '" + termSourceUri + "'" + ")";
|
99
|
datasource.executeUpdate(sqlInsertTerm);
|
100
|
|
101
|
//language id
|
102
|
int langId;
|
103
|
String uuidLanguage = Language.uuidEnglish.toString();
|
104
|
String sqlLangId = " SELECT id FROM DefinedTermBase WHERE uuid = '" + uuidLanguage + "'";
|
105
|
rs = datasource.executeQuery(sqlLangId);
|
106
|
if (rs.next()){
|
107
|
langId = rs.getInt("id");
|
108
|
}else{
|
109
|
String warning = "Term for default language (English) not does not exist!";
|
110
|
monitor.warning(warning);
|
111
|
return null;
|
112
|
}
|
113
|
|
114
|
//representation
|
115
|
int repId;
|
116
|
sqlMaxId = " SELECT max(id)+1 as maxId FROM Representation";
|
117
|
rs = datasource.executeQuery(sqlMaxId);
|
118
|
if (rs.next()){
|
119
|
repId = rs.getInt("maxId");
|
120
|
}else{
|
121
|
String warning = "No representations do exist yet. Can't update terms!";
|
122
|
monitor.warning(warning);
|
123
|
return null;
|
124
|
}
|
125
|
|
126
|
UUID uuidRepresentation = UUID.randomUUID();
|
127
|
String sqlInsertRepresentation = " INSERT INTO Representation (id, created, uuid, text, abbreviatedlabel, label, language_id) " +
|
128
|
"VALUES (" + repId + ", '" + created + "', '" + uuidRepresentation + "', '" + description + "', '" + label + "', '" + abbrev + "', " + langId + ")";
|
129
|
|
130
|
datasource.executeUpdate(sqlInsertRepresentation);
|
131
|
|
132
|
//Vocabulary_representation
|
133
|
String sqlInsertMN = "INSERT INTO TermVocabulary_Representation (TermVocabulary_id, representations_id) " +
|
134
|
" VALUES ("+ vocId +"," +repId+ " )";
|
135
|
|
136
|
datasource.executeUpdate(sqlInsertMN);
|
137
|
|
138
|
return vocId;
|
139
|
}
|
140
|
|
141
|
|
142
|
|
143
|
private static String makeStepName(String label) {
|
144
|
String stepName = "Create new vocabulary '"+ label + "'";
|
145
|
return stepName;
|
146
|
}
|
147
|
|
148
|
}
|