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: 48
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 9
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.Map;
28: import java.util.Objects;
29:
30: public abstract class IntegrityConstraintsValidator {
31:
32: private static IntegrityConstraintsValidator generalValidator = initGeneralValidator();
33:
34: private static IntegrityConstraintsValidator initGeneralValidator() {
35: final GeneralIntegrityConstraintsValidator validator = new GeneralIntegrityConstraintsValidator();
36: validator.addValidator(new CardinalityConstraintsValidator());
37: return validator;
38: }
39:
40:
41: public static IntegrityConstraintsValidator getValidator() {
42: return generalValidator;
43: }
44:
45: /**
46: * Validates integrity constraints of all attributes of the specified instance.
47: *
48: * @param instance The instance to validate
49: * @param et EntityType of the instance
50: * @param skipLazy Whether to skip validation of lazily loaded attributes
51: * @param <T> Entity class type
52: */
53: public <T> void validate(T instance, EntityType<T> et, boolean skipLazy) {
54: Objects.requireNonNull(instance, ErrorUtils.constructNPXMessage("instance"));
55: Objects.requireNonNull(et, ErrorUtils.constructNPXMessage("et"));
56:
57: final Object id = EntityPropertiesUtils.getPrimaryKey(instance, et);
58:• for (Attribute<T, ?> att : et.getDeclaredAttributes()) {
59:• if (skipLazy && att.getFetchType() == FetchType.LAZY) {
60: continue;
61: }
62: final Object value = EntityPropertiesUtils.getAttributeValue(att, instance);
63: validate(id, att, value);
64: }
65: }
66:
67: /**
68: * Validates integrity constraints for changes in the specified change set.
69: *
70: * @param changeSet The change set to validate
71: */
72: public void validate(ObjectChangeSet changeSet, Metamodel metamodel) {
73: Objects.requireNonNull(changeSet, ErrorUtils.constructNPXMessage("changeSet"));
74: Objects.requireNonNull(metamodel, ErrorUtils.constructNPXMessage("metamodel"));
75:
76: final EntityType<?> et = metamodel.entity(changeSet.getObjectClass());
77: final Object id = EntityPropertiesUtils.getPrimaryKey(changeSet.getCloneObject(), et);
78:• for (Map.Entry<String, ChangeRecord> entry : changeSet.getChanges().entrySet()) {
79: final FieldSpecification<?, ?> fieldSpec = et.getFieldSpecification(entry.getKey());
80: validate(id, fieldSpec, entry.getValue().getNewValue());
81: }
82: }
83:
84: /**
85: * Validates whether the specified value conforms to the attribute integrity constraints.
86: *
87: * @param identifier Instance identifier
88: * @param attribute Attribute metadata with integrity constraints
89: * @param attributeValue Value to be validated
90: */
91: public abstract void validate(Object identifier, FieldSpecification<?, ?> attribute, Object attributeValue);
92: }