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

openmrs / openmrs-core / 44649983700

20 Nov 2026 12:57AM UTC coverage: 67.361% (+0.01%) from 67.351%
44649983700

push

github

web-flow
TRUNK-6868: Allow the session timeout to be configured (2.9.x) (#6840)

24 of 28 new or added lines in 2 files covered. (85.71%)

10 existing lines in 8 files now uncovered.

26929 of 39977 relevant lines covered (67.36%)

0.67 hits per line

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

93.96
/api/src/main/java/org/openmrs/api/impl/ConceptServiceImpl.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.api.impl;
11

12
import java.lang.reflect.InvocationTargetException;
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.Iterator;
20
import java.util.List;
21
import java.util.Locale;
22
import java.util.Map;
23
import java.util.Set;
24

25
import org.apache.commons.beanutils.BeanUtils;
26
import org.apache.commons.text.StringEscapeUtils;
27
import org.apache.commons.collections.CollectionUtils;
28
import org.apache.commons.lang3.StringUtils;
29
import org.hibernate.Hibernate;
30
import org.openmrs.Concept;
31
import org.openmrs.ConceptAnswer;
32
import org.openmrs.ConceptAttribute;
33
import org.openmrs.ConceptAttributeType;
34
import org.openmrs.ConceptClass;
35
import org.openmrs.ConceptComplex;
36
import org.openmrs.ConceptDatatype;
37
import org.openmrs.ConceptDescription;
38
import org.openmrs.ConceptMap;
39
import org.openmrs.ConceptMapType;
40
import org.openmrs.ConceptName;
41
import org.openmrs.ConceptNameTag;
42
import org.openmrs.ConceptNumeric;
43
import org.openmrs.ConceptProposal;
44
import org.openmrs.ConceptReferenceRange;
45
import org.openmrs.ConceptReferenceRangeContext;
46
import org.openmrs.ConceptReferenceTerm;
47
import org.openmrs.ConceptReferenceTermMap;
48
import org.openmrs.ConceptSearchResult;
49
import org.openmrs.ConceptSet;
50
import org.openmrs.ConceptSource;
51
import org.openmrs.ConceptStopWord;
52
import org.openmrs.Drug;
53
import org.openmrs.DrugIngredient;
54
import org.openmrs.Obs;
55
import org.openmrs.Patient;
56
import org.openmrs.Person;
57
import org.openmrs.api.APIException;
58
import org.openmrs.api.AdministrationService;
59
import org.openmrs.api.ConceptInUseException;
60
import org.openmrs.api.ConceptNameInUseException;
61
import org.openmrs.api.ConceptService;
62
import org.openmrs.api.ConceptStopWordException;
63
import org.openmrs.api.ConceptsLockedException;
64
import org.openmrs.api.context.Context;
65
import org.openmrs.api.db.ConceptDAO;
66
import org.openmrs.api.db.DAOException;
67
import org.openmrs.api.db.hibernate.HibernateUtil;
68
import org.openmrs.customdatatype.CustomDatatypeUtil;
69
import org.openmrs.parameter.ConceptSearchCriteria;
70
import org.openmrs.parameter.ConceptSearchCriteriaBuilder;
71
import org.openmrs.util.ConceptReferenceRangeUtility;
72
import org.openmrs.util.OpenmrsConstants;
73
import org.openmrs.util.OpenmrsUtil;
74
import org.openmrs.util.UuidUtil;
75
import org.openmrs.validator.ValidateUtil;
76
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
78
import org.springframework.cache.annotation.CacheEvict;
79
import org.springframework.cache.annotation.Cacheable;
80
import org.springframework.transaction.annotation.Transactional;
81

82
/**
83
 * Default Implementation of ConceptService service layer classes
84
 * 
85
 * @see org.openmrs.api.ConceptService to access these methods
86
 */
87
@Transactional
88
public class ConceptServiceImpl extends BaseOpenmrsService implements ConceptService {
1 ✔
89
        
90
        private static final Logger log = LoggerFactory.getLogger(ConceptServiceImpl.class);
1 ✔
91
        
92
        private ConceptDAO dao;
93
        
94
        private static Concept trueConcept;
95
        
96
        private static Concept falseConcept;
97
        
98
        private static Concept unknownConcept;
99

100
        private static final String ERROR_MESSAGE = "Error generated";
101

102
        private static final String CONCEPT_IDS_BY_MAPPING_CACHE_NAME = "conceptIdsByMapping";
103

104
        /**
105
         * @see org.openmrs.api.ConceptService#setConceptDAO(org.openmrs.api.db.ConceptDAO)
106
         */
107
        @Override
108
        public void setConceptDAO(ConceptDAO dao) {
109
                this.dao = dao;
1 ✔
110
        }
1 ✔
111

112
        /**
113
         * @see org.openmrs.api.ConceptService#saveConcept(org.openmrs.Concept)
114
         * <strong>Should</strong> return the concept with new conceptID if creating new concept
115
         * <strong>Should</strong> return the concept with same conceptID if updating existing concept
116
         * <strong>Should</strong> leave preferred name preferred if set
117
         * <strong>Should</strong> set default preferred name to fully specified first
118
         * <strong>Should</strong> not set default preferred name to short or index terms
119
     * <strong>Should</strong> force set flag if set members exist
120
         */
121
        @Override
122
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
123
        public Concept saveConcept(Concept concept) throws APIException {
124
                ensureConceptMapTypeIsSet(concept);
1 ✔
125

126
                CustomDatatypeUtil.saveAttributesIfNecessary(concept);
1 ✔
127

128
                // make sure the administrator hasn't turned off concept editing
129
                checkIfLocked();
1 ✔
130
                checkIfDatatypeCanBeChanged(concept);
1 ✔
131
                
132
                List<ConceptName> changedConceptNames = null;
1 ✔
133
                Map<String, ConceptName> uuidClonedConceptNameMap = null;
1 ✔
134
                
135
                if (concept.getConceptId() != null) {
1 ✔
136
                        uuidClonedConceptNameMap = new HashMap<>();
1 ✔
137
                        for (ConceptName conceptName : concept.getNames()) {
1 ✔
138
                                // ignore newly added names
139
                                if (conceptName.getConceptNameId() != null) {
1 ✔
140
                                        ConceptName clone = cloneConceptName(conceptName);
1 ✔
141
                                        clone.setConceptNameId(null);
1 ✔
142
                                        uuidClonedConceptNameMap.put(conceptName.getUuid(), clone);
1 ✔
143
                                        
144
                                        if (hasNameChanged(conceptName)) {
1 ✔
145
                                                if (changedConceptNames == null) {
1 ✔
146
                                                        changedConceptNames = new ArrayList<>();
1 ✔
147
                                                }
148
                                                changedConceptNames.add(conceptName);
1 ✔
149
                                        } else {
150
                                                // put back the concept name id
151
                                                clone.setConceptNameId(conceptName.getConceptNameId());
1 ✔
152
                                                // Use the cloned version
153
                                                try {
154
                                                        BeanUtils.copyProperties(conceptName, clone);
1 ✔
155
                                                }
156
                                                catch (IllegalAccessException | InvocationTargetException e) {
×
157
                                                        log.error(ERROR_MESSAGE, e);
×
158
                                                }
1 ✔
159
                                        }
160
                                }
161
                        }
1 ✔
162
                }
163
                
164
                if (CollectionUtils.isNotEmpty(changedConceptNames)) {
1 ✔
165
                        for (ConceptName changedName : changedConceptNames) {
1 ✔
166
                                // void old concept name
167
                                changedName.setVoided(true);
1 ✔
168
                                changedName.setDateVoided(new Date());
1 ✔
169
                                changedName.setVoidedBy(Context.getAuthenticatedUser());
1 ✔
170
                                changedName.setVoidReason(Context.getMessageSourceService().getMessage("Concept.name.voidReason.nameChanged"));
1 ✔
171

172
                                makeVoidedNameSynonym(changedName);
1 ✔
173
                                makeLocaleNotPreferred(changedName);
1 ✔
174
                                
175
                                // create a new concept name from the matching cloned
176
                                // conceptName
177
                                ConceptName clone = uuidClonedConceptNameMap.get(changedName.getUuid());
1 ✔
178
                                clone.setUuid(UuidUtil.newUuidString());
1 ✔
179
                                clone.setDateCreated(null);
1 ✔
180
                                clone.setCreator(null);
1 ✔
181
                                concept.addName(clone);
1 ✔
182
                        }
1 ✔
183
                }
184
                ensurePreferredNameForLocale(concept);
1 ✔
185
                logConceptChangedData(concept);
1 ✔
186
                
187
                // force isSet when concept has members
188
                if (!concept.getSet() && (!concept.getSetMembers().isEmpty())) {
1 ✔
189
                        concept.setSet(true);
1 ✔
190
                }
191

192
                return dao.saveConcept(concept);
1 ✔
193
        }
194

195
        private void ensureConceptMapTypeIsSet(Concept concept) {
196
                ConceptMapType defaultConceptMapType = null;
1 ✔
197
                for (ConceptMap map : concept.getConceptMappings()) {
1 ✔
198
                        if (map.getConceptMapType() == null) {
1 ✔
199
                                if (defaultConceptMapType == null) {
×
200
                                        defaultConceptMapType = Context.getConceptService().getDefaultConceptMapType();
×
201
                                }
202
                                map.setConceptMapType(defaultConceptMapType);
×
203
                        }
204
                }
1 ✔
205
        }
1 ✔
206

207
        private void makeVoidedNameSynonym(ConceptName conceptName) {
208
                // Helps to avoid having  multiple fully
209
                // specified or preferred names in a locale
210
                // in case the name is unvoided
211
                if (!conceptName.isSynonym()) {
1 ✔
212
                        conceptName.setConceptNameType(null);
1 ✔
213
                }
214
        }
1 ✔
215

216
        private void makeLocaleNotPreferred(ConceptName conceptName) {
217
                if (conceptName.getLocalePreferred()) {
1 ✔
218
                        conceptName.setLocalePreferred(false);
1 ✔
219
                }
220
        }
1 ✔
221

222
        private void ensurePreferredNameForLocale(Concept concept) {
223
                //Ensure if there's a name for a locale that at least one suitable name is marked preferred in that locale
224
                //Order of preference is:
225
                // 1) any name that concept.getPreferredName returns
226
                // 2) fully specified name
227
                // 3) any synonym
228
                // short name and index terms are never preferred.
229

230
                Set<Locale> checkedLocales = new HashSet<>();
1 ✔
231
                for (ConceptName n : concept.getNames()) {
1 ✔
232
                        Locale locale = n.getLocale();
1 ✔
233
                        if (checkedLocales.contains(locale)) {
1 ✔
234
                                continue; //we've already checked this locale
1 ✔
235
                        }
236

237
                        //getPreferredName(locale) returns any name marked preferred,
238
                        //or the fullySpecifiedName even if not marked preferred
239
                        ConceptName possiblePreferredName = concept.getPreferredName(locale);
1 ✔
240

241
                        if (possiblePreferredName != null) {
1 ✔
242
                                //do nothing yet, but stick around to setLocalePreferred(true)
243
                        } else if (concept.getFullySpecifiedName(locale) != null) {
1 ✔
244
                                possiblePreferredName = concept.getFullySpecifiedName(locale);
×
245
                        } else if (!CollectionUtils.isEmpty(concept.getSynonyms(locale))) {
1 ✔
246
                                concept.getSynonyms(locale).iterator().next().setLocalePreferred(true);
1 ✔
247
                        }
248
                        //index terms are never used as preferred name
249

250
                        if (possiblePreferredName != null) { //there may have been none
1 ✔
251
                                possiblePreferredName.setLocalePreferred(true);
1 ✔
252
                        }
253
                        checkedLocales.add(locale);
1 ✔
254
                }
1 ✔
255
        }
1 ✔
256

257
        private void logConceptChangedData(Concept concept) {
258
                concept.setDateChanged(new Date());
1 ✔
259
                concept.setChangedBy(Context.getAuthenticatedUser());
1 ✔
260
        }
1 ✔
261

262
        /**
263
         * @see org.openmrs.api.ConceptService#saveDrug(org.openmrs.Drug)
264
         */
265
        @Override
266
        public Drug saveDrug(Drug drug) throws APIException {
267
                checkIfLocked();
1 ✔
268
                return dao.saveDrug(drug);
1 ✔
269
        }
270
        
271
        /**
272
         * @see org.openmrs.api.ConceptService#purgeConcept(Concept)
273
         */
274
        @Override
275
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
276
        public void purgeConcept(Concept concept) throws APIException {
277
                checkIfLocked();
1 ✔
278
                
279
                if (concept.getConceptId() != null) {
1 ✔
280
                        for (ConceptName conceptName : concept.getNames()) {
1 ✔
281
                                if (hasAnyObservation(conceptName)) {
1 ✔
282
                                        throw new ConceptNameInUseException("Can't delete concept with id : " + concept.getConceptId()
1 ✔
283
                                                + " because it has a name '" + conceptName.getName()
1 ✔
284
                                                + "' which is being used by some observation(s)");
285
                                }
286
                        }
1 ✔
287
                }
288
                
289
                dao.purgeConcept(concept);
1 ✔
290
        }
1 ✔
291
        
292
        /**
293
         * @see org.openmrs.api.ConceptService#retireConcept(org.openmrs.Concept, java.lang.String)
294
         */
295
        @Override
296
        public Concept retireConcept(Concept concept, String reason) throws APIException {
297
                if (StringUtils.isBlank(reason)) {
1 ✔
298
                        throw new IllegalArgumentException(Context.getMessageSourceService().getMessage("general.voidReason.empty"));
1 ✔
299
                }
300
                
301
                // only do this if the concept isn't retired already
302
                if (!concept.getRetired()) {
1 ✔
303
                        checkIfLocked();
×
304
                        
305
                        concept.setRetired(true);
×
306
                        concept.setRetireReason(reason);
×
307
                        return Context.getConceptService().saveConcept(concept);
×
308
                }
309
                
310
                return concept;
1 ✔
311
        }
312
        
313
        /**
314
         * @see org.openmrs.api.ConceptService#retireDrug(org.openmrs.Drug, java.lang.String)
315
         * @throws APIException
316
         */
317
        @Override
318
        public Drug retireDrug(Drug drug, String reason) throws APIException {
319
                return dao.saveDrug(drug);
1 ✔
320
        }
321
        
322
        /**
323
         * @see org.openmrs.api.ConceptService#unretireDrug(org.openmrs.Drug)
324
         */
325
        @Override
326
        public Drug unretireDrug(Drug drug) throws APIException {
327
                return Context.getConceptService().saveDrug(drug);
1 ✔
328
        }
329
        
330
        /**
331
         * @see org.openmrs.api.ConceptService#purgeDrug(org.openmrs.Drug)
332
         * @throws APIException
333
         */
334
        @Override
335
        public void purgeDrug(Drug drug) throws APIException {
336
                dao.purgeDrug(drug);
1 ✔
337
        }
1 ✔
338
        
339
        /**
340
         * @see org.openmrs.api.ConceptService#getConcept(java.lang.Integer)
341
         */
342
        @Override
343
        @Transactional(readOnly = true)
344
        public Concept getConcept(Integer conceptId) throws APIException {
345
                return dao.getConcept(conceptId);
1 ✔
346
        }
347

348
        /**
349
         * @see org.openmrs.api.ConceptService#getConceptByReference(String conceptRef)
350
         */
351
        @Override
352
        @Transactional(readOnly = true)
353
        public Concept getConceptByReference(String conceptRef) {
354
                if (StringUtils.isBlank(conceptRef)) {
1 ✔
355
                        return null;
1 ✔
356
                }
357
                ConceptSearchCriteria criteria = new ConceptSearchCriteriaBuilder().addConceptReference(conceptRef)
1 ✔
358
                        .includeRetired(true).build();
1 ✔
359
                List<Concept> concepts = getConcepts(criteria);
1 ✔
360
                return concepts.isEmpty() ? null : concepts.get(0);
1 ✔
361
        }
362
        
363
        /**
364
         * @see org.openmrs.api.ConceptService#getConceptName(java.lang.Integer)
365
         */
366
        @Override
367
        @Transactional(readOnly = true)
368
        public ConceptName getConceptName(Integer conceptNameId) throws APIException {
369
                return dao.getConceptName(conceptNameId);
1 ✔
370
        }
371
        
372
        /**
373
         * @see org.openmrs.api.ConceptService#getConceptAnswer(java.lang.Integer)
374
         */
375
        @Override
376
        @Transactional(readOnly = true)
377
        public ConceptAnswer getConceptAnswer(Integer conceptAnswerId) throws APIException {
378
                return dao.getConceptAnswer(conceptAnswerId);
1 ✔
379
        }
380
        
381
        /**
382
         * @see org.openmrs.api.ConceptService#getDrug(java.lang.Integer)
383
         */
384
        @Override
385
        @Transactional(readOnly = true)
386
        public Drug getDrug(Integer drugId) throws APIException {
387
                return dao.getDrug(drugId);
1 ✔
388
        }
389
        
390
        /**
391
         * @see org.openmrs.api.ConceptService#getConceptNumeric(java.lang.Integer)
392
         */
393
        @Override
394
        @Transactional(readOnly = true)
395
        public ConceptNumeric getConceptNumeric(Integer conceptId) throws APIException {
396
                return dao.getConceptNumeric(conceptId);
1 ✔
397
        }
398
        
399
        /**
400
         * @see org.openmrs.api.ConceptService#getConceptComplex(java.lang.Integer)
401
         */
402
        @Override
403
        @Transactional(readOnly = true)
404
        public ConceptComplex getConceptComplex(Integer conceptId) {
405
                return dao.getConceptComplex(conceptId);
1 ✔
406
        }
407
        
408
        /**
409
         * @see org.openmrs.api.ConceptService#getAllConcepts()
410
         */
411
        @Override
412
        @Transactional(readOnly = true)
413
        public List<Concept> getAllConcepts() throws APIException {
414
                return Context.getConceptService().getAllConcepts(null, true, true);
1 ✔
415
        }
416
        
417
        /**
418
         * @see org.openmrs.api.ConceptService#getAllConcepts(java.lang.String, boolean, boolean)
419
         */
420
        @Override
421
        @Transactional(readOnly = true)
422
        public List<Concept> getAllConcepts(String sortBy, boolean asc, boolean includeRetired) throws APIException {
423
                String tmpSortBy = sortBy == null ? "conceptId" : sortBy;
1 ✔
424
                
425
                return dao.getAllConcepts(tmpSortBy, asc, includeRetired);
1 ✔
426
        }
427

428
        /**
429
         * @see org.openmrs.api.ConceptService#getConceptsByName(java.lang.String)
430
         */
431
        @Override
432
        @Transactional(readOnly = true)
433
        public List<Concept> getConceptsByName(String name) throws APIException {
434
                return getConcepts(name, Context.getLocale(), true, null, null);
1 ✔
435
        }
436
        
437
        /**
438
         * @see org.openmrs.api.ConceptService#getConceptByName(java.lang.String)
439
         */
440
        @Override
441
        @Transactional(readOnly = true)
442
        public Concept getConceptByName(String name) {
443
                if (StringUtils.isBlank(name)) {
1 ✔
444
                        return null;
1 ✔
445
                }
446
                return dao.getConceptByName(name);
1 ✔
447
        }
448

449
        /**
450
         * @see org.openmrs.api.ConceptService#getConcept(java.lang.String)
451
         */
452
        @Override
453
        @Transactional(readOnly = true)
454
        public Concept getConcept(String conceptIdOrName) {
455
                Concept c;
456
                Integer conceptId;
457
                try {
458
                        conceptId = Integer.valueOf(conceptIdOrName);
1 ✔
459
                }
460
                catch (NumberFormatException nfe) {
1 ✔
461
                        conceptId = null;
1 ✔
462
                }
1 ✔
463
                
464
                if (conceptId != null) {
1 ✔
465
                        c = Context.getConceptService().getConcept(conceptId);
1 ✔
466
                } else {
467
                        c = Context.getConceptService().getConceptByName(conceptIdOrName);
1 ✔
468
                }
469
                return c;
1 ✔
470
        }
471
        
472
        /**
473
         * Generic getConcepts method (used internally) to get concepts matching a on name
474
         * 
475
         * @param name
476
         * @param loc
477
         * @param searchOnPhrase
478
         * @return
479
         */
480
        private List<Concept> getConcepts(String name, Locale loc, boolean searchOnPhrase, List<ConceptClass> classes,
481
                List<ConceptDatatype> datatypes) {
482
                List<ConceptClass> tmpClasses = classes == null ? new ArrayList<>() : classes;
1 ✔
483
                List<ConceptDatatype> tmpDatatypes = datatypes == null ? new ArrayList<>() : datatypes;
1 ✔
484
                
485
                return dao.getConcepts(name, loc, searchOnPhrase, tmpClasses, tmpDatatypes);
1 ✔
486
        }
487
        
488
        /**
489
         * @see org.openmrs.api.ConceptService#getDrug(java.lang.String)
490
         */
491
        @Override
492
        @Transactional(readOnly = true)
493
        public Drug getDrug(String drugNameOrId) {
494
                Integer drugId;
495
                
496
                try {
497
                        drugId = Integer.valueOf(drugNameOrId);
×
498
                }
499
                catch (NumberFormatException nfe) {
1 ✔
500
                        drugId = null;
1 ✔
501
                }
×
502
                
503
                if (drugId != null) {
1 ✔
504
                        return Context.getConceptService().getDrug(drugId);
×
505
                } else {
506
                        List<Drug> drugs = dao.getDrugs(drugNameOrId, null, false);
1 ✔
507
                        if (drugs.size() > 1) {
1 ✔
508
                                log.warn("more than one drug name returned with name:" + drugNameOrId);
×
509
                        }
510
                        if (drugs.isEmpty()) {
1 ✔
511
                                return null;
1 ✔
512
                        }
513
                        return drugs.get(0);
1 ✔
514
                }
515
        }
516
        
517
        /**
518
         * @see org.openmrs.api.ConceptService#getAllDrugs()
519
         */
520
        @Override
521
        @Transactional(readOnly = true)
522
        public List<Drug> getAllDrugs() {
523
                return Context.getConceptService().getAllDrugs(true);
1 ✔
524
        }
525
        
526
        /**
527
         * @see org.openmrs.api.ConceptService#getAllDrugs(boolean)
528
         */
529
        @Override
530
        @Transactional(readOnly = true)
531
        public List<Drug> getAllDrugs(boolean includeRetired) {
532
                return dao.getDrugs(null, null, includeRetired);
1 ✔
533
        }
534
        
535
        /**
536
         * @see org.openmrs.api.ConceptService#getDrugsByConcept(org.openmrs.Concept)
537
         */
538
        @Override
539
        @Transactional(readOnly = true)
540
        public List<Drug> getDrugsByConcept(Concept concept) {
541
                return dao.getDrugs(null, concept, false);
1 ✔
542
        }
543
        
544
        /**
545
         * @see org.openmrs.api.ConceptService#getDrugs(java.lang.String)
546
         */
547
        @Override
548
        @Transactional(readOnly = true)
549
        public List<Drug> getDrugs(String phrase) {
550
                List<Drug> drugs = new ArrayList<>();
1 ✔
551
                // trying to treat search phrase as drug id
552
                try {
553
                        Integer drugId = Integer.parseInt(phrase);
1 ✔
554
                        Drug targetDrug = Context.getConceptService().getDrug(drugId);
1 ✔
555
                        // if drug was found add it to result
556
                        if (targetDrug != null) {
1 ✔
557
                                drugs.add(targetDrug);
1 ✔
558
                        }
559
                }
560
                catch (NumberFormatException e) {
1 ✔
561
                        // do nothing
562
                }
1 ✔
563
                
564
                // also try to treat search phrase as drug concept id
565
                try {
566
                        Integer conceptId = Integer.parseInt(phrase);
1 ✔
567
                        Concept targetConcept = Context.getConceptService().getConcept(conceptId);
1 ✔
568
                        if (targetConcept != null) {
1 ✔
569
                                drugs.addAll(Context.getConceptService().getDrugsByConcept(targetConcept));
1 ✔
570
                        }
571
                }
572
                catch (NumberFormatException e) {
1 ✔
573
                        // do nothing
574
                }
1 ✔
575
                
576
                drugs.addAll(dao.getDrugs(phrase));
1 ✔
577
                return drugs;
1 ✔
578
        }
579
        
580
        /**
581
         * @see org.openmrs.api.ConceptService#getConceptsByClass(org.openmrs.ConceptClass)
582
         */
583
        @Override
584
        @Transactional(readOnly = true)
585
        public List<Concept> getConceptsByClass(ConceptClass cc) {                
586
                return dao.getConceptsByClass(cc);
1 ✔
587
        }
588
        
589
        /**
590
         * @see org.openmrs.api.ConceptService#getAllConceptClasses(boolean)
591
         */
592
        @Override
593
        @Transactional(readOnly = true)
594
        public List<ConceptClass> getAllConceptClasses(boolean includeRetired) {
595
                return dao.getAllConceptClasses(includeRetired);
1 ✔
596
        }
597
        
598
        /**
599
         * @see org.openmrs.api.ConceptService#getConceptClass(java.lang.Integer)
600
         */
601
        @Override
602
        @Transactional(readOnly = true)
603
        public ConceptClass getConceptClass(Integer i) {
604
                return dao.getConceptClass(i);
1 ✔
605
        }
606
        
607
        /**
608
         * @see org.openmrs.api.ConceptService#getConceptClassByName(java.lang.String)
609
         */
610
        @Override
611
        @Transactional(readOnly = true)
612
        public ConceptClass getConceptClassByName(String name) {
613
                List<ConceptClass> ccList = dao.getConceptClasses(name);
1 ✔
614
                if (ccList.size() > 1) {
1 ✔
615
                        log.warn("More than one ConceptClass found with name: " + name);
×
616
                }
617
                if (ccList.size() == 1) {
1 ✔
618
                        return ccList.get(0);
1 ✔
619
                }
620
                return null;
1 ✔
621
        }
622
        
623
        /**
624
         * @see org.openmrs.api.ConceptService#getAllConceptClasses(boolean)
625
         */
626
        @Override
627
        @Transactional(readOnly = true)
628
        public List<ConceptClass> getAllConceptClasses() throws APIException {
629
                return Context.getConceptService().getAllConceptClasses(true);
1 ✔
630
        }
631
        
632
        /**
633
         * @see org.openmrs.api.ConceptService#saveConceptClass(org.openmrs.ConceptClass)
634
         */
635
        @Override
636
        public ConceptClass saveConceptClass(ConceptClass cc) throws APIException {
637
                return dao.saveConceptClass(cc);
1 ✔
638
        }
639
        
640
        /**
641
         * @see org.openmrs.api.ConceptService#purgeConceptClass(org.openmrs.ConceptClass)
642
         */
643
        @Override
644
        public void purgeConceptClass(ConceptClass cc) {
645
                dao.purgeConceptClass(cc);
1 ✔
646
        }
1 ✔
647
        
648
        /**
649
         * @see org.openmrs.api.ConceptService#purgeConceptNameTag(org.openmrs.ConceptNameTag)
650
         */
651
        @Override
652
        public void purgeConceptNameTag(ConceptNameTag cnt) {
653
                dao.deleteConceptNameTag(cnt);
1 ✔
654
        }
1 ✔
655
        
656
        /**
657
         * @see org.openmrs.api.ConceptService#getAllConceptDatatypes()
658
         */
659
        @Override
660
        @Transactional(readOnly = true)
661
        public List<ConceptDatatype> getAllConceptDatatypes() {
662
                return Context.getConceptService().getAllConceptDatatypes(true);
1 ✔
663
        }
664
        
665
        /**
666
         * @see org.openmrs.api.ConceptService#getAllConceptDatatypes(boolean)
667
         */
668
        @Override
669
        @Transactional(readOnly = true)
670
        public List<ConceptDatatype> getAllConceptDatatypes(boolean includeRetired) throws APIException {
671
                return dao.getAllConceptDatatypes(includeRetired);
1 ✔
672
        }
673
        
674
        /**
675
         * @see org.openmrs.api.ConceptService#getConceptDatatype(java.lang.Integer)
676
         */
677
        @Override
678
        @Transactional(readOnly = true)
679
        public ConceptDatatype getConceptDatatype(Integer i) {
680
                return dao.getConceptDatatype(i);
1 ✔
681
        }
682
        
683
        /**
684
         * @see org.openmrs.api.ConceptService#getConceptDatatypeByName(java.lang.String)
685
         */
686
        @Override
687
        @Transactional(readOnly = true)
688
        public ConceptDatatype getConceptDatatypeByName(String name) {
689
                return dao.getConceptDatatypeByName(name);
1 ✔
690
        }
691
        
692
        /**
693
         * @see org.openmrs.api.ConceptService#getConceptSetsByConcept(org.openmrs.Concept)
694
         */
695
        @Override
696
        @Transactional(readOnly = true)
697
        public List<ConceptSet> getConceptSetsByConcept(Concept concept) throws APIException {
698
                return dao.getConceptSetsByConcept(concept);
1 ✔
699
        }
700
        
701
        /**
702
         * @see org.openmrs.api.ConceptService#getConceptsByConceptSet(Concept)
703
         */
704
        @Override
705
        @Transactional(readOnly = true)
706
        public List<Concept> getConceptsByConceptSet(Concept c) {
707
                Set<Integer> alreadySeen = new HashSet<>();
1 ✔
708
                List<Concept> ret = new ArrayList<>();
1 ✔
709
                explodeConceptSetHelper(c, ret, alreadySeen);
1 ✔
710
                return ret;
1 ✔
711
        }
712
        
713
        /**
714
         * @see org.openmrs.api.ConceptService#getSetsContainingConcept(org.openmrs.Concept)
715
         */
716
        @Override
717
        @Transactional(readOnly = true)
718
        public List<ConceptSet> getSetsContainingConcept(Concept concept) {
719
                if (concept.getConceptId() == null) {
1 ✔
720
                        return Collections.emptyList();
1 ✔
721
                }
722
                
723
                return dao.getSetsContainingConcept(concept);
1 ✔
724
        }
725
        
726
        /**
727
         * @see org.openmrs.api.ConceptService#getConceptProposal(java.lang.Integer)
728
         */
729
        @Override
730
        @Transactional(readOnly = true)
731
        public ConceptProposal getConceptProposal(Integer conceptProposalId) {
732
                return dao.getConceptProposal(conceptProposalId);
1 ✔
733
        }
734
        
735
        /**
736
         * @see org.openmrs.api.ConceptService#getAllConceptProposals(boolean)
737
         */
738
        @Override
739
        @Transactional(readOnly = true)
740
        public List<ConceptProposal> getAllConceptProposals(boolean includeCompleted) {
741
                return dao.getAllConceptProposals(includeCompleted);
1 ✔
742
        }
743
        
744
        /**
745
         * @see org.openmrs.api.ConceptService#getConceptProposals(java.lang.String)
746
         */
747
        @Override
748
        @Transactional(readOnly = true)
749
        public List<ConceptProposal> getConceptProposals(String cp) {
750
                return dao.getConceptProposals(cp);
1 ✔
751
        }
752
        
753
        /**
754
         * @see org.openmrs.api.ConceptService#getProposedConcepts(java.lang.String)
755
         */
756
        @Override
757
        @Transactional(readOnly = true)
758
        public List<Concept> getProposedConcepts(String text) {
759
                return dao.getProposedConcepts(text);
1 ✔
760
        }
761
        
762
        /**
763
         * @see org.openmrs.api.ConceptService#saveConceptProposal(org.openmrs.ConceptProposal)
764
         */
765
        @Override
766
        public ConceptProposal saveConceptProposal(ConceptProposal conceptProposal) throws APIException {
767
                return dao.saveConceptProposal(conceptProposal);
1 ✔
768
        }
769
        
770
        /**
771
         * @see org.openmrs.api.ConceptService#purgeConceptProposal(org.openmrs.ConceptProposal)
772
         */
773
        @Override
774
        public void purgeConceptProposal(ConceptProposal cp) throws APIException {
775
                dao.purgeConceptProposal(cp);
1 ✔
776
        }
1 ✔
777
        
778
        /**
779
         * @see org.openmrs.api.ConceptService#mapConceptProposalToConcept(ConceptProposal, Concept, Locale)
780
         */
781
        @Override
782
        public Concept mapConceptProposalToConcept(ConceptProposal cp, Concept mappedConcept, Locale locale) throws APIException {
783
                
784
                if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_REJECT)) {
1 ✔
785
                        cp.rejectConceptProposal();
1 ✔
786
                        Context.getConceptService().saveConceptProposal(cp);
1 ✔
787
                        return null;
1 ✔
788
                }
789
                
790
                if (mappedConcept == null) {
1 ✔
791
                        throw new APIException("Concept.mapped.illegal", (Object[]) null);
1 ✔
792
                }
793
                
794
                ConceptName conceptName = null;
1 ✔
795
                if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT) || StringUtils.isBlank(cp.getFinalText())) {
1 ✔
796
                        cp.setState(OpenmrsConstants.CONCEPT_PROPOSAL_CONCEPT);
1 ✔
797
                        cp.setFinalText("");
1 ✔
798
                } else if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
1 ✔
799
                        
800
                        checkIfLocked();
1 ✔
801
                        
802
                        String finalText = cp.getFinalText();
1 ✔
803
                        conceptName = new ConceptName(finalText, null);
1 ✔
804
                        conceptName.setConcept(mappedConcept);
1 ✔
805
                        conceptName.setLocale(locale == null ? Context.getLocale() : locale);
1 ✔
806
                        conceptName.setDateCreated(new Date());
1 ✔
807
                        conceptName.setCreator(Context.getAuthenticatedUser());
1 ✔
808
                        //If this is pre 1.9
809
                        if (conceptName.getUuid() == null) {
1 ✔
810
                                conceptName.setUuid(UuidUtil.newUuidString());
×
811
                        }
812
                        mappedConcept.addName(conceptName);
1 ✔
813
                        mappedConcept.setChangedBy(Context.getAuthenticatedUser());
1 ✔
814
                        mappedConcept.setDateChanged(new Date());
1 ✔
815
                        ValidateUtil.validate(mappedConcept);
1 ✔
816
            Context.getConceptService().saveConcept(mappedConcept);
1 ✔
817
                }
818
                
819
                cp.setMappedConcept(mappedConcept);
1 ✔
820
                
821
                if (cp.getObsConcept() != null) {
1 ✔
822
                        Obs ob = new Obs();
1 ✔
823
                        ob.setEncounter(cp.getEncounter());
1 ✔
824
                        ob.setConcept(cp.getObsConcept());
1 ✔
825
                        ob.setValueCoded(cp.getMappedConcept());
1 ✔
826
                        if (cp.getState().equals(OpenmrsConstants.CONCEPT_PROPOSAL_SYNONYM)) {
1 ✔
827
                                ob.setValueCodedName(conceptName);
1 ✔
828
                        }
829
                        ob.setCreator(Context.getAuthenticatedUser());
1 ✔
830
                        ob.setDateCreated(new Date());
1 ✔
831
                        ob.setObsDatetime(cp.getEncounter().getEncounterDatetime());
1 ✔
832
                        ob.setLocation(cp.getEncounter().getLocation());
1 ✔
833
                        ob.setPerson(cp.getEncounter().getPatient());
1 ✔
834
                        if (ob.getUuid() == null) {
1 ✔
835
                                ob.setUuid(UuidUtil.newUuidString());
×
836
                        }
837
            Context.getObsService().saveObs(ob, null);
1 ✔
838
                        cp.setObs(ob);
1 ✔
839
                }
840
                
841
                return mappedConcept;
1 ✔
842
        }
843
        
844
        /**
845
         * @see org.openmrs.api.ConceptService#mapConceptProposalToConcept(org.openmrs.ConceptProposal,
846
         *      org.openmrs.Concept)
847
         */
848
        @Override
849
        public Concept mapConceptProposalToConcept(ConceptProposal cp, Concept mappedConcept) throws APIException {
850
                return Context.getConceptService().mapConceptProposalToConcept(cp, mappedConcept, null);
1 ✔
851
        }
852
        
853
        /**
854
         * @see org.openmrs.api.ConceptService#getConceptsByAnswer(org.openmrs.Concept)
855
         */
856
        @Override
857
        @Transactional(readOnly = true)
858
        public List<Concept> getConceptsByAnswer(Concept concept) throws APIException {
859
                if (concept.getConceptId() == null) {
1 ✔
860
                        return Collections.emptyList();
1 ✔
861
                }
862
                
863
                return dao.getConceptsByAnswer(concept);
1 ✔
864
        }
865
        
866
        /**
867
         * @see org.openmrs.api.ConceptService#getPrevConcept(org.openmrs.Concept)
868
         */
869
        @Override
870
        @Transactional(readOnly = true)
871
        public Concept getPrevConcept(Concept c) {
872
                return dao.getPrevConcept(c);
1 ✔
873
        }
874
        
875
        /**
876
         * @see org.openmrs.api.ConceptService#getNextConcept(org.openmrs.Concept)
877
         */
878
        @Override
879
        @Transactional(readOnly = true)
880
        public Concept getNextConcept(Concept c) {
881
                return dao.getNextConcept(c);
1 ✔
882
        }
883
        
884
        /**
885
         * @see org.openmrs.api.ConceptService#checkIfLocked()
886
         */
887
        @Override
888
        @Transactional(readOnly = true)
889
        public void checkIfLocked() throws ConceptsLockedException {
890
                String locked = Context.getAdministrationService().getGlobalProperty(
1 ✔
891
                    OpenmrsConstants.GLOBAL_PROPERTY_CONCEPTS_LOCKED, "false");
892
                if ("true".equalsIgnoreCase(locked)) {
1 ✔
893
                        throw new ConceptsLockedException();
×
894
                }
895
        }
1 ✔
896
        
897
        /**
898
         * @see org.openmrs.api.ConceptService#getConceptsWithDrugsInFormulary()
899
         */
900
        @Override
901
        @Transactional(readOnly = true)
902
        public List<Concept> getConceptsWithDrugsInFormulary() {
903
                return dao.getConceptsWithDrugsInFormulary();
1 ✔
904
        }
905
        
906
        /**
907
         * @see ConceptService#getMaxConceptId()
908
         */
909
        @Override
910
        @Transactional(readOnly = true)
911
        public Integer getMaxConceptId() {
912
                return dao.getMaxConceptId();
1 ✔
913
        }
914
        
915
        /**
916
         * Utility method used by getConceptsInSet(Concept concept)
917
         * 
918
         * @param concept
919
         * @param ret
920
         * @param alreadySeen
921
         */
922
        private void explodeConceptSetHelper(Concept concept, Collection<Concept> ret, Collection<Integer> alreadySeen) {
923
                if (alreadySeen.contains(concept.getConceptId())) {
1 ✔
924
                        return;
×
925
                }
926
                alreadySeen.add(concept.getConceptId());
1 ✔
927
                List<ConceptSet> cs = getConceptSetsByConcept(concept);
1 ✔
928
                for (ConceptSet set : cs) {
1 ✔
929
                        Concept c = set.getConcept();
1 ✔
930
                        if (c.getSet()) {
1 ✔
931
                                ret.add(c);
1 ✔
932
                                explodeConceptSetHelper(c, ret, alreadySeen);
1 ✔
933
                        } else {
934
                                ret.add(c);
1 ✔
935
                        }
936
                }
1 ✔
937
        }
1 ✔
938
        
939
        /**
940
         * @see org.openmrs.api.ConceptService#getConceptNameTagByName(java.lang.String)
941
         */
942
        @Override
943
        @Transactional(readOnly = true)
944
        public ConceptNameTag getConceptNameTagByName(String tagName) {
945
                return dao.getConceptNameTagByName(tagName);
1 ✔
946
        }
947
        
948
        /**
949
         * @see org.openmrs.api.ConceptService#getLocalesOfConceptNames()
950
         */
951
        @Override
952
        @Transactional(readOnly = true)
953
        public Set<Locale> getLocalesOfConceptNames() {
954
                return dao.getLocalesOfConceptNames();
1 ✔
955
        }
956
        
957
        /**
958
         * @see org.openmrs.api.ConceptService#getConceptSource(java.lang.Integer)
959
         */
960
        @Override
961
        @Transactional(readOnly = true)
962
        public ConceptSource getConceptSource(Integer conceptSourceId) {
963
                return dao.getConceptSource(conceptSourceId);
1 ✔
964
        }
965
        
966
        /**
967
         * @see org.openmrs.api.ConceptService#getAllConceptSources(boolean)
968
         */
969
        @Override
970
        @Transactional(readOnly = true)
971
        public List<ConceptSource> getAllConceptSources(boolean includeRetired) {
972
                return dao.getAllConceptSources(includeRetired);
1 ✔
973
        }
974
        
975
        /**
976
         * @see org.openmrs.api.ConceptService#purgeConceptSource(org.openmrs.ConceptSource)
977
         */
978
        @Override
979
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
980
        public ConceptSource purgeConceptSource(ConceptSource cs) throws APIException {
981
                return dao.deleteConceptSource(cs);
1 ✔
982
        }
983
        
984
        /**
985
         * @see org.openmrs.api.ConceptService#retireConceptSource(org.openmrs.ConceptSource, String)
986
         */
987
        @Override
988
        public ConceptSource retireConceptSource(ConceptSource cs, String reason) throws APIException {
989
                // retireReason is automatically set in BaseRetireHandler
990
                return Context.getConceptService().saveConceptSource(cs);
1 ✔
991
        }
992
        
993
        /**
994
         * @see org.openmrs.api.ConceptService#saveConceptSource(org.openmrs.ConceptSource)
995
         */
996
        @Override
997
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
998
        public ConceptSource saveConceptSource(ConceptSource conceptSource) throws APIException {
999
                return dao.saveConceptSource(conceptSource);
1 ✔
1000
        }
1001
        
1002
        /**
1003
         * @see org.openmrs.api.ConceptService#saveConceptNameTag(org.openmrs.ConceptNameTag)
1004
         */
1005
        @Override
1006
        public ConceptNameTag saveConceptNameTag(ConceptNameTag nameTag) {
1007
                checkIfLocked();
1 ✔
1008
                
1009
                return dao.saveConceptNameTag(nameTag);
1 ✔
1010
        }
1011
        
1012
        /**
1013
         * @see org.openmrs.api.ConceptService#conceptIterator()
1014
         */
1015
        @Override
1016
        @Transactional(readOnly = true)
1017
        public Iterator<Concept> conceptIterator() {
1018
                return dao.conceptIterator();
1 ✔
1019
        }
1020
        
1021
        /**
1022
         * @see org.openmrs.api.ConceptService#getConceptByUuid(java.lang.String)
1023
         */
1024
        @Override
1025
        @Transactional(readOnly = true)
1026
        public Concept getConceptByUuid(String uuid) {
1027
                return dao.getConceptByUuid(uuid);
1 ✔
1028
        }
1029
        
1030
        /**
1031
         * @see org.openmrs.api.ConceptService#getConcepts(ConceptSearchCriteria)
1032
         */
1033
        @Override
1034
        @Transactional(readOnly = true)
1035
        public List<Concept> getConcepts(ConceptSearchCriteria conceptSearchCriteria) {
1036
                return dao.getConcepts(conceptSearchCriteria);
1 ✔
1037
        }
1038

1039
        /**
1040
         * @see org.openmrs.api.ConceptService#getConceptClassByUuid(java.lang.String)
1041
         */
1042
        @Override
1043
        @Transactional(readOnly = true)
1044
        public ConceptClass getConceptClassByUuid(String uuid) {
1045
                return dao.getConceptClassByUuid(uuid);
1 ✔
1046
        }
1047
        
1048
        @Override
1049
        @Transactional(readOnly = true)
1050
        public ConceptAnswer getConceptAnswerByUuid(String uuid) {
1051
                return dao.getConceptAnswerByUuid(uuid);
1 ✔
1052
        }
1053
        
1054
        @Override
1055
        @Transactional(readOnly = true)
1056
        public ConceptName getConceptNameByUuid(String uuid) {
1057
                return dao.getConceptNameByUuid(uuid);
1 ✔
1058
        }
1059
        
1060
        @Override
1061
        public ConceptSet getConceptSetByUuid(String uuid) {
1062
                return dao.getConceptSetByUuid(uuid);
1 ✔
1063
        }
1064
        
1065
        @Override
1066
        @Transactional(readOnly = true)
1067
        public ConceptSource getConceptSourceByUuid(String uuid) {
1068
                return dao.getConceptSourceByUuid(uuid);
1 ✔
1069
        }
1070
        
1071
        /**
1072
         * @see org.openmrs.api.ConceptService#getConceptDatatypeByUuid(java.lang.String)
1073
         */
1074
        @Override
1075
        @Transactional(readOnly = true)
1076
        public ConceptDatatype getConceptDatatypeByUuid(String uuid) {
1077
                return dao.getConceptDatatypeByUuid(uuid);
1 ✔
1078
        }
1079
        
1080
        /**
1081
         * @see org.openmrs.api.ConceptService#getConceptNumericByUuid(java.lang.String)
1082
         */
1083
        @Override
1084
        @Transactional(readOnly = true)
1085
        public ConceptNumeric getConceptNumericByUuid(String uuid) {
1086
                return dao.getConceptNumericByUuid(uuid);
1 ✔
1087
        }
1088
        
1089
        /**
1090
         * @see org.openmrs.api.ConceptService#getConceptProposalByUuid(java.lang.String)
1091
         */
1092
        @Override
1093
        @Transactional(readOnly = true)
1094
        public ConceptProposal getConceptProposalByUuid(String uuid) {
1095
                return dao.getConceptProposalByUuid(uuid);
1 ✔
1096
        }
1097
        
1098
        /**
1099
         * @see org.openmrs.api.ConceptService#getDrugByUuid(java.lang.String)
1100
         */
1101
        @Override
1102
        @Transactional(readOnly = true)
1103
        public Drug getDrugByUuid(String uuid) {
1104
                return dao.getDrugByUuid(uuid);
1 ✔
1105
        }
1106
        
1107
        /**
1108
         * @see org.openmrs.api.ConceptService#getDrugIngredientByUuid(java.lang.String)
1109
         */
1110
        @Override
1111
        @Transactional(readOnly = true)
1112
        public DrugIngredient getDrugIngredientByUuid(String uuid) {
1113
                return dao.getDrugIngredientByUuid(uuid);
1 ✔
1114
        }
1115
        
1116
        /**
1117
         * @see org.openmrs.api.ConceptService#getConceptDescriptionByUuid(java.lang.String)
1118
         */
1119
        @Override
1120
        @Transactional(readOnly = true)
1121
        public ConceptDescription getConceptDescriptionByUuid(String uuid) {
1122
                return dao.getConceptDescriptionByUuid(uuid);
1 ✔
1123
        }
1124
        
1125
        /**
1126
         * @see org.openmrs.api.ConceptService#getConceptNameTagByUuid(java.lang.String)
1127
         */
1128
        @Override
1129
        @Transactional(readOnly = true)
1130
        public ConceptNameTag getConceptNameTagByUuid(String uuid) {
1131
                return dao.getConceptNameTagByUuid(uuid);
1 ✔
1132
        }
1133
        
1134
        /**
1135
         * @see org.openmrs.api.ConceptService#getAllConceptNameTags()
1136
         */
1137
        @Override
1138
        @Transactional(readOnly = true)
1139
        public List<ConceptNameTag> getAllConceptNameTags() {
1140
                return dao.getAllConceptNameTags();
1 ✔
1141
        }
1142
        
1143
        /**
1144
         * @see org.openmrs.api.ConceptService#getConceptNameTag(java.lang.Integer)
1145
         */
1146
        @Override
1147
        @Transactional(readOnly = true)
1148
        public ConceptNameTag getConceptNameTag(Integer id) {
1149
                return dao.getConceptNameTag(id);
1 ✔
1150
        }
1151
        
1152
        /**
1153
         * @see org.openmrs.api.ConceptService#getConceptByMapping(java.lang.String, java.lang.String)
1154
         */
1155
        @Override
1156
        @Transactional(readOnly = true)
1157
        public Concept getConceptByMapping(String code, String sourceName) throws APIException {
1158
                return Context.getConceptService().getConceptByMapping(code, sourceName, true);
1 ✔
1159
        }
1160
        
1161
        /**
1162
         * @see org.openmrs.api.ConceptService#getConceptByMapping(java.lang.String, java.lang.String,
1163
         *      java.lang.Boolean)
1164
         */
1165
        @Override
1166
        @Transactional(readOnly = true)
1167
        public Concept getConceptByMapping(String code, String sourceName, Boolean includeRetired) throws APIException {
1168
                List<Concept> concepts = Context.getConceptService().getConceptsByMapping(code, sourceName, includeRetired);
1 ✔
1169
                
1170
                if (concepts.isEmpty()) {
1 ✔
1171
                        return null;
1 ✔
1172
                }
1173
                // we want to throw an exception if there is more than one non-retired concept; 
1174
                // since the getConceptByMapping DAO method returns a list with all non-retired concept
1175
                // sorted to the front of the list, we can test if there is more than one retired concept
1176
                // by testing if the second concept in the list is retired or not
1177
                else if (concepts.size() > 1 && !concepts.get(1).getRetired()) {
1 ✔
1178
                        throw new APIException("Concept.error.multiple.non.retired", new Object[] { code, sourceName });
1 ✔
1179
                } else {
1180
                        return concepts.get(0);
1 ✔
1181
                }
1182
        }
1183
        
1184
        /**
1185
         * @see org.openmrs.api.ConceptService#getConceptsByMapping(java.lang.String, java.lang.String)
1186
         */
1187
        @Override
1188
        @Transactional(readOnly = true)
1189
        public List<Concept> getConceptsByMapping(String code, String sourceName) throws APIException {
1190
                return Context.getConceptService().getConceptsByMapping(code, sourceName, true);
1 ✔
1191
        }
1192
        
1193
        /**
1194
         * @see org.openmrs.api.ConceptService#getConceptsByMapping(java.lang.String, java.lang.String, boolean)
1195
         */
1196
        @Override
1197
        @Transactional(readOnly = true)
1198
        public List<Concept> getConceptsByMapping(String code, String sourceName, boolean includeRetired) throws APIException {
1199
                List<Concept> concepts = new ArrayList<>();
1 ✔
1200
                for (Integer conceptId : Context.getConceptService().getConceptIdsByMapping(code, sourceName, includeRetired)) {
1 ✔
1201
                        concepts.add(getConcept(conceptId));
1 ✔
1202
                }
1 ✔
1203
                return concepts;
1 ✔
1204
        }
1205

1206
        /**
1207
         * @see org.openmrs.api.ConceptService#getConceptIdsByMapping(java.lang.String, java.lang.String, boolean)
1208
         */
1209
        @Override
1210
        @Transactional(readOnly = true)
1211
        @Cacheable(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME)
1212
        public List<Integer> getConceptIdsByMapping(String code, String sourceName, boolean includeRetired) throws APIException {
1213
                return dao.getConceptIdsByMapping(code, sourceName, includeRetired);
1 ✔
1214
        }
1215
        
1216
        /**
1217
         * @see org.openmrs.api.ConceptService#getFalseConcept()
1218
         */
1219
        @Override
1220
        @Transactional(readOnly = true)
1221
        public Concept getFalseConcept() {
1222
                if (falseConcept == null) {
1 ✔
1223
                        setBooleanConcepts();
1 ✔
1224
                }
1225
                
1226
                return falseConcept;
1 ✔
1227
        }
1228
        
1229
        /**
1230
         * @see org.openmrs.api.ConceptService#getTrueConcept()
1231
         */
1232
        @Override
1233
        @Transactional(readOnly = true)
1234
        public Concept getTrueConcept() {
1235
                if (trueConcept == null) {
1 ✔
UNCOV
1236
                        setBooleanConcepts();
×
1237
                }
1238
                
1239
                return trueConcept;
1 ✔
1240
        }
1241
        
1242
        /**
1243
         * @see org.openmrs.api.ConceptService#getUnknownConcept()
1244
         */
1245
        @Override
1246
        @Transactional(readOnly = true)
1247
        public Concept getUnknownConcept() {
1248
                if (unknownConcept == null) {
1 ✔
1249
                        try {
1250
                                Concept unknownConcept = Context.getConceptService().getConcept(
1 ✔
1251
                                        Integer.parseInt(Context.getAdministrationService().getGlobalProperty(
1 ✔
1252
                                                OpenmrsConstants.GLOBAL_PROPERTY_UNKNOWN_CONCEPT)));
1253
                                initializeLazyPropertiesForConcept(unknownConcept);
1 ✔
1254
                                
1255
                                ConceptServiceImpl.setStaticUnknownConcept(unknownConcept);
1 ✔
1256
                        }
1257
                        catch (NumberFormatException e) {
×
1258
                                log.warn("Concept id for unknown concept should be a number");
×
1259
                        }
1 ✔
1260
                }
1261
                
1262
                return unknownConcept;
1 ✔
1263
        }
1264
        
1265
        /**
1266
         * Sets unknownConcept using static method
1267
         *
1268
         * @param currentUnknownConcept
1269
         */
1270
        private static void setStaticUnknownConcept(Concept currentUnknownConcept) {
1271
                ConceptServiceImpl.unknownConcept = currentUnknownConcept;
1 ✔
1272
        }
1 ✔
1273
        
1274
        /**
1275
         * Sets the TRUE and FALSE concepts by reading their ids from the global_property table
1276
         */
1277
        private void setBooleanConcepts() {
1278
                
1279
                try {
1280
                        trueConcept = Context.getConceptService().getConcept(
1 ✔
1281
                            Integer.parseInt(Context.getAdministrationService().getGlobalProperty(
1 ✔
1282
                                OpenmrsConstants.GLOBAL_PROPERTY_TRUE_CONCEPT)));
1283
                        initializeLazyPropertiesForConcept(trueConcept);
1 ✔
1284
                        
1285
                        falseConcept = Context.getConceptService().getConcept(
1 ✔
1286
                            Integer.parseInt(Context.getAdministrationService().getGlobalProperty(
1 ✔
1287
                                OpenmrsConstants.GLOBAL_PROPERTY_FALSE_CONCEPT)));
1288
                        initializeLazyPropertiesForConcept(falseConcept);
1 ✔
1289
                }
1290
                catch (NumberFormatException e) {
×
1291
                        log.warn("Concept ids for boolean concepts should be numbers");
×
1292
                }
1 ✔
1293
        }
1 ✔
1294

1295
        private void initializeLazyPropertiesForConcept(Concept concept) {
1296
                Hibernate.initialize(concept.getRetiredBy());
1 ✔
1297
                Hibernate.initialize(concept.getCreator());
1 ✔
1298
                Hibernate.initialize(concept.getChangedBy());
1 ✔
1299
                Hibernate.initialize(concept.getNames());
1 ✔
1300
                Hibernate.initialize(concept.getAnswers());
1 ✔
1301
                Hibernate.initialize(concept.getConceptSets());
1 ✔
1302
                Hibernate.initialize(concept.getDescriptions());
1 ✔
1303
                Hibernate.initialize(concept.getConceptMappings());
1 ✔
1304
                Hibernate.initialize(concept.getAttributes());
1 ✔
1305
        }
1 ✔
1306

1307
        /**
1308
         * @see org.openmrs.api.ConceptService#getConceptSourceByName(java.lang.String)
1309
         */
1310
        @Override
1311
        @Transactional(readOnly = true)
1312
        public ConceptSource getConceptSourceByName(String conceptSourceName) throws APIException {
1313
                return dao.getConceptSourceByName(conceptSourceName);
1 ✔
1314
        }
1315

1316
        /**
1317
         * @see org.openmrs.api.ConceptService#getConceptSourceByUniqueId(java.lang.String)
1318
         */
1319
        @Override
1320
        @Transactional(readOnly = true)
1321
        public ConceptSource getConceptSourceByUniqueId(String uniqueId) throws APIException {
1322
                if (uniqueId == null) {
1 ✔
1323
                        throw new IllegalArgumentException("uniqueId is required");
1 ✔
1324
                }
1325
                return dao.getConceptSourceByUniqueId(uniqueId);
1 ✔
1326
        }
1327

1328
        /**
1329
         * @see org.openmrs.api.ConceptService#getConceptSourceByHL7Code(java.lang.String)
1330
         */
1331
        @Override
1332
        @Transactional(readOnly = true)
1333
        public ConceptSource getConceptSourceByHL7Code(String hl7Code) throws APIException {
1334
                if (hl7Code == null) {
1 ✔
1335
                        throw new IllegalArgumentException("hl7Code is required");
1 ✔
1336
                }
1337
                return dao.getConceptSourceByHL7Code(hl7Code);
1 ✔
1338
        }
1339

1340
        /**
1341
         * Utility method to check if the concept is already attached to an observation (including
1342
         * voided ones) and if the datatype of the concept has changed, an exception indicating that the
1343
         * datatype cannot be modified will be reported if the concept is attached to an observation.
1344
         * This method will only allow changing boolean concepts to coded.
1345
         * 
1346
         * @param concept
1347
         * @throws ConceptInUseException
1348
         */
1349
        private void checkIfDatatypeCanBeChanged(Concept concept) {
1350
                if (concept.getId() != null && hasAnyObservation(concept) && hasDatatypeChanged(concept)) {
1 ✔
1351
                        // allow boolean concepts to be converted to coded
1352
                        if (!(dao.getSavedConceptDatatype(concept).isBoolean() && concept.getDatatype().isCoded())) {
1 ✔
1353
                                throw new ConceptInUseException();
1 ✔
1354
                        }
1355
                        log.debug("Converting datatype of concept with id {} from Boolean to coded", concept.getConceptId());
1 ✔
1356
                }
1357
        }
1 ✔
1358
        
1359
        /**
1360
         * Utility method which loads the previous version of a concept to check if the datatype has
1361
         * changed.
1362
         * 
1363
         * @param concept to be modified
1364
         * @return boolean indicating change in the datatype
1365
         */
1366
        private boolean hasDatatypeChanged(Concept concept) {
1367
                ConceptDatatype oldConceptDatatype = dao.getSavedConceptDatatype(concept);
1 ✔
1368
                return !oldConceptDatatype.equals(concept.getDatatype());
1 ✔
1369
        }
1370
        
1371
        /**
1372
         * @see org.openmrs.api.ConceptService#hasAnyObservation(org.openmrs.Concept)
1373
         */
1374
        @Override
1375
        @Transactional(readOnly = true)
1376
        public boolean hasAnyObservation(Concept concept) {
1377
                List<Concept> concepts = new ArrayList<>();
1 ✔
1378
                concepts.add(concept);
1 ✔
1379
                Integer count = Context.getObsService().getObservationCount(null, null, concepts, null, null, null, null, null,
1 ✔
1380
                    null, true);
1381
                return count > 0;
1 ✔
1382
        }
1383
        
1384
        /**
1385
         * @see org.openmrs.api.ConceptService#convertBooleanConceptToCoded(org.openmrs.Concept)
1386
         */
1387
        @Override
1388
        public void convertBooleanConceptToCoded(Concept conceptToChange) throws APIException {
1389
                if (conceptToChange != null) {
1 ✔
1390
                        if (!conceptToChange.getDatatype().isBoolean()) {
1 ✔
1391
                                throw new APIException("Concept.datatype.invalid", (Object[]) null);
1 ✔
1392
                        }
1393
                        
1394
                        conceptToChange.setDatatype(getConceptDatatypeByName("Coded"));
1 ✔
1395
                        conceptToChange.addAnswer(new ConceptAnswer(getTrueConcept()));
1 ✔
1396
                        conceptToChange.addAnswer(new ConceptAnswer(getFalseConcept()));
1 ✔
1397
                        Context.getConceptService().saveConcept(conceptToChange);
1 ✔
1398
                }
1399
        }
1 ✔
1400
        
1401
        /**
1402
         * @see org.openmrs.api.ConceptService#hasAnyObservation(org.openmrs.ConceptName)
1403
         */
1404
        @Override
1405
        @Transactional(readOnly = true)
1406
        public boolean hasAnyObservation(ConceptName conceptName) throws APIException {
1407
                List<ConceptName> conceptNames = new ArrayList<>();
1 ✔
1408
                conceptNames.add(conceptName);
1 ✔
1409
                Integer count = Context.getObsService().getObservationCount(conceptNames, true);
1 ✔
1410
                return count > 0;
1 ✔
1411
        }
1412
        
1413
        /**
1414
         * Utility method which loads the previous version of a conceptName to check if the name
1415
         * property of the given conceptName has changed.
1416
         * 
1417
         * @param conceptName to be modified
1418
         * @return boolean indicating change in the name property
1419
         */
1420
        private boolean hasNameChanged(ConceptName conceptName) {
1421
                String newName = conceptName.getName();
1 ✔
1422
                String oldName = dao.getSavedConceptName(conceptName).getName();
1 ✔
1423
                return !oldName.equalsIgnoreCase(newName);
1 ✔
1424
        }
1425
        
1426
        /**
1427
         * Creates a copy of a conceptName
1428
         * 
1429
         * @param conceptName the conceptName to be cloned
1430
         * @return the cloned conceptName
1431
         */
1432
        private ConceptName cloneConceptName(ConceptName conceptName) {
1433
                ConceptName copy = new ConceptName();
1 ✔
1434
                try {
1435
                        copy = (ConceptName) BeanUtils.cloneBean(conceptName);
1 ✔
1436
                }
1437
                catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException e) {
×
1438
                        
1439
                        log.warn(ERROR_MESSAGE, e);
×
1440
                }
1 ✔
1441
                return copy;
1 ✔
1442
        }
1443
        
1444
        /**
1445
         * @see ConceptService#findConceptAnswers(String, Locale, Concept)
1446
         */
1447
        @Override
1448
        @Transactional(readOnly = true)
1449
        public List<ConceptSearchResult> findConceptAnswers(String phrase, Locale locale, Concept concept) throws APIException {
1450

1451
                return getConcepts(phrase, Collections.singletonList(locale), false, null, null, null, null,
1 ✔
1452
                    concept, null, null);
1453
        }
1454
        
1455
        /**
1456
         * @see org.openmrs.api.ConceptService#getConceptStopWords(java.util.Locale)
1457
         */
1458
        @Override
1459
        @Transactional(readOnly = true)
1460
        public List<String> getConceptStopWords(Locale locale) {
1461
                return dao.getConceptStopWords(locale);
1 ✔
1462
        }
1463
        
1464
        /**
1465
         * @see org.openmrs.api.ConceptService#saveConceptStopWord(org.openmrs.ConceptStopWord)
1466
         */
1467
        @Override
1468
        public ConceptStopWord saveConceptStopWord(ConceptStopWord conceptStopWord) throws APIException {
1469
                try {
1470
                        return dao.saveConceptStopWord(conceptStopWord);
1 ✔
1471
                }
1472
                catch (DAOException e) {
1 ✔
1473
                        if ("Duplicate ConceptStopWord Entry".equalsIgnoreCase(e.getMessage())) {
1 ✔
1474
                                throw new ConceptStopWordException("ConceptStopWord.duplicated", e);
1 ✔
1475
                        }
1476
                        throw new ConceptStopWordException("ConceptStopWord.notSaved", e);
×
1477
                }
1478
        }
1479
        
1480
        /**
1481
         * @see org.openmrs.api.ConceptService#deleteConceptStopWord(Integer)
1482
         */
1483
        @Override
1484
        public void deleteConceptStopWord(Integer conceptStopWordId) throws APIException {
1485
                try {
1486
                        dao.deleteConceptStopWord(conceptStopWordId);
1 ✔
1487
                }
1488
                catch (DAOException e) {
×
1489
                        if (StringUtils.contains(e.getMessage(), "Concept Stop Word not found or already deleted")) {
×
1490
                                throw new ConceptStopWordException("ConceptStopWord.error.notfound", e);
×
1491
                        }
1492
                        throw new ConceptStopWordException("general.cannot.delete", e);
×
1493
                }
1 ✔
1494
        }
1 ✔
1495
        
1496
        /**
1497
         * @see org.openmrs.api.ConceptService#getAllConceptStopWords()
1498
         */
1499
        @Override
1500
        @Transactional(readOnly = true)
1501
        public List<ConceptStopWord> getAllConceptStopWords() {
1502
                return dao.getAllConceptStopWords();
1 ✔
1503
        }
1504
        
1505
        /**
1506
         * @see ConceptService#getConcepts(String, List, boolean, List, List, List, List, Concept,
1507
         *      Integer, Integer)
1508
         */
1509
        @Override
1510
        @Transactional(readOnly = true)
1511
        public List<ConceptSearchResult> getConcepts(String phrase, List<Locale> locales, boolean includeRetired,
1512
                List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses, List<ConceptDatatype> requireDatatypes,
1513
                List<ConceptDatatype> excludeDatatypes, Concept answersToConcept, Integer start, Integer size)
1514
                throws APIException {
1515

1516
                List<ConceptClass> tmpRequireClasses = requireClasses == null ? new ArrayList<>() : requireClasses;
1 ✔
1517
                List<ConceptClass> tmpExcludeClasses = excludeClasses == null ? new ArrayList<>() : excludeClasses;
1 ✔
1518
                List<ConceptDatatype> tmpRequireDatatypes = requireDatatypes == null ? new ArrayList<>() : requireDatatypes;
1 ✔
1519
                List<ConceptDatatype> tmpExcludeDatatypes = excludeDatatypes == null ? new ArrayList<>() : excludeDatatypes;
1 ✔
1520
                
1521
                return dao.getConcepts(phrase, locales, includeRetired, tmpRequireClasses, tmpExcludeClasses, tmpRequireDatatypes,
1 ✔
1522
                    tmpExcludeDatatypes, answersToConcept, start, size);
1523
                
1524
        }
1525
        
1526
        /**
1527
         * @see ConceptService#updateConceptIndex(Concept)
1528
         */
1529
        @Override
1530
        public void updateConceptIndex(Concept concept) throws APIException {
1531
                Context.updateSearchIndexForObject(concept);
×
1532
        }
×
1533
        
1534
        /**
1535
         * @see ConceptService#updateConceptIndexes()
1536
         */
1537
        @Override
1538
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
1539
        public void updateConceptIndexes() throws APIException {
1540
                Context.updateSearchIndexForType(ConceptName.class);
1 ✔
1541
        }
1 ✔
1542
        
1543
        /**
1544
         * @see ConceptService#getCountOfConcepts(String, List, boolean, List, List, List, List,
1545
         *      Concept)
1546
         */
1547
        @Override
1548
        @Transactional(readOnly = true)
1549
        public Integer getCountOfConcepts(String phrase, List<Locale> locales, boolean includeRetired,
1550
                List<ConceptClass> requireClasses, List<ConceptClass> excludeClasses, List<ConceptDatatype> requireDatatypes,
1551
                List<ConceptDatatype> excludeDatatypes, Concept answersToConcept) {
1552

1553
                List<ConceptClass> tmpRequireClasses = requireClasses == null ? new ArrayList<>() : requireClasses;
1 ✔
1554
                List<ConceptClass> tmpExcludeClasses = excludeClasses == null ? new ArrayList<>() : excludeClasses;
1 ✔
1555
                List<ConceptDatatype> tmpRequireDatatypes = requireDatatypes == null ? new ArrayList<>() : requireDatatypes;
1 ✔
1556
                List<ConceptDatatype> tmpExcludeDatatypes = excludeDatatypes == null ? new ArrayList<>() : excludeDatatypes;
1 ✔
1557
                
1558
                return dao.getCountOfConcepts(phrase, locales, includeRetired, tmpRequireClasses, tmpExcludeClasses, tmpRequireDatatypes,
1 ✔
1559
                    tmpExcludeDatatypes, answersToConcept);
1560
        }
1561
        
1562
        /**
1563
         * @see ConceptService#getCountOfDrugs(String, Concept, boolean, boolean, boolean)
1564
         */
1565
        @Override
1566
        @Transactional(readOnly = true)
1567
        public Integer getCountOfDrugs(String drugName, Concept concept, boolean searchOnPhrase, boolean searchDrugConceptNames,
1568
                boolean includeRetired) throws APIException {
1569
                return OpenmrsUtil.convertToInteger(dao.getCountOfDrugs(drugName, concept, searchOnPhrase, searchDrugConceptNames,
1 ✔
1570
                    includeRetired));
1571
        }
1572
        
1573
        /**
1574
         * @see ConceptService#getDrugs(String, Concept, boolean, boolean, boolean, Integer, Integer)
1575
         */
1576
        @Override
1577
        @Transactional(readOnly = true)
1578
        public List<Drug> getDrugs(String drugName, Concept concept, boolean searchOnPhrase, boolean searchDrugConceptNames,
1579
                boolean includeRetired, Integer start, Integer length) throws APIException {
1580
                return dao.getDrugs(drugName, concept, searchOnPhrase, searchDrugConceptNames, includeRetired, start, length);
1 ✔
1581
        }
1582
        
1583
        /**
1584
         * @see ConceptService#getConcepts(String, Locale, boolean)
1585
         */
1586
        @Override
1587
        @Transactional(readOnly = true)
1588
        public List<ConceptSearchResult> getConcepts(String phrase, Locale locale, boolean includeRetired) throws APIException {
1589
                List<Locale> locales = new ArrayList<>();
1 ✔
1590
                if (locale != null) {
1 ✔
1591
                        locales.add(locale);
1 ✔
1592
                }
1593
                
1594
                return Context.getConceptService().getConcepts(phrase, locales, includeRetired, null, null, null, null, null, null,
1 ✔
1595
                    null);
1596
        }
1597
        
1598
        /**
1599
         * @see org.openmrs.api.ConceptService#getDrugsByIngredient(org.openmrs.Concept)
1600
         */
1601
        @Override
1602
        @Transactional(readOnly = true)
1603
        public List<Drug> getDrugsByIngredient(Concept ingredient) throws APIException {
1604
                if (ingredient == null) {
1 ✔
1605
                        throw new IllegalArgumentException("ingredient is required");
1 ✔
1606
                }
1607
                
1608
                return dao.getDrugsByIngredient(ingredient);
1 ✔
1609
        }
1610
        
1611
        /**
1612
         * @see ConceptService#getConceptMappingsToSource(ConceptSource)
1613
         */
1614
        @Override
1615
        @Transactional(readOnly = true)
1616
        public List<ConceptMap> getConceptMappingsToSource(ConceptSource conceptSource) throws APIException {
1617
                return dao.getConceptMapsBySource(conceptSource);
1 ✔
1618
        }
1619
        
1620
        /**
1621
         * @see ConceptService#getActiveConceptMapTypes()
1622
         */
1623
        @Override
1624
        @Transactional(readOnly = true)
1625
        public List<ConceptMapType> getActiveConceptMapTypes() throws APIException {
1626
                return Context.getConceptService().getConceptMapTypes(true, false);
1 ✔
1627
        }
1628
        
1629
        /**
1630
         * @see ConceptService#getConceptMapTypes(boolean, boolean)
1631
         */
1632
        @Override
1633
        @Transactional(readOnly = true)
1634
        public List<ConceptMapType> getConceptMapTypes(boolean includeRetired, boolean includeHidden) throws APIException {
1635
                return dao.getConceptMapTypes(includeRetired, includeHidden);
1 ✔
1636
        }
1637
        
1638
        /**
1639
         * @see ConceptService#getConceptMapType(Integer)
1640
         */
1641
        @Override
1642
        @Transactional(readOnly = true)
1643
        public ConceptMapType getConceptMapType(Integer conceptMapTypeId) throws APIException {
1644
                return dao.getConceptMapType(conceptMapTypeId);
1 ✔
1645
        }
1646
        
1647
        /**
1648
         * @see ConceptService#getConceptMapTypeByUuid(String)
1649
         */
1650
        @Override
1651
        @Transactional(readOnly = true)
1652
        public ConceptMapType getConceptMapTypeByUuid(String uuid) throws APIException {
1653
                return dao.getConceptMapTypeByUuid(uuid);
1 ✔
1654
        }
1655
        
1656
        /**
1657
         * @see org.openmrs.api.ConceptService#getConceptMapTypeByName(java.lang.String)
1658
         */
1659
        @Override
1660
        @Transactional(readOnly = true)
1661
        public ConceptMapType getConceptMapTypeByName(String name) throws APIException {
1662
                return dao.getConceptMapTypeByName(name);
1 ✔
1663
        }
1664
        
1665
        /**
1666
         * @see org.openmrs.api.ConceptService#saveConceptMapType(org.openmrs.ConceptMapType)
1667
         */
1668
        @Override
1669
        public ConceptMapType saveConceptMapType(ConceptMapType conceptMapType) throws APIException {
1670
                return dao.saveConceptMapType(conceptMapType);
1 ✔
1671
        }
1672
        
1673
        /**
1674
         * @see org.openmrs.api.ConceptService#retireConceptMapType(org.openmrs.ConceptMapType,
1675
         *      java.lang.String)
1676
         */
1677
        @Override
1678
        public ConceptMapType retireConceptMapType(ConceptMapType conceptMapType, String retireReason) throws APIException {
1679
                String tmpRetireReason = retireReason;
1 ✔
1680
                if (StringUtils.isBlank(tmpRetireReason)) {
1 ✔
1681
                        tmpRetireReason = Context.getMessageSourceService().getMessage("general.default.retireReason");
1 ✔
1682
                }
1683
                conceptMapType.setRetireReason(tmpRetireReason);
1 ✔
1684
                return dao.saveConceptMapType(conceptMapType);
1 ✔
1685
        }
1686
        
1687
        /**
1688
         * @see org.openmrs.api.ConceptService#unretireConceptMapType(org.openmrs.ConceptMapType)
1689
         */
1690
        @Override
1691
        public ConceptMapType unretireConceptMapType(ConceptMapType conceptMapType) throws APIException {
1692
                return Context.getConceptService().saveConceptMapType(conceptMapType);
1 ✔
1693
        }
1694
        
1695
        /**
1696
         * @see org.openmrs.api.ConceptService#purgeConceptMapType(org.openmrs.ConceptMapType)
1697
         */
1698
        @Override
1699
        public void purgeConceptMapType(ConceptMapType conceptMapType) throws APIException {
1700
                if (dao.isConceptMapTypeInUse(conceptMapType)) {
1 ✔
1701
                        throw new APIException("ConceptMapType.inUse", (Object[]) null);
×
1702
                }
1703
                dao.deleteConceptMapType(conceptMapType);
1 ✔
1704
        }
1 ✔
1705
        
1706
        /**
1707
         * @see org.openmrs.api.ConceptService#getAllConceptReferenceTerms()
1708
         */
1709
        @Override
1710
        @Transactional(readOnly = true)
1711
        public List<ConceptReferenceTerm> getAllConceptReferenceTerms() throws APIException {
1712
                return Context.getConceptService().getConceptReferenceTerms(true);
1 ✔
1713
        }
1714
        
1715
        /**
1716
         * @see ConceptService#getConceptReferenceTerms(boolean)
1717
         */
1718
        @Override
1719
        @Transactional(readOnly = true)
1720
        public List<ConceptReferenceTerm> getConceptReferenceTerms(boolean includeRetired) throws APIException {
1721
                return dao.getConceptReferenceTerms(includeRetired);
1 ✔
1722
        }
1723
        
1724
        /**
1725
         * @see org.openmrs.api.ConceptService#getConceptReferenceTerm(java.lang.Integer)
1726
         */
1727
        @Override
1728
        @Transactional(readOnly = true)
1729
        public ConceptReferenceTerm getConceptReferenceTerm(Integer conceptReferenceTermId) throws APIException {
1730
                return dao.getConceptReferenceTerm(conceptReferenceTermId);
1 ✔
1731
        }
1732
        
1733
        /**
1734
         * @see org.openmrs.api.ConceptService#getConceptReferenceTermByUuid(java.lang.String)
1735
         */
1736
        @Override
1737
        @Transactional(readOnly = true)
1738
        public ConceptReferenceTerm getConceptReferenceTermByUuid(String uuid) throws APIException {
1739
                return dao.getConceptReferenceTermByUuid(uuid);
1 ✔
1740
        }
1741
        
1742
        /**
1743
         * @see org.openmrs.api.ConceptService#getConceptReferenceTermByName(java.lang.String,
1744
         *      org.openmrs.ConceptSource)
1745
         */
1746
        @Override
1747
        @Transactional(readOnly = true)
1748
        public ConceptReferenceTerm getConceptReferenceTermByName(String name, ConceptSource conceptSource) throws APIException {
1749
                //On addition of extra attributes to concept maps, terms that were generated from existing maps have 
1750
                //empty string values for the name property, ignore the search when name is an empty string but allow 
1751
                //white space characters
1752
                if (StringUtils.isBlank(name)) {
1 ✔
1753
                        return null;
1 ✔
1754
                }
1755
                return dao.getConceptReferenceTermByName(name, conceptSource);
1 ✔
1756
        }
1757
        
1758
        /**
1759
         * @see org.openmrs.api.ConceptService#getConceptReferenceTermByCode(java.lang.String,
1760
         *      org.openmrs.ConceptSource)
1761
         */
1762
        @Override
1763
        @Transactional(readOnly = true)
1764
        public ConceptReferenceTerm getConceptReferenceTermByCode(String code, ConceptSource conceptSource) throws APIException {
1765
                return dao.getConceptReferenceTermByCode(code, conceptSource);
1 ✔
1766
        }
1767

1768
        @Override
1769
        @Transactional(readOnly = true)
1770
        public List<ConceptReferenceTerm> getConceptReferenceTermByCode(String code, ConceptSource conceptSource, boolean includeRetired) throws APIException {
1771
                return dao.getConceptReferenceTermByCode(code, conceptSource, includeRetired);
1 ✔
1772
        }
1773

1774
        /**
1775
         * @see org.openmrs.api.ConceptService#saveConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)
1776
         */
1777
        @Override
1778
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
1779
        public ConceptReferenceTerm saveConceptReferenceTerm(ConceptReferenceTerm conceptReferenceTerm) throws APIException {
1780
                return dao.saveConceptReferenceTerm(conceptReferenceTerm);
1 ✔
1781
        }
1782
        
1783
        /**
1784
         * @see org.openmrs.api.ConceptService#retireConceptReferenceTerm(ConceptReferenceTerm, String)
1785
         */
1786
        @Override
1787
        public ConceptReferenceTerm retireConceptReferenceTerm(ConceptReferenceTerm conceptReferenceTerm, String retireReason)
1788
                throws APIException {
1789
                String tmpRetireReason = retireReason;
1 ✔
1790
                if (StringUtils.isBlank(tmpRetireReason)) {
1 ✔
1791
                        tmpRetireReason = Context.getMessageSourceService().getMessage("general.default.retireReason");
1 ✔
1792
                }
1793
                conceptReferenceTerm.setRetireReason(tmpRetireReason);
1 ✔
1794
                return Context.getConceptService().saveConceptReferenceTerm(conceptReferenceTerm);
1 ✔
1795
        }
1796
        
1797
        /**
1798
         * @see org.openmrs.api.ConceptService#unretireConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)
1799
         */
1800
        @Override
1801
        public ConceptReferenceTerm unretireConceptReferenceTerm(ConceptReferenceTerm conceptReferenceTerm) throws APIException {
1802
                return Context.getConceptService().saveConceptReferenceTerm(conceptReferenceTerm);
1 ✔
1803
        }
1804
        
1805
        /**
1806
         * @see org.openmrs.api.ConceptService#purgeConceptReferenceTerm(org.openmrs.ConceptReferenceTerm)
1807
         */
1808
        @Override
1809
        @CacheEvict(value = CONCEPT_IDS_BY_MAPPING_CACHE_NAME, allEntries = true)
1810
        public void purgeConceptReferenceTerm(ConceptReferenceTerm conceptReferenceTerm) throws APIException {
1811
                if (dao.isConceptReferenceTermInUse(conceptReferenceTerm)) {
1 ✔
1812
                        throw new APIException("ConceptRefereceTerm.inUse", (Object[]) null);
1 ✔
1813
                }
1814
                dao.deleteConceptReferenceTerm(conceptReferenceTerm);
1 ✔
1815
        }
1 ✔
1816
        
1817
        /**
1818
         * @see org.openmrs.api.ConceptService#getConceptReferenceTerms(java.lang.String,
1819
         *      org.openmrs.ConceptSource, java.lang.Integer, java.lang.Integer, boolean)
1820
         */
1821
        @Override
1822
        @Transactional(readOnly = true)
1823
        public List<ConceptReferenceTerm> getConceptReferenceTerms(String query, ConceptSource conceptSource, Integer start,
1824
                Integer length, boolean includeRetired) throws APIException {
1825
                Integer tmpLength = length;
1 ✔
1826
                if (tmpLength == null) {
1 ✔
1827
                        tmpLength = 10000;
1 ✔
1828
                }
1829
                return dao.getConceptReferenceTerms(query, conceptSource, start, tmpLength, includeRetired);
1 ✔
1830
        }
1831
        
1832
        /**
1833
         * @see org.openmrs.api.ConceptService#getCountOfConceptReferenceTerms(String, ConceptSource,
1834
         *      boolean)
1835
         */
1836
        @Override
1837
        @Transactional(readOnly = true)
1838
        public Integer getCountOfConceptReferenceTerms(String query, ConceptSource conceptSource, boolean includeRetired) {
1839
                return OpenmrsUtil.convertToInteger(dao.getCountOfConceptReferenceTerms(query, conceptSource, includeRetired));
1 ✔
1840
        }
1841
        
1842
        /**
1843
         * @see org.openmrs.api.ConceptService#getReferenceTermMappingsTo(ConceptReferenceTerm)
1844
         */
1845
        @Override
1846
        @Transactional(readOnly = true)
1847
        public List<ConceptReferenceTermMap> getReferenceTermMappingsTo(ConceptReferenceTerm term) throws APIException {
1848
                return dao.getReferenceTermMappingsTo(term);
1 ✔
1849
        }
1850
        
1851
        /**
1852
         * @see org.openmrs.api.ConceptService#getConceptsByName(java.lang.String, java.util.Locale,
1853
         *      java.lang.Boolean)
1854
         */
1855
        @Override
1856
        @Transactional(readOnly = true)
1857
        public List<Concept> getConceptsByName(String name, Locale locale, Boolean exactLocale) throws APIException {
1858
                return dao.getConceptsByName(name, locale, exactLocale);
1 ✔
1859
        }
1860
        
1861
        /**
1862
         * @see org.openmrs.api.ConceptService#getDefaultConceptMapType()
1863
         */
1864
        @Override
1865
        @Transactional(readOnly = true)
1866
        public ConceptMapType getDefaultConceptMapType() throws APIException {
1867
                //We need to fetch it in DAO since it must be done in the MANUAL fush mode to prevent pre-mature flushes.
1868
                return dao.getDefaultConceptMapType();
1 ✔
1869
        }
1870
        
1871
        /**
1872
         * @see org.openmrs.api.ConceptService#isConceptNameDuplicate(org.openmrs.ConceptName)
1873
         */
1874
        @Override
1875
        public boolean isConceptNameDuplicate(ConceptName name) {
1876
                return dao.isConceptNameDuplicate(name);
1 ✔
1877
        }
1878
        
1879
        /**
1880
         * @see ConceptService#getDrugs(String, java.util.Locale, boolean, boolean)
1881
         */
1882
        @Override
1883
        @Transactional(readOnly = true)
1884
        public List<Drug> getDrugs(String searchPhrase, Locale locale, boolean exactLocale, boolean includeRetired)
1885
                throws APIException {
1886
                if (searchPhrase == null) {
1 ✔
1887
                        throw new IllegalArgumentException("searchPhrase is required");
1 ✔
1888
                }
1889
                return dao.getDrugs(searchPhrase, locale, exactLocale, includeRetired);
1 ✔
1890
        }
1891
        
1892
        /**
1893
         * @see org.openmrs.api.ConceptService#getDrugsByMapping(String, ConceptSource, Collection,
1894
         *      boolean)
1895
         */
1896
        @Override
1897
        @Transactional(readOnly = true)
1898
        public List<Drug> getDrugsByMapping(String code, ConceptSource conceptSource,
1899
                Collection<ConceptMapType> withAnyOfTheseTypes, boolean includeRetired) throws APIException {
1900
                Collection<ConceptMapType> tmpWithAnyOfTheseTypes = withAnyOfTheseTypes == null ? Collections.emptyList() : withAnyOfTheseTypes;
1 ✔
1901

1902
                if (conceptSource == null) {
1 ✔
1903
                        throw new APIException("ConceptSource.is.required", (Object[]) null);
1 ✔
1904
                }
1905

1906
                return dao.getDrugsByMapping(code, conceptSource, tmpWithAnyOfTheseTypes, includeRetired);
1 ✔
1907
        }
1908
        
1909
        /**
1910
         * @see org.openmrs.api.ConceptService#getDrugByMapping(String, org.openmrs.ConceptSource, java.util.Collection)
1911
         */
1912
        @Override
1913
        @Transactional(readOnly = true)
1914
        public Drug getDrugByMapping(String code, ConceptSource conceptSource,
1915
                Collection<ConceptMapType> withAnyOfTheseTypesOrOrderOfPreference) throws APIException {
1916
                Collection<ConceptMapType> tmpWithAnyOfTheseTypesOrOrderOfPreference = withAnyOfTheseTypesOrOrderOfPreference == null
1 ✔
1917
                                ? Collections.emptyList() : withAnyOfTheseTypesOrOrderOfPreference;
1 ✔
1918

1919
                if (conceptSource == null) {
1 ✔
1920
                        throw new APIException("ConceptSource.is.required", (Object[]) null);
1 ✔
1921
                }
1922

1923
                return dao.getDrugByMapping(code, conceptSource, tmpWithAnyOfTheseTypesOrOrderOfPreference);
1 ✔
1924
        }
1925
        
1926
        /**
1927
         * @see org.openmrs.api.ConceptService#getOrderableConcepts(String, java.util.List, boolean,
1928
         *      Integer, Integer)
1929
         */
1930
        @Override
1931
        @Transactional(readOnly = true)
1932
        public List<ConceptSearchResult> getOrderableConcepts(String phrase, List<Locale> locales, boolean includeRetired,
1933
                Integer start, Integer length) {
1934
                List<ConceptClass> mappedClasses = getConceptClassesOfOrderTypes();
1 ✔
1935
                if (mappedClasses.isEmpty()) {
1 ✔
1936
                        return Collections.emptyList();
×
1937
                }
1938
                List<Locale> tmpLocales = locales;
1 ✔
1939
                if (tmpLocales == null) {
1 ✔
1940
                        tmpLocales = new ArrayList<>();
1 ✔
1941
                        tmpLocales.add(Context.getLocale());
1 ✔
1942
                }
1943
                return dao.getConcepts(phrase, tmpLocales, false, mappedClasses, Collections.emptyList(), Collections.emptyList(),
1 ✔
1944
                    Collections.emptyList(), null, start, length);
1 ✔
1945
        }
1946

1947
        /**
1948
         * @see ConceptService#getAllConceptAttributeTypes()
1949
         */
1950
        @Override
1951
        @Transactional(readOnly = true)
1952
        public List<ConceptAttributeType> getAllConceptAttributeTypes() {
1953
                return dao.getAllConceptAttributeTypes();
1 ✔
1954
        }
1955

1956
        /**
1957
         * @see org.openmrs.api.ConceptService#saveConceptAttributeType(ConceptAttributeType)
1958
         */
1959
        @Override
1960
        public ConceptAttributeType saveConceptAttributeType(ConceptAttributeType conceptAttributeType) {
1961
                return dao.saveConceptAttributeType(conceptAttributeType);
1 ✔
1962
        }
1963

1964
        /**
1965
         * @see org.openmrs.api.ConceptService#getConceptAttributeType(Integer)
1966
         */
1967
        @Override
1968
        @Transactional(readOnly = true)
1969
        public ConceptAttributeType getConceptAttributeType(Integer id) {
1970
                return dao.getConceptAttributeType(id);
1 ✔
1971
        }
1972

1973
        /**
1974
         * @see org.openmrs.api.ConceptService#getConceptAttributeTypeByUuid(String)
1975
         */
1976
        @Override
1977
        @Transactional(readOnly = true)
1978
        public ConceptAttributeType getConceptAttributeTypeByUuid(String uuid) {
1979
                return dao.getConceptAttributeTypeByUuid(uuid);
1 ✔
1980
        }
1981

1982
        /**
1983
         * @see org.openmrs.api.ConceptService#purgeConceptAttributeType(ConceptAttributeType)
1984
         */
1985
        @Override
1986
        public void purgeConceptAttributeType(ConceptAttributeType conceptAttributeType) {
1987
                dao.deleteConceptAttributeType(conceptAttributeType);
1 ✔
1988

1989
        }
1 ✔
1990

1991
        /**
1992
         * @see org.openmrs.api.ConceptService#getConceptAttributeTypes(String)
1993
         */
1994
        @Override
1995
        @Transactional(readOnly = true)
1996
        public List<ConceptAttributeType> getConceptAttributeTypes(String name) throws APIException {
1997
                return dao.getConceptAttributeTypes(name);
1 ✔
1998
        }
1999

2000
        /**
2001
         * @see org.openmrs.api.ConceptService#getConceptAttributeTypeByName(String)
2002
         */
2003
        @Override
2004
        @Transactional(readOnly = true)
2005
        public ConceptAttributeType getConceptAttributeTypeByName(String exactName) {
2006
                return dao.getConceptAttributeTypeByName(exactName);
1 ✔
2007
        }
2008

2009
        /**
2010
         * @see org.openmrs.api.ConceptService#retireConceptAttributeType(ConceptAttributeType, String)
2011
         */
2012
        @Override
2013
        public ConceptAttributeType retireConceptAttributeType(ConceptAttributeType conceptAttributeType, String reason) {
2014
                return dao.saveConceptAttributeType(conceptAttributeType);
1 ✔
2015
        }
2016

2017
        /**
2018
         * @see org.openmrs.api.ConceptService#unretireConceptAttributeType(ConceptAttributeType)
2019
         */
2020
        @Override
2021
        public ConceptAttributeType unretireConceptAttributeType(ConceptAttributeType conceptAttributeType) {
2022
                return Context.getConceptService().saveConceptAttributeType(conceptAttributeType);
1 ✔
2023
        }
2024

2025
        /**
2026
         * @see org.openmrs.api.ConceptService#getConceptAttributeByUuid(String)
2027
         */
2028
        @Override
2029
        @Transactional(readOnly = true)
2030
        public ConceptAttribute getConceptAttributeByUuid(String uuid) {
2031
                return dao.getConceptAttributeByUuid(uuid);
1 ✔
2032
        }
2033

2034
        /**
2035
         * @see org.openmrs.api.ConceptService#hasAnyConceptAttribute(ConceptAttributeType)
2036
         */
2037
        @Override
2038
        @Transactional(readOnly = true)
2039
        public boolean hasAnyConceptAttribute(ConceptAttributeType conceptAttributeType) {
2040
                return dao.getConceptAttributeCount(conceptAttributeType) > 0;
1 ✔
2041
        }
2042

2043
        /**
2044
         * @see ConceptService#saveConceptReferenceRange(ConceptReferenceRange)
2045
         */
2046
        @Override
2047
        public ConceptReferenceRange saveConceptReferenceRange(ConceptReferenceRange conceptReferenceRange) {
2048
                return dao.saveConceptReferenceRange(conceptReferenceRange);
1 ✔
2049
        }
2050

2051
        /**
2052
         * @see ConceptService#getConceptReferenceRangesByConceptId(Integer)
2053
         */
2054
        @Override
2055
        @Transactional(readOnly = true)
2056
        public List<ConceptReferenceRange> getConceptReferenceRangesByConceptId(Integer conceptId) {
2057
                return dao.getConceptReferenceRangesByConceptId(conceptId);
1 ✔
2058
        }
2059

2060
        @Override
2061
        public ConceptReferenceRange getConceptReferenceRangeByUuid(String uuid) {
2062
                return dao.getConceptReferenceRangeByUuid(uuid);
1 ✔
2063
        }
2064
        
2065
        @Override
2066
        public ConceptReferenceRange getConceptReferenceRange(Person person, Concept concept) {
2067
                if (person == null || concept == null) {
1 ✔
2068
                        return null;
1 ✔
2069
                }
2070
                return Context.getConceptService().getConceptReferenceRange(
1 ✔
2071
                        new ConceptReferenceRangeContext(person, concept, null));
2072
        }
2073

2074
        @Override
2075
        public ConceptReferenceRange getConceptReferenceRange(ConceptReferenceRangeContext context) {
2076
                if (context == null) {
1 ✔
2077
                        throw new IllegalArgumentException("ConceptReferenceRangeContext must not be null");
1 ✔
2078
                }
2079

2080
                Concept concept = HibernateUtil.getRealObjectFromProxy(context.getConcept());
1 ✔
2081
                if (!(concept instanceof ConceptNumeric) || concept.getDatatype() == null || !concept.getDatatype().isNumeric()) {
1 ✔
2082
                        return null;
1 ✔
2083
                }
2084
                ConceptNumeric conceptNumeric = (ConceptNumeric) concept;
1 ✔
2085

2086
                List<ConceptReferenceRange> referenceRanges =
2087
                        Context.getConceptService().getConceptReferenceRangesByConceptId(concept.getConceptId());
1 ✔
2088

2089
                if (referenceRanges.isEmpty()) {
1 ✔
2090
                        return getDefaultReferenceRange(conceptNumeric);
1 ✔
2091
                }
2092

2093
                ConceptReferenceRangeUtility referenceRangeUtility = new ConceptReferenceRangeUtility();
1 ✔
2094
                List<ConceptReferenceRange> validRanges = new ArrayList<>();
1 ✔
2095

2096
                for (ConceptReferenceRange referenceRange : referenceRanges) {
1 ✔
2097
                        if (referenceRangeUtility.evaluateCriteria(
1 ✔
2098
                                        StringEscapeUtils.unescapeHtml4(referenceRange.getCriteria()), context)) {
1 ✔
2099
                                validRanges.add(referenceRange);
1 ✔
2100
                        }
2101
                }
1 ✔
2102

2103
                if (validRanges.isEmpty()) {
1 ✔
2104
                        ConceptReferenceRange defaultReferenceRange = getDefaultReferenceRange(conceptNumeric);
1 ✔
2105
                        if (defaultReferenceRange != null) {
1 ✔
2106
                                return defaultReferenceRange;
1 ✔
2107
                        }
2108
                        return null;
1 ✔
2109
                }
2110

2111
                return findStrictestReferenceRange(validRanges);
1 ✔
2112
        }
2113

2114
        /**
2115
         * Returns a reference range derived from the ConceptNumeric's own range fields.
2116
         * Used as a fallback when no ConceptReferenceRange records exist or match.
2117
         */
2118
        private static ConceptReferenceRange getDefaultReferenceRange(ConceptNumeric conceptNumeric) {
2119
                if (conceptNumeric == null || (
1 ✔
2120
                        conceptNumeric.getHiAbsolute() == null &&
1 ✔
2121
                        conceptNumeric.getHiCritical() == null &&
1 ✔
2122
                        conceptNumeric.getHiNormal() == null &&
1 ✔
2123
                        conceptNumeric.getLowAbsolute() == null &&
1 ✔
2124
                        conceptNumeric.getLowCritical() == null &&
1 ✔
2125
                        conceptNumeric.getLowNormal() == null
1 ✔
2126
                )) {
2127
                        return null;
1 ✔
2128
                }
2129

2130
                ConceptReferenceRange defaultReferenceRange = new ConceptReferenceRange();
1 ✔
2131
                defaultReferenceRange.setConceptNumeric(conceptNumeric);
1 ✔
2132
                defaultReferenceRange.setHiAbsolute(conceptNumeric.getHiAbsolute());
1 ✔
2133
                defaultReferenceRange.setHiCritical(conceptNumeric.getHiCritical());
1 ✔
2134
                defaultReferenceRange.setHiNormal(conceptNumeric.getHiNormal());
1 ✔
2135
                defaultReferenceRange.setLowAbsolute(conceptNumeric.getLowAbsolute());
1 ✔
2136
                defaultReferenceRange.setLowCritical(conceptNumeric.getLowCritical());
1 ✔
2137
                defaultReferenceRange.setLowNormal(conceptNumeric.getLowNormal());
1 ✔
2138
                return defaultReferenceRange;
1 ✔
2139
        }
2140

2141
        /**
2142
         * Combines multiple matching reference ranges into one by selecting the strictest bound for
2143
         * each limit. For low bounds, the highest value is strictest; for high bounds, the lowest.
2144
         * For example, ranges 80-150 and 60-140 combine to 80-140.
2145
         */
2146
        private static ConceptReferenceRange findStrictestReferenceRange(List<ConceptReferenceRange> conceptReferenceRanges) {
2147
                if (conceptReferenceRanges.size() == 1) {
1 ✔
2148
                        return conceptReferenceRanges.get(0);
1 ✔
2149
                }
2150

2151
                ConceptReferenceRange strictestRange = new ConceptReferenceRange();
1 ✔
2152
                strictestRange.setConceptNumeric(conceptReferenceRanges.get(0).getConceptNumeric());
1 ✔
2153

2154
                for (ConceptReferenceRange conceptReferenceRange : conceptReferenceRanges) {
1 ✔
2155
                        if (conceptReferenceRange.getLowAbsolute() != null &&
1 ✔
2156
                                        (strictestRange.getLowAbsolute() == null || strictestRange.getLowAbsolute() < conceptReferenceRange.getLowAbsolute())) {
1 ✔
2157
                                strictestRange.setLowAbsolute(conceptReferenceRange.getLowAbsolute());
1 ✔
2158
                        }
2159

2160
                        if (conceptReferenceRange.getLowCritical() != null &&
1 ✔
2161
                                        (strictestRange.getLowCritical() == null || strictestRange.getLowCritical() < conceptReferenceRange.getLowCritical())) {
1 ✔
2162
                                strictestRange.setLowCritical(conceptReferenceRange.getLowCritical());
1 ✔
2163
                        }
2164

2165
                        if (conceptReferenceRange.getLowNormal() != null &&
1 ✔
2166
                                        (strictestRange.getLowNormal() == null || strictestRange.getLowNormal() < conceptReferenceRange.getLowNormal())) {
1 ✔
2167
                                strictestRange.setLowNormal(conceptReferenceRange.getLowNormal());
1 ✔
2168
                        }
2169

2170
                        if (conceptReferenceRange.getHiNormal() != null &&
1 ✔
2171
                                        (strictestRange.getHiNormal() == null || strictestRange.getHiNormal() > conceptReferenceRange.getHiNormal())) {
1 ✔
2172
                                strictestRange.setHiNormal(conceptReferenceRange.getHiNormal());
1 ✔
2173
                        }
2174

2175
                        if (conceptReferenceRange.getHiCritical() != null &&
1 ✔
2176
                                        (strictestRange.getHiCritical() == null || strictestRange.getHiCritical() > conceptReferenceRange.getHiCritical())) {
1 ✔
2177
                                strictestRange.setHiCritical(conceptReferenceRange.getHiCritical());
1 ✔
2178
                        }
2179

2180
                        if (conceptReferenceRange.getHiAbsolute() != null &&
1 ✔
2181
                                        (strictestRange.getHiAbsolute() == null || strictestRange.getHiAbsolute() > conceptReferenceRange.getHiAbsolute())) {
1 ✔
2182
                                strictestRange.setHiAbsolute(conceptReferenceRange.getHiAbsolute());
1 ✔
2183
                        }
2184
                }
1 ✔
2185

2186
                return strictestRange;
1 ✔
2187
        }
2188

2189
        private List<ConceptClass> getConceptClassesOfOrderTypes() {
2190
                List<ConceptClass> mappedClasses = new ArrayList<>();
1 ✔
2191
                AdministrationService administrationService = Context.getAdministrationService();
1 ✔
2192
                List<List<Object>> result = administrationService.executeSQL(
1 ✔
2193
                    "SELECT DISTINCT concept_class_id FROM order_type_class_map", true);
2194
                for (List<Object> temp : result) {
1 ✔
2195
                        for (Object value : temp) {
1 ✔
2196
                                if (value != null) {
1 ✔
2197
                                        mappedClasses.add(this.getConceptClass((Integer) value));
1 ✔
2198
                                }
2199
                        }
1 ✔
2200
                }
1 ✔
2201
                return mappedClasses;
1 ✔
2202
        }
2203
        
2204
        /**
2205
         * @see org.openmrs.api.ConceptService#purgeConceptReferenceRange(ConceptReferenceRange)
2206
         */
2207
        @Override
2208
        public void purgeConceptReferenceRange(ConceptReferenceRange conceptReferenceRange) {
2209
                checkIfLocked();
1 ✔
2210
                dao.purgeConceptReferenceRange(conceptReferenceRange);
1 ✔
2211
        }
1 ✔
2212
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc