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

openmrs / openmrs-core / 18855934517

27 Oct 2025 09:07PM UTC coverage: 64.894%. Remained the same
18855934517

push

github

web-flow
TRUNK-6457 - Core DAO authenticate method fails to retrieve global properties (#5439)

7 of 15 new or added lines in 1 file covered. (46.67%)

3 existing lines in 2 files now uncovered.

23439 of 36119 relevant lines covered (64.89%)

0.65 hits per line

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

83.74
/api/src/main/java/org/openmrs/Concept.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;
11

12
import java.io.Serializable;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.Date;
17
import java.util.HashMap;
18
import java.util.HashSet;
19
import java.util.LinkedHashSet;
20
import java.util.List;
21
import java.util.Locale;
22
import java.util.Map;
23
import java.util.Set;
24
import java.util.TreeSet;
25
import java.util.stream.Collectors;
26

27
import org.apache.commons.lang3.StringUtils;
28
import org.codehaus.jackson.annotate.JsonIgnore;
29
import org.hibernate.envers.Audited;
30
import org.hibernate.search.annotations.ContainedIn;
31
import org.hibernate.search.annotations.DocumentId;
32
import org.hibernate.search.annotations.Field;
33
import org.hibernate.search.annotations.FullTextFilterDef;
34
import org.hibernate.search.annotations.FullTextFilterDefs;
35
import org.hibernate.search.annotations.IndexedEmbedded;
36
import org.openmrs.annotation.AllowDirectAccess;
37
import org.openmrs.api.APIException;
38
import org.openmrs.api.ConceptNameType;
39
import org.openmrs.api.ConceptService;
40
import org.openmrs.api.context.Context;
41
import org.openmrs.api.db.hibernate.search.TermsFilterFactory;
42
import org.openmrs.customdatatype.CustomValueDescriptor;
43
import org.openmrs.customdatatype.Customizable;
44
import org.openmrs.util.LocaleUtility;
45
import org.openmrs.util.OpenmrsUtil;
46
import org.slf4j.Logger;
47
import org.slf4j.LoggerFactory;
48
import org.springframework.util.ObjectUtils;
49

50
/**
51
 * A Concept object can represent either a question or an answer to a data point. That data point is
52
 * usually an {@link Obs}. <br>
53
 * <br>
54
 * A Concept can have multiple names and multiple descriptions within one locale and across multiple
55
 * locales.<br>
56
 * <br>
57
 * To save a Concept to the database, first build up the Concept object in java, then pass that
58
 * object to the {@link ConceptService}.<br>
59
 * <br>
60
 * To get a Concept that is stored in the database, call a method in the {@link ConceptService} to
61
 * fetch an object. To get child objects off of that Concept, further calls to the
62
 * {@link ConceptService} or the database are not needed. e.g. To get the list of answers that are
63
 * stored to a concept, get the concept, then call {@link Concept#getAnswers()}
64
 * 
65
 * @see ConceptName
66
 * @see ConceptDescription
67
 * @see ConceptAnswer
68
 * @see ConceptSet
69
 * @see ConceptMap
70
 * @see ConceptService
71
 */
72
@FullTextFilterDefs( { @FullTextFilterDef(name = "termsFilterFactory", impl = TermsFilterFactory.class) })
73
@Audited
74
public class Concept extends BaseOpenmrsObject implements Auditable, Retireable, Serializable, Attributable<Concept>,Customizable<ConceptAttribute> {
75
        
76
        public static final long serialVersionUID = 57332L;
77
        
78
        private static final Logger log = LoggerFactory.getLogger(Concept.class);
1✔
79
        private static final String CONCEPT_NAME_LOCALE_NULL = "Concept.name.locale.null";
80
        
81
        // Fields
82
        @DocumentId
83
        private Integer conceptId;
84
        
85
        @Field
1✔
86
        private Boolean retired = false;
1✔
87
        
88
        private User retiredBy;
89
        
90
        private Date dateRetired;
91
        
92
        private String retireReason;
93
        
94
        @IndexedEmbedded(includeEmbeddedObjectId = true)
95
        private ConceptDatatype datatype;
96
        
97
        @IndexedEmbedded(includeEmbeddedObjectId = true)
98
        private ConceptClass conceptClass;
99
        
100
        private Boolean set = false;
1✔
101
        
102
        private String version;
103
        
104
        private User creator;
105
        
106
        private Date dateCreated;
107
        
108
        private User changedBy;
109
        
110
        private Date dateChanged;
111
        
112
        @AllowDirectAccess
113
        @ContainedIn
114
        private Collection<ConceptName> names;
115
        
116
        @AllowDirectAccess
117
        private Collection<ConceptAnswer> answers;
118
        
119
        private Collection<ConceptSet> conceptSets;
120
        
121
        private Collection<ConceptDescription> descriptions;
122
        
123
        @IndexedEmbedded(includeEmbeddedObjectId = true)
124
        private Collection<ConceptMap> conceptMappings;
125
        
126
        /**
127
         * A cache of locales to names which have compatible locales. Built on-the-fly by
128
         * getCompatibleNames().
129
         */
130
        private Map<Locale, List<ConceptName>> compatibleCache;
131

132
        private Set<ConceptAttribute> attributes = new LinkedHashSet<>();
1✔
133

134
        /** default constructor */
135
        public Concept() {
1✔
136
                names = new HashSet<>();
1✔
137
                answers = new HashSet<>();
1✔
138
                conceptSets = new TreeSet<>();
1✔
139
                descriptions = new HashSet<>();
1✔
140
                conceptMappings = new HashSet<>();
1✔
141
        }
1✔
142
        
143
        /**
144
         * Convenience constructor with conceptid to save to {@link #setConceptId(Integer)}. This
145
         * effectively creates a concept stub that can be used to make other calls. Because the
146
         * {@link #equals(Object)} and {@link #hashCode()} methods rely on conceptId, this allows a stub
147
         * to masquerade as a full concept as long as other objects like {@link #getAnswers()} and
148
         * {@link #getNames()} are not needed/called.
149
         * 
150
         * @param conceptId the concept id to set
151
         */
152
        public Concept(Integer conceptId) {
153
                this();
1✔
154
                this.conceptId = conceptId;
1✔
155
        }
1✔
156
        
157
        /**
158
         * @return Returns all answers (including retired answers).
159
         * <strong>Should</strong> return retired and non-retired answers
160
         * <strong>Should</strong> not return null if answers is null or empty
161
         */
162
        public Collection<ConceptAnswer> getAnswers() {
163
                if (answers == null) {
1✔
164
                        answers = new HashSet<>();
1✔
165
                }
166
                return answers;
1✔
167
        }
168
        
169
        /**
170
         * If <code>includeRetired</code> is true, then the returned object is the actual stored list of
171
         * {@link ConceptAnswer}s
172
         * 
173
         * @param includeRetired true/false whether to also include the retired answers
174
         * @return Returns the answers for this Concept
175
         * <strong>Should</strong> return the same as getAnswers() if includeRetired is true
176
         * <strong>Should</strong> not return retired answers if includeRetired is false
177
         */
178
        public Collection<ConceptAnswer> getAnswers(boolean includeRetired) {
179
                if (includeRetired) {
1✔
180
                        return getAnswers();
1✔
181
                } else {
182
                        return getAnswers().stream()
1✔
183
                                        .filter(a -> !a.getAnswerConcept().getRetired())
1✔
184
                                        .collect(Collectors.toSet());
1✔
185
                }
186
        }
187

188
        /**
189
         * Set this Concept as having the given <code>answers</code>; This method assumes that the
190
         * sort_weight has already been set.
191
         * 
192
         * @param answers The answers to set.
193
         */
194
        public void setAnswers(Collection<ConceptAnswer> answers) {
195
                this.answers = answers;
1✔
196
        }
1✔
197
        
198
        /**
199
         * Add the given ConceptAnswer to the list of answers for this Concept
200
         * 
201
         * @param conceptAnswer
202
         * <strong>Should</strong> add the ConceptAnswer to Concept
203
         * <strong>Should</strong> not fail if answers list is null
204
         * <strong>Should</strong> not fail if answers contains ConceptAnswer already
205
         * <strong>Should</strong> set the sort weight to the max plus one if not provided
206
         */
207
        public void addAnswer(ConceptAnswer conceptAnswer) {
208
                if (conceptAnswer != null) {
1✔
209
                        if (!getAnswers().contains(conceptAnswer)) {
1✔
210
                                conceptAnswer.setConcept(this);
1✔
211
                                getAnswers().add(conceptAnswer);
1✔
212
                        }
213
                        
214
                        if ((conceptAnswer.getSortWeight() == null) || (conceptAnswer.getSortWeight() <= 0)) {
1✔
215
                                //find largest sort weight
216
                                ConceptAnswer a = Collections.max(answers);
1✔
217
                                //a.sortWeight can be NULL
218
                                Double sortWeight = (a == null) ? 1d : ((a.getSortWeight() == null) ? 1d : a.getSortWeight() + 1d);
1✔
219
                                conceptAnswer.setSortWeight(sortWeight);
1✔
220
                        }
221
                }
222
        }
1✔
223
        
224
        /**
225
         * Remove the given answer from the list of answers for this Concept
226
         * 
227
         * @param conceptAnswer answer to remove
228
         * @return true if the entity was removed, false otherwise
229
         * <strong>Should</strong> not fail if answers is empty
230
         * <strong>Should</strong> not fail if given answer does not exist in list
231
         */
232
        public boolean removeAnswer(ConceptAnswer conceptAnswer) {
233
                return getAnswers().remove(conceptAnswer);
1✔
234
        }
235
        
236
        /**
237
         * @return Returns the changedBy.
238
         */
239
        @Override
240
        public User getChangedBy() {
241
                return changedBy;
1✔
242
        }
243
        
244
        /**
245
         * @param changedBy The changedBy to set.
246
         */
247
        @Override
248
        public void setChangedBy(User changedBy) {
249
                this.changedBy = changedBy;
1✔
250
        }
1✔
251
        
252
        /**
253
         * @return Returns the conceptClass.
254
         */
255
        public ConceptClass getConceptClass() {
256
                return conceptClass;
1✔
257
        }
258
        
259
        /**
260
         * @param conceptClass The conceptClass to set.
261
         */
262
        public void setConceptClass(ConceptClass conceptClass) {
263
                this.conceptClass = conceptClass;
1✔
264
        }
1✔
265
        
266
        /**
267
         * whether or not this concept is a set
268
         * 
269
         * @deprecated as of 2.0, use {@link #getSet()}
270
         */
271
        @Deprecated
272
        @JsonIgnore
273
        public Boolean isSet() {
274
                return getSet();
×
275
        }
276
        
277
        /**
278
         * @param set whether or not this concept is a set
279
         */
280
        public void setSet(Boolean set) {
281
                this.set = set;
1✔
282
        }
1✔
283
        
284
        public Boolean getSet() {
285
                return set;
1✔
286
        }
287
        
288
        /**
289
         * @return Returns the conceptDatatype.
290
         */
291
        public ConceptDatatype getDatatype() {
292
                return datatype;
1✔
293
        }
294
        
295
        /**
296
         * @param conceptDatatype The conceptDatatype to set.
297
         */
298
        public void setDatatype(ConceptDatatype conceptDatatype) {
299
                this.datatype = conceptDatatype;
1✔
300
        }
1✔
301
        
302
        /**
303
         * @return Returns the conceptId.
304
         */
305
        public Integer getConceptId() {
306
                return conceptId;
1✔
307
        }
308
        
309
        /**
310
         * @param conceptId The conceptId to set.
311
         */
312
        public void setConceptId(Integer conceptId) {
313
                this.conceptId = conceptId;
1✔
314
        }
1✔
315
        
316
        /**
317
         * @return Returns the creator.
318
         */
319
        @Override
320
        public User getCreator() {
321
                return creator;
1✔
322
        }
323
        
324
        /**
325
         * @param creator The creator to set.
326
         */
327
        @Override
328
        public void setCreator(User creator) {
329
                this.creator = creator;
1✔
330
        }
1✔
331
        
332
        /**
333
         * @return Returns the dateChanged.
334
         */
335
        @Override
336
        public Date getDateChanged() {
337
                return dateChanged;
1✔
338
        }
339
        
340
        /**
341
         * @param dateChanged The dateChanged to set.
342
         */
343
        @Override
344
        public void setDateChanged(Date dateChanged) {
345
                this.dateChanged = dateChanged;
1✔
346
        }
1✔
347
        
348
        /**
349
         * @return Returns the dateCreated.
350
         */
351
        @Override
352
        public Date getDateCreated() {
353
                return dateCreated;
1✔
354
        }
355
        
356
        /**
357
         * @param dateCreated The dateCreated to set.
358
         */
359
        @Override
360
        public void setDateCreated(Date dateCreated) {
361
                this.dateCreated = dateCreated;
1✔
362
        }
1✔
363
        
364
        /**
365
         * Sets the preferred name /in this locale/ to the specified conceptName and its Locale, if
366
         * there is an existing preferred name for this concept in the same locale, this one will
367
         * replace the old preferred name. Also, the name is added to the concept if it is not already
368
         * among the concept names.
369
         * 
370
         * @param preferredName The name to be marked as preferred in its locale
371
         * <strong>Should</strong> only allow one preferred name
372
         * <strong>Should</strong> add the name to the list of names if it not among them before
373
         * <strong>Should</strong> fail if the preferred name to set to is an index term
374
         */
375
        public void setPreferredName(ConceptName preferredName) {
376
                
377
                if (preferredName == null || preferredName.getVoided() || preferredName.isIndexTerm()) {
1✔
378
                        throw new APIException("Concept.error.preferredName.null", (Object[]) null);
1✔
379
                } else if (preferredName.getLocale() == null) {
1✔
380
                        throw new APIException(CONCEPT_NAME_LOCALE_NULL, (Object[]) null);
×
381
                }
382
                
383
                //first revert the current preferred name(if any) from being preferred
384
                ConceptName oldPreferredName = getPreferredName(preferredName.getLocale(), true);
1✔
385
                if (oldPreferredName != null) {
1✔
386
                        oldPreferredName.setLocalePreferred(false);
1✔
387
                }
388
                
389
                preferredName.setLocalePreferred(true);
1✔
390
                //add this name, if it is new or not among this concept's names
391
                if (preferredName.getConceptNameId() == null || !getNames().contains(preferredName)) {
1✔
392
                        addName(preferredName);
1✔
393
                }
394
        }
1✔
395
        
396
        /**
397
         * A convenience method to get the concept-name (if any) which has a particular tag. This does
398
         * not guarantee that the returned name is the only one with the tag.
399
         * 
400
         * @param conceptNameTag the tag for which to look
401
         * @return the tagged name, or null if no name has the tag
402
         */
403
        public ConceptName findNameTaggedWith(ConceptNameTag conceptNameTag) {
404
                ConceptName taggedName = null;
×
405
                for (ConceptName possibleName : getNames()) {
×
406
                        if (possibleName.hasTag(conceptNameTag)) {
×
407
                                taggedName = possibleName;
×
408
                                break;
×
409
                        }
410
                }
×
411
                return taggedName;
×
412
        }
413
        
414
        /**
415
         * Returns a name in the given locale. If a name isn't found with an exact match, a compatible
416
         * locale match is returned. If no name is found matching either of those, the first name
417
         * defined for this concept is returned.
418
         * 
419
         * @param locale the locale to fetch for
420
         * @return ConceptName attributed to the Concept in the given locale
421
         * @since 1.5
422
         * @see Concept#getNames(Locale) to get all the names for a locale,
423
         * @see Concept#getPreferredName(Locale) for the preferred name (if any)
424
         */
425
        public ConceptName getName(Locale locale) {
426
                return getName(locale, false);
1✔
427
        }
428
        
429
        /**
430
         * Returns concept name, the look up for the appropriate name is done in the following order;
431
         * <ul>
432
         * <li>First name found in any locale that is explicitly marked as preferred while searching
433
         * available locales in order of preference (the locales are traversed in their order as they
434
         * are listed in the 'locale.allowed.list' including english global property).</li>
435
         * <li>First "Fully Specified" name found while searching available locales in order of
436
         * preference.</li>
437
         * <li>The first fully specified name found while searching through all names for the concept</li>
438
         * <li>The first synonym found while searching through all names for the concept.</li>
439
         * <li>The first random name found(except index terms) while searching through all names.</li>
440
         * </ul>
441
         * 
442
         * @return {@link ConceptName} in the current locale or any locale if none found
443
         * @since 1.5
444
         * @see Concept#getNames(Locale) to get all the names for a locale
445
         * @see Concept#getPreferredName(Locale) for the preferred name (if any)
446
         * <strong>Should</strong> return the name explicitly marked as locale preferred if any is present
447
         * <strong>Should</strong> return the fully specified name in a locale if no preferred name is set
448
         * <strong>Should</strong> return null if the only added name is an index term
449
         * <strong>Should</strong> return name in broader locale in case none is found in specific one
450
         */
451
        public ConceptName getName() {
452
                if (getNames().isEmpty()) {
1✔
453
                        log.debug("there are no names defined for: {}", conceptId);
1✔
454
                        return null;
1✔
455
                }
456
                
457
                for (Locale currentLocale : LocaleUtility.getLocalesInOrder()) {
1✔
458
                        ConceptName preferredName = getPreferredName(currentLocale);
1✔
459
                        if (preferredName != null) {
1✔
460
                                return preferredName;
1✔
461
                        }
462
                        
463
                        ConceptName fullySpecifiedName = getFullySpecifiedName(currentLocale);
1✔
464
                        if (fullySpecifiedName != null) {
1✔
465
                                return fullySpecifiedName;
×
466
                        }
467
                        
468
                        //if the locale has an variants e.g en_GB, try names in the locale excluding the country code i.e en
469
                        if (!StringUtils.isBlank(currentLocale.getCountry()) || !StringUtils.isBlank(currentLocale.getVariant())) {
1✔
470
                                Locale broaderLocale = new Locale(currentLocale.getLanguage());
1✔
471
                                ConceptName prefNameInBroaderLoc = getPreferredName(broaderLocale);
1✔
472
                                if (prefNameInBroaderLoc != null) {
1✔
473
                                        return prefNameInBroaderLoc;
1✔
474
                                }
475
                                
476
                                ConceptName fullySpecNameInBroaderLoc = getFullySpecifiedName(broaderLocale);
1✔
477
                                if (fullySpecNameInBroaderLoc != null) {
1✔
478
                                        return fullySpecNameInBroaderLoc;
×
479
                                }
480
                        }
481
                }
1✔
482
                
483
                for (ConceptName cn : getNames()) {
1✔
484
                        if (cn.isFullySpecifiedName()) {
1✔
485
                                return cn;
1✔
486
                        }
487
                }
1✔
488
                
489
                if (!getSynonyms().isEmpty()) {
1✔
490
                        return getSynonyms().iterator().next();
×
491
                }
492
                
493
                // we don't expect to get here since every concept name must have at least
494
                // one fully specified name, but just in case (probably inconsistent data)
495
                
496
                return null;
1✔
497
        }
498
        
499
        /**
500
         * Checks whether this concept has the given string in any of the names in the given locale
501
         * already.
502
         * 
503
         * @param name the ConceptName.name to compare to
504
         * @param locale the locale to look in (null to check all locales)
505
         * @return true/false whether the name exists already
506
         * <strong>Should</strong> return false if name is null
507
         * <strong>Should</strong> return true if locale is null but name exists
508
         * <strong>Should</strong> return false if locale is null but name does not exist
509
         */
510
        public boolean hasName(String name, Locale locale) {
511
                if (name == null) {
1✔
512
                        return false;
1✔
513
                }
514
                
515
                Collection<ConceptName> currentNames;
516
                if (locale == null) {
1✔
517
                        currentNames = getNames();
1✔
518
                } else {
519
                        currentNames = getNames(locale);
1✔
520
                }
521
                
522
                for (ConceptName currentName : currentNames) {
1✔
523
                        if (name.equalsIgnoreCase(currentName.getName())) {
1✔
524
                                return true;
1✔
525
                        }
526
                }
1✔
527
                
528
                return false;
1✔
529
        }
530
        
531
        /**
532
         * Returns concept name depending of locale, type (short, fully specified, etc) and tag.
533
         * Searches in the locale, and then the locale's parent if nothing is found.
534
         * 
535
         * @param ofType find a name of this type (optional)
536
         * @param havingTag find a name with this tag (optional)
537
         * @param locale find a name with this locale (required)
538
         * @return a name that matches the arguments, or null if none is found. If there are multiple
539
         *         matches and one is locale_preferred, that will be returned, otherwise a random one of
540
         *         the matches will be returned.
541
         * @since 1.9
542
         **/
543
        public ConceptName getName(Locale locale, ConceptNameType ofType, ConceptNameTag havingTag) {
544
                Collection<ConceptName> namesInLocale = getNames(locale);
×
545
                if (!namesInLocale.isEmpty()) {
×
546
                        //Pass the possible candidates through a stream and save the ones that match requirements to the list
547
                        List<ConceptName> matches = namesInLocale.stream().filter(
×
548
                                c->(ofType==null || ofType.equals(c.getConceptNameType())) && (havingTag==null || c.hasTag(havingTag))
×
549
                        ).collect(Collectors.toList());
×
550
                        
551
                        // if we have any matches, we'll return one of them
552
                        if (matches.size() == 1) {
×
553
                                return matches.get(0);
×
554
                        } else if (matches.size() > 1) {
×
555
                                for (ConceptName match : matches) {
×
556
                                        if (match.getLocalePreferred()) {
×
557
                                                return match;
×
558
                                        }
559
                                }
×
560
                                // none was explicitly marked as preferred
561
                                return matches.get(0);
×
562
                        }
563
                }
564
                
565
                // if we reach here, there were no matching names, so try to look in the parent locale
566
                Locale parent = new Locale(locale.getLanguage());
×
567
                if (!parent.equals(locale)) {
×
568
                        return getName(parent, ofType, havingTag);
×
569
                } else {
570
                        return null;
×
571
                }
572
        }
573
        
574
        /**
575
         * Returns a name in the given locale. If a name isn't found with an exact match, a compatible
576
         * locale match is returned. If no name is found matching either of those, the first name
577
         * defined for this concept is returned.
578
         * 
579
         * @param locale the language and country in which the name is used
580
         * @param exact true/false to return only exact locale (no default locale)
581
         * @return the closest name in the given locale, or the first name
582
         * @see Concept#getNames(Locale) to get all the names for a locale,
583
         * @see Concept#getPreferredName(Locale) for the preferred name (if any)
584
         * <strong>Should</strong> return exact name locale match given exact equals true
585
         * <strong>Should</strong> return loose match given exact equals false
586
         * <strong>Should</strong> return null if no names are found in locale given exact equals true
587
         * <strong>Should</strong> return any name if no locale match given exact equals false
588
         * <strong>Should</strong> return name in broader locale in case none is found in specific one
589
         */
590
        public ConceptName getName(Locale locale, boolean exact) {
591
                
592
                // fail early if this concept has no names defined
593
                if (getNames().isEmpty()) {
1✔
594
                        log.debug("there are no names defined for: {}", conceptId);
1✔
595
                        return null;
1✔
596
                }
597
                
598
                log.debug("Getting conceptName for locale: {}", locale);
1✔
599
                
600
                ConceptName exactName = getNameInLocale(locale);
1✔
601
                
602
                if (exactName != null) {
1✔
603
                        return exactName;
1✔
604
                }
605
                
606
                if (!exact) {
1✔
607
                        Locale broaderLocale = new Locale(locale.getLanguage());
1✔
608
                        ConceptName name = getNameInLocale(broaderLocale);
1✔
609
                        return name != null ? name : getName();
1✔
610
                }
611
                return null;
1✔
612
        }
613
        
614
        /**
615
         * Gets the best name in the specified locale.
616
         * 
617
         * @param locale
618
         * @return null if name in given locale doesn't exist
619
         */
620
        private ConceptName getNameInLocale(Locale locale) {
621
                ConceptName preferredName = getPreferredName(locale);
1✔
622
                if (preferredName != null) {
1✔
623
                        return preferredName;
1✔
624
                }
625
                
626
                ConceptName fullySpecifiedName = getFullySpecifiedName(locale);
1✔
627
                if (fullySpecifiedName != null) {
1✔
628
                        return fullySpecifiedName;
×
629
                } else if (!getSynonyms(locale).isEmpty()) {
1✔
630
                        return getSynonyms(locale).iterator().next();
1✔
631
                }
632
                
633
                return null;
1✔
634
        }
635
        
636
        public ConceptName getPreferredName(Locale forLocale) {
637
                return getPreferredName(forLocale, false);
1✔
638
        }
639
        
640
        /**
641
         * Returns the name which is explicitly marked as preferred for a given locale.
642
         * 
643
         * @param forLocale locale for which to return a preferred name
644
         * @return preferred name for the locale, or null if no preferred name is specified
645
         * <strong>Should</strong> return the concept name explicitly marked as locale preferred
646
         * <strong>Should</strong> return the concept name marked as locale preferred a partial match locale (same language but different country) if no exact match and exact set to false
647
         * <strong>Should</strong> return the fully specified name if no name is explicitly marked as locale preferred and exact set to false
648
         */
649
        public ConceptName getPreferredName(Locale forLocale, Boolean exact) {
650
                
651
                if (log.isDebugEnabled()) {
1✔
652
                        log.debug("Getting preferred conceptName for locale: " + forLocale);
×
653
                }
654
                
655
                if (forLocale == null) {
1✔
656
                        log.warn("Locale cannot be null");
×
657
                        return null;
×
658
                }
659
                
660
                for (ConceptName nameInLocale : getNames(forLocale)) {
1✔
661
                        if (ObjectUtils.nullSafeEquals(nameInLocale.getLocalePreferred(), true)) {
1✔
662
                                return nameInLocale;
1✔
663
                        }
664
                }
1✔
665
                
666
                if (exact) {
1✔
667
                        return null;
1✔
668
                } else {
669
                        // look for partially locale match - any language matches takes precedence over country matches.
670
                        ConceptName bestMatch = null;
1✔
671

672
                        for (ConceptName nameInLocale : getPartiallyCompatibleNames(forLocale)) {
1✔
673
                                if (ObjectUtils.nullSafeEquals(nameInLocale.getLocalePreferred(), true)) {
1✔
674
                                        Locale nameLocale = nameInLocale.getLocale();
1✔
675
                                        if (forLocale.getLanguage().equals(nameLocale.getLanguage())) {
1✔
676
                                                return nameInLocale;
1✔
677
                                        } else {
678
                                                bestMatch = nameInLocale;
×
679
                                        }
680

681
                                }
682
                        }
1✔
683

684
                        if (bestMatch != null) {
1✔
685
                                return bestMatch;
×
686
                        }
687

688
                        return getFullySpecifiedName(forLocale);
1✔
689
                }
690
        }
691
        
692
        /**
693
         * Convenience method that returns the fully specified name in the locale
694
         * 
695
         * @param locale locale from which to look up the fully specified name
696
         * @return the name explicitly marked as fully specified for the locale
697
         * <strong>Should</strong> return the name marked as fully specified for the given locale
698
         */
699
        public ConceptName getFullySpecifiedName(Locale locale) {
700
                if (locale != null && !getNames(locale).isEmpty()) {
1✔
701
                        //get the first fully specified name, since every concept must have a fully specified name,
702
                        //then, this loop will have to return a name
703
                        for (ConceptName conceptName : getNames(locale)) {
1✔
704
                                if (ObjectUtils.nullSafeEquals(conceptName.isFullySpecifiedName(), true)) {
1✔
705
                                        return conceptName;
1✔
706
                                }
707
                        }
1✔
708
                        
709
                        // look for partially locale match - any language matches takes precedence over country matches.
710
                        ConceptName bestMatch = null;
1✔
711
                        for (ConceptName conceptName : getPartiallyCompatibleNames(locale)) {
1✔
712
                                if (ObjectUtils.nullSafeEquals(conceptName.isFullySpecifiedName(), true)) {
1✔
713
                                        Locale nameLocale = conceptName.getLocale();
1✔
714
                                        if (locale.getLanguage().equals(nameLocale.getLanguage())) {
1✔
715
                                                return conceptName;
1✔
716
                                        }
717
                                        bestMatch = conceptName;
×
718
                                }
719
                        }
1✔
720
                        return bestMatch;
1✔
721
                        
722
                }
723
                return null;
1✔
724
        }
725
        
726
        /**
727
         * Returns all names available in a specific locale. <br>
728
         * <br>
729
         * This is recommended when managing the concept dictionary.
730
         * 
731
         * @param locale locale for which names should be returned
732
         * @return Collection of ConceptNames with the given locale
733
         */
734
        public Collection<ConceptName> getNames(Locale locale) {
735
                return getNames().stream()
1✔
736
                                .filter(n -> n.getLocale().equals(locale))
1✔
737
                                .collect(Collectors.toSet());
1✔
738
        }
739
        
740
        /**
741
         * Returns all names available for locale language "or" country. <br>
742
         * <br>
743
         * 
744
         * @param locale locale for which names should be returned
745
         * @return Collection of ConceptNames with the given locale language or country
746
         */
747
        private Collection<ConceptName> getPartiallyCompatibleNames(Locale locale) {
748
                String language = locale.getLanguage();
1✔
749
                String country = locale.getCountry();
1✔
750
                
751
                return getNames().stream()
1✔
752
                                .filter(n -> language.equals(n.getLocale().getLanguage()) || 
1✔
753
                                                        StringUtils.isNotBlank(country) && country.equals(n.getLocale().getCountry()))
1✔
754
                                .collect(Collectors.toSet());
1✔
755
        }
756
        
757
        /**
758
         * Returns all names from compatible locales. A locale is considered compatible if it is exactly
759
         * the same locale, or if either locale has no country specified and the language matches. <br>
760
         * <br>
761
         * This is recommended when presenting possible names to the use.
762
         * 
763
         * @param desiredLocale locale with which the names should be compatible
764
         * @return Collection of compatible names
765
         * <strong>Should</strong> exclude incompatible country locales
766
         * <strong>Should</strong> exclude incompatible language locales
767
         */
768
        public List<ConceptName> getCompatibleNames(Locale desiredLocale) {
769
                // lazy create the cache
770
                List<ConceptName> compatibleNames = null;
1✔
771
                if (compatibleCache == null) {
1✔
772
                        compatibleCache = new HashMap<>();
1✔
773
                } else {
774
                        compatibleNames = compatibleCache.get(desiredLocale);
×
775
                }
776
                
777
                if (compatibleNames == null) {
1✔
778
                        compatibleNames = new ArrayList<>();
1✔
779
                        for (ConceptName possibleName : getNames()) {
1✔
780
                                if (LocaleUtility.areCompatible(possibleName.getLocale(), desiredLocale)) {
1✔
781
                                        compatibleNames.add(possibleName);
1✔
782
                                }
783
                        }
1✔
784
                        compatibleCache.put(desiredLocale, compatibleNames);
1✔
785
                }
786
                return compatibleNames;
1✔
787
        }
788
        
789
        /**
790
         * Sets the specified name as the fully specified name for the locale and the current fully
791
         * specified (if any) ceases to be the fully specified name for the locale.
792
         * 
793
         * @param fullySpecifiedName the new fully specified name to set
794
         * <strong>Should</strong> set the concept name type of the specified name to fully specified
795
         * <strong>Should</strong> convert the previous fully specified name if any to a synonym
796
         * <strong>Should</strong> add the name to the list of names if it not among them before
797
         */
798
        public void setFullySpecifiedName(ConceptName fullySpecifiedName) {
799
                if (fullySpecifiedName == null || fullySpecifiedName.getLocale() == null) {
1✔
800
                        throw new APIException(CONCEPT_NAME_LOCALE_NULL, (Object[]) null);
×
801
                } else if (fullySpecifiedName.getVoided()) {
1✔
802
                        throw new APIException("Concept.error.fullySpecifiedName.null", (Object[]) null);
×
803
                }
804
                
805
                ConceptName oldFullySpecifiedName = getFullySpecifiedName(fullySpecifiedName.getLocale());
1✔
806
                if (oldFullySpecifiedName != null) {
1✔
807
                        oldFullySpecifiedName.setConceptNameType(null);
1✔
808
                }
809
                fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
1✔
810
                //add this name, if it is new or not among this concept's names
811
                if (fullySpecifiedName.getConceptNameId() == null || !getNames().contains(fullySpecifiedName)) {
1✔
812
                        addName(fullySpecifiedName);
1✔
813
                }
814
        }
1✔
815
        
816
        /**
817
         * Sets the specified name as the short name for the locale and the current shortName(if any)
818
         * ceases to be the short name for the locale.
819
         * 
820
         * @param shortName the new shortName to set
821
         * <strong>Should</strong> set the concept name type of the specified name to short
822
         * <strong>Should</strong> convert the previous shortName if any to a synonym
823
         * <strong>Should</strong> add the name to the list of names if it not among them before
824
         * <strong>Should</strong> void old short name if new one is blank (do not save blanks!)
825
         */
826
        public void setShortName(ConceptName shortName) {
827
                if (shortName != null) {
1✔
828
                        if (shortName.getLocale() == null) {
1✔
829
                                throw new APIException(CONCEPT_NAME_LOCALE_NULL, (Object[]) null);
×
830
                        }
831
                        ConceptName oldShortName = getShortNameInLocale(shortName.getLocale());
1✔
832
                        if (oldShortName != null) {
1✔
833
                                oldShortName.setConceptNameType(null);
1✔
834
                        }
835
                        shortName.setConceptNameType(ConceptNameType.SHORT);
1✔
836
                        if (StringUtils.isNotBlank(shortName.getName())
1✔
837
                                && (shortName.getConceptNameId() == null || !getNames().contains(shortName))) {
1✔
838
                                //add this name, if it is new or not among this concept's names
839
                                addName(shortName);
1✔
840
                        }
841
                } else {
1✔
842
                        throw new APIException("Concept.error.shortName.null", (Object[]) null);
×
843
                }
844
        }
1✔
845
        
846
        /**
847
         * Gets the explicitly specified short name for a locale.
848
         * 
849
         * @param locale locale for which to find a short name
850
         * @return the short name, or null if none has been explicitly set
851
         */
852
        public ConceptName getShortNameInLocale(Locale locale) {
853
                ConceptName bestMatch = null;
1✔
854
                if (locale != null && !getShortNames().isEmpty()) {
1✔
855
                        for (ConceptName shortName : getShortNames()) {
1✔
856
                                Locale nameLocale = shortName.getLocale();
1✔
857
                                if (nameLocale.equals(locale)) {
1✔
858
                                        return shortName;
1✔
859
                                }
860
                                // test for partially locale match - any language matches takes precedence over country matches.
861
                                if (OpenmrsUtil.nullSafeEquals(locale.getLanguage(), nameLocale.getLanguage())) {
1✔
862
                                        bestMatch = shortName;
1✔
863
                                } else if (bestMatch == null && StringUtils.isNotBlank(locale.getCountry())
1✔
UNCOV
864
                                        && locale.getCountry().equals(nameLocale.getCountry())) {
×
865
                                        bestMatch = shortName;
×
866
                                }
867
                        }
1✔
868
                }
869
                return bestMatch;
1✔
870
        }
871
        
872
        /**
873
         * Gets a collection of short names for this concept from all locales.
874
         * 
875
         * @return a collection of all short names for this concept
876
         */
877
        public Collection<ConceptName> getShortNames() {
878
                List<ConceptName> shortNames = new ArrayList<>();
1✔
879
                if (getNames().isEmpty()) {
1✔
880
                        if (log.isDebugEnabled()) {
×
881
                                log.debug("The Concept with id: " + conceptId + " has no names");
×
882
                        }
883
                } else {
884
                        shortNames = getNames().stream()
1✔
885
                                                        .filter(ConceptName::isShort)
1✔
886
                                                        .collect(Collectors.toList());
1✔
887
                }
888
                return shortNames;
1✔
889
        }
890
        
891
        /**
892
         * Returns the short form name for a locale, or if none has been identified, the shortest name
893
         * available in the locale. If exact is false, the shortest name from any locale is returned
894
         * 
895
         * @param locale the language and country in which the short name is used
896
         * @param exact true/false to return only exact locale (no default locale)
897
         * @return the appropriate short name, or null if not found
898
         * <strong>Should</strong> return the name marked as the shortName for the locale if it is present
899
         * <strong>Should</strong> return the shortest name in a given locale for a concept if exact is true
900
         * <strong>Should</strong> return the shortest name for the concept from any locale if exact is false
901
         * <strong>Should</strong> return null if there are no names in the specified locale and exact is true
902
         */
903
        public ConceptName getShortestName(Locale locale, Boolean exact) {
904
                if (log.isDebugEnabled()) {
1✔
905
                        log.debug("Getting shortest conceptName for locale: " + locale);
×
906
                }
907
                
908
                ConceptName shortNameInLocale = getShortNameInLocale(locale);
1✔
909
                if (shortNameInLocale != null) {
1✔
910
                        return shortNameInLocale;
1✔
911
                }
912
                
913
                ConceptName shortestNameForLocale = null;
1✔
914
                ConceptName shortestNameForConcept = null;
1✔
915
                
916
                if (locale != null) {
1✔
917
                        for (ConceptName possibleName : getNames()) {
1✔
918
                                if (possibleName.getLocale().equals(locale)
1✔
919
                                        && ((shortestNameForLocale == null) || (possibleName.getName().length() < shortestNameForLocale
1✔
920
                                                .getName().length()))) {
1✔
921
                                        shortestNameForLocale = possibleName;
1✔
922
                                }
923
                                if ((shortestNameForConcept == null)
1✔
924
                                        || (possibleName.getName().length() < shortestNameForConcept.getName().length())) {
1✔
925
                                        shortestNameForConcept = possibleName;
1✔
926
                                }
927
                        }
1✔
928
                }
929
                
930
                if (exact) {
1✔
931
                        if (shortestNameForLocale == null) {
1✔
932
                                log.warn("No short concept name found for concept id " + conceptId + " for locale "
1✔
933
                                        + locale.getDisplayName());
1✔
934
                        }
935
                        return shortestNameForLocale;
1✔
936
                }
937
                
938
                return shortestNameForConcept;
1✔
939
        }
940
        
941
        /**
942
         * @param name A name
943
         * @return whether this concept has the given name in any locale
944
         */
945
        public boolean isNamed(String name) {
946
                return getNames().stream().anyMatch(cn -> name.equals(cn.getName()));
1✔
947
        }
948
        
949
        /**
950
         * Gets the list of all non-retired concept names which are index terms for this concept
951
         * 
952
         * @return a collection of concept names which are index terms for this concept
953
         * @since 1.7
954
         */
955
        public Collection<ConceptName> getIndexTerms() {
956
                return getNames().stream()
×
957
                                .filter(ConceptName::isIndexTerm)
×
958
                                .collect(Collectors.toSet());                
×
959
        }
960
        
961
        /**
962
         * Gets the list of all non-retired concept names which are index terms in a given locale
963
         * 
964
         * @param locale the locale for the index terms to return
965
         * @return a collection of concept names which are index terms in the given locale
966
         * @since 1.7
967
         */
968
        public Collection<ConceptName> getIndexTermsForLocale(Locale locale) {
969
                return getIndexTerms().stream()
×
970
                                .filter(n -> n.getLocale().equals(locale))
×
971
                        .collect(Collectors.toList());
×
972
        }
973
        
974
        /**
975
         * @return Returns the names.
976
         */
977
        public Collection<ConceptName> getNames() {
978
                return getNames(false);
1✔
979
        }
980
        
981
        /**
982
         * @return Returns the names.
983
         * @param includeVoided Include voided ConceptNames if true.
984
         */
985
        public Collection<ConceptName> getNames(boolean includeVoided) {
986
                if (names == null) {
1✔
987
                        names = new HashSet<>();
×
988
                }
989

990
                return names.stream()
1✔
991
                                .filter(n -> includeVoided || !n.getVoided())
1✔
992
                                .collect(Collectors.toSet());
1✔
993
        }
994
        
995
        /**
996
         * @param names The names to set.
997
         */
998
        public void setNames(Collection<ConceptName> names) {
999
                this.names = names;
1✔
1000
        }
1✔
1001
        
1002
        /**
1003
         * Add the given ConceptName to the list of names for this Concept
1004
         * 
1005
         * @param conceptName
1006
         * <strong>Should</strong> replace the old preferred name with a current one
1007
         * <strong>Should</strong> replace the old fully specified name with a current one
1008
         * <strong>Should</strong> replace the old short name with a current one
1009
         * <strong>Should</strong> mark the first name added as fully specified
1010
         */
1011
        public void addName(ConceptName conceptName) {
1012
                if (conceptName != null) {
1✔
1013
                        conceptName.setConcept(this);
1✔
1014
                        if (names == null) {
1✔
1015
                                names = new HashSet<>();
×
1016
                        }
1017
                        if (!names.contains(conceptName)) {
1✔
1018
                                if (getNames().isEmpty()
1✔
1019
                                        && !ConceptNameType.FULLY_SPECIFIED.equals(conceptName.getConceptNameType())) {
1✔
1020
                                        conceptName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
1✔
1021
                                } else {
1022
                                        if (conceptName.isPreferred() && !conceptName.isIndexTerm() && conceptName.getLocale() != null) {
1✔
1023
                                                ConceptName prefName = getPreferredName(conceptName.getLocale(), true);
1✔
1024
                                                if (prefName != null) {
1✔
1025
                                                        prefName.setLocalePreferred(false);
1✔
1026
                                                }
1027
                                        }
1028
                                        if (conceptName.isFullySpecifiedName() && conceptName.getLocale() != null) {
1✔
1029
                                                ConceptName fullySpecName = getFullySpecifiedName(conceptName.getLocale());
1✔
1030
                                                if (fullySpecName != null) {
1✔
1031
                                                        fullySpecName.setConceptNameType(null);
1✔
1032
                                                }
1033
                                        } else if (conceptName.isShort() && conceptName.getLocale() != null) {
1✔
1034
                                                ConceptName shortName = getShortNameInLocale(conceptName.getLocale());
1✔
1035
                                                if (shortName != null) {
1✔
1036
                                                        shortName.setConceptNameType(null);
×
1037
                                                }
1038
                                        }
1039
                                }
1040
                                names.add(conceptName);
1✔
1041
                                if (compatibleCache != null) {
1✔
1042
                                        // clear the locale cache, forcing it to be rebuilt
1043
                                        compatibleCache.clear();
×
1044
                                }
1045
                        }
1046
                }
1047
        }
1✔
1048
        
1049
        /**
1050
         * Remove the given name from the list of names for this Concept
1051
         * 
1052
         * @param conceptName
1053
         * @return true if the entity was removed, false otherwise
1054
         */
1055
        public boolean removeName(ConceptName conceptName) {
1056
                if (names != null) {
1✔
1057
                        return names.remove(conceptName);
1✔
1058
                } else {
1059
                        return false;
×
1060
                }
1061
        }
1062
        
1063
        /**
1064
         * Finds the description of the concept using the current locale in Context.getLocale(). Returns
1065
         * null if none found.
1066
         * 
1067
         * @return ConceptDescription attributed to the Concept in the given locale
1068
         */
1069
        public ConceptDescription getDescription() {
1070
                return getDescription(Context.getLocale());
1✔
1071
        }
1072
        
1073
        /**
1074
         * Finds the description of the concept in the given locale. Returns null if none found.
1075
         * 
1076
         * @param locale
1077
         * @return ConceptDescription attributed to the Concept in the given locale
1078
         */
1079
        public ConceptDescription getDescription(Locale locale) {
1080
                return getDescription(locale, false);
1✔
1081
        }
1082
        
1083
        /**
1084
         * Returns the preferred description for a locale.
1085
         * 
1086
         * @param locale the language and country in which the description is used
1087
         * @param exact true/false to return only exact locale (no default locale)
1088
         * @return the appropriate description, or null if not found
1089
         * <strong>Should</strong> return match on locale exactly
1090
         * <strong>Should</strong> return match on language only
1091
         * <strong>Should</strong> not return match on language only if exact match exists
1092
         * <strong>Should</strong> not return language only match for exact matches
1093
         */
1094
        public ConceptDescription getDescription(Locale locale, boolean exact) {
1095
                log.debug("Getting ConceptDescription for locale: " + locale);
1✔
1096
                
1097
                ConceptDescription foundDescription = null;
1✔
1098
                
1099
                if (locale == null) {
1✔
1100
                        locale = LocaleUtility.getDefaultLocale();
×
1101
                }
1102
                
1103
                Locale desiredLocale = locale;
1✔
1104
                
1105
                ConceptDescription defaultDescription = null;
1✔
1106
                for (ConceptDescription availableDescription : getDescriptions()) {
1✔
1107
                        Locale availableLocale = availableDescription.getLocale();
1✔
1108
                        if (availableLocale.equals(desiredLocale)) {
1✔
1109
                                foundDescription = availableDescription;
1✔
1110
                                // skip out now because we found an exact locale match
1111
                                break;
1✔
1112
                        }
1113
                        if (!exact && LocaleUtility.areCompatible(availableLocale, desiredLocale)) {
1✔
1114
                                foundDescription = availableDescription;
1✔
1115
                        }
1116
                        if (availableLocale.equals(LocaleUtility.getDefaultLocale())) {
1✔
1117
                                defaultDescription = availableDescription;
×
1118
                        }
1119
                }
1✔
1120
                
1121
                if (foundDescription == null) {
1✔
1122
                        // no description with the given locale was found.
1123
                        // return null if exact match desired
1124
                        if (exact) {
1✔
1125
                                log.debug("No concept description found for concept id " + conceptId + " for locale "
1✔
1126
                                        + desiredLocale.toString());
1✔
1127
                        } else {
1128
                                // returning default description locale ("en") if exact match
1129
                                // not desired
1130
                                if (defaultDescription == null) {
1✔
1131
                                        log.debug("No concept description found for default locale for concept id " + conceptId);
1✔
1132
                                } else {
1133
                                        foundDescription = defaultDescription;
×
1134
                                }
1135
                        }
1136
                }
1137
                return foundDescription;
1✔
1138
        }
1139
        
1140
        /**
1141
         * @return the retiredBy
1142
         */
1143
        @Override
1144
        public User getRetiredBy() {
1145
                return retiredBy;
1✔
1146
        }
1147
        
1148
        /**
1149
         * @param retiredBy the retiredBy to set
1150
         */
1151
        @Override
1152
        public void setRetiredBy(User retiredBy) {
1153
                this.retiredBy = retiredBy;
1✔
1154
        }
1✔
1155
        
1156
        /**
1157
         * @return the dateRetired
1158
         */
1159
        @Override
1160
        public Date getDateRetired() {
1161
                return dateRetired;
1✔
1162
        }
1163
        
1164
        /**
1165
         * @param dateRetired the dateRetired to set
1166
         */
1167
        @Override
1168
        public void setDateRetired(Date dateRetired) {
1169
                this.dateRetired = dateRetired;
1✔
1170
        }
1✔
1171
        
1172
        /**
1173
         * @return the retireReason
1174
         */
1175
        @Override
1176
        public String getRetireReason() {
1177
                return retireReason;
1✔
1178
        }
1179
        
1180
        /**
1181
         * @param retireReason the retireReason to set
1182
         */
1183
        @Override
1184
        public void setRetireReason(String retireReason) {
1185
                this.retireReason = retireReason;
1✔
1186
        }
1✔
1187
        
1188
        /**
1189
         * @return Returns the descriptions.
1190
         */
1191
        public Collection<ConceptDescription> getDescriptions() {
1192
                if (descriptions == null) {
1✔
1193
                        descriptions = new HashSet<>();
1✔
1194
                }
1195
                return descriptions;
1✔
1196
        }
1197
        
1198
        /**
1199
         * Sets the collection of descriptions for this Concept.
1200
         * 
1201
         * @param descriptions the collection of descriptions
1202
         */
1203
        public void setDescriptions(Collection<ConceptDescription> descriptions) {
1204
                this.descriptions = descriptions;
1✔
1205
        }
1✔
1206
        
1207
        /**
1208
         * Add the given description to the list of descriptions for this Concept
1209
         * 
1210
         * @param description the description to add
1211
         */
1212
        public void addDescription(ConceptDescription description) {
1213
                if (description != null && StringUtils.isNotBlank(description.getDescription()) && !descriptions.contains(description)) {
1✔
1214
                        description.setConcept(this);
1✔
1215
                        descriptions.add(description);
1✔
1216
                }
1217
        }
1✔
1218
        
1219
        /**
1220
         * Remove the given description from the list of descriptions for this Concept
1221
         * 
1222
         * @param description the description to remove
1223
         * @return true if the entity was removed, false otherwise
1224
         * <strong>Should</strong> should remove description passed from list of descriptions
1225
         */
1226
        public boolean removeDescription(ConceptDescription description) {
1227
                return descriptions.remove(description);
1✔
1228
        }
1229
        
1230
        /**
1231
         * @return Returns the retired.
1232
         * 
1233
         * @deprecated as of 2.0, use {@link #getRetired()}
1234
         */
1235
        @Override
1236
        @Deprecated
1237
        @JsonIgnore
1238
        public Boolean isRetired() {
1239
                return getRetired();
1✔
1240
        }
1241
        
1242
        /**
1243
         * This method delegates to {@link #isRetired()}. This is only needed for jstl syntax like
1244
         * ${concept.retired} because the return type is a Boolean object instead of a boolean
1245
         * primitive type.
1246
         * 
1247
         * @see org.openmrs.Retireable#isRetired()
1248
         */
1249
        @Override
1250
        public Boolean getRetired() {
1251
                return retired;
1✔
1252
        }
1253
        
1254
        /**
1255
         * @param retired The retired to set.
1256
         */
1257
        @Override
1258
        public void setRetired(Boolean retired) {
1259
                this.retired = retired;
1✔
1260
        }
1✔
1261
        
1262
        /**
1263
         * Gets the synonyms in the given locale. Returns a list of names from the same language with
1264
         * the preferred synonym sorted first, or an empty list if none found.
1265
         * 
1266
         * @param locale
1267
         * @return Collection of ConceptNames which are synonyms for the Concept in the given locale
1268
         */
1269
        public Collection<ConceptName> getSynonyms(Locale locale) {
1270
                
1271
                List<ConceptName> syns = new ArrayList<>();
1✔
1272
                ConceptName preferredConceptName = null;
1✔
1273
                for (ConceptName possibleSynonymInLoc : getSynonyms()) {
1✔
1274
                        if (locale.equals(possibleSynonymInLoc.getLocale())) {
1✔
1275
                                if (possibleSynonymInLoc.isPreferred()) {
1✔
1276
                                        preferredConceptName = possibleSynonymInLoc;
1✔
1277
                                } else {
1278
                                        syns.add(possibleSynonymInLoc);
1✔
1279
                                }
1280
                        }
1281
                }
1✔
1282
                
1283
                // Add preferred name first in the list.
1284
                if (preferredConceptName != null) {
1✔
1285
                        syns.add(0, preferredConceptName);
1✔
1286
                }
1287
                log.debug("returning: " + syns);
1✔
1288
                return syns;
1✔
1289
        }
1290
        
1291
        /**
1292
         * Gets all the non-retired synonyms.
1293
         * 
1294
         * @return Collection of ConceptNames which are synonyms for the Concept or an empty list if
1295
         *         none is found
1296
         * @since 1.7
1297
         */
1298
        public Collection<ConceptName> getSynonyms() {
1299
                return getNames().stream()
1✔
1300
                                .filter(ConceptName::isSynonym)
1✔
1301
                                .collect(Collectors.toSet());
1✔
1302
        }
1303
        
1304
        /**
1305
         * @return Returns the version.
1306
         */
1307
        public String getVersion() {
1308
                return version;
1✔
1309
        }
1310
        
1311
        /**
1312
         * @param version The version to set.
1313
         */
1314
        public void setVersion(String version) {
1315
                this.version = version;
1✔
1316
        }
1✔
1317
        
1318
        /**
1319
         * @return Returns the conceptSets.
1320
         */
1321
        public Collection<ConceptSet> getConceptSets() {
1322
                return conceptSets;
1✔
1323
        }
1324
        
1325
        /**
1326
         * @param conceptSets The conceptSets to set.
1327
         */
1328
        public void setConceptSets(Collection<ConceptSet> conceptSets) {
1329
                this.conceptSets = conceptSets;
1✔
1330
        }
1✔
1331
        
1332
        /**
1333
         * Whether this concept is numeric or not. This will <i>always</i> return false for concept
1334
         * objects. ConceptNumeric.isNumeric() will then <i>always</i> return true.
1335
         * 
1336
         * @return false
1337
         */
1338
        public boolean isNumeric() {
1339
                return false;
1✔
1340
        }
1341
        
1342
        /**
1343
         * @return the conceptMappings for this concept
1344
         */
1345
        public Collection<ConceptMap> getConceptMappings() {
1346
                if (conceptMappings == null) {
1✔
1347
                        conceptMappings = new HashSet<>();
×
1348
                }
1349
                return conceptMappings;
1✔
1350
        }
1351
        
1352
        /**
1353
         * @param conceptMappings the conceptMappings to set
1354
         */
1355
        public void setConceptMappings(Collection<ConceptMap> conceptMappings) {
1356
                this.conceptMappings = conceptMappings;
1✔
1357
        }
1✔
1358
        
1359
        /**
1360
         * Add the given ConceptMap object to this concept's list of concept mappings. If there is
1361
         * already a corresponding ConceptMap object for this concept already, this one will not be
1362
         * added.
1363
         * 
1364
         * @param newConceptMap
1365
         */
1366
        public void addConceptMapping(ConceptMap newConceptMap) {
1367
                if (newConceptMap != null) {
1✔
1368
                        newConceptMap.setConcept(this);
1✔
1369
                }
1370
                if (newConceptMap != null && !getConceptMappings().contains(newConceptMap)) {
1✔
1371
                        if (newConceptMap.getConceptMapType() == null) {
1✔
1372
                                newConceptMap.setConceptMapType(Context.getConceptService().getDefaultConceptMapType());
1✔
1373
                        }
1374
                        getConceptMappings().add(newConceptMap);
1✔
1375
                }
1376
        }
1✔
1377
        
1378
        /**
1379
         * Child Class ConceptComplex overrides this method and returns true. See
1380
         * {@link org.openmrs.ConceptComplex#isComplex()}. Otherwise this method returns false.
1381
         * 
1382
         * @return false
1383
         * @since 1.5
1384
         */
1385
        public boolean isComplex() {
1386
                return false;
1✔
1387
        }
1388
        
1389
        /**
1390
         * Remove the given ConceptMap from the list of mappings for this Concept
1391
         * 
1392
         * @param conceptMap
1393
         * @return true if the entity was removed, false otherwise
1394
         * <strong>Should</strong> remove concept map passed from list of mappings 
1395
         */
1396
        public boolean removeConceptMapping(ConceptMap conceptMap) {
1397
                return getConceptMappings().remove(conceptMap);
1✔
1398
        }
1399
        
1400
        /**
1401
         * @see java.lang.Object#toString()
1402
         */
1403
        @Override
1404
        public String toString() {
1405
                return "Concept #" + conceptId;
1✔
1406
        }
1407
        
1408
        /**
1409
         * @see org.openmrs.Attributable#findPossibleValues(java.lang.String)
1410
         */
1411
        @Override
1412
        @Deprecated
1413
        public List<Concept> findPossibleValues(String searchText) {
1414
                List<Concept> concepts = new ArrayList<>();
1✔
1415
                try {
1416
                        
1417
                        for (ConceptSearchResult searchResult : Context.getConceptService().getConcepts(searchText,
1✔
1418
                            Collections.singletonList(Context.getLocale()), false, null, null, null, null, null, null, null)) {
1✔
1419
                                concepts.add(searchResult.getConcept());
1✔
1420
                        }
1✔
1421
                }
1422
                catch (Exception e) {
×
1423
                        // pass
1424
                }
1✔
1425
                return concepts;
1✔
1426
        }
1427
        
1428
        /**
1429
         * @see org.openmrs.Attributable#getPossibleValues()
1430
         */
1431
        @Override
1432
        @Deprecated
1433
        public List<Concept> getPossibleValues() {
1434
                try {
1435
                        return Context.getConceptService().getConceptsByName("");
×
1436
                }
1437
                catch (Exception e) {
×
1438
                        // pass
1439
                }
1440
                return Collections.emptyList();
×
1441
        }
1442
        
1443
        /**
1444
         * @see org.openmrs.Attributable#hydrate(java.lang.String)
1445
         */
1446
        @Override
1447
        public Concept hydrate(String reference) {
1448
                try {
1449
                        return Context.getConceptService().getConceptByReference(reference);
1✔
1450
                }
1451
                catch (Exception e) {
×
1452
                        // pass
1453
                }
1454
                return null;
×
1455
        }
1456
        
1457
        /**
1458
         * Turns this concept into a very simple serialized string
1459
         * 
1460
         * @see org.openmrs.Attributable#serialize()
1461
         */
1462
        @Override
1463
        public String serialize() {
1464
                if (this.getConceptId() == null) {
×
1465
                        return "";
×
1466
                }
1467
                
1468
                return "" + this.getConceptId();
×
1469
        }
1470
        
1471
        /**
1472
         * @see org.openmrs.Attributable#getDisplayString()
1473
         */
1474
        @Override
1475
        public String getDisplayString() {
1476
                if (getName() == null) {
1✔
1477
                        return toString();
1✔
1478
                } else {
1479
                        return getName().getName();
1✔
1480
                }
1481
        }
1482
        
1483
        /**
1484
         * Convenience method that returns a set of all the locales in which names have been added for
1485
         * this concept.
1486
         * 
1487
         * @return a set of all locales for names for this concept
1488
         * @since 1.7
1489
         * <strong>Should</strong> return all locales for conceptNames for this concept without duplicates
1490
         */
1491
        public Set<Locale> getAllConceptNameLocales() {
1492
                if (getNames().isEmpty()) {
1✔
1493
                        if (log.isDebugEnabled()) {
×
1494
                                log.debug("The Concept with id: " + conceptId + " has no names");
×
1495
                        }
1496
                        return null;
×
1497
                }
1498
                
1499
                Set<Locale> locales = new HashSet<>();
1✔
1500
                
1501
                for (ConceptName cn : getNames()) {
1✔
1502
                        locales.add(cn.getLocale());
1✔
1503
                }
1✔
1504
                
1505
                return locales;
1✔
1506
        }
1507
        
1508
        /**
1509
         * @since 1.5
1510
         * @see org.openmrs.OpenmrsObject#getId()
1511
         */
1512
        @Override
1513
        public Integer getId() {
1514
                return getConceptId();
1✔
1515
        }
1516
        
1517
        /**
1518
         * @since 1.5
1519
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
1520
         */
1521
        @Override
1522
        public void setId(Integer id) {
1523
                setConceptId(id);
1✔
1524
        }
1✔
1525
        
1526
        /**
1527
         * Sort the ConceptSet based on the weight
1528
         * 
1529
         * @return sortedConceptSet Collection&lt;ConceptSet&gt;
1530
         */
1531
        private List<ConceptSet> getSortedConceptSets() {
1532
                List<ConceptSet> cs = new ArrayList<>();
1✔
1533
                if (conceptSets != null) {
1✔
1534
                        cs.addAll(conceptSets);
1✔
1535
                        Collections.sort(cs);
1✔
1536
                }
1537
                
1538
                return cs;
1✔
1539
        }
1540
        
1541
        /**
1542
         * Get all the concept members of current concept
1543
         * 
1544
         * @since 1.7
1545
         * @return List&lt;Concept&gt; the Concepts that are members of this Concept's set
1546
         * <strong>Should</strong> return concept set members sorted according to the sort weight
1547
         * <strong>Should</strong> return all the conceptMembers of current Concept
1548
         * <strong>Should</strong> return unmodifiable list of conceptMember list
1549
         * <strong>Should</strong> return concept set members sorted with retired last
1550
         */
1551
        public List<Concept> getSetMembers() {
1552
                List<Concept> conceptMembers = new ArrayList<>();
1✔
1553
                
1554
                Collection<ConceptSet> sortedConceptSet = getSortedConceptSets();
1✔
1555
                
1556
                for (ConceptSet conceptSet : sortedConceptSet) {
1✔
1557
                        conceptMembers.add(conceptSet.getConcept());
1✔
1558
                }
1✔
1559
                return Collections.unmodifiableList(conceptMembers);
1✔
1560
        }
1561

1562
        /**
1563
         * If includeRetired is true, then the returned object is the list of all the concept
1564
         * set members of current concept, else retired concept set members are excluded.
1565
         *
1566
         * @param includeRetired true/false whether to also include/exclude the retired concepts
1567
         * @since 2.5
1568
         */
1569
        public List<Concept> getSetMembers(boolean includeRetired) {
1570
                if (includeRetired) {
1✔
1571
                        return getSetMembers();
1✔
1572
                } else {
1573
                        return getSetMembers().stream()
1✔
1574
                                .filter(a -> !a.getRetired())
1✔
1575
                                .collect(Collectors.toList());
1✔
1576
                }
1577
        }
1578
        
1579
        /**
1580
         * Appends the concept to the end of the existing list of concept members for this Concept
1581
         * 
1582
         * @since 1.7
1583
         * @param setMember Concept to add to the
1584
         * <strong>Should</strong> add concept as a conceptSet
1585
         * <strong>Should</strong> append concept to the existing list of conceptSet
1586
         * <strong>Should</strong> place the new concept last in the list
1587
         * <strong>Should</strong> assign the calling component as parent to the ConceptSet
1588
         */
1589
        public void addSetMember(Concept setMember) {
1590
                addSetMember(setMember, -1);
1✔
1591
        }
1✔
1592
        
1593
        /**
1594
         * Add the concept to the existing member to the list of set members in the given location. <br>
1595
         * <br>
1596
         * index of 0 is before the first concept<br>
1597
         * index of -1 is after last.<br>
1598
         * index of 1 is after the first but before the second, etc<br>
1599
         * 
1600
         * @param setMember the Concept to add as a child of this Concept
1601
         * @param index where in the list of set members to put this setMember
1602
         * @since 1.7
1603
         * <strong>Should</strong> assign the given concept as a ConceptSet
1604
         * <strong>Should</strong> insert the concept before the first with zero index
1605
         * <strong>Should</strong> insert the concept at the end with negative one index
1606
         * <strong>Should</strong> insert the concept in the third slot
1607
         * <strong>Should</strong> assign the calling component as parent to the ConceptSet
1608
         * <strong>Should</strong> add the concept to the current list of conceptSet
1609
         * @see #getSortedConceptSets()
1610
         */
1611
        public void addSetMember(Concept setMember, int index) {
1612
                List<ConceptSet> sortedConceptSets = getSortedConceptSets();
1✔
1613
                int setsSize = sortedConceptSets.size();
1✔
1614
                
1615
                //after sorting, we need to reset the sort weights because retired
1616
                //sets have moved to the bottom and hence need to be reassigned
1617
                //higher sort weights than the non retired ones
1618
                double weight = 990.0;
1✔
1619
                for (ConceptSet conceptSet : sortedConceptSets) {
1✔
1620
                        weight += 10.0;
1✔
1621
                        conceptSet.setSortWeight(weight);
1✔
1622
                }
1✔
1623
                
1624
                if (sortedConceptSets.isEmpty()) {
1✔
1625
                        weight = 1000.0;
1✔
1626
                } else if (index == -1 || index >= setsSize) {
1✔
1627
                        // deals with list size of 1 and any large index given by dev
1628
                        weight = sortedConceptSets.get(setsSize - 1).getSortWeight() + 10.0;
1✔
1629
                } else if (index == 0) {
1✔
1630
                        weight = sortedConceptSets.get(0).getSortWeight() - 10.0;
1✔
1631
                } else {
1632
                        // put the weight between two
1633
                        double prevSortWeight = sortedConceptSets.get(index - 1).getSortWeight();
1✔
1634
                        double nextSortWeight = sortedConceptSets.get(index).getSortWeight();
1✔
1635
                        weight = (prevSortWeight + nextSortWeight) / 2;
1✔
1636
                }
1637
                
1638
                ConceptSet conceptSet = new ConceptSet(setMember, weight);
1✔
1639
                conceptSet.setConceptSet(this);
1✔
1640
                conceptSets.add(conceptSet);
1✔
1641
        }
1✔
1642

1643
        /**
1644
         * @see org.openmrs.customdatatype.Customizable#getAttributes()
1645
         */
1646
        @Override
1647
        public Set<ConceptAttribute> getAttributes() {
1648
                if (attributes == null) {
1✔
1649
                        attributes = new LinkedHashSet<>();
×
1650
                }
1651
                return attributes;
1✔
1652
        }
1653

1654
        /**
1655
         * @see org.openmrs.customdatatype.Customizable#getActiveAttributes()
1656
         */
1657
        @Override
1658
        public Collection<ConceptAttribute> getActiveAttributes() {
1659
                return getAttributes().stream()
1✔
1660
                                .filter(attr -> !attr.getVoided())
1✔
1661
                                .collect(Collectors.toList());
1✔
1662
        }
1663

1664
        /**
1665
         * @see org.openmrs.customdatatype.Customizable#getActiveAttributes(org.openmrs.customdatatype.CustomValueDescriptor)
1666
         */
1667
        @Override
1668
        public List<ConceptAttribute> getActiveAttributes(CustomValueDescriptor ofType) {
1669
                return getAttributes().stream()
×
1670
                                .filter(attr -> attr.getAttributeType().equals(ofType) && !attr.getVoided())
×
1671
                                .collect(Collectors.toList());
×
1672
        }
1673

1674
        /**
1675
         * @param attributes the attributes to set
1676
         */
1677
        public void setAttributes(Set<ConceptAttribute> attributes) {
1678
                this.attributes = attributes;
1✔
1679
        }
1✔
1680

1681
        /**
1682
         * @see org.openmrs.customdatatype.Customizable#addAttribute(Attribute)
1683
         */
1684
        @Override
1685
        public void addAttribute(ConceptAttribute attribute) {
1686
                getAttributes().add(attribute);
×
1687
                attribute.setOwner(this);
×
1688
        }
×
1689

1690
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc