Project

General

Profile

Download (4.95 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.database;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.jdbc.datasource.DriverManagerDataSource;
17

    
18
import eu.etaxonomy.cdm.database.types.H2DatabaseType;
19
import eu.etaxonomy.cdm.database.types.HSqlDbDatabaseType;
20
import eu.etaxonomy.cdm.database.types.IDatabaseType;
21
import eu.etaxonomy.cdm.database.types.MySQLDatabaseType;
22
import eu.etaxonomy.cdm.database.types.OdbcDatabaseType;
23
import eu.etaxonomy.cdm.database.types.PostgreSQLDatabaseType;
24
import eu.etaxonomy.cdm.database.types.SqlServer2005DatabaseType;
25
import eu.etaxonomy.cdm.database.types.SqlServer2000DatabaseType;
26

    
27
/**
28
 * @author a.mueller
29
 *
30
 */
31
public enum DatabaseTypeEnum {
32
	HSqlDb(1),
33
	MySQL(2),
34
	ODBC(3),
35
	PostgreSQL(4),
36
	SqlServer2000(5),
37
	SqlServer2005(6),
38
	H2(7)
39
	;
40

    
41
	/**
42
	 * Constructor
43
	 * @param i
44
	 */
45
	private DatabaseTypeEnum(int i) {
46
		switch(i)
47
        {
48
        	case 1:
49
        		this.dbType = new HSqlDbDatabaseType(); break;
50
        	case 2:
51
        		this.dbType = new MySQLDatabaseType(); break;
52
        	case 3:
53
        		this.dbType = new OdbcDatabaseType(); break;
54
        	case 4:
55
            	this.dbType = new PostgreSQLDatabaseType(); break;
56
            case 5:
57
            	this.dbType = new SqlServer2000DatabaseType(); break;
58
            case 6:
59
            	this.dbType = new SqlServer2005DatabaseType(); break;
60
            case 7:
61
            	this.dbType = new H2DatabaseType(); break;
62
            default:
63
                //TODO Exception
64
        }
65
	}
66
	
67
	public IDatabaseType getDatabaseType(){
68
		return dbType;
69
	}
70
	
71
 	//Logger
72
	private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
73
	protected IDatabaseType dbType;
74
	
75
	   
76
    /**
77
     * @return
78
     */
79
    public String getName(){
80
    	return dbType.getName();
81
    }
82
    
83
	/**
84
	 * @return
85
	 */
86
	public String getDriverClassName(){
87
		return dbType.getClassString();
88
	}
89
    
90
	/**
91
	 * Returns the DriverManagerDataSource class that that the datasource needs to create a spring bean
92
	 * @return the DriverManagerDataSource class
93
	 */
94
	public Class<? extends DriverManagerDataSource> getDriverManagerDataSourceClass(){
95
		return dbType.getDriverManagerDataSourceClass();
96
	}
97
	
98
	/**
99
	 * @return
100
	 */
101
	public String getUrl(){
102
		return dbType.getUrlString();
103
	}
104
	
105
	/**
106
	 * @return
107
	 */
108
	public String getHibernateDialect(){
109
		return dbType.getHibernateDialect();
110
	}
111
	   
112
    /**
113
     * @return
114
     */
115
    public int getDefaultPort(){
116
    	return dbType.getDefaultPort();
117
    }
118

    
119
	/**
120
     * returns the connection string 
121
     * @param server the server, e.g. IP-Address
122
     * @param database the database name on the server (e.g. "testDB")
123
     * @param port the port number
124
     * @return the connection string
125
     */
126
    public String getConnectionString(ICdmDataSource cdmDataSource){
127
    	String result = dbType.getConnectionString(cdmDataSource);
128
    	logger.debug("Connection String: " + result);	
129
        return result;
130
    }
131
    
132

    
133

    
134
	/**
135
     * Returns the Name of the initialization method to be used when a hibernate datasource is created for this database
136
	 * @return String name of the init method
137
	 */
138
    public String getInitMethod(){
139
    	String result = dbType.getInitMethod();
140
    	logger.debug("InitMethod: " + result);	
141
        return result;
142
    }
143
    
144
	/**
145
	 * Returns the Name of the destroying method to be used when a hibernate datasource representing this database is destroyed
146
	 * @return String name of the destroy method
147
	 */
148
    public String getDestroyMethod(){
149
    	String result = dbType.getDestroyMethod();
150
    	logger.debug("DestroyMethod: " + result);	
151
        return result;
152
    }
153
    
154
    /**
155
     * Returns a List of all available DatabaseEnums.
156
     * @return List of DatabaseEnums
157
     */
158
    public static List<DatabaseTypeEnum> getAllTypes(){
159
    	List<DatabaseTypeEnum> result = new ArrayList<DatabaseTypeEnum>();
160
    	for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
161
    		result.add(dbEnum);
162
    	}
163
    	return result;
164
    }
165

    
166
    /**
167
     * Returns the DatabaseTypeEnum to a given DriverClass
168
     * @param strDriverClass
169
     * @return the according DatabaseTypeEnum. Null if the driver class does not exist.
170
     */
171
    public static DatabaseTypeEnum getDatabaseEnumByDriverClass(String strDriverClass){
172
    	for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
173
    		if (dbEnum.getDriverClassName().equals(strDriverClass)){
174
    			return dbEnum;
175
    		}
176
    	}
177
    	logger.warn("Unknown driver class " + strDriverClass==null ? "null" : strDriverClass);
178
    	return null;
179
    }
180
    
181
    
182

    
183
 
184
}
185

    
(5-5/11)