View Javadoc
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   * A Class to access an oracle database with a JDBC Driver.
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  	 * The Oracle Database Manager is a singleton, therefore one single instance is stored in this static field.
32  	 */
33  	private static OracleDatabaseManager instance;
34  	
35  	/**
36  	 * A boolean flag indicating whether a connection to the database is established or not.
37  	 */
38  	private boolean isConnected;
39  	
40  	/**
41  	 * The Name of the Oracle Schema, which the Manager will connect to. Usually its the same as the user name.
42  	 */
43  	private String schemaName;
44  	
45  	/**
46  	 * The Username of an valid oracle user account, which has resource privilegies and has full access to packages in
47  	 * the given Schema.
48  	 */
49  	private String userName;
50  	
51  	/**
52  	 * The password of the given user. Password will be stored in plain text.
53  	 */
54  	private String userPassword;
55  	
56  	/**
57  	 * The SID of the Oracle Database.
58  	 */
59  	private String oracleSID;
60  	
61  	/**
62  	 * The name of the host where the oracle database is running.
63  	 */
64  	private String hostname;
65  	
66  	/**
67  	 * The port on which the Oracle Net Listener listens.
68  	 */
69  	private String port;
70  	
71  	/**
72  	 * The Oracle Database Manager will establish a single connection.
73  	 */
74  	private Connection defaultConnection;
75  	
76  	/**
77  	 * Creates a new unconnected Database Manager.
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  	 * @see de.fhdw.wtf.persistence.facade.DatabaseManager#connect()
95  	 * @throws OracleJDBCDriverMissing
96  	 *             if JDBC driver is missing
97  	 * @throws NotConnectableException
98  	 *             if an SQL error occurs
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 	 * This Method provides the default connection object to the Database. This Method will only works if connect has
120 	 * been successfully executed before otherwise an exception will be thrown.
121 	 * 
122 	 * @return Provides the default database connection.
123 	 * @throws PersistenceException
124 	 *             This Exception will be thrown if the Manager has not been connected to Orcle before.
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 	 * Provides the singleton instance of the Oracle Database Manager.
170 	 * 
171 	 * @return The Oracle Database manager.
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 	 * @return Provides the set Schema Name.
199 	 */
200 	public String getSchemaName() {
201 		return this.schemaName;
202 	}
203 	
204 	/**
205 	 * 
206 	 * @return Provides the set Username.
207 	 */
208 	public String getUserName() {
209 		return this.userName;
210 	}
211 	
212 	/**
213 	 * 
214 	 * @return Provides the set Password.
215 	 */
216 	public String getUserPassword() {
217 		return this.userPassword;
218 	}
219 	
220 	/**
221 	 * 
222 	 * @return Provides the Set Oracle SID.
223 	 */
224 	public String getOracleSID() {
225 		return this.oracleSID;
226 	}
227 	
228 	/**
229 	 * 
230 	 * @return Provides the set Hostname.
231 	 */
232 	public String getHostname() {
233 		return this.hostname;
234 	}
235 	
236 	/**
237 	 * 
238 	 * @return Provides the set port.
239 	 */
240 	public String getPort() {
241 		return this.port;
242 	}
243 	
244 }