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

openmrs / openmrs-core / 43502855768

13 Nov 2026 08:33PM UTC coverage: 67.328% (+0.4%) from 66.969%
43502855768

push

github

ibacher
TRUNK-6616: Count obs version history statements without the privilege check

getObsVersionHistory_shouldLoadTheWholeChainInTwoQueries counted the
statements of the service call, which include its privilege check. When
test setup has cleared the role privilege cache in the same transaction,
that check reads roles from the database, so the count depended on test
order. Measure the DAO call instead.

26896 of 39948 relevant lines covered (67.33%)

0.67 hits per line

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

93.72
/api/src/main/java/org/openmrs/api/impl/ObsServiceImpl.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.util.ArrayList;
13
import java.util.Date;
14
import java.util.LinkedHashMap;
15
import java.util.List;
16
import java.util.Map;
17

18
import org.openmrs.Concept;
19
import org.openmrs.ConceptName;
20
import org.openmrs.Encounter;
21
import org.openmrs.Location;
22
import org.openmrs.Obs;
23
import org.openmrs.Patient;
24
import org.openmrs.Person;
25
import org.openmrs.Visit;
26
import org.openmrs.aop.RequiredDataAdvice;
27
import org.openmrs.api.APIException;
28
import org.openmrs.api.EncounterService;
29
import org.openmrs.api.ObsService;
30
import org.openmrs.api.PatientService;
31
import org.openmrs.api.context.Context;
32
import org.openmrs.api.db.ObsDAO;
33
import org.openmrs.api.handler.SaveHandler;
34
import org.openmrs.obs.ComplexData;
35
import org.openmrs.obs.ComplexObsHandler;
36
import org.openmrs.parameter.ObsSearchCriteria;
37
import org.openmrs.parameter.ObsSearchCriteriaBuilder;
38
import org.openmrs.util.OpenmrsClassLoader;
39
import org.openmrs.util.OpenmrsConstants.PERSON_TYPE;
40
import org.openmrs.util.OpenmrsUtil;
41
import org.openmrs.util.PrivilegeConstants;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
import org.springframework.transaction.annotation.Transactional;
45

46
/**
47
 * Default implementation of the Observation Service
48
 * 
49
 * @see org.openmrs.api.ObsService
50
 */
51
@Transactional
52
public class ObsServiceImpl extends BaseOpenmrsService implements ObsService {
53

54
        private static final Logger log = LoggerFactory.getLogger(ObsServiceImpl.class);
1 ✔
55

56
        /**
57
         * The data access object for the obs service
58
         */
59
        protected ObsDAO dao;
60
        
61
        /**
62
         * Report handlers that have been registered. This is filled via {@link #setHandlers(Map)} and
63
         * spring's applicationContext-service.xml object
64
         */
65
        private static Map<String, ComplexObsHandler> handlers = null;
1 ✔
66
        
67
        /**
68
         * Default empty constructor for this obs service
69
         */
70
        public ObsServiceImpl() {
1 ✔
71
        }
1 ✔
72
        
73
        /**
74
         * @see org.openmrs.api.ObsService#setObsDAO(org.openmrs.api.db.ObsDAO)
75
         */
76
        @Override
77
        public void setObsDAO(ObsDAO dao) {
78
                this.dao = dao;
1 ✔
79
        }
1 ✔
80
        
81
        /**
82
         * @see org.openmrs.api.ObsService#getObsVersionHistory(Obs)
83
         */
84
        @Override
85
        @Transactional(readOnly = true)
86
        public List<Obs> getObsVersionHistory(Obs obs) {
87
                return dao.getObsVersionHistory(obs);
1 ✔
88
        }
89

90
        /**
91
         * Clean up after this class. Set the static var to null so that the classloader can reclaim the
92
         * space.
93
         * 
94
         * @see org.openmrs.api.impl.BaseOpenmrsService#onShutdown()
95
         */
96
        @Override
97
        public void onShutdown() {
98
                setHandlers(null);
×
99
        }
×
100
        
101
        /**
102
         * @see org.openmrs.api.ObsService#saveObs(org.openmrs.Obs, String)
103
         */
104
        @Override
105
        public Obs saveObs(Obs obs, String changeMessage) throws APIException {
106
                if(obs == null){
1 ✔
107
                        throw new APIException("Obs.error.cannot.be.null", (Object[]) null);
1 ✔
108
                }
109

110
                if(obs.getId() != null && changeMessage == null){
1 ✔
111
                        throw new APIException("Obs.error.ChangeMessage.required", (Object[]) null);
×
112
                }
113
                
114
                ensureRequirePrivilege(obs);
1 ✔
115

116
                //Should allow updating a voided Obs, it seems to be pointless to restrict it,
117
                //otherwise operations like merge patients won't be possible when to moving voided obs
118
                if (obs.getObsId() == null || obs.getVoided()) {
1 ✔
119
                        return saveNewOrVoidedObs(obs,changeMessage);
1 ✔
120
                } else if(!obs.isDirty()){
1 ✔
121
                        setPersonFromEncounter(obs);
1 ✔
122
                        return saveObsNotDirty(obs, changeMessage);
1 ✔
123
                } else {
124
                        setPersonFromEncounter(obs);
1 ✔
125
                        return saveExistingObs(obs,changeMessage);
1 ✔
126
                }
127
        }
128

129
        private void setPersonFromEncounter(Obs obs) {
130
                Encounter encounter = obs.getEncounter();
1 ✔
131
                if (encounter != null) {
1 ✔
132
                        obs.setPerson(encounter.getPatient());
1 ✔
133
                }
134
        }
1 ✔
135

136
        private void voidExistingObs(Obs obs, String changeMessage, Obs newObs) {
137
                // void out the original observation to keep it around for
138
                // historical purposes
139
                try {
140
                        Context.addProxyPrivilege(PrivilegeConstants.DELETE_OBS);
1 ✔
141

142
                        // fetch a clean copy of this obs from the database so that
143
                        // we don't write the changes to the database when we save
144
                        // the fact that the obs is now voided
145
                        evictObsAndChildren(obs);
1 ✔
146
                        obs = Context.getObsService().getObs(obs.getObsId());
1 ✔
147
                        //delete the previous file from the appdata/complex_obs folder
148
                        if (newObs.hasPreviousVersion() && newObs.getPreviousVersion().isComplex()) {
1 ✔
149
                                ComplexObsHandler handler = getHandler(newObs.getPreviousVersion());
1 ✔
150
                                handler.purgeComplexData(newObs.getPreviousVersion());
1 ✔
151
                        }
152
                        // calling this via the service so that AOP hooks are called
153
                        Context.getObsService().voidObs(obs, changeMessage);
1 ✔
154

155
                }
156
                finally {
157
                        Context.removeProxyPrivilege(PrivilegeConstants.DELETE_OBS);
1 ✔
158
                }
159
        }
1 ✔
160

161
        private Obs saveExistingObs(Obs obs, String changeMessage) {
162
                // get a copy of the passed in obs and save it to the
163
                // database. This allows us to create a new row and new obs_id
164
                // this method doesn't copy the obs_id
165
                Obs newObs = Obs.newInstance(obs);
1 ✔
166

167
                unsetVoidedAndCreationProperties(newObs,obs);
1 ✔
168
                handleObsWithComplexConcept(newObs);
1 ✔
169
                Obs.Status originalStatus = dao.getSavedStatus(obs);
1 ✔
170
                updateStatusIfNecessary(newObs, originalStatus);
1 ✔
171

172
                RequiredDataAdvice.recursivelyHandle(SaveHandler.class, newObs, changeMessage);
1 ✔
173

174
                // save the new row to the database with the changes that
175
                // have been made to it
176
                dao.saveObs(newObs);
1 ✔
177

178
                saveObsGroup(newObs,null);
1 ✔
179

180
                voidExistingObs(obs, changeMessage, newObs);
1 ✔
181

182
                return newObs;
1 ✔
183

184
        }
185
        
186
        private void updateStatusIfNecessary(Obs newObs, Obs.Status originalStatus) {
187
                if (Obs.Status.FINAL.equals(originalStatus)) {
1 ✔
188
                        newObs.setStatus(Obs.Status.AMENDED);
1 ✔
189
                }
190
        }
1 ✔
191
        
192
        private void unsetVoidedAndCreationProperties(Obs newObs,Obs obs) {
193
                newObs.setVoided(false);
1 ✔
194
                newObs.setVoidReason(null);
1 ✔
195
                newObs.setDateVoided(null);
1 ✔
196
                newObs.setVoidedBy(null);
1 ✔
197
                newObs.setCreator(null);
1 ✔
198
                newObs.setDateCreated(null);
1 ✔
199
                newObs.setPreviousVersion(obs);
1 ✔
200
        }
1 ✔
201

202
        private Obs saveObsNotDirty(Obs obs, String changeMessage) {
203
                if(!obs.isObsGrouping()){
1 ✔
204
                        return obs;
1 ✔
205
                }
206

207
                ObsService os = Context.getObsService();
1 ✔
208
                boolean refreshNeeded = false;
1 ✔
209
                for (Obs o : obs.getGroupMembers(true)) {
1 ✔
210
                        if (o.getId() == null) {
1 ✔
211
                                os.saveObs(o, null);
1 ✔
212
                        } else {
213
                                Obs newObs = os.saveObs(o, changeMessage);
1 ✔
214
                                refreshNeeded = !newObs.equals(o) || refreshNeeded;
1 ✔
215
                        }
216
                }
1 ✔
217

218
                if(refreshNeeded) {
1 ✔
219
                        Context.refreshEntity(obs);
1 ✔
220
                }
221
                return obs;
1 ✔
222
        }
223

224
        private Obs saveNewOrVoidedObs(Obs obs, String changeMessage) {
225
                handleObsWithComplexConcept(obs);
1 ✔
226
                Obs ret = dao.saveObs(obs);
1 ✔
227
                saveObsGroup(ret,changeMessage);
1 ✔
228
                return ret;
1 ✔
229
        }
230

231
        private void evictObsAndChildren(Obs obs) {
232
                Context.evictFromSession(obs);
1 ✔
233
                if(obs.hasGroupMembers()) {
1 ✔
234
                        for(Obs member : obs.getGroupMembers()) {
1 ✔
235
                                evictObsAndChildren(member);
1 ✔
236
                        }
1 ✔
237
                }
238
        }
1 ✔
239

240
        private void ensureRequirePrivilege(Obs obs){
241
                if (obs.getObsId() == null) {
1 ✔
242
                        Context.requirePrivilege(PrivilegeConstants.ADD_OBS);
1 ✔
243
                } else {
244
                        Context.requirePrivilege(PrivilegeConstants.EDIT_OBS);
1 ✔
245
                }
246
        }
1 ✔
247

248
        private void saveObsGroup(Obs obs, String changeMessage){
249
                if (obs.isObsGrouping()) {
1 ✔
250
                        for (Obs o : obs.getGroupMembers(true)) {
1 ✔
251
                                Context.getObsService().saveObs(o, changeMessage);
1 ✔
252
                        }
1 ✔
253
                }
254
        }
1 ✔
255

256
        private void handleObsWithComplexConcept(Obs obs) {
257
                ComplexData complexData = obs.getComplexData();
1 ✔
258
                Concept concept = obs.getConcept();
1 ✔
259
                if (null != concept && concept.isComplex()
1 ✔
260
                        && null != complexData && null != complexData.getData()) {
1 ✔
261
                        // save or update complexData object on this obs
262
                        // this is done before the database save so that the obs.valueComplex
263
                        // can be filled in by the handler.
264
                        ComplexObsHandler handler = getHandler(obs);
1 ✔
265
                        if (null != handler) {
1 ✔
266
                                handler.saveObs(obs);
1 ✔
267
                        } else {
268
                                throw new APIException("unknown.handler", new Object[] {concept});
×
269
                        }
270
                }
271
        }
1 ✔
272

273
        /**
274
         * @see org.openmrs.api.ObsService#getObs(java.lang.Integer)
275
         */
276
        @Override
277
        @Transactional(readOnly = true)
278
        public Obs getObs(Integer obsId) throws APIException {
279
                Obs obs = dao.getObs(obsId);
1 ✔
280
                if (obs != null && obs.isComplex()) {
1 ✔
281
                        return getHandler(obs).getObs(obs,ComplexObsHandler.RAW_VIEW);
1 ✔
282
                }
283
                return obs;
1 ✔
284
        }
285
        
286
        /**
287
         * Voids an Obs If the Obs argument is an obsGroup, all group members will be voided.
288
         * 
289
         * @see org.openmrs.api.ObsService#voidObs(org.openmrs.Obs, java.lang.String)
290
         * @param obs the Obs to void
291
         * @param reason the void reason
292
         * @throws APIException
293
         */
294
        @Override
295
        public Obs voidObs(Obs obs, String reason) throws APIException {
296
                return dao.saveObs(obs);
1 ✔
297
        }
298
        
299
        /**
300
         * Unvoids an Obs
301
         * <p>
302
         * If the Obs argument is an obsGroup, all group members with the same dateVoided will also be
303
         * unvoided.
304
         * 
305
         * @see org.openmrs.api.ObsService#unvoidObs(org.openmrs.Obs)
306
         * @param obs the Obs to unvoid
307
         * @return the unvoided Obs
308
         * @throws APIException
309
         */
310
        @Override
311
        public Obs unvoidObs(Obs obs) throws APIException {
312
                return Context.getObsService().saveObs(obs,"unvoid obs");
1 ✔
313
        }
314
        
315
        /**
316
         * @see org.openmrs.api.ObsService#purgeObs(org.openmrs.Obs, boolean)
317
         */
318
        @Override
319
        public void purgeObs(Obs obs, boolean cascade) throws APIException {
320
                if (!purgeComplexData(obs)) {
1 ✔
321
                        // Log a warning instead of throwing an error.
322
                        // This allows purging the obs row even if the associated file is missing,
323
                        // which matches the behavior expected by modules like Attachments.
324
                        log.warn("purgeComplexData returned false for Obs ID: " + (obs != null ? obs.getObsId() : "null") + ". " + 
1 ✔
325
                                "This may mean the file is already missing. Proceeding to purge the database row.");
326
                }
327

328
                if (cascade) {
1 ✔
329
                        throw new APIException("Obs.error.cascading.purge.not.implemented", (Object[]) null);
1 ✔
330
                        // TODO delete any related objects here before deleting the obs
331
                        // obsGroups objects?
332
                        // orders?
333
                }
334
                dao.deleteObs(obs);
1 ✔
335
        }
1 ✔
336
        
337
        /**
338
         * @see org.openmrs.api.ObsService#purgeObs(org.openmrs.Obs)
339
         */
340
        @Override
341
        public void purgeObs(Obs obs) throws APIException {
342
                Context.getObsService().purgeObs(obs, false);
1 ✔
343
        }
1 ✔
344
        
345
        /**
346
         * @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
347
         *      java.util.List, java.util.List, List, List, java.util.List, java.lang.Integer,
348
         *      java.lang.Integer, java.util.Date, java.util.Date, boolean)
349
         */
350
        @Override
351
        @Transactional(readOnly = true)
352
        public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
353
                List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
354
                Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
355
                throws APIException {
356

357
                return dao.getObservations(whom, encounters, questions, answers, personTypes, locations, sort, mostRecentN,
1 ✔
358
                    obsGroupId, fromDate, toDate, includeVoidedObs, null);
359
        }
360
        
361
        /**
362
         * @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
363
         *      java.util.List, java.util.List, List, List, java.util.List, java.lang.Integer,
364
         *      java.lang.Integer, java.util.Date, java.util.Date, boolean, java.lang.String)
365
         */
366
        @Override
367
        @Transactional(readOnly = true)
368
        public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
369
                                         List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations,
370
                                         List<String> sort, Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate,
371
                                         boolean includeVoidedObs, String accessionNumber) throws APIException {
372
                
373
                return this.getObservations(whom, encounters, questions, answers, personTypes, locations, sort, null,
1 ✔
374
                                mostRecentN, obsGroupId, fromDate, toDate, includeVoidedObs, accessionNumber);
375
        }
376
        
377
        /**
378
         * @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List,
379
         *      java.util.List, java.util.List, List, List, java.util.List, java.util.List, java.lang.Integer,
380
         *      java.lang.Integer, java.util.Date, java.util.Date, boolean, java.lang.String)
381
         */
382
        @Override
383
        @Transactional(readOnly = true)
384
        public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
385
                List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
386
                List<Visit> visits, Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate,
387
                boolean includeVoidedObs, String accessionNumber) throws APIException {
388

389
                return dao.getObservations(whom, encounters, questions, answers, personTypes, locations, sort, visits, mostRecentN,
1 ✔
390
                    obsGroupId, fromDate, toDate, includeVoidedObs, accessionNumber);
391
        }
392
        
393
        /**
394
         * @see org.openmrs.api.ObsService#getObservations(ObsSearchCriteria)
395
         */
396
        @Override
397
        @Transactional(readOnly = true)
398
        public List<Obs> getObservations(ObsSearchCriteria obsSearchCriteria) throws APIException {
399
                requireSearchCriteria(obsSearchCriteria);
1 ✔
400

401
                return dao.getObservations(obsSearchCriteria);
1 ✔
402
        }
403

404
        /**
405
         * Every individual search parameter is optional, so passing null for the criteria object as a whole
406
         * is an easy mistake to make. Caught at the service boundary it names the problem, where the DAO
407
         * would only raise an NPE from inside a query.
408
         *
409
         * @param obsSearchCriteria the criteria to check
410
         * @throws IllegalArgumentException if obsSearchCriteria is null
411
         */
412
        private static void requireSearchCriteria(ObsSearchCriteria obsSearchCriteria) {
413
                if (obsSearchCriteria == null) {
1 ✔
414
                        throw new IllegalArgumentException("obsSearchCriteria cannot be null");
1 ✔
415
                }
416
        }
1 ✔
417

418
        /**
419
         * @see org.openmrs.api.ObsService#getObservations(java.util.List, java.util.List, java.util.List,
420
         *      java.util.List, List, List, java.util.List, java.util.List, java.lang.Integer,
421
         *      java.lang.Integer, java.util.Date, java.util.Date, boolean, java.lang.String,
422
         *      java.lang.Integer, java.lang.Integer)
423
         */
424
        @Override
425
        @SuppressWarnings("squid:S107")
426
        @Transactional(readOnly = true)
427
        public List<Obs> getObservations(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
428
                List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<String> sort,
429
                List<Visit> visits, Integer mostRecentN, Integer obsGroupId, Date fromDate, Date toDate,
430
                boolean includeVoidedObs, String accessionNumber, Integer startIndex, Integer maxResults) throws APIException {
431

432
                return this.getObservations(new ObsSearchCriteriaBuilder().setWhom(whom).setEncounters(encounters)
1 ✔
433
                        .setQuestions(questions).setAnswers(answers).setPersonTypes(personTypes).setLocations(locations)
1 ✔
434
                        .setSort(sort).setVisits(visits).setMostRecentN(mostRecentN).setObsGroupId(obsGroupId).setFromDate(fromDate)
1 ✔
435
                        .setToDate(toDate).setIncludeVoidedObs(includeVoidedObs).setAccessionNumber(accessionNumber)
1 ✔
436
                        .setStartIndex(startIndex).setMaxResults(maxResults).createObsSearchCriteria());
1 ✔
437
        }
438

439
        /**
440
         * @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
441
         *      java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
442
         *      java.util.Date, java.util.Date, boolean)
443
         */
444
        @Override
445
        @Transactional(readOnly = true)
446
        public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
447
                                           List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations,
448
                                           Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs)
449
            throws APIException {
450
                return OpenmrsUtil.convertToInteger(dao.getObservationCount(whom, encounters, questions, answers, personTypes,
1 ✔
451
                    locations, obsGroupId, fromDate, toDate, null, includeVoidedObs, null));
452
        }
453
        
454
        /**
455
         * @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
456
         *      java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
457
         *      java.util.Date, java.util.Date, boolean, java.lang.String)
458
         */
459
        @Override
460
        @Transactional(readOnly = true)
461
        public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
462
                                           List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations,
463
                                           Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
464
                                           String accessionNumber) throws APIException {
465
                return this.getObservationCount(whom, encounters, questions, answers, personTypes, locations, null, obsGroupId,
1 ✔
466
                                fromDate, toDate, includeVoidedObs, accessionNumber);
467
        }
468
        
469
        /**
470
         * @see org.openmrs.api.ObsService#getObservationCount(java.util.List, java.util.List,
471
         *      java.util.List, java.util.List, java.util.List, java.util.List, java.util.List, java.lang.Integer,
472
         *      java.util.Date, java.util.Date, boolean, java.lang.String)
473
         */
474
        @Override
475
        @Transactional(readOnly = true)
476
        public Integer getObservationCount(List<Person> whom, List<Encounter> encounters, List<Concept> questions,
477
                                           List<Concept> answers, List<PERSON_TYPE> personTypes, List<Location> locations, List<Visit> visits,
478
                                           Integer obsGroupId, Date fromDate, Date toDate, boolean includeVoidedObs,
479
                                           String accessionNumber) throws APIException {
480
                return OpenmrsUtil.convertToInteger(dao.getObservationCount(whom, encounters, questions, answers, personTypes,
1 ✔
481
                    locations, obsGroupId, fromDate, toDate, null, visits, includeVoidedObs, accessionNumber));
482
        }
483
        
484
        /**
485
         * @see org.openmrs.api.ObsService#getObservationCount(ObsSearchCriteria)
486
         */
487
        @Override
488
        @Transactional(readOnly = true)
489
        public Integer getObservationCount(ObsSearchCriteria obsSearchCriteria) throws APIException {
490
                requireSearchCriteria(obsSearchCriteria);
1 ✔
491

492
                // the sort and the row bounds the criteria carry say which of the matching rows come back, not
493
                // which rows match, so a count deliberately ignores them: a paging client counting the result set
494
                // it is walking wants the whole of it. valueCodedNameAnswers is passed as null because the search
495
                // criteria cannot express it, matching the other getObservationCount() overloads
496
                return OpenmrsUtil.convertToInteger(dao.getObservationCount(obsSearchCriteria.getWhom(),
1 ✔
497
                    obsSearchCriteria.getEncounters(), obsSearchCriteria.getQuestions(), obsSearchCriteria.getAnswers(),
1 ✔
498
                    obsSearchCriteria.getPersonTypes(), obsSearchCriteria.getLocations(), obsSearchCriteria.getObsGroupId(),
1 ✔
499
                    obsSearchCriteria.getFromDate(), obsSearchCriteria.getToDate(), null, obsSearchCriteria.getVisits(),
1 ✔
500
                    obsSearchCriteria.isIncludeVoidedObs(), obsSearchCriteria.getAccessionNumber()));
1 ✔
501
        }
502

503
        /**
504
         * This implementation queries the obs table comparing the given <code>searchString</code> with the
505
         * patient's identifier, encounterId, and obsId
506
         *
507
         * @see org.openmrs.api.ObsService#getObservations(java.lang.String)
508
         */
509
        @Override
510
        @Transactional(readOnly = true)
511
        public List<Obs> getObservations(String searchString) {
512
                
513
                // search on patient identifier
514
                PatientService ps = Context.getPatientService();
1 ✔
515
                List<Patient> patients = ps.getPatients(searchString);
1 ✔
516
                List<Person> persons = new ArrayList<>(patients);
1 ✔
517
                
518
                // try to search on encounterId
519
                EncounterService es = Context.getEncounterService();
1 ✔
520
                List<Encounter> encounters = new ArrayList<>();
1 ✔
521
                try {
522
                        Encounter e = es.getEncounter(Integer.valueOf(searchString));
1 ✔
523
                        if (e != null) {
1 ✔
524
                                encounters.add(e);
1 ✔
525
                        }
526
                }
527
                catch (NumberFormatException e) {
1 ✔
528
                        // pass
529
                }
1 ✔
530
                
531
                List<Obs> returnList = new ArrayList<>();
1 ✔
532
                
533
                if (!encounters.isEmpty() || !persons.isEmpty()) {
1 ✔
534
                        returnList = Context.getObsService().getObservations(persons, encounters, null, null, null, null, null, null,
1 ✔
535
                            null, null, null, false);
536
                }
537
                
538
                // try to search on obsId
539
                try {
540
                        Obs o = getObs(Integer.valueOf(searchString));
1 ✔
541
                        if (o != null) {
1 ✔
542
                                returnList.add(o);
1 ✔
543
                        }
544
                }
545
                catch (NumberFormatException e) {
1 ✔
546
                        // pass
547
                }
1 ✔
548
                
549
                return returnList;
1 ✔
550
        }
551
        
552
        /**
553
         * @see org.openmrs.api.ObsService#getObservationsByPerson(org.openmrs.Person)
554
         */
555
        @Override
556
        @Transactional(readOnly = true)
557
        public List<Obs> getObservationsByPerson(Person who) {
558
                List<Person> whom = new ArrayList<>();
1 ✔
559
                whom.add(who);
1 ✔
560
                return Context.getObsService().getObservations(whom, null, null, null, null, null, null, null, null, null, null,
1 ✔
561
                    false);
562
        }
563
        
564
        /**
565
         * @see org.openmrs.api.ObsService#getObservationsByPersonAndConcept(org.openmrs.Person,
566
         *      org.openmrs.Concept)
567
         */
568
        @Override
569
        @Transactional(readOnly = true)
570
        public List<Obs> getObservationsByPersonAndConcept(Person who, Concept question) throws APIException {
571
                List<Person> whom = new ArrayList<>();
1 ✔
572
                if (who != null && who.getPersonId() != null) {
1 ✔
573
                        whom.add(who);
1 ✔
574
                }
575
                List<Concept> questions = new ArrayList<>();
1 ✔
576
                questions.add(question);
1 ✔
577
                
578
                return Context.getObsService().getObservations(whom, null, questions, null, null, null, null, null, null, null,
1 ✔
579
                    null, false);
580
        }
581
        
582
        /**
583
         * @see org.openmrs.api.ObsService#getObsByUuid(java.lang.String)
584
         */
585
        @Override
586
        @Transactional(readOnly = true)
587
        public Obs getObsByUuid(String uuid) throws APIException {
588
                Obs obsByUuid = dao.getObsByUuid(uuid);
1 ✔
589
                if (obsByUuid != null && obsByUuid.isComplex()) {
1 ✔
590
                        return getHandler(obsByUuid).getObs(obsByUuid,ComplexObsHandler.RAW_VIEW);
1 ✔
591
                }
592
                return obsByUuid;
1 ✔
593
        }
594

595
        /**
596
         * @see org.openmrs.api.ObsService#getRevisionObs(org.openmrs.Obs)
597
         */
598
        @Transactional(readOnly = true)
599
        public Obs getRevisionObs(Obs initialObs) {
600
                return dao.getRevisionObs(initialObs);
1 ✔
601
        }
602

603
        /**
604
         * @see org.openmrs.api.ObsService#getComplexObs(Integer, String)
605
         */
606
        @Override
607
        @Transactional(readOnly = true)
608
        public Obs getComplexObs(Integer obsId, String view) throws APIException {
609
                Obs obs = dao.getObs(obsId);
1 ✔
610
                
611
                if (obs != null && obs.isComplex()) {
1 ✔
612
                        return getHandler(obs).getObs(obs, view);
1 ✔
613
                }
614
                
615
                return obs;
1 ✔
616
        }
617
        
618
        /**
619
         * Internal method to remove ComplexData when an Obs is purged.
620
         */
621
        protected boolean purgeComplexData(Obs obs) throws APIException {
622
                if (obs.isComplex()) {
1 ✔
623
                        ComplexObsHandler handler = getHandler(obs);
1 ✔
624
                        if (null != handler) {
1 ✔
625
                                return handler.purgeComplexData(obs);
1 ✔
626
                        }
627
                }
628
                
629
                return true;
1 ✔
630
        }
631
        
632
        /**
633
         * @see org.openmrs.api.ObsService#getHandler(org.openmrs.Obs)
634
         */
635
        @Override
636
        @Transactional(readOnly = true)
637
        public ComplexObsHandler getHandler(Obs obs) throws APIException {
638
                if (obs.getConcept().isComplex()) {
1 ✔
639
                        // Get the ConceptComplex from the ConceptService then return its
640
                        // handler.
641
                        if (obs.getConcept() == null) {
1 ✔
642
                                throw new APIException("Obs.error.unable.get.handler", new Object[] { obs });
×
643
                        }
644
                        
645
                        String handlerString = Context.getConceptService().getConceptComplex(obs.getConcept().getConceptId())
1 ✔
646
                                .getHandler();
1 ✔
647
                        
648
                        if (handlerString == null) {
1 ✔
649
                                throw new APIException("Obs.error.unable.get.handler.and.concept", new Object[] { obs, obs.getConcept() });
×
650
                        }
651
                        
652
                        return this.getHandler(handlerString);
1 ✔
653
                }
654
                
655
                return null;
×
656
        }
657
        
658
        /**
659
         * @see org.openmrs.api.ObsService#getHandler(java.lang.String)
660
         */
661
        @Override
662
        public ComplexObsHandler getHandler(String key) {
663
                return handlers.get(key);
1 ✔
664
        }
665
        
666
        /**
667
         * @see org.openmrs.api.ObsService#setHandlers(Map)
668
         * @see #registerHandler(String, ComplexObsHandler)
669
         */
670
        @Override
671
        public void setHandlers(Map<String, ComplexObsHandler> newHandlers) throws APIException {
672
                if (newHandlers == null) {
1 ✔
673
                        ObsServiceImpl.setStaticHandlers(null);
×
674
                        return;
×
675
                }
676
                for (Map.Entry<String, ComplexObsHandler> entry : newHandlers.entrySet()) {
1 ✔
677
                        registerHandler(entry.getKey(), entry.getValue());
1 ✔
678
                }
1 ✔
679
        }
1 ✔
680
        
681
        /**
682
         * Sets handlers using static method
683
         *
684
         * @param currentHandlers
685
         */
686
        private static void setStaticHandlers(Map<String, ComplexObsHandler> currentHandlers) {
687
                ObsServiceImpl.handlers = currentHandlers;
×
688
        }
×
689
        
690
        /**
691
         * @see org.openmrs.api.ObsService#getHandlers()
692
         */
693
        @Override
694
        @Transactional(readOnly = true)
695
        public Map<String, ComplexObsHandler> getHandlers() throws APIException {
696
                if (handlers == null) {
1 ✔
697
                        handlers = new LinkedHashMap<>();
1 ✔
698
                }
699
                
700
                return handlers;
1 ✔
701
        }
702
        
703
        /**
704
         * @see org.openmrs.api.ObsService#registerHandler(String, ComplexObsHandler)
705
         */
706
        @Override
707
        public void registerHandler(String key, ComplexObsHandler handler) throws APIException {
708
                getHandlers().put(key, handler);
1 ✔
709
        }
1 ✔
710
        
711
        /**
712
         * @see org.openmrs.api.ObsService#registerHandler(String, String)
713
         */
714
        @Override
715
        public void registerHandler(String key, String handlerClass) throws APIException {
716
                try {
717
                        Class<?> loadedClass = OpenmrsClassLoader.getInstance().loadClass(handlerClass);
1 ✔
718
                        registerHandler(key, (ComplexObsHandler) loadedClass.newInstance());
1 ✔
719
                        
720
                }
721
                catch (Exception e) {
×
722
                        throw new APIException("unable.load.and.instantiate.handler", null, e);
×
723
                }
1 ✔
724
        }
1 ✔
725
        
726
        /**
727
         * @see org.openmrs.api.ObsService#getObservationCount(java.util.List, boolean)
728
         */
729
        @Override
730
        @Transactional(readOnly = true)
731
        public Integer getObservationCount(List<ConceptName> conceptNames, boolean includeVoided) {
732
                return OpenmrsUtil.convertToInteger(dao.getObservationCount(null, null, null, null, null, null, null, null, null,
1 ✔
733
                    conceptNames, true, null));
734
        }
735
        
736
        /**
737
         * @see org.openmrs.api.ObsService#removeHandler(java.lang.String)
738
         */
739
        @Override
740
        public void removeHandler(String key) {
741
                handlers.remove(key);
1 ✔
742
        }
1 ✔
743
}
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