Skip to content

Method: TwoStepInstanceLoader.TwoStepInstanceLoaderBuilder()

1: /**
2: * Copyright (C) 2016 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.metamodel.PolymorphicEntityTypeResolver;
21: import cz.cvut.kbss.jopa.sessions.LoadingParameters;
22: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
23: import cz.cvut.kbss.ontodriver.model.Axiom;
24: import cz.cvut.kbss.ontodriver.model.NamedResource;
25:
26: import java.net.URI;
27: import java.util.Set;
28:
29: class TwoStepInstanceLoader extends EntityInstanceLoader {
30:
31: TwoStepInstanceLoader(TwoStepInstanceLoaderBuilder builder) {
32: super(builder);
33: }
34:
35: @Override
36: <T> T loadEntity(LoadingParameters<T> loadingParameters) {
37: final NamedResource individual = NamedResource.create(loadingParameters.getIdentifier());
38: final EntityTypeImpl<T> rootEt = metamodel.entity(loadingParameters.getEntityType());
39: try {
40: final Set<Axiom<URI>> types = storageConnection.types().getTypes(individual,
41: loadingParameters.getDescriptor().getContext(), false);
42: final EntityType<? extends T> et =
43: new PolymorphicEntityTypeResolver<>(individual, rootEt, types).determineActualEntityType();
44: if (et == null) {
45: return null;
46: }
47: return loadInstance(loadingParameters, et);
48: } catch (OntoDriverException e) {
49: throw new StorageAccessException(e);
50: }
51: }
52:
53: static TwoStepInstanceLoaderBuilder builder() {
54: return new TwoStepInstanceLoaderBuilder();
55: }
56:
57: static class TwoStepInstanceLoaderBuilder extends EntityInstanceLoaderBuilder {
58:
59: @Override
60: EntityInstanceLoader build() {
61: return new TwoStepInstanceLoader(this);
62: }
63: }
64: }