Skip to content

Method: getName()

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.datatype.DatatypeTransformer;
21: import cz.cvut.kbss.jopa.exception.SparqlResultMappingException;
22: import cz.cvut.kbss.jopa.model.annotations.VariableResult;
23: import cz.cvut.kbss.jopa.model.metamodel.Converters;
24: import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
25: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
26: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
27: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
28:
29: /**
30: * Maps SPARQL query result to target value based on a {@link VariableResult} configuration.
31: */
32: class VariableResultMapper implements SparqlResultMapper {
33:
34: private final String name;
35: private final Class<?> targetType;
36:
37: VariableResultMapper(VariableResult mapping) {
38: this.name = mapping.name();
39: this.targetType = mapping.type();
40: }
41:
42: String getName() {
43: return name;
44: }
45:
46: Class<?> getTargetType() {
47: return targetType;
48: }
49:
50: /**
51: * Maps value from the current line of the specified result set according to the {@link VariableResult}
52: * configuration represented by this instance.
53: *
54: * @param resultRow Query result set to read
55: * @param uow UnitOfWork instance
56: * @return The mapped value
57: */
58: @Override
59: public Object map(ResultRow resultRow, UnitOfWork uow) {
60: try {
61: if (!resultRow.isBound(name)) {
62: return null;
63: }
64: final Object value = resultRow.getObject(name);
65: if (!void.class.equals(targetType)) {
66: if (Converters.getDefaultConverters().containsKey(targetType)) {
67: return ((ConverterWrapper) Converters.getDefaultConverters()
68: .get(targetType)).convertToAttribute(value);
69: }
70: return DatatypeTransformer.transform(value, targetType);
71: }
72: return value;
73: } catch (OntoDriverException e) {
74: throw new SparqlResultMappingException(e);
75: }
76: }
77: }