Project

General

Profile

Download (6.06 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.database.update;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.List;
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.term.TermType;
23

    
24
/**
25
 * @author a.mueller
26
 * @since 10.09.2010
27
 *
28
 */
29
public class VocabularyCreator extends SchemaUpdaterStepBase {
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = Logger.getLogger(VocabularyCreator.class);
32

    
33
// **************************** STATIC METHODS ********************************/
34

    
35
	public static final VocabularyCreator NewVocabularyInstance(List<ISchemaUpdaterStep> stepList, UUID uuidVocabulary, String description,  String label, String abbrev, boolean isOrdered, Class<?> termclass, TermType termType){
36
		String stepName = makeStepName(label);
37
		return new VocabularyCreator(stepList, 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(List<ISchemaUpdaterStep> stepList, String stepName, UUID uuidVocabulary, String description, String label, String abbrev, boolean isOrdered, Class<?> termClass, TermType termType) {
52
		super(stepList, 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
    @Override
65
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
66
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
67
        ResultSet rs;
68

    
69
		String sqlCheckTermExists = " SELECT count(*) as n FROM @@TermVocabulary@@ WHERE uuid = '" + uuidVocabulary + "'";
70
		Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
71
		if (n != 0){
72
		    String message = "Vocabulary already exists: " + label + "(" + uuidVocabulary + ")";
73
			monitor.warning(message);
74
			result.addError(message, this, "invoke");
75
			return;
76
		}
77

    
78

    
79
		//vocId
80
		Integer vocId;
81
		String sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("TermVocabulary");
82
		rs = datasource.executeQuery(sqlMaxId);
83
		if (rs.next()){
84
			vocId = rs.getInt("maxId");
85
		}else{
86
			String message = "No vocabularies do exist yet. Can't create vocabulary!";
87
			monitor.warning(message);
88
			result.addError(message, this, "invoke");
89
            return;
90
		}
91

    
92

    
93
		String id = Integer.toString(vocId);
94
		String created  = getNowString();
95
		String dtype;
96
		if (isOrdered){
97
			dtype = "OrderedTermVocabulary";  //TODO case required here?
98
		}else{
99
			dtype = "TermVocabulary";
100
		}
101
		String titleCache = (StringUtils.isNotBlank(label))? label : ( (StringUtils.isNotBlank(abbrev))? abbrev : description );
102
		String protectedTitleCache = getBoolean(false, datasource);
103
		String termSourceUri = termClass.getCanonicalName();
104
		String sqlInsertTerm = " INSERT INTO @@TermVocabulary@@ (DTYPE, id, uuid, created, protectedtitlecache, titleCache, termsourceuri, termType)" +
105
				"VALUES ('" + dtype + "', " + id + ", '" + uuidVocabulary + "', '" + created + "', " + protectedTitleCache + ", '" + titleCache + "', '" + termSourceUri + "', '" + termType.getKey() + "')";
106
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertTerm));
107

    
108
		//language id
109
		int langId;
110
		String uuidLanguage = Language.uuidEnglish.toString();
111
		String sqlLangId = " SELECT id FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidLanguage + "'";
112
		rs = datasource.executeQuery(caseType.replaceTableNames(sqlLangId));
113
		if (rs.next()){
114
			langId = rs.getInt("id");
115
		}else{
116
			String message = "Term for default language (English) not  does not exist!";
117
			monitor.warning(message);
118
			result.addError(message, this, "invoke");
119
            return;
120
		}
121

    
122
		//representation
123
		int repId;
124
		sqlMaxId = " SELECT max(id)+1 as maxId FROM " + caseType.transformTo("Representation");
125
		rs = datasource.executeQuery(sqlMaxId);
126
		if (rs.next()){
127
			repId = rs.getInt("maxId");
128
		}else{
129
			String message = "No representations do exist yet. Can't update terms!";
130
			monitor.warning(message);
131
			result.addError(message, this, "invoke");
132
            return;
133
		}
134

    
135
		UUID uuidRepresentation = UUID.randomUUID();
136
		String sqlInsertRepresentation = " INSERT INTO @@Representation@@ (id, created, uuid, text, label, abbreviatedlabel, language_id) " +
137
				"VALUES (" + repId + ", '" + created + "', '" + uuidRepresentation + "', '" + description +  "', '" + label +  "'," + nullSafeString(abbrev) +  ", " + langId + ")";
138

    
139
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertRepresentation));
140

    
141
		//Vocabulary_representation
142
		String sqlInsertMN = "INSERT INTO @@TermVocabulary_Representation@@ (TermVocabulary_id, representations_id) " +
143
				" VALUES ("+ vocId +"," +repId+ " )";
144

    
145
		datasource.executeUpdate(caseType.replaceTableNames(sqlInsertMN));
146

    
147
		return;
148
	}
149

    
150

    
151

    
152
	private String nullSafeString(String abbrev) {
153
		if (abbrev == null){
154
			return "NULL";
155
		}else{
156
			return "'" + abbrev + "'";
157
		}
158
	}
159

    
160
	private static String makeStepName(String label) {
161
		String stepName = "Create new vocabulary '"+ label + "'";
162
		return stepName;
163
	}
164

    
165
}
(41-41/41)