Skip to contentMethod: lambda$map$0(UnitOfWork, Object, Object)
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.query.mapper;
19:
20: import cz.cvut.kbss.jopa.model.annotations.FieldResult;
21: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
22: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
23: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
26: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
27:
28: import java.util.Optional;
29:
30: /**
31: * Mapping SPARQL SELECT results to object property fields.
32: * <p>
33: * This means that the referenced instance has to be loaded (unless the field is plain identifier).
34: */
35: class ObjectPropertyFieldResultMapper extends FieldResultMapper {
36:
37: ObjectPropertyFieldResultMapper(FieldResult fieldResult, FieldSpecification<?, ?> fieldSpec) {
38: super(fieldResult, fieldSpec);
39: }
40:
41: ObjectPropertyFieldResultMapper(FieldSpecification<?, ?> fieldSpec) {
42: super(fieldSpec);
43: }
44:
45: @Override
46: void map(ResultRow resultRow, Object target, UnitOfWork uow) {
47: final Optional<Object> id = getVariableValue(resultRow);
48: id.ifPresent(idValue -> {
49: final Object value = resolveValue(uow, idValue);
50: EntityPropertiesUtils.setFieldValue(getFieldSpecification().getJavaField(), target, value);
51: });
52: }
53:
54: private Object resolveValue(UnitOfWork uow, Object id) {
55: if (IdentifierTransformer.isValidIdentifierType(getFieldSpecification().getJavaType())) {
56: return IdentifierTransformer.transformToIdentifier(id, getFieldSpecification().getJavaType());
57: }
58: return uow.readObjectWithoutRegistration(getFieldSpecification().getJavaType(), id, new EntityDescriptor());
59: }
60: }