01: /*
02: * EntityIterable.java
03: *
04: * Created on Jun 27, 2007, 8:22:02 AM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09: package it.biobytes.ammentos;
10:
11: import it.biobytes.ammentos.util.Invoker;
12: import java.util.Iterator;
13: import java.util.logging.Level;
14: import java.util.logging.Logger;
15:
16: /**
17: *
18: * @author davide
19: */
20: public abstract class EntityIterable<T> implements Iterable<T> {
21:
22: private Class<T> m_class;
23: // Getting persistent parent for deep loading, if present
24: Class super Class = null;
25: Field super PKey = null;
26:
27: public abstract void close();
28:
29: protected abstract Iterator<T> getIterator();
30:
31: private class EntityIterator<T> implements Iterator<T> {
32:
33: private Iterator<T> m_delegate;
34:
35: public EntityIterator(Iterator<T> delegate) {
36: m_delegate = delegate;
37: }
38:
39: public void remove() {
40: m_delegate.remove();
41: }
42:
43: public T next() {
44: T res = m_delegate.next();
45: try {
46: Metadata m = Ammentos.getMetadata(res.getClass());
47: Invoker.invokeHandlers(m.getAfterLoadHandlers(), res);
48: } catch (Exception e) {
49: throw new RuntimeException(e);
50: }
51:
52: if (super PKey != null) {
53: try {
54: Ammentos.loadDeeply(super Class, res, super PKey
55: .get(res));
56: } catch (PersistenceException ex) {
57: Logger.getLogger("global").log(Level.SEVERE, null,
58: ex);
59: }
60: }
61: return res;
62: }
63:
64: public boolean hasNext() {
65: return m_delegate.hasNext();
66: }
67: }
68:
69: public EntityIterable(Class<T> c) throws PersistenceException {
70:
71: Class[] cPath = Ammentos.getMetadata(c).inheritancePath();
72: if (cPath.length > 1) {
73: super Class = cPath[1];
74: }
75:
76: // Deep loading
77: if (super Class != null) {
78: // Getting superclass' primary key
79: super PKey = Ammentos.getMetadata(c).getSuperKeyField();
80: }
81: m_class = c;
82: }
83:
84: public final Iterator<T> iterator() {
85: return new EntityIterator<T>(getIterator());
86: }
87: }
|