Skip to content

Package: TwoStepInstanceLoader$TwoStepInstanceLoaderBuilder

TwoStepInstanceLoader$TwoStepInstanceLoaderBuilder

nameinstructionbranchcomplexitylinemethod
TwoStepInstanceLoader.TwoStepInstanceLoaderBuilder()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
build()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
18: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityTypeImpl;
20: import cz.cvut.kbss.jopa.oom.exceptions.EntityReconstructionException;
21: import cz.cvut.kbss.jopa.oom.metamodel.PolymorphicEntityTypeResolver;
22: import cz.cvut.kbss.jopa.sessions.LoadingParameters;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.model.Axiom;
25: import cz.cvut.kbss.ontodriver.model.NamedResource;
26:
27: import java.net.URI;
28: import java.util.Set;
29:
30: class TwoStepInstanceLoader extends EntityInstanceLoader {
31:
32: private TwoStepInstanceLoader(TwoStepInstanceLoaderBuilder builder) {
33: super(builder);
34: }
35:
36: @Override
37: <T> T loadEntity(LoadingParameters<T> loadingParameters) {
38: final EntityTypeImpl<T> rootEt = metamodel.entity(loadingParameters.getEntityType());
39: try {
40: final EntityType<? extends T> et = resolveEntityType(loadingParameters, rootEt);
41: if (et == null) {
42: return null;
43: }
44: return loadInstance(loadingParameters, et);
45: } catch (OntoDriverException e) {
46: throw new StorageAccessException(e);
47: }
48: }
49:
50: @Override
51: <T> T loadReference(LoadingParameters<T> loadingParameters) {
52: final EntityTypeImpl<T> rootEt = metamodel.entity(loadingParameters.getEntityType());
53: try {
54: final EntityType<? extends T> et = resolveEntityType(loadingParameters, rootEt);
55: return et != null ? entityBuilder.createEntityInstance(loadingParameters.getIdentifier(), et) : null;
56: } catch (OntoDriverException e) {
57: throw new StorageAccessException(e);
58: } catch (IllegalAccessException | InstantiationException e) {
59: throw new EntityReconstructionException(e);
60: }
61: }
62:
63: private <T> EntityType<? extends T> resolveEntityType(LoadingParameters<T> loadingParameters,
64: EntityTypeImpl<T> rootEt) throws OntoDriverException {
65: NamedResource individual = NamedResource.create(loadingParameters.getIdentifier());
66: final Set<Axiom<URI>> types = storageConnection.types().getTypes(individual,
67: loadingParameters.getDescriptor().getContext(), false);
68: return new PolymorphicEntityTypeResolver<>(individual, rootEt, types).determineActualEntityType();
69: }
70:
71: static TwoStepInstanceLoaderBuilder builder() {
72: return new TwoStepInstanceLoaderBuilder();
73: }
74:
75: static class TwoStepInstanceLoaderBuilder extends EntityInstanceLoaderBuilder {
76:
77: @Override
78: EntityInstanceLoader build() {
79: return new TwoStepInstanceLoader(this);
80: }
81: }
82: }