Skip to content

Method: ObjectConverter(boolean)

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.oom.converter;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
22: import cz.cvut.kbss.ontodriver.model.LangString;
23: import cz.cvut.kbss.ontodriver.model.NamedResource;
24:
25: /**
26: * Allows to convert values between arbitrary types.
27: * <p>
28: * The main intended use is for annotation property mapping to attributes of type {@link Object}, which can hold both
29: * literal values and references to other individuals. In that case, the loaded instance is a {@link NamedResource} and
30: * needs to be transformed to a {@link java.net.URI} to prevent the internal OntoDriver API from leaking into the
31: * application.
32: * <p>
33: * Similarly, OntoDriver API's {@link LangString} is transformed to {@link MultilingualString}.
34: * <p>
35: * In all other cases, the values will be just returned without any conversion.
36: */
37: public class ObjectConverter implements ConverterWrapper<Object, Object> {
38:
39: private final boolean preferMultilingualString;
40:
41: public ObjectConverter() {
42: this.preferMultilingualString = false;
43: }
44:
45: public ObjectConverter(boolean preferMultilingualString) {
46: this.preferMultilingualString = preferMultilingualString;
47: }
48:
49: @Override
50: public Object convertToAxiomValue(Object value) {
51: if (IdentifierTransformer.isValidIdentifierType(value.getClass()) && !(value instanceof String)) {
52: return NamedResource.create(IdentifierTransformer.valueAsUri(value));
53: }
54: return value;
55: }
56:
57: @Override
58: public Object convertToAttribute(Object value) {
59: if (value instanceof NamedResource) {
60: return ((NamedResource) value).getIdentifier();
61: } else if (value instanceof LangString) {
62: final LangString ls = (LangString) value;
63: if (preferMultilingualString) {
64: final MultilingualString ms = new MultilingualString();
65: ms.set(ls.getLanguage().orElse(null), ls.getValue());
66: return ms;
67: } else {
68: return ls.getValue();
69: }
70: }
71: return value;
72: }
73:
74: @Override
75: public boolean supportsAxiomValueType(Class<?> type) {
76: return true;
77: }
78:
79: public boolean doesPreferMultilingualString() {
80: return preferMultilingualString;
81: }
82: }