Project

General

Profile

Download (5.95 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.UUID;
14

    
15
import org.apache.commons.lang.StringUtils;
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19
import eu.etaxonomy.cdm.database.ICdmDataSource;
20
import eu.etaxonomy.cdm.model.common.Language;
21
import eu.etaxonomy.cdm.model.common.TermType;
22

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

    
32
// **************************** STATIC METHODS ********************************/
33

    
34
	public static final VocabularyCreator NewVocabularyInstance(UUID uuidVocabulary, String description,  String label, String abbrev, boolean isOrdered, Class<?> termclass, TermType termType){
35
		String stepName = makeStepName(label);
36
		return new VocabularyCreator(stepName, uuidVocabulary, description, label, abbrev, isOrdered, termclass, termType);
37
	}
38

    
39
// *************************** VARIABLES *****************************************/
40
	private UUID uuidVocabulary;
41
	private String description;
42
	private String label;
43
	private String abbrev;
44
	private boolean isOrdered;
45
	private Class<?> termClass;
46
	private TermType termType;
47

    
48
// ***************************** CONSTRUCTOR ***************************************/
49

    
50
	private VocabularyCreator(String stepName, UUID uuidVocabulary, String description, String label, String abbrev, boolean isOrdered, Class<?> termClass, TermType termType) {
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
		this.termType = termType;
59
	}
60

    
61
// ******************************* METHODS *************************************************/
62

    
63
    @Override
64
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
65
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
66
        ResultSet rs;
67

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

    
77

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

    
91

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

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

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

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

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

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

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

    
146
		return;
147
	}
148

    
149

    
150

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

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

    
164
}
(36-36/36)