• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmrs / openmrs-core / 41064958897

30 Oct 2026 01:38PM UTC coverage: 66.747% (+0.02%) from 66.732%
41064958897

push

github

web-flow
Add @Testcontainers(disabledWithoutDocker = true) to ContextDAOConcurrentLockoutIT (#6793)

26241 of 39314 relevant lines covered (66.75%)

0.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/api/src/main/java/org/openmrs/validator/ConceptValidator.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs.validator;
11

12
import java.util.Collection;
13
import java.util.HashSet;
14
import java.util.Locale;
15
import java.util.Set;
16

17
import org.apache.commons.collections.CollectionUtils;
18
import org.apache.commons.lang3.StringUtils;
19
import org.openmrs.Concept;
20
import org.openmrs.ConceptAnswer;
21
import org.openmrs.ConceptMap;
22
import org.openmrs.ConceptName;
23
import org.openmrs.ConceptNumeric;
24
import org.openmrs.ConceptReferenceRange;
25
import org.openmrs.annotation.Handler;
26
import org.openmrs.api.APIException;
27
import org.openmrs.api.DuplicateConceptNameException;
28
import org.openmrs.api.context.Context;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31
import org.springframework.validation.Errors;
32
import org.springframework.validation.ValidationUtils;
33
import org.springframework.validation.Validator;
34

35
/**
36
 * Validates {@link Concept} objects. <br>
37
 * These validations are also documented at <a
38
 * href="https://wiki.openmrs.org/x/-gkdAg">https://wiki.openmrs.org/x/-gkdAg</a>. Any changes made
39
 * to this source also need to be reflected on that page.
40
 */
41
@Handler(supports = { Concept.class }, order = 50)
42
public class ConceptValidator extends BaseCustomizableValidator implements Validator {
1 ✔
43
        
44
        // Logger for this class
45
        private static final Logger log = LoggerFactory.getLogger(ConceptValidator.class);
1 ✔
46
        
47
        /**
48
         * Determines if the command object being submitted is a valid type
49
         *
50
         * @see org.springframework.validation.Validator#supports(java.lang.Class)
51
         */
52
        @Override
53
        public boolean supports(Class<?> c) {
54
                return Concept.class.isAssignableFrom(c);
1 ✔
55
        }
56
        
57
        /**
58
         * Checks that a given concept object is valid.
59
         *
60
         * @see org.springframework.validation.Validator#validate(java.lang.Object,
61
         *      org.springframework.validation.Errors)
62
         * <strong>Should</strong> pass if the concept has at least one fully specified name added to it
63
         * <strong>Should</strong> fail if there is a duplicate unretired concept name in the locale
64
         * <strong>Should</strong> fail if there is a duplicate unretired preferred name in the same locale
65
         * <strong>Should</strong> fail if there is a duplicate unretired fully specified name in the same locale
66
         * <strong>Should</strong> fail if any names in the same locale for this concept are similar
67
         * <strong>Should</strong> pass if the concept with a duplicate name is retired
68
         * <strong>Should</strong> pass if the concept being validated is retired and has a duplicate name
69
         * <strong>Should</strong> fail if any name is an empty string
70
         * <strong>Should</strong> fail if the object parameter is null
71
         * <strong>Should</strong> pass if the concept is being updated with no name change
72
         * <strong>Should</strong> fail if any name is a null value
73
         * <strong>Should</strong> not allow multiple preferred names in a given locale
74
         * <strong>Should</strong> not allow multiple fully specified conceptNames in a given locale
75
         * <strong>Should</strong> not allow multiple short names in a given locale
76
         * <strong>Should</strong> not allow an index term to be a locale preferred name
77
         * <strong>Should</strong> fail if there is no name explicitly marked as fully specified
78
         * <strong>Should</strong> pass if the duplicate ConceptName is neither preferred nor fully Specified
79
         * <strong>Should</strong> pass if the concept has a synonym that is also a short name
80
         * <strong>Should</strong> fail if a term is mapped multiple times to the same concept
81
         * <strong>Should</strong> not fail if a term has two new mappings on it
82
         * <strong>Should</strong> fail if there is a duplicate unretired concept name in the same locale different than
83
         *         the system locale
84
         * <strong>Should</strong> pass for a new concept with a map created with deprecated concept map methods
85
         * <strong>Should</strong> pass for an edited concept with a map created with deprecated concept map methods
86
         * <strong>Should</strong> pass validation if field lengths are correct
87
         * <strong>Should</strong> fail validation if field lengths are not correct
88
         * <strong>Should</strong> pass if fully specified name is the same as short name
89
         * <strong>Should</strong> pass if different concepts have the same short name
90
         * <strong>Should</strong> fail if the concept datatype is null
91
         * <strong>Should</strong> fail if the concept class is null
92
         * <strong>Should</strong> fail if a reference range criteria does not parse
93
         * <strong>Should</strong> pass if the concept is retired and the only validation failures would be in ConceptName 
94
         * or ConceptMap, as a retired Concept bypasses ConceptName and ConceptMap validation.
95
         */
96
        @Override
97
        public void validate(Object obj, Errors errors) throws APIException, DuplicateConceptNameException {
98
                
99
                if (obj == null || !(obj instanceof Concept)) {
1 ✔
100
                        throw new IllegalArgumentException("The parameter obj should not be null and must be of type" + Concept.class);
1 ✔
101
                }
102
                
103
                Concept conceptToValidate = (Concept) obj;
1 ✔
104
                //no name to validate, but why is this the case?
105
                if (conceptToValidate.getNames().isEmpty()) {
1 ✔
106
                        errors.reject("Concept.name.atLeastOneRequired");
1 ✔
107
                        return;
1 ✔
108
                }
109

110
                ValidationUtils.rejectIfEmpty(errors, "datatype", "Concept.datatype.empty");
1 ✔
111
                ValidationUtils.rejectIfEmpty(errors, "conceptClass", "Concept.conceptClass.empty");
1 ✔
112
                
113
                boolean hasFullySpecifiedName = false;
1 ✔
114
                for (Locale conceptNameLocale : conceptToValidate.getAllConceptNameLocales()) {
1 ✔
115
                        if (conceptToValidate.getRetired()) {
1 ✔
116
                                continue;
1 ✔
117
                        }
118
                        
119
                        boolean fullySpecifiedNameForLocaleFound = false;
1 ✔
120
                        boolean preferredNameForLocaleFound = false;
1 ✔
121
                        boolean shortNameForLocaleFound = false;
1 ✔
122
                        Set<String> validNamesFoundInLocale = new HashSet<>();
1 ✔
123
                        Collection<ConceptName> namesInLocale = conceptToValidate.getNames(conceptNameLocale);
1 ✔
124
                        for (ConceptName nameInLocale : namesInLocale) {
1 ✔
125
                                if (StringUtils.isBlank(nameInLocale.getName())) {
1 ✔
126
                                        log.debug("Name in locale '" + conceptNameLocale.toString()
1 ✔
127
                                                + "' cannot be an empty string or white space");
128
                                        errors.reject("Concept.name.empty");
1 ✔
129
                                }
130
                                if (nameInLocale.getLocalePreferred() != null) {
1 ✔
131
                                        if (nameInLocale.getLocalePreferred() && !preferredNameForLocaleFound) {
1 ✔
132
                                                if (nameInLocale.isIndexTerm()) {
1 ✔
133
                                                        log.warn("Preferred name in locale '" + conceptNameLocale.toString()
×
134
                                                                + "' shouldn't be an index term");
135
                                                        errors.reject("Concept.error.preferredName.is.indexTerm");
×
136
                                                } else if (nameInLocale.isShort()) {
1 ✔
137
                                                        log.warn("Preferred name in locale '" + conceptNameLocale.toString()
×
138
                                                                + "' shouldn't be a short name");
139
                                                        errors.reject("Concept.error.preferredName.is.shortName");
×
140
                                                } else if (nameInLocale.getVoided()) {
1 ✔
141
                                                        log.warn("Preferred name in locale '" + conceptNameLocale.toString()
×
142
                                                                + "' shouldn't be a voided name");
143
                                                        errors.reject("Concept.error.preferredName.is.voided");
×
144
                                                }
145
                                                
146
                                                preferredNameForLocaleFound = true;
1 ✔
147
                                        }
148
                                        //should have one preferred name per locale
149
                                        else if (nameInLocale.getLocalePreferred() && preferredNameForLocaleFound) {
1 ✔
150
                                                log.warn("Found multiple preferred names in locale '" + conceptNameLocale.toString() + "'");
×
151
                                                errors.reject("Concept.error.multipleLocalePreferredNames");
×
152
                                        }
153
                                }
154
                                
155
                                if (nameInLocale.isFullySpecifiedName()) {
1 ✔
156
                                        if (!hasFullySpecifiedName) {
1 ✔
157
                                                hasFullySpecifiedName = true;
1 ✔
158
                                        }
159
                                        if (!fullySpecifiedNameForLocaleFound) {
1 ✔
160
                                                fullySpecifiedNameForLocaleFound = true;
1 ✔
161
                                        } else {
162
                                                log.warn("Found multiple fully specified names in locale '" + conceptNameLocale.toString() + "'");
×
163
                                                errors.reject("Concept.error.multipleFullySpecifiedNames");
×
164
                                        }
165
                                        if (nameInLocale.getVoided()) {
1 ✔
166
                                                log.warn("Fully Specified name in locale '" + conceptNameLocale.toString()
×
167
                                                        + "' shouldn't be a voided name");
168
                                                errors.reject("Concept.error.fullySpecifiedName.is.voided");
×
169
                                        }
170
                                }
171
                                
172
                                if (nameInLocale.isShort()) {
1 ✔
173
                                        if (!shortNameForLocaleFound) {
1 ✔
174
                                                shortNameForLocaleFound = true;
1 ✔
175
                                        }
176
                                        //should have one short name per locale
177
                                        else {
178
                                                log.warn("Found multiple short names in locale '" + conceptNameLocale.toString() + "'");
×
179
                                                errors.reject("Concept.error.multipleShortNames");
×
180
                                        }
181
                                }
182
                                
183
                                //find duplicate names for a non-retired concept
184
                                if (Context.getConceptService().isConceptNameDuplicate(nameInLocale)) {
1 ✔
185
                                        throw new DuplicateConceptNameException("'" + nameInLocale.getName()
1 ✔
186
                                                + "' is a duplicate name in locale '" + conceptNameLocale.toString() + "'");
1 ✔
187
                                }
188
                                
189
                                //
190
                                if (errors.hasErrors()) {
1 ✔
191
                                        log.debug("Concept name '" + nameInLocale.getName() + "' for locale '" + conceptNameLocale
1 ✔
192
                                                + "' is invalid");
193
                                        //if validation fails for any conceptName in current locale, don't proceed
194
                                        //This helps not to have multiple messages shown that are identical though they might be
195
                                        //for different conceptNames
196
                                        return;
1 ✔
197
                                }
198
                                
199
                                //No duplicate names allowed for the same locale and concept, keep the case the same
200
                                //except for short names
201
                                if (!nameInLocale.isShort() && !validNamesFoundInLocale.add(nameInLocale.getName().toLowerCase())) {
1 ✔
202
                                        throw new DuplicateConceptNameException("'" + nameInLocale.getName()
1 ✔
203
                                                + "' is a duplicate name in locale '" + conceptNameLocale.toString() + "' for the same concept");
1 ✔
204
                                }
205
                                
206
                                log.debug("Valid name found: {}", nameInLocale.getName());
1 ✔
207
                        }
1 ✔
208
                }
1 ✔
209
                
210
                //Ensure that each concept has at least a fully specified name
211
                if (!hasFullySpecifiedName && !conceptToValidate.getRetired()) {
1 ✔
212
                        log.debug("Concept has no fully specified name");
1 ✔
213
                        errors.reject("Concept.error.no.FullySpecifiedName");
1 ✔
214
                }
215
                
216
                if (CollectionUtils.isNotEmpty(conceptToValidate.getConceptMappings()) && !conceptToValidate.getRetired()) {
1 ✔
217
                        //validate all the concept maps
218
                        int index = 0;
1 ✔
219
                        Set<Integer> mappedTermIds = null;
1 ✔
220
                        for (ConceptMap map : conceptToValidate.getConceptMappings()) {
1 ✔
221
                                if (map.getConceptReferenceTerm().getConceptReferenceTermId() == null) {
1 ✔
222
                                        //if this term is getting created on the fly e.g. from old legacy code, validate it
223
                                        try {
224
                                                errors.pushNestedPath("conceptMappings[" + index + "].conceptReferenceTerm");
1 ✔
225
                                                ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), map.getConceptReferenceTerm(),
1 ✔
226
                                                    errors);
227
                                        }
228
                                        finally {
229
                                                errors.popNestedPath();
1 ✔
230
                                        }
231
                                        
232
                                }
233

234
                                //don't proceed to the next maps since the current one already has errors
235
                                if (errors.hasErrors()) {
1 ✔
236
                                        return;
1 ✔
237
                                }
238
                                
239
                                if (mappedTermIds == null) {
1 ✔
240
                                        mappedTermIds = new HashSet<>();
1 ✔
241
                                }
242
                                
243
                                //if we already have a mapping to this term, reject it this map
244
                                if (map.getConceptReferenceTerm().getId() != null
1 ✔
245
                                        && !mappedTermIds.add(map.getConceptReferenceTerm().getId())) {
1 ✔
246
                                        errors.rejectValue("conceptMappings[" + index + "]", "ConceptReferenceTerm.term.alreadyMapped",
1 ✔
247
                                            "Cannot map a reference term multiple times to the same concept");
248
                                }
249
                                
250
                                index++;
1 ✔
251
                        }
1 ✔
252
                }
253
                if (CollectionUtils.isNotEmpty(conceptToValidate.getAnswers())) {
1 ✔
254
                        for (ConceptAnswer conceptAnswer : conceptToValidate.getAnswers()) {
1 ✔
255
                                if (conceptAnswer.getAnswerConcept().equals(conceptToValidate)) {
1 ✔
256
                                        errors.reject("Concept.contains.itself.as.answer");
1 ✔
257
                                }
258
                        }
1 ✔
259
                }
260
                if (conceptToValidate instanceof ConceptNumeric) {
1 ✔
261
                        ConceptNumeric conceptNumeric = (ConceptNumeric) conceptToValidate;
1 ✔
262
                        Set<ConceptReferenceRange> referenceRanges = conceptNumeric.getReferenceRanges();
1 ✔
263
                        if (CollectionUtils.isNotEmpty(referenceRanges)) {
1 ✔
264
                                int index = 0;
1 ✔
265
                                for (ConceptReferenceRange referenceRange : referenceRanges) {
1 ✔
266
                                        try {
267
                                                errors.pushNestedPath("referenceRanges[" + index + "]");
1 ✔
268
                                                ValidationUtils.invokeValidator(new ConceptReferenceRangeValidator(), referenceRange, errors);
1 ✔
269
                                        } finally {
270
                                                errors.popNestedPath();
1 ✔
271
                                        }
272
                                        index++;
1 ✔
273
                                }
1 ✔
274
                        }
275
                }
276
                ValidateUtil.validateFieldLengths(errors, obj.getClass(), "version", "retireReason");
1 ✔
277
                super.validateAttributes(conceptToValidate, errors, Context.getConceptService().getAllConceptAttributeTypes());
1 ✔
278
        }
1 ✔
279
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc