Project

General

Profile

Download (3.97 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.ArrayList;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.log4j.Logger;
19
import org.joda.time.DateTime;
20

    
21
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
22
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
23
import eu.etaxonomy.cdm.database.ICdmDataSource;
24
import eu.etaxonomy.cdm.model.common.Language;
25

    
26
/**
27
 * @author a.mueller
28
 * @date 13.09.2010
29
 *
30
 */
31
public abstract class SchemaUpdaterStepBase<T extends SchemaUpdaterStepBase<T>> implements ISchemaUpdaterStep {
32
	private static final Logger logger = Logger.getLogger(SchemaUpdaterStepBase.class);
33

    
34
	protected String stepName;
35

    
36
	private boolean ignoreErrors;
37

    
38

    
39
//************************ CONSTRUCTOR ***********************************/
40

    
41
	protected SchemaUpdaterStepBase(String stepName){
42
		this.setStepName(stepName);
43
	}
44

    
45

    
46
	@Override
47
	public abstract Integer invoke (ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException;
48

    
49

    
50
	@Override
51
	public void setStepName(String stepName) {
52
		this.stepName = stepName;
53
	}
54

    
55
	@Override
56
	public String getStepName() {
57
		return stepName;
58
	}
59

    
60
	protected String getBoolean(boolean value, ICdmDataSource datasource) {
61

    
62
		String result;
63
		DatabaseTypeEnum type = datasource.getDatabaseType();
64
		//TODO use
65
//		type.getHibernateDialect().toBooleanValueString(bool);
66
		int intValue = value == true? 1 : 0;
67
		if (type.equals(DatabaseTypeEnum.MySQL)){
68
			result = "b'"+intValue+"'";
69
		}else if (type.equals(DatabaseTypeEnum.PostgreSQL)){
70
			result = "'"+intValue+"'";
71
		}else if (type.equals(DatabaseTypeEnum.H2)){
72
			result = value == true ? "TRUE" : "FALSE";
73
		}else if (type.equals(DatabaseTypeEnum.SqlServer2005)){
74
			logger.warn("SQLServer boolean not tested yet");
75
			result = "b'"+intValue+"'";
76
		}else{
77
			throw new RuntimeException("Database type not supported for boolean" + type.getName());
78
		}
79
		return result;
80
	}
81

    
82
	protected Integer getEnglishLanguageId(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
83
		return getLanguageId(Language.uuidEnglish, datasource, monitor, caseType);
84
	}
85

    
86
	/**
87
	 * @param uuidLanguage
88
	 * @param datasource
89
	 * @param monitor
90
	 * @return
91
	 * @throws SQLException
92
	 */
93
	protected Integer getLanguageId(UUID uuidLanguage, ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
94

    
95
		ResultSet rs;
96
		Integer langId = null;
97
		String sqlLangId = " SELECT id FROM %s WHERE uuid = '%s'";
98
		sqlLangId = String.format(sqlLangId, caseType.transformTo("DefinedTermBase"), uuidLanguage.toString() );
99
		rs = datasource.executeQuery(sqlLangId);
100
		if (rs.next()){
101
			langId = rs.getInt("id");
102
		}else{
103
			String warning = "Term for language (" +  uuidLanguage + ") does not exist!";
104
			monitor.warning(warning);
105
		}
106
		return langId;
107
	}
108

    
109
	/**
110
	 * {@inheritDoc}
111
	 */
112
	@Override
113
	public List<ISchemaUpdaterStep> getInnerSteps(){
114
		return new ArrayList<ISchemaUpdaterStep>();
115
	}
116

    
117
	@Override
118
	public boolean isIgnoreErrors() {
119
		return ignoreErrors;
120
	}
121

    
122

    
123
	@Override
124
	public void setIgnoreErrors(boolean ignoreErrors) {
125
		this.ignoreErrors = ignoreErrors;
126
	}
127

    
128

    
129
	/**
130
	 * Returns a time string with date and time (without millis) that
131
	 * can be used as a time string for database insert and update
132
	 * @return
133
	 */
134
	protected String getNowString() {
135
		return DateTime.now().toString("YYYY-MM-dd HH:mm:ss");
136
	}
137

    
138
	@Override
139
	public String toString(){
140
		if (StringUtils.isNotBlank(stepName)){
141
			return stepName;
142
		}else{
143
			return super.toString();
144
		}
145
	}
146

    
147
}
(22-22/36)