Package: EntityResultMapper
EntityResultMapper
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
EntityResultMapper(IdentifiableEntityType) |
|
|
|
|
|
||||||||||||||||||||
addFieldMapper(FieldResultMapper) |
|
|
|
|
|
||||||||||||||||||||
getEntityType() |
|
|
|
|
|
||||||||||||||||||||
getFieldMappers() |
|
|
|
|
|
||||||||||||||||||||
lambda$map$0(ResultRow, Object, UnitOfWork, LoadStateDescriptor, FieldResultMapper) |
|
|
|
|
|
||||||||||||||||||||
map(ResultRow, UnitOfWork) |
|
|
|
|
|
Coverage
1: /*
2: * JOPA
3: * Copyright (C) 2023 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.query.mapper;
19:
20: import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
21: import cz.cvut.kbss.jopa.model.LoadState;
22: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
23: import cz.cvut.kbss.jopa.model.lifecycle.PostLoadInvoker;
24: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
25: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
26: import cz.cvut.kbss.jopa.model.metamodel.IdentifiableEntityType;
27: import cz.cvut.kbss.jopa.sessions.descriptor.LoadStateDescriptor;
28: import cz.cvut.kbss.jopa.sessions.descriptor.LoadStateDescriptorFactory;
29: import cz.cvut.kbss.jopa.sessions.util.CloneRegistrationDescriptor;
30: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
31: import cz.cvut.kbss.jopa.utils.ReflectionUtils;
32: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
33:
34: import java.util.ArrayList;
35: import java.util.Collections;
36: import java.util.List;
37:
38: /**
39: * Maps result of a SPARQL SELECT query to an entity instance.
40: */
41: class EntityResultMapper<T> implements SparqlResultMapper {
42:
43: private final IdentifiableEntityType<T> et;
44:
45: private final List<FieldResultMapper> fieldMappers = new ArrayList<>();
46:
47: EntityResultMapper(IdentifiableEntityType<T> et) {
48: this.et = et;
49: }
50:
51: void addFieldMapper(FieldResultMapper mapper) {
52: fieldMappers.add(mapper);
53: }
54:
55: List<FieldResultMapper> getFieldMappers() {
56: return Collections.unmodifiableList(fieldMappers);
57: }
58:
59: EntityType<T> getEntityType() {
60: return et;
61: }
62:
63: @Override
64: public T map(ResultRow resultRow, UnitOfWork uow) {
65: try {
66: final T instance = ReflectionUtils.instantiateUsingDefaultConstructor(et.getJavaType());
67: final LoadStateDescriptor<T> loadStateDescriptor = LoadStateDescriptorFactory.createAllUnknown(instance, et);
68: fieldMappers.forEach(m -> {
69: m.map(resultRow, instance, uow);
70: loadStateDescriptor.setLoaded((FieldSpecification<? super T, ?>) m.getFieldSpecification(), LoadState.LOADED);
71: });
72: uow.getLoadStateRegistry().put(instance, loadStateDescriptor);
73: return et.getJavaType()
74: .cast(uow.registerExistingObject(instance, new CloneRegistrationDescriptor(new EntityDescriptor()).postCloneHandlers(List.of(new PostLoadInvoker(uow.getMetamodel())))));
75: } catch (cz.cvut.kbss.jopa.exception.InstantiationException e) {
76: // This is not expected, since an entity class must have a public no-arg constructor
77: throw new SparqlResultMappingException(e);
78: }
79: }
80: }