1 package de.fhdw.wtf.persistence.facade;
2
3 import java.io.IOException;
4 import java.sql.Connection;
5 import java.sql.DriverManager;
6 import java.sql.SQLException;
7
8 import de.fhdw.wtf.persistence.exception.ClassFacadeUninitializedException;
9 import de.fhdw.wtf.persistence.exception.NotConnectableException;
10 import de.fhdw.wtf.persistence.exception.NotConnectedException;
11 import de.fhdw.wtf.persistence.exception.OracleJDBCDriverMissing;
12 import de.fhdw.wtf.persistence.exception.OtherSQLException;
13 import de.fhdw.wtf.persistence.exception.PersistenceException;
14 import de.fhdw.wtf.persistence.utils.PropertiesReader;
15
16
17
18
19
20 public final class OracleDatabaseManager extends DatabaseManager {
21
22 private static final String JDBC_SEPERATOR = ":";
23
24 private static final String CONNECTION_PREFIX_ORACLE = "jdbc:oracle:thin:@";
25
26 private static final String JDBC_ORACLE_DRIVER_NAME = "oracle.jdbc.OracleDriver";
27
28 private ClassFacade classFacade;
29
30
31
32
33 private static OracleDatabaseManager instance;
34
35
36
37
38 private boolean isConnected;
39
40
41
42
43 private String schemaName;
44
45
46
47
48
49 private String userName;
50
51
52
53
54 private String userPassword;
55
56
57
58
59 private String oracleSID;
60
61
62
63
64 private String hostname;
65
66
67
68
69 private String port;
70
71
72
73
74 private Connection defaultConnection;
75
76
77
78
79 private OracleDatabaseManager() {
80 this.isConnected = false;
81 }
82
83 @Override
84 public void setConnectionConstantsFromFile(final PropertiesReader reader) throws IOException {
85 this.schemaName = reader.getProperty("schema");
86 this.userName = reader.getProperty("username");
87 this.userPassword = reader.getProperty("password");
88 this.oracleSID = reader.getProperty("sid");
89 this.hostname = reader.getProperty("hostname");
90 this.port = reader.getProperty("port");
91 }
92
93
94
95
96
97
98
99
100 @Override
101 public void connect() throws PersistenceException {
102 try {
103 Class.forName(JDBC_ORACLE_DRIVER_NAME);
104 this.isConnected = true;
105 this.defaultConnection =
106 DriverManager.getConnection(
107 CONNECTION_PREFIX_ORACLE + this.getHostname() + JDBC_SEPERATOR + this.getPort()
108 + JDBC_SEPERATOR + this.getOracleSID(),
109 this.getUserName(),
110 this.getUserPassword());
111 } catch (final ClassNotFoundException e) {
112 throw new OracleJDBCDriverMissing();
113 } catch (final SQLException e) {
114 throw new NotConnectableException(this, e);
115 }
116 }
117
118
119
120
121
122
123
124
125
126 public Connection getConnection() throws PersistenceException {
127 if (this.isConnected) {
128 return this.defaultConnection;
129 }
130 throw new NotConnectedException();
131 }
132
133 @Override
134 public ClassFacade getClassFacade() {
135 if (this.classFacade == null) {
136 this.classFacade = new OracleClassFacadeImplementation(this);
137 }
138 return this.classFacade;
139 }
140
141 @Override
142 public ObjectFacade getObjectFacade() throws ClassFacadeUninitializedException {
143 return new OracleObjectFacadeImplementation(this, this.getClassFacade().getTypeManager());
144 }
145
146 @Override
147 public void resetDatabase() throws PersistenceException, IOException {
148 final OracleDataBasePreparator prep = new OracleDataBasePreparator();
149 final boolean tableStructureValid = prep.isTableStructureValid();
150 if (tableStructureValid) {
151 this.clearTables();
152 } else {
153 prep.dropWholeSchema();
154 prep.createWholeSchema();
155 }
156 final boolean areProceduresCreated = prep.areProceduresCreated();
157 if (!areProceduresCreated) {
158 prep.createProcedures();
159 }
160 }
161
162 @Override
163 public void clearTables() throws PersistenceException {
164 new OracleObjectFacadeImplementation(this, null).clear();
165 this.getClassFacade().clear();
166 }
167
168
169
170
171
172
173 public static synchronized OracleDatabaseManager getInstance() {
174 if (instance == null) {
175 instance = new OracleDatabaseManager();
176 }
177 return instance;
178 }
179
180 @Override
181 public void disconnect() throws PersistenceException {
182 try {
183 this.getConnection().close();
184 } catch (final SQLException e) {
185 throw new OtherSQLException(e);
186 }
187 this.isConnected = false;
188
189 }
190
191 @Override
192 public boolean isConnected() {
193 return this.isConnected;
194 }
195
196
197
198
199
200 public String getSchemaName() {
201 return this.schemaName;
202 }
203
204
205
206
207
208 public String getUserName() {
209 return this.userName;
210 }
211
212
213
214
215
216 public String getUserPassword() {
217 return this.userPassword;
218 }
219
220
221
222
223
224 public String getOracleSID() {
225 return this.oracleSID;
226 }
227
228
229
230
231
232 public String getHostname() {
233 return this.hostname;
234 }
235
236
237
238
239
240 public String getPort() {
241 return this.port;
242 }
243
244 }