View Javadoc
1   package de.fhdw.wtf.context.core;
2   
3   import java.math.BigInteger;
4   import java.util.Collection;
5   import java.util.HashSet;
6   
7   import de.fhdw.wtf.persistence.exception.ObjectNotFoundException;
8   import de.fhdw.wtf.persistence.exception.PersistenceException;
9   import de.fhdw.wtf.persistence.exception.RuntimePersistenceException;
10  import de.fhdw.wtf.persistence.facade.ObjectFacade;
11  import de.fhdw.wtf.persistence.facade.TypeManager;
12  import de.fhdw.wtf.persistence.meta.Link;
13  import de.fhdw.wtf.persistence.meta.MapLink;
14  import de.fhdw.wtf.persistence.meta.Object;
15  import de.fhdw.wtf.persistence.meta.Transaction;
16  import de.fhdw.wtf.persistence.meta.UnidirectionalLink;
17  import de.fhdw.wtf.persistence.meta.UserObject;
18  import de.fhdw.wtf.persistence.meta.UserType;
19  import de.fhdw.wtf.persistence.utils.Tuple;
20  
21  /**
22   * A class to represent an intermediate interface to the persistence layer. A context belongs to one long Transaction.
23   * It encapsulates database access and is capable of providing a caching mechanism.
24   * 
25   */
26  public class PersistenceContext extends ContextWithDatabaseAccess {
27  	
28  	/**
29  	 * The long transaction of this context.
30  	 */
31  	public final Transaction transaction;
32  	
33  	/**
34  	 * Creates a new PersistenceContext.
35  	 * 
36  	 * @param objectFacade
37  	 *            The database facade.
38  	 * @param transaction
39  	 *            The long transaction of this context.
40  	 */
41  	public PersistenceContext(final ObjectFacade objectFacade, final Transaction transaction) {
42  		super(objectFacade);
43  		this.transaction = transaction;
44  	}
45  	
46  	@Override
47  	public Collection<UserObject> find(final String associationName, final String value) {
48  		try {
49  			return this.objectFacade.find(
50  					this.typeManager.getUnidirectionalAssociationForName(associationName),
51  					value,
52  					this.transaction);
53  		} catch (final PersistenceException e) {
54  			Logger.getInstance().log(e);
55  			throw new RuntimePersistenceException();
56  		}
57  	}
58  	
59  	@Override
60  	public Collection<UserObject> find(final String associationName, final BigInteger value) {
61  		try {
62  			return this.objectFacade.find(
63  					this.typeManager.getUnidirectionalAssociationForName(associationName),
64  					value,
65  					this.transaction);
66  		} catch (final PersistenceException e) {
67  			Logger.getInstance().log(e);
68  			throw new RuntimePersistenceException();
69  		}
70  	}
71  	
72  	@Override
73  	public Collection<Tuple<UnidirectionalLink, UserObject>> inverseGet(final String associationName,
74  			final UserObject object) {
75  		try {
76  			return this.objectFacade.inverseGet(
77  					object,
78  					this.typeManager.getUnidirectionalAssociationForName(associationName),
79  					this.transaction);
80  		} catch (final PersistenceException e) {
81  			Logger.getInstance().log(e);
82  			throw new RuntimePersistenceException();
83  		}
84  	}
85  	
86  	@Override
87  	public Collection<Tuple<UnidirectionalLink, Object>> get(final UserObject object, final String associationName) {
88  		try {
89  			return this.objectFacade.get(
90  					object,
91  					this.typeManager.getUnidirectionalAssociationForName(associationName),
92  					this.transaction);
93  		} catch (final PersistenceException e) {
94  			Logger.getInstance().log(e);
95  			throw new RuntimePersistenceException();
96  		}
97  	}
98  	
99  	@Override
100 	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
101 			final String associationName,
102 			final String key) {
103 		try {
104 			return this.objectFacade.get(
105 					object,
106 					this.typeManager.getMapAssociationForName(associationName),
107 					key,
108 					this.transaction);
109 		} catch (final ObjectNotFoundException e) {
110 			return new HashSet<>();
111 		} catch (final PersistenceException e) {
112 			Logger.getInstance().log(e);
113 			throw new RuntimePersistenceException();
114 		}
115 	}
116 	
117 	@Override
118 	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
119 			final String associationName,
120 			final BigInteger key) {
121 		try {
122 			return this.objectFacade.get(
123 					object,
124 					this.typeManager.getMapAssociationForName(associationName),
125 					key,
126 					this.transaction);
127 		} catch (final ObjectNotFoundException e) {
128 			return new HashSet<>();
129 		} catch (final PersistenceException e) {
130 			Logger.getInstance().log(e);
131 			throw new RuntimePersistenceException();
132 		}
133 	}
134 	
135 	@Override
136 	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
137 			final String associationName,
138 			final UserObject key) {
139 		try {
140 			return this.objectFacade.get(
141 					object,
142 					this.typeManager.getMapAssociationForName(associationName),
143 					key,
144 					this.transaction);
145 		} catch (final ObjectNotFoundException e) {
146 			return new HashSet<>();
147 		} catch (final PersistenceException e) {
148 			Logger.getInstance().log(e);
149 			throw new RuntimePersistenceException();
150 		}
151 	}
152 	
153 	@Override
154 	public UserObject create(final String typeName) {
155 		try {
156 			return this.objectFacade.create((UserType) this.typeManager.getTypeforName(typeName), this.transaction);
157 		} catch (final PersistenceException e) {
158 			Logger.getInstance().log(e);
159 			throw new RuntimePersistenceException();
160 		}
161 	}
162 	
163 	@Override
164 	public UnidirectionalLink set(final UserObject object, final String associationName, final String val) {
165 		try {
166 			return this.objectFacade.set(
167 					object,
168 					this.typeManager.getUnidirectionalAssociationForName(associationName),
169 					val,
170 					this.transaction);
171 		} catch (final PersistenceException e) {
172 			Logger.getInstance().log(e);
173 			throw new RuntimePersistenceException();
174 		}
175 	}
176 	
177 	@Override
178 	public UnidirectionalLink set(final UserObject object, final String associationName, final BigInteger val) {
179 		try {
180 			return this.objectFacade.set(
181 					object,
182 					this.typeManager.getUnidirectionalAssociationForName(associationName),
183 					val,
184 					this.transaction);
185 		} catch (final PersistenceException e) {
186 			Logger.getInstance().log(e);
187 			throw new RuntimePersistenceException();
188 		}
189 	}
190 	
191 	@Override
192 	public UnidirectionalLink set(final UserObject object, final String associationName, final UserObject target) {
193 		try {
194 			return this.objectFacade.set(
195 					object,
196 					this.typeManager.getUnidirectionalAssociationForName(associationName),
197 					target,
198 					this.transaction);
199 		} catch (final PersistenceException e) {
200 			Logger.getInstance().log(e);
201 			throw new RuntimePersistenceException();
202 		}
203 	}
204 	
205 	@Override
206 	public MapLink put(final UserObject owner, final String associationName, final UserObject target, final String key) {
207 		try {
208 			return this.objectFacade.put(
209 					owner,
210 					this.typeManager.getMapAssociationForName(associationName),
211 					target,
212 					key,
213 					this.transaction);
214 		} catch (final PersistenceException e) {
215 			Logger.getInstance().log(e);
216 			throw new RuntimePersistenceException();
217 		}
218 	}
219 	
220 	@Override
221 	public MapLink put(final UserObject owner, final String associationName, final BigInteger target, final String key) {
222 		try {
223 			return this.objectFacade.put(
224 					owner,
225 					this.typeManager.getMapAssociationForName(associationName),
226 					target,
227 					key,
228 					this.transaction);
229 		} catch (final PersistenceException e) {
230 			Logger.getInstance().log(e);
231 			throw new RuntimePersistenceException();
232 		}
233 	}
234 	
235 	@Override
236 	public MapLink put(final UserObject owner, final String associationName, final String target, final String key) {
237 		try {
238 			return this.objectFacade.put(
239 					owner,
240 					this.typeManager.getMapAssociationForName(associationName),
241 					target,
242 					key,
243 					this.transaction);
244 		} catch (final PersistenceException e) {
245 			Logger.getInstance().log(e);
246 			throw new RuntimePersistenceException();
247 		}
248 	}
249 	
250 	@Override
251 	public MapLink put(final UserObject owner,
252 			final String associationName,
253 			final UserObject target,
254 			final BigInteger key) {
255 		try {
256 			return this.objectFacade.put(
257 					owner,
258 					this.typeManager.getMapAssociationForName(associationName),
259 					target,
260 					key,
261 					this.transaction);
262 		} catch (final PersistenceException e) {
263 			Logger.getInstance().log(e);
264 			throw new RuntimePersistenceException();
265 		}
266 	}
267 	
268 	@Override
269 	public MapLink put(final UserObject owner,
270 			final String associationName,
271 			final BigInteger target,
272 			final BigInteger key) {
273 		try {
274 			return this.objectFacade.put(
275 					owner,
276 					this.typeManager.getMapAssociationForName(associationName),
277 					target,
278 					key,
279 					this.transaction);
280 		} catch (final PersistenceException e) {
281 			Logger.getInstance().log(e);
282 			throw new RuntimePersistenceException();
283 		}
284 	}
285 	
286 	@Override
287 	public MapLink put(final UserObject owner, final String associationName, final String target, final BigInteger key) {
288 		try {
289 			return this.objectFacade.put(
290 					owner,
291 					this.typeManager.getMapAssociationForName(associationName),
292 					target,
293 					key,
294 					this.transaction);
295 		} catch (final PersistenceException e) {
296 			Logger.getInstance().log(e);
297 			throw new RuntimePersistenceException();
298 		}
299 	}
300 	
301 	@Override
302 	public MapLink put(final UserObject owner,
303 			final String associationName,
304 			final UserObject target,
305 			final UserObject key) {
306 		try {
307 			return this.objectFacade.put(
308 					owner,
309 					this.typeManager.getMapAssociationForName(associationName),
310 					target,
311 					key,
312 					this.transaction);
313 		} catch (final PersistenceException e) {
314 			Logger.getInstance().log(e);
315 			throw new RuntimePersistenceException();
316 		}
317 	}
318 	
319 	@Override
320 	public MapLink put(final UserObject owner,
321 			final String associationName,
322 			final BigInteger target,
323 			final UserObject key) {
324 		try {
325 			return this.objectFacade.put(
326 					owner,
327 					this.typeManager.getMapAssociationForName(associationName),
328 					target,
329 					key,
330 					this.transaction);
331 		} catch (final PersistenceException e) {
332 			Logger.getInstance().log(e);
333 			throw new RuntimePersistenceException();
334 		}
335 	}
336 	
337 	@Override
338 	public MapLink put(final UserObject owner, final String associationName, final String target, final UserObject key) {
339 		try {
340 			return this.objectFacade.put(
341 					owner,
342 					this.typeManager.getMapAssociationForName(associationName),
343 					target,
344 					key,
345 					this.transaction);
346 		} catch (final PersistenceException e) {
347 			Logger.getInstance().log(e);
348 			throw new RuntimePersistenceException();
349 		}
350 	}
351 	
352 	@Override
353 	public void commit() {
354 		try {
355 			this.objectFacade.commit(this.transaction);
356 		} catch (final PersistenceException e) {
357 			Logger.getInstance().log(e);
358 			throw new RuntimePersistenceException();
359 		}
360 	}
361 	
362 	@Override
363 	public void rollback() {
364 		try {
365 			this.objectFacade.rollback(this.transaction);
366 		} catch (final PersistenceException e) {
367 			Logger.getInstance().log(e);
368 			throw new RuntimePersistenceException();
369 		}
370 	}
371 	
372 	@Override
373 	public void savePoint() {
374 		try {
375 			this.objectFacade.savePoint(this.transaction);
376 		} catch (final PersistenceException e) {
377 			Logger.getInstance().log(e);
378 			throw new RuntimePersistenceException();
379 		}
380 	}
381 	
382 	@Override
383 	public void rollbackToSavePoint() {
384 		try {
385 			this.objectFacade.rollbackToSavePoint(this.transaction);
386 		} catch (final PersistenceException e) {
387 			Logger.getInstance().log(e);
388 			throw new RuntimePersistenceException();
389 			
390 		}
391 	}
392 	
393 	@Override
394 	public void unset(final Link toUnset) {
395 		try {
396 			this.objectFacade.unset(toUnset, this.transaction);
397 		} catch (final PersistenceException e) {
398 			Logger.getInstance().log(e);
399 			throw new RuntimePersistenceException();
400 		}
401 	}
402 	
403 	@Override
404 	public TypeManager getTypeManager() {
405 		return this.objectFacade.getTypeManager();
406 	}
407 	
408 	@Override
409 	public Collection<UserObject> getObjectsByType(final String typeName) {
410 		try {
411 			return this.objectFacade.findAllObjects(
412 					(UserType) this.typeManager.getTypeforName(typeName),
413 					this.transaction);
414 		} catch (final PersistenceException e) {
415 			Logger.getInstance().log(e);
416 			throw new RuntimePersistenceException();
417 		}
418 	}
419 	
420 	@Override
421 	public UserObject checkout(final long object) {
422 		try {
423 			final UserObject result = this.objectFacade.checkUserObjectOut(object, this.transaction);
424 			return result;
425 		} catch (final PersistenceException e) {
426 			Logger.getInstance().log(e);
427 			throw new RuntimePersistenceException();
428 		}
429 	}
430 	
431 }