Skip to content

Method: getValueAsURI(Object)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.utils;
14:
15: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
16: import cz.cvut.kbss.jopa.model.annotations.Transient;
17: import cz.cvut.kbss.jopa.model.metamodel.*;
18: import cz.cvut.kbss.ontodriver.exception.PrimaryKeyNotSetException;
19: import cz.cvut.kbss.ontodriver.exception.UnassignableIdentifierException;
20:
21: import java.lang.reflect.Field;
22: import java.lang.reflect.Modifier;
23: import java.net.URI;
24: import java.util.*;
25:
26: /**
27: * Utility class for entity properties.
28: */
29: public class EntityPropertiesUtils {
30:
31: /**
32: * Private constructor
33: */
34: private EntityPropertiesUtils() {
35: throw new AssertionError("I am not for instantiation.");
36: }
37:
38: /**
39: * Extracts primary key from the specified {@code entity} and returns it. </p>
40: *
41: * @param entity The entity to extract primary key from
42: * @param metamodel Metamodel
43: * @return IRI of the entity or null if it is not set
44: * @throws NullPointerException If {@code entity} or {@code metamodel} is null
45: * @throws OWLPersistenceException If {@code entity} is not an entity or if the identifier is of an unknown type
46: */
47: public static Object getPrimaryKey(Object entity, Metamodel metamodel) {
48: Objects.requireNonNull(entity);
49: Objects.requireNonNull(metamodel);
50:
51: Object fieldValue;
52: final EntityType<?> type = metamodel.entity(entity.getClass());
53: fieldValue = getFieldValue(type.getIdentifier().getJavaField(), entity);
54: return fieldValue;
55: }
56:
57: /**
58: * Sets value of the specified field.
59: *
60: * @param field Field to set value on
61: * @param instance Target instance (may be null for static fields)
62: * @param value The value to set
63: */
64: public static void setFieldValue(Field field, Object instance, Object value) {
65: Objects.requireNonNull(field);
66: if (!field.isAccessible()) {
67: field.setAccessible(true);
68: }
69: try {
70: field.set(instance, value);
71: } catch (IllegalAccessException e) {
72: throw new OWLPersistenceException("Unable to set field value.", e);
73: }
74: }
75:
76: /**
77: * Gets value of the specified field from the specified instance.
78: *
79: * @param field Field to get value of
80: * @param instance Instance that contains the field
81: * @return Field value
82: */
83: public static Object getFieldValue(Field field, Object instance) {
84: Objects.requireNonNull(field);
85: if (!field.isAccessible()) {
86: field.setAccessible(true);
87: }
88: try {
89: return field.get(instance);
90: } catch (IllegalAccessException e) {
91: throw new OWLPersistenceException("Unable to extract field value.", e);
92: }
93: }
94:
95: /**
96: * Gets value of the specified attribute.
97: *
98: * @param attribute Attribute to extract value of
99: * @param instance Instance from which value will be extracted
100: * @return Attribute value
101: */
102: public static Object getAttributeValue(FieldSpecification<?, ?> attribute, Object instance) {
103: Objects.requireNonNull(attribute);
104: final Field field = attribute.getJavaField();
105: return getFieldValue(field, instance);
106: }
107:
108: /**
109: * Extracts entity's primary key according to the specified entity type.
110: *
111: * @param entity Entity
112: * @param et Entity type
113: * @return Primary key, possibly null
114: */
115: public static <T> URI getPrimaryKey(T entity, EntityType<?> et) {
116: try {
117: final Object id = getFieldValue(et.getIdentifier().getJavaField(), entity);
118: if (id == null) {
119: return null;
120: }
121: return getValueAsURI(id);
122: } catch (IllegalArgumentException e) {
123: throw new OWLPersistenceException("Unable to extract entity identifier.", e);
124: }
125: }
126:
127: /**
128: * Sets the specified primary key on the specified entity.
129: *
130: * @param primaryKey The key to set
131: * @param entity Target entity
132: * @param et Entity type
133: */
134: public static <T> void setPrimaryKey(Object primaryKey, T entity, EntityType<T> et) {
135: final Identifier id = et.getIdentifier();
136: final Field idField = id.getJavaField();
137: try {
138: final Object assignablePk = IdentifierTransformer.transformToIdentifier(primaryKey, idField.getType());
139: setFieldValue(idField, entity, assignablePk);
140: } catch (IllegalArgumentException e) {
141: throw new UnassignableIdentifierException(e);
142: }
143: }
144:
145: /**
146: * Transforms the specified value to URI (if possible). </p>
147: *
148: * @param value The value to transform
149: * @return {@code URI}
150: * @throws NullPointerException If {@code value} is {@code null}
151: * @throws IllegalArgumentException If {@code value} cannot be transformed to URI
152: */
153: public static URI getValueAsURI(Object value) {
154: Objects.requireNonNull(value, ErrorUtils.constructNPXMessage("value"));
155:
156: return IdentifierTransformer.valueAsUri(value);
157: }
158:
159: /**
160: * Gets all instance fields of the specified class, including inherited ones. </p>
161: *
162: * @param cls The class to search
163: * @return List of declared fields
164: */
165: public static List<Field> getAllFields(Class<?> cls) {
166: final List<Field> fields = new ArrayList<>();
167: fields.addAll(Arrays.asList(cls.getDeclaredFields()));
168: Class<?> tmp = cls.getSuperclass();
169: while (tmp != null) {
170: fields.addAll(Arrays.asList(tmp.getDeclaredFields()));
171: tmp = tmp.getSuperclass();
172: }
173: Iterator<Field> it = fields.iterator();
174: while (it.hasNext()) {
175: Field f = it.next();
176: if (Modifier.isStatic(f.getModifiers())) {
177: it.remove();
178: }
179: }
180: return fields;
181: }
182:
183: /**
184: * Verifies, that the primary key (identifier) of the specified instance is generated. </p>
185: * <p>
186: * If not, an exception is thrown.
187: *
188: * @param instance The instance to verify
189: * @param entityType Entity type of the instance, as specified by metamodel
190: * @throws PrimaryKeyNotSetException If the identifier is not generated
191: */
192: public static void verifyIdentifierIsGenerated(Object instance, EntityType<?> entityType) {
193: if (!entityType.getIdentifier().isGenerated()) {
194: throw new PrimaryKeyNotSetException("The id for entity " + instance
195: + " is null and it is not specified as \'generated\' ");
196: }
197: }
198:
199: /**
200: * Checks whether the specified field should not be persisted, i.e. whether it is transient in the persistence
201: * sense.
202: * <p>
203: * A field is transient if it is:
204: * <pre>
205: * <ul>
206: * <li>static</li>
207: * <li>or final</li>
208: * <li>or transient</li>
209: * <li>or annotated with the {@link Transient} annotation</li>
210: * </ul>
211: * </pre>
212: *
213: * @param field The field to investigate
214: * @return Whether the field is transient
215: */
216: public static boolean isFieldTransient(Field field) {
217: Objects.requireNonNull(field);
218: final int modifiers = field.getModifiers();
219: if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers) || Modifier.isTransient(modifiers)) {
220: return true;
221: }
222: final Transient transientAnnotation = field.getAnnotation(Transient.class);
223: return transientAnnotation != null;
224: }
225: }