Skip to content

Package: IntegrityConstraintsValidator

IntegrityConstraintsValidator

nameinstructionbranchcomplexitylinemethod
IntegrityConstraintsValidator()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getValidator()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
initGeneralValidator()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
validate(Object, EntityType, boolean)
M: 0 C: 43
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
validate(ObjectChangeSet, Metamodel)
M: 0 C: 40
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.validator;
16:
17: import cz.cvut.kbss.jopa.model.annotations.FetchType;
18: import cz.cvut.kbss.jopa.model.metamodel.Attribute;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.model.metamodel.FieldSpecification;
21: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
22: import cz.cvut.kbss.jopa.sessions.ChangeRecord;
23: import cz.cvut.kbss.jopa.sessions.ObjectChangeSet;
24: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
25: import cz.cvut.kbss.jopa.utils.ErrorUtils;
26:
27: import java.util.Objects;
28:
29: public abstract class IntegrityConstraintsValidator {
30:
31: private static IntegrityConstraintsValidator generalValidator = initGeneralValidator();
32:
33: private static IntegrityConstraintsValidator initGeneralValidator() {
34: final GeneralIntegrityConstraintsValidator validator = new GeneralIntegrityConstraintsValidator();
35: validator.addValidator(new CardinalityConstraintsValidator());
36: return validator;
37: }
38:
39:
40: public static IntegrityConstraintsValidator getValidator() {
41: return generalValidator;
42: }
43:
44: /**
45: * Validates integrity constraints of all attributes of the specified instance.
46: *
47: * @param instance The instance to validate
48: * @param et EntityType of the instance
49: * @param skipLazy Whether to skip validation of lazily loaded attributes
50: * @param <T> Entity class type
51: */
52: public <T> void validate(T instance, EntityType<T> et, boolean skipLazy) {
53: Objects.requireNonNull(instance, ErrorUtils.getNPXMessageSupplier("instance"));
54: Objects.requireNonNull(et, ErrorUtils.getNPXMessageSupplier("et"));
55:
56: final Object id = EntityPropertiesUtils.getIdentifier(instance, et);
57:• for (Attribute<? super T, ?> att : et.getAttributes()) {
58:• if (skipLazy && att.getFetchType() == FetchType.LAZY) {
59: continue;
60: }
61: final Object value = EntityPropertiesUtils.getAttributeValue(att, instance);
62: validate(id, att, value);
63: }
64: }
65:
66: /**
67: * Validates integrity constraints for changes in the specified change set.
68: *
69: * @param changeSet The change set to validate
70: * @param metamodel Metamodel of the persistence unit
71: */
72: public void validate(ObjectChangeSet changeSet, Metamodel metamodel) {
73: Objects.requireNonNull(changeSet, ErrorUtils.getNPXMessageSupplier("changeSet"));
74: Objects.requireNonNull(metamodel, ErrorUtils.getNPXMessageSupplier("metamodel"));
75:
76: final EntityType<?> et = metamodel.entity(changeSet.getObjectClass());
77: final Object id = EntityPropertiesUtils.getIdentifier(changeSet.getCloneObject(), et);
78:• for (ChangeRecord change : changeSet.getChanges()) {
79: validate(id, change.getAttribute(), change.getNewValue());
80: }
81: }
82:
83: /**
84: * Validates whether the specified value conforms to the attribute integrity constraints.
85: *
86: * @param identifier Instance identifier
87: * @param attribute Attribute metadata with integrity constraints
88: * @param attributeValue Value to be validated
89: */
90: public abstract void validate(Object identifier, FieldSpecification<?, ?> attribute, Object attributeValue);
91: }