Skip to content

Method: triggerLazyLoading()

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.proxy.lazy;
19:
20: import cz.cvut.kbss.jopa.exception.LazyLoadingException;
21: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
22: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
23:
24: import java.util.Collection;
25: import java.util.Iterator;
26:
27: /**
28: * Collection proxy that triggers field lazy loading when accessed (and connected to an active persistence context).
29: *
30: * @param <O> Owner object type
31: * @param <T> Wrapped object type
32: */
33: abstract class LazyLoadingCollectionProxy<O, T extends Collection<E>, E> implements LazyLoadingProxy<T>, Collection<E> {
34:
35: protected final O owner;
36: protected final FieldSpecification<? super O, T> fieldSpec;
37: protected final UnitOfWork persistenceContext;
38:
39: private T value;
40:
41: public LazyLoadingCollectionProxy(O owner, FieldSpecification<? super O, T> fieldSpec,
42: UnitOfWork persistenceContext) {
43: this.owner = owner;
44: this.fieldSpec = fieldSpec;
45: this.persistenceContext = persistenceContext;
46: }
47:
48: @Override
49: public T triggerLazyLoading() {
50:• if (value != null) {
51: return value;
52: }
53:• if (persistenceContext == null || !persistenceContext.isActive()) {
54: throw new LazyLoadingException("No active persistence context is available in lazy loading proxy for attribute "
55: + fieldSpec + " of entity " + owner);
56: }
57: this.value = (T) persistenceContext.loadEntityField(owner, fieldSpec);
58: return value;
59: }
60:
61: @Override
62: public boolean isLoaded() {
63: return value != null;
64: }
65:
66: @Override
67: public T getLoadedValue() {
68: if (value == null) {
69: throw new IllegalStateException("Proxy has not been loaded, yet.");
70: }
71: return value;
72: }
73:
74: @Override
75: public int size() {
76: return triggerLazyLoading().size();
77: }
78:
79: @Override
80: public boolean isEmpty() {
81: return triggerLazyLoading().isEmpty();
82: }
83:
84: @Override
85: public boolean contains(Object o) {
86: return triggerLazyLoading().contains(o);
87: }
88:
89: @Override
90: public Iterator<E> iterator() {
91: return triggerLazyLoading().iterator();
92: }
93:
94: @Override
95: public Object[] toArray() {
96: return triggerLazyLoading().toArray();
97: }
98:
99: @Override
100: public <ET> ET[] toArray(ET[] a) {
101: return triggerLazyLoading().toArray(a);
102: }
103:
104: @Override
105: public boolean add(E e) {
106: return triggerLazyLoading().add(e);
107: }
108:
109: @Override
110: public boolean remove(Object o) {
111: return triggerLazyLoading().remove(o);
112: }
113:
114: @Override
115: public boolean containsAll(Collection<?> c) {
116: return triggerLazyLoading().containsAll(c);
117: }
118:
119: @Override
120: public boolean addAll(Collection<? extends E> c) {
121: return triggerLazyLoading().addAll(c);
122: }
123:
124: @Override
125: public boolean removeAll(Collection<?> c) {
126: return triggerLazyLoading().removeAll(c);
127: }
128:
129: @Override
130: public boolean retainAll(Collection<?> c) {
131: return triggerLazyLoading().retainAll(c);
132: }
133:
134: @Override
135: public void clear() {
136: triggerLazyLoading().clear();
137: }
138:
139: @Override
140: public String toString() {
141: return getClass().getSimpleName() + "[" + owner.getClass().getSimpleName() + "." + fieldSpec.getName() + "]";
142: }
143: }