Project

General

Profile

Download (3.98 KB) Statistics
| Branch: | Tag: | Revision:
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.ArrayList;
15
import java.util.List;
16
import java.util.UUID;
17

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

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

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

    
35
	protected String stepName;
36

    
37
	private boolean ignoreErrors;
38

    
39

    
40
//************************ CONSTRUCTOR ***********************************/
41

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

    
46

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

    
50

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

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

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

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

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

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

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

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

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

    
123

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

    
129

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

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

    
148
}
(20-20/34)