Skip to contentMethod: isNotInferred()
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.sessions.validator;
19:
20: import cz.cvut.kbss.jopa.model.annotations.FetchType;
21: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
22: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
23: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
24: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
25: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
26: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
27:
28: import java.util.Objects;
29: import java.util.function.Predicate;
30: import java.util.stream.Stream;
31:
32: public abstract class IntegrityConstraintsValidator {
33:
34: private static final IntegrityConstraintsValidator generalValidator = initGeneralValidator();
35:
36: private static IntegrityConstraintsValidator initGeneralValidator() {
37: final GeneralIntegrityConstraintsValidator validator = new GeneralIntegrityConstraintsValidator();
38: validator.addValidator(new CardinalityConstraintsValidator());
39: return validator;
40: }
41:
42:
43: public static IntegrityConstraintsValidator getValidator() {
44: return generalValidator;
45: }
46:
47: /**
48: * Validates integrity constraints of all attributes of the specified instance.
49: *
50: * @param instance The instance to validate
51: * @param et EntityType of the instance
52: * @param filters Filters allowing to specify attributes whose validation should be skipped
53: * @param <T> Entity class type
54: */
55: @SafeVarargs
56: public final <T> void validate(T instance, EntityType<T> et, Predicate<FieldSpecification<? super T, ?>>... filters) {
57: Objects.requireNonNull(instance);
58: Objects.requireNonNull(et);
59:
60: final Object id = EntityPropertiesUtils.getIdentifier(instance, et);
61: et.getFieldSpecifications().stream()
62: .filter(att -> Stream.of(filters).allMatch(p -> p.test(att)))
63: .forEach(att -> {
64: final Object value = EntityPropertiesUtils.getAttributeValue(att, instance);
65: validate(id, att, value);
66: });
67: }
68:
69: /**
70: * Validates integrity constraints for changes in the specified change set.
71: *
72: * @param changeSet The change set to validate
73: * @param metamodel Metamodel of the persistence unit
74: */
75: public void validate(ObjectChangeSet changeSet, Metamodel metamodel) {
76: Objects.requireNonNull(changeSet);
77: Objects.requireNonNull(metamodel);
78:
79: final EntityType<?> et = metamodel.entity(changeSet.getObjectClass());
80: final Object id = EntityPropertiesUtils.getIdentifier(changeSet.getCloneObject(), et);
81: for (ChangeRecord change : changeSet.getChanges()) {
82: validate(id, change.getAttribute(), change.getNewValue());
83: }
84: }
85:
86: /**
87: * Validates whether the specified value conforms to the attribute integrity constraints.
88: *
89: * @param identifier Instance identifier
90: * @param attribute Attribute metadata with integrity constraints
91: * @param attributeValue Value to be validated
92: */
93: public abstract void validate(Object identifier, FieldSpecification<?, ?> attribute, Object attributeValue);
94:
95: /**
96: * Creates a predicate filtering attributes whose fetch type is {@link FetchType#LAZY}.
97: *
98: * @param <T> Entity type
99: * @return Predicate
100: */
101: public static <T> Predicate<FieldSpecification<? super T, ?>> isNotLazy() {
102: return att -> att.getFetchType() != FetchType.LAZY;
103: }
104:
105: /**
106: * Creates a predicate filtering attributes that contain inferred values.
107: *
108: * @param <T> Entity type
109: * @return Predicate
110: */
111: public static <T> Predicate<FieldSpecification<? super T, ?>> isNotInferred() {
112: return att -> !att.isInferred();
113: }
114: }