Refactored methods in VersionableDaoBase etc + added Query-By-Example for CdmEntityDa...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / DatabaseTypeEnum.java
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 javax.sql.DataSource;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.database.types.H2DatabaseType;
20 import eu.etaxonomy.cdm.database.types.HSqlDbDatabaseType;
21 import eu.etaxonomy.cdm.database.types.IDatabaseType;
22 import eu.etaxonomy.cdm.database.types.MySQLDatabaseType;
23 import eu.etaxonomy.cdm.database.types.OdbcDatabaseType;
24 import eu.etaxonomy.cdm.database.types.OracleDatabaseType;
25 import eu.etaxonomy.cdm.database.types.PostgreSQLDatabaseType;
26 import eu.etaxonomy.cdm.database.types.SqlServer2005DatabaseType;
27 import eu.etaxonomy.cdm.database.types.SybaseDatabaseType;
28
29 /**
30 * @author a.mueller
31 *
32 */
33 public enum DatabaseTypeEnum {
34 HSqlDb(1),
35 MySQL(2),
36 ODBC(3),
37 PostgreSQL(4),
38 Oracle(5),
39 //SqlServer2000(6),
40 SqlServer2005(7),
41 Sybase(8),
42 H2(9)
43 ;
44
45 /**
46 * Constructor
47 * @param i
48 */
49 private DatabaseTypeEnum(int i) {
50 switch(i)
51 {
52 case 1:
53 this.dbType = new HSqlDbDatabaseType(); break;
54 case 2:
55 this.dbType = new MySQLDatabaseType(); break;
56 case 3:
57 this.dbType = new OdbcDatabaseType(); break;
58 case 4:
59 this.dbType = new PostgreSQLDatabaseType(); break;
60 case 5:
61 this.dbType = new OracleDatabaseType(); break;
62 // case 6:
63 // this.dbType = new SqlServer2000DatabaseType(); break;
64 case 7:
65 this.dbType = new SqlServer2005DatabaseType(); break;
66 case 8:
67 this.dbType = new SybaseDatabaseType(); break;
68 case 9:
69 this.dbType = new H2DatabaseType(); break;
70 default:
71 //TODO Exception
72 }
73 }
74
75 public IDatabaseType getDatabaseType(){
76 return dbType;
77 }
78
79 //Logger
80 private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
81 protected IDatabaseType dbType;
82
83
84 /**
85 * @return
86 */
87 public String getName(){
88 return dbType.getName();
89 }
90
91 /**
92 * @return
93 */
94 public String getDriverClassName(){
95 return dbType.getClassString();
96 }
97
98 /**
99 * Returns the DataSource class that that the datasource needs to create a spring bean
100 * @return the DataSource class
101 */
102 public Class<? extends DataSource> getDataSourceClass(){
103 return dbType.getDataSourceClass();
104 }
105
106 /**
107 * @return
108 */
109 public String getUrl(){
110 return dbType.getUrlString();
111 }
112
113 /**
114 * @return
115 */
116 public String getHibernateDialect(){
117 return dbType.getHibernateDialect();
118 }
119
120 /**
121 * @return
122 */
123 public int getDefaultPort(){
124 return dbType.getDefaultPort();
125 }
126
127 /**
128 * returns the connection string
129 * @param server the server, e.g. IP-Address
130 * @param database the database name on the server (e.g. "testDB")
131 * @param port the port number
132 * @return the connection string
133 */
134 public String getConnectionString(ICdmDataSource cdmDataSource){
135 String result = dbType.getConnectionString(cdmDataSource);
136 logger.debug("Connection String: " + result);
137 return result;
138 }
139
140
141
142 /**
143 * Returns the Name of the initialization method to be used when a hibernate datasource is created for this database
144 * @return String name of the init method
145 */
146 public String getInitMethod(){
147 String result = dbType.getInitMethod();
148 logger.debug("InitMethod: " + result);
149 return result;
150 }
151
152 /**
153 * Returns the Name of the destroying method to be used when a hibernate datasource representing this database is destroyed
154 * @return String name of the destroy method
155 */
156 public String getDestroyMethod(){
157 String result = dbType.getDestroyMethod();
158 logger.debug("DestroyMethod: " + result);
159 return result;
160 }
161
162 /**
163 * Returns a List of all available DatabaseEnums.
164 * @return List of DatabaseEnums
165 */
166 public static List<DatabaseTypeEnum> getAllTypes(){
167 List<DatabaseTypeEnum> result = new ArrayList<DatabaseTypeEnum>();
168 for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
169 result.add(dbEnum);
170 }
171 return result;
172 }
173
174 /**
175 * Returns the DatabaseTypeEnum to a given DriverClass
176 * @param strDriverClass
177 * @return the according DatabaseTypeEnum. Null if the driver class does not exist.
178 */
179 public static DatabaseTypeEnum getDatabaseEnumByDriverClass(String strDriverClass){
180 for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
181 if (dbEnum.getDriverClassName().equals(strDriverClass)){
182 return dbEnum;
183 }
184 }
185 logger.warn("Unknown driver class " + strDriverClass==null ? "null" : strDriverClass);
186 return null;
187 }
188
189
190
191
192 }
193