Skip to contentMethod: createReferenceProxy(LoadingParameters)
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.oom;
19:
20: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
21: import cz.cvut.kbss.jopa.model.MetamodelImpl;
22: import cz.cvut.kbss.jopa.proxy.reference.EntityReferenceProxy;
23: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
24: import cz.cvut.kbss.jopa.sessions.util.LoadingParameters;
25: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
26:
27: import java.lang.reflect.InvocationTargetException;
28:
29: class EntityReferenceFactory {
30:
31: private final MetamodelImpl metamodel;
32:
33: private final UnitOfWork uow;
34:
35: EntityReferenceFactory(MetamodelImpl metamodel, UnitOfWork uow) {
36: this.metamodel = metamodel;
37: this.uow = uow;
38: }
39:
40: /**
41: * Creates entity reference proxy with initialized identifier and associated persistence context.
42: *
43: * @param loadingParameters Parameters for creating the reference
44: * @param <T> Type of the expected instance
45: * @return Entity reference proxy
46: */
47: <T> T createReferenceProxy(LoadingParameters<T> loadingParameters) {
48:• assert loadingParameters != null;
49:
50: final Class<? extends T> referenceProxyClass = metamodel.getEntityReferenceProxy(loadingParameters.getEntityClass());
51: try {
52: final T reference = referenceProxyClass.getDeclaredConstructor().newInstance();
53:• assert reference instanceof EntityReferenceProxy<?>
54: final EntityReferenceProxy<?> referenceProxy = (EntityReferenceProxy<?>) reference;
55: referenceProxy.setIdentifier(loadingParameters.getIdentifier());
56: referenceProxy.setType((Class) loadingParameters.getEntityClass());
57: referenceProxy.setPersistenceContext(uow);
58: referenceProxy.setDescriptor(loadingParameters.getDescriptor());
59: EntityPropertiesUtils.setIdentifier(loadingParameters.getIdentifier(), reference, metamodel.entity(loadingParameters.getEntityClass()));
60: return reference;
61: } catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
62: InvocationTargetException e) {
63: // We do not expect this to happen, as we generated the proxy class
64: throw new OWLPersistenceException("Unable to instantiate entity reference proxy class " + referenceProxyClass, e);
65: }
66: }
67: }