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

openmrs / openmrs-core / 10946298164

19 Sep 2024 05:33PM UTC coverage: 63.759% (+0.09%) from 63.671%
10946298164

push

github

mogoodrich
TRUNK-6265: Bugs in Concept.getPreferredName(Locale) method (#4749)

(cherry picked from commit bc725f242)

14 of 16 new or added lines in 1 file covered. (87.5%)

12 existing lines in 9 files now uncovered.

21692 of 34022 relevant lines covered (63.76%)

0.64 hits per line

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

82.72
/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.search.annotations.ContainedIn;
30
import org.hibernate.search.annotations.DocumentId;
31
import org.hibernate.search.annotations.Field;
32
import org.hibernate.search.annotations.FullTextFilterDef;
33
import org.hibernate.search.annotations.FullTextFilterDefs;
34
import org.hibernate.search.annotations.IndexedEmbedded;
35
import org.openmrs.annotation.AllowDirectAccess;
36
import org.openmrs.api.APIException;
37
import org.openmrs.api.ConceptNameType;
38
import org.openmrs.api.ConceptService;
39
import org.openmrs.api.context.Context;
40
import org.openmrs.api.db.hibernate.search.TermsFilterFactory;
41
import org.openmrs.customdatatype.CustomValueDescriptor;
42
import org.openmrs.customdatatype.Customizable;
43
import org.openmrs.util.LocaleUtility;
44
import org.openmrs.util.OpenmrsUtil;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
47
import org.springframework.util.ObjectUtils;
48

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

130
        private Set<ConceptAttribute> attributes = new LinkedHashSet<>();
1✔
131

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

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

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

679
                                }
680
                        }
1✔
681

682
                        if (bestMatch != null) {
1✔
NEW
683
                                return bestMatch;
×
684
                        }
685

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

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

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

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

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

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

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

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

1688
}
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