Project

General

Profile

Download (5.94 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.logging.log4j.LogManager;import org.apache.logging.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
public class VocabularyCreator extends SchemaUpdaterStepBase {
29

    
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = LogManager.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

    
42
	private UUID uuidVocabulary;
43
	private String description;
44
	private String label;
45
	private String abbrev;
46
	private boolean isOrdered;
47
	private Class<?> termClass;
48
	private TermType termType;
49

    
50
// ***************************** CONSTRUCTOR ***************************************/
51

    
52
	private VocabularyCreator(List<ISchemaUpdaterStep> stepList, String stepName, UUID uuidVocabulary, String description, String label, String abbrev, boolean isOrdered, Class<?> termClass, TermType termType) {
53
		super(stepList, stepName);
54
		this.uuidVocabulary = uuidVocabulary;
55
		this.description = description;
56
		this.abbrev = abbrev;
57
		this.label = label;
58
		this.isOrdered = isOrdered;
59
		this.termClass = termClass;
60
		this.termType = termType;
61
	}
62

    
63
// ******************************* METHODS *************************************************/
64

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

    
70
		String sqlCheckTermExists = " SELECT count(*) as n FROM @@TermVocabulary@@ WHERE uuid = '" + uuidVocabulary + "'";
71
		Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
72
		if (n != 0){
73
		    String message = "Vocabulary already exists: " + label + "(" + uuidVocabulary + ")";
74
			monitor.warning(message);
75
			result.addError(message, this, "invoke");
76
			return;
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
		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
	private String nullSafeString(String abbrev) {
150
		if (abbrev == null){
151
			return "NULL";
152
		}else{
153
			return "'" + abbrev + "'";
154
		}
155
	}
156

    
157
	private static String makeStepName(String label) {
158
		String stepName = "Create new vocabulary '"+ label + "'";
159
		return stepName;
160
	}
161
}
(41-41/41)