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

openmrs / openmrs-core / 40531146454

27 Oct 2026 03:27PM UTC coverage: 66.688% (+0.03%) from 66.659%
40531146454

push

github

web-flow
Apply encounter-type privileges consistently across EncounterService(#6737) (#6758)

60 of 60 new or added lines in 2 files covered. (100.0%)

6 existing lines in 3 files now uncovered.

26185 of 39265 relevant lines covered (66.69%)

0.67 hits per line

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

95.85
/api/src/main/java/org/openmrs/api/impl/EncounterServiceImpl.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.Collection;
14
import java.util.Date;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19

20
import org.apache.commons.collections.CollectionUtils;
21
import org.apache.commons.lang3.StringUtils;
22
import org.openmrs.Cohort;
23
import org.openmrs.Encounter;
24
import org.openmrs.EncounterProvider;
25
import org.openmrs.EncounterRole;
26
import org.openmrs.EncounterType;
27
import org.openmrs.Form;
28
import org.openmrs.Location;
29
import org.openmrs.Obs;
30
import org.openmrs.Order;
31
import org.openmrs.OrderGroup;
32
import org.openmrs.Patient;
33
import org.openmrs.Privilege;
34
import org.openmrs.Provider;
35
import org.openmrs.User;
36
import org.openmrs.Visit;
37
import org.openmrs.VisitType;
38
import org.openmrs.api.APIException;
39
import org.openmrs.api.DiagnosisService;
40
import org.openmrs.api.EncounterService;
41
import org.openmrs.api.EncounterTypeLockedException;
42
import org.openmrs.api.ObsService;
43
import org.openmrs.api.OrderService;
44
import org.openmrs.api.context.Context;
45
import org.openmrs.api.db.EncounterDAO;
46
import org.openmrs.api.handler.EncounterVisitHandler;
47
import org.openmrs.parameter.EncounterSearchCriteria;
48
import org.openmrs.parameter.EncounterSearchCriteriaBuilder;
49
import org.openmrs.util.HandlerUtil;
50
import org.openmrs.util.OpenmrsClassLoader;
51
import org.openmrs.util.OpenmrsConstants;
52
import org.openmrs.util.OpenmrsUtil;
53
import org.openmrs.util.PrivilegeConstants;
54
import org.openmrs.validator.ValidateUtil;
55
import org.springframework.transaction.annotation.Transactional;
56

57
/**
58
 * Default implementation of the {@link EncounterService}
59
 * <p>
60
 * This class should not be instantiated alone, get a service class from the Context:
61
 * Context.getEncounterService();
62
 * 
63
 * @see org.openmrs.api.context.Context
64
 * @see org.openmrs.api.EncounterService
65
 */
66
@Transactional
67
public class EncounterServiceImpl extends BaseOpenmrsService implements EncounterService {
1 ✔
68
        
69
        private EncounterDAO dao;
70
        
71
        /**
72
         * @see org.openmrs.api.EncounterService#setEncounterDAO(org.openmrs.api.db.EncounterDAO)
73
         */
74
        @Override
75
        public void setEncounterDAO(EncounterDAO dao) {
76
                this.dao = dao;
1 ✔
77
        }
1 ✔
78
        
79
        /**
80
         * @see org.openmrs.api.EncounterService#getEncountersByPatient(java.lang.String, boolean)
81
         */
82
        @Override
83
        @Transactional(readOnly = true)
84
        public List<Encounter> getEncountersByPatient(String query, boolean includeVoided) throws APIException {
85
                if (query == null) {
1 ✔
86
                        throw new IllegalArgumentException("The 'query' parameter is required and cannot be null");
1 ✔
87
                }
88
                
89
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
90
                    dao.getEncounters(query, null, null, null, includeVoided), null);
1 ✔
91
        }
92
        
93
        /**
94
         * @see org.openmrs.api.EncounterService#saveEncounter(org.openmrs.Encounter)
95
         */
96
        @Override
97
        public Encounter saveEncounter(Encounter encounter) throws APIException {
98
                
99
                // if authenticated user is not supposed to edit encounter of certain type
100
                failIfDeniedToEdit(encounter);
1 ✔
101
                
102
                //If new encounter, try to assign a visit using the registered visit assignment handler.
103
                createVisitForNewEncounter(encounter);
1 ✔
104
                
105
                // check permissions
106
                boolean isNewEncounter = requirePrivilege(encounter);
1 ✔
107
                
108
                // This must be done after setting dateCreated etc on the obs because
109
                // of the way the ORM tools flush things and check for nullity
110
                // This also must be done before the save encounter so we can use the
111
                // orig date
112
                // after the save
113
                Patient p = encounter.getPatient();
1 ✔
114
                Date originalDate;
115
                Location originalLocation = null;
1 ✔
116
                
117
                if (!isNewEncounter) {
1 ✔
118
                        // fetch the datetime from the database prior to saving for this
119
                        // encounter
120
                        // to see if it has changed and change all obs after saving if so
121
                        originalDate = dao.getSavedEncounterDatetime(encounter);
1 ✔
122
                        if (encounter.getLocation() != null) {
1 ✔
123
                                originalLocation = dao.getSavedEncounterLocation(encounter);
1 ✔
124
                        }
125
                        // Our data model duplicates the patient column to allow for
126
                        // observations to
127
                        // not have to look up the parent Encounter to find the patient
128
                        // Therefore, encounter.patient must always equal
129
                        // encounter.observations[0-n].patient
130
                        
131
                        // If we are changing encounter.encounterDatetime, then we need to
132
                        // also apply that
133
                        // to Obs that inherited their obsDatetime from the encounter in the
134
                        // first place
135
                        
136
                        Date newDate = encounter.getEncounterDatetime();
1 ✔
137
                        Location newLocation = encounter.getLocation();
1 ✔
138
                        for (Obs obs : encounter.getAllFlattenedObs(true)) {
1 ✔
139
                                // if the date was changed
140
                                if (OpenmrsUtil.compare(originalDate, newDate) != 0
1 ✔
141
                                        && OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {
1 ✔
142
                                        
143
                                        // if the obs datetime is the same as the
144
                                        // original encounter datetime, fix it
145
                                        obs.setObsDatetime(newDate);
1 ✔
146
                                        
147
                                }
148
                                
149
                                if (!OpenmrsUtil.nullSafeEquals(newLocation, originalLocation) && obs.getLocation().equals(originalLocation)) {
1 ✔
150
                                        obs.setLocation(newLocation);
1 ✔
151
                                }
152
                                
153
                                // if the Person in the obs doesn't match the Patient in the
154
                                // encounter, fix it
155
                                if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
1 ✔
156
                                        obs.setPerson(p);
1 ✔
157
                                }
158
                        }
1 ✔
159
                }
160
                // same goes for Orders
161
                for (Order o : encounter.getOrders()) {
1 ✔
162
                        if (!p.equals(o.getPatient())) {
1 ✔
163
                                o.setPatient(p);
1 ✔
164
                        }
165
                }
1 ✔
166

167
                // do the actual saving to the database
168
                dao.saveEncounter(encounter);
1 ✔
169

170
                // save the new orderGroups
171
                for (OrderGroup orderGroup : encounter.getOrderGroups()) {
1 ✔
172
                        Context.getOrderService().saveOrderGroup(orderGroup);
1 ✔
173
                }
1 ✔
174
                //save the new orders which do not have order groups
175
                for (Order o : encounter.getOrdersWithoutOrderGroups()) {
1 ✔
176
                        if (o.getOrderId() == null) {
1 ✔
177
                                Context.getOrderService().saveOrder(o, null);
1 ✔
178
                        }
179
                        else {
180
                                // Since orders are only saved via the OrderService if they are new, 
181
                                // we need to explicitly re-validate any existing orders
182
                                ValidateUtil.validate(o);
1 ✔
183
                        }
184
                }
1 ✔
185
                
186
                // save the Obs
187
                String changeMessage = Context.getMessageSourceService().getMessage("Obs.void.reason.default");
1 ✔
188
                ObsService os = Context.getObsService();
1 ✔
189
                List<Obs> obsToRemove = new ArrayList<>();
1 ✔
190
                List<Obs> obsToAdd = new ArrayList<>();
1 ✔
191
                for (Obs o : encounter.getObsAtTopLevel(true)) {
1 ✔
192
                        if (o.getId() == null) {
1 ✔
193
                                os.saveObs(o, null);
1 ✔
194
                        } else {
195
                                Obs newObs = os.saveObs(o, changeMessage);
1 ✔
196
                                //The logic in saveObs evicts the old obs instance, so we need to update the collection
197
                                //with the newly loaded and voided instance, apparently reloading the encounter
198
                                //didn't do the tick
199
                                obsToRemove.add(o);
1 ✔
200
                                obsToAdd.add(os.getObs(o.getId()));
1 ✔
201
                                obsToAdd.add(newObs);
1 ✔
202
                        }
203
                }
1 ✔
204

205
                removeGivenObsAndTheirGroupMembersFromEncounter(obsToRemove, encounter);
1 ✔
206
                addGivenObsAndTheirGroupMembersToEncounter(obsToAdd, encounter);
1 ✔
207
                
208
                // save the conditions
209
                encounter.getConditions().forEach(Context.getConditionService()::saveCondition);
1 ✔
210

211
                // save the allergies
212
                encounter.getAllergies().forEach(Context.getPatientService()::saveAllergy);
1 ✔
213

214
                // save the diagnoses
215
                encounter.getDiagnoses().stream().forEach(diagnosis -> {
1 ✔
216
                        diagnosis.setPatient(p);
1 ✔
217
                        diagnosis.setEncounter(encounter);
1 ✔
218
                });
1 ✔
219
                encounter.getDiagnoses().forEach(Context.getDiagnosisService()::save);
1 ✔
220
                
221
                return encounter;
1 ✔
222
        }
223
        
224
        /**
225
         * This method safely checks if authenticated user is not supposed to edit encounter of certain type
226
         * 
227
         * @param encounter encounter which is to be edited
228
         * @throws APIException if not allowed to edit encounter
229
         */
230
        private void failIfDeniedToEdit(Encounter encounter) throws APIException {
231
                Privilege missingPrivilege = getMissingEditPrivilege(encounter, null);
1 ✔
232
                if (missingPrivilege != null) {
1 ✔
233
                        throw new APIException("Encounter.error.privilege.required.edit", new Object[] { missingPrivilege });
1 ✔
234
                }
235
        }
1 ✔
236
        
237
        /**
238
         * This method assigns a visit to a new encounter using the registered visit assignment handler
239
         * 
240
         * @param encounter a new encounter
241
         */
242
        private void createVisitForNewEncounter(Encounter encounter) {
243
                if (encounter.getEncounterId() == null) {
1 ✔
244
                        
245
                        //Am using Context.getEncounterService().getActiveEncounterVisitHandler() instead of just
246
                        //getActiveEncounterVisitHandler() for modules which may want to AOP around this call.
247
                        EncounterVisitHandler encounterVisitHandler = Context.getEncounterService().getActiveEncounterVisitHandler();
1 ✔
248
                        if (encounterVisitHandler != null) {
1 ✔
249
                                encounterVisitHandler.beforeCreateEncounter(encounter);
1 ✔
250
                                
251
                                //If we have been assigned a new visit, persist it.
252
                                if (encounter.getVisit() != null && encounter.getVisit().getVisitId() == null) {
1 ✔
253
                                        Context.getVisitService().saveVisit(encounter.getVisit());
1 ✔
254
                                }
255
                        }
256
                }
257
        }
1 ✔
258
        
259
        /**
260
         * Checks for the type of privilege required
261
         * 
262
         * @param encounter which is to be edited
263
         */
264
        private boolean requirePrivilege(Encounter encounter) {
265
                boolean isNewEncounter = false;
1 ✔
266
                if (encounter.getEncounterId() == null) {
1 ✔
267
                        isNewEncounter = true;
1 ✔
268
                        Context.requirePrivilege(PrivilegeConstants.ADD_ENCOUNTERS);
1 ✔
269
                } else {
270
                        Context.requirePrivilege(PrivilegeConstants.EDIT_ENCOUNTERS);
1 ✔
271
                }
272
                return isNewEncounter;
1 ✔
273
        }
274

275
        /**
276
         * This method will remove given Collection of obs and their group members from encounter
277
         *
278
         * @param obsToRemove Collection of obs that need to be removed recursively
279
         * @param encounter the encounter from which the obs will be removed
280
         */
281
        private void removeGivenObsAndTheirGroupMembersFromEncounter(Collection<Obs> obsToRemove, Encounter encounter) {
282
                for (Obs o : obsToRemove) {
1 ✔
283
                        encounter.removeObs(o);
1 ✔
284
                        Set<Obs> groupMembers = o.getGroupMembers(true);
1 ✔
285
                        if (CollectionUtils.isNotEmpty(groupMembers)) {
1 ✔
286
                                removeGivenObsAndTheirGroupMembersFromEncounter(groupMembers, encounter);
1 ✔
287
                        }
288
                }
1 ✔
289
        }
1 ✔
290

291
        /**
292
         * This method will add given Collection of obs and their group members to encounter
293
         *
294
         * @param obsToAdd Collection of obs that need to be added recursively
295
         * @param encounter the encounter to which the obs will be added
296
         */
297
        private void addGivenObsAndTheirGroupMembersToEncounter(Collection<Obs> obsToAdd, Encounter encounter) {
298
                for (Obs o : obsToAdd) {
1 ✔
299
                        encounter.addObs(o);
1 ✔
300
                        Set<Obs> groupMembers = o.getGroupMembers(true);
1 ✔
301
                        if (CollectionUtils.isNotEmpty(groupMembers)) {
1 ✔
302
                                addGivenObsAndTheirGroupMembersToEncounter(groupMembers, encounter);
1 ✔
303
                        }
304
                }
1 ✔
305
        }
1 ✔
306

307
        /**
308
         * @see org.openmrs.api.EncounterService#getEncounter(java.lang.Integer)
309
         */
310
        @Override
311
        @Transactional(readOnly = true)
312
        public Encounter getEncounter(Integer encounterId) throws APIException {
313
                Encounter encounter = dao.getEncounter(encounterId);
1 ✔
314
                if (encounter == null) {
1 ✔
315
                        return null;
1 ✔
316
                } else if (canViewEncounter(encounter, null)) {
1 ✔
317
                        return encounter;
1 ✔
318
                } else {
319
                        throw new APIException("Encounter.error.privilege.required.view", new Object[] { encounter.getEncounterType()
1 ✔
320
                                .getViewPrivilege() });
1 ✔
321
                }
322
        }
323
        
324
        /**
325
         * @see org.openmrs.api.EncounterService#getEncountersByPatient(org.openmrs.Patient)
326
         */
327
        @Override
328
        @Transactional(readOnly = true)
329
        public List<Encounter> getEncountersByPatient(Patient patient) throws APIException {
330
                if (patient == null) {
1 ✔
331
                        throw new IllegalArgumentException("The 'patient' parameter is requred and cannot be null");
1 ✔
332
                }
333
                
334
                EncounterSearchCriteria encounterSearchCriteria = new EncounterSearchCriteriaBuilder().setPatient(patient)
1 ✔
335
                        .setIncludeVoided(false).createEncounterSearchCriteria();
1 ✔
336
                
337
                return Context.getEncounterService().getEncounters(encounterSearchCriteria);
1 ✔
338
        }
339
        
340
        /**
341
         * @see org.openmrs.api.EncounterService#getEncountersByPatient(String)
342
         */
343
        @Override
344
        @Transactional(readOnly = true)
345
        public List<Encounter> getEncountersByPatient(String query) throws APIException {
346
                
347
                return Context.getEncounterService().filterEncountersByViewPermissions(getEncountersByPatient(query, false), null);
×
348
        }
349
        
350
        /**
351
         * @see org.openmrs.api.EncounterService#getEncountersByPatientId(java.lang.Integer)
352
         */
353
        @Override
354
        @Transactional(readOnly = true)
355
        public List<Encounter> getEncountersByPatientId(Integer patientId) throws APIException {
356
                if (patientId == null) {
1 ✔
357
                        throw new IllegalArgumentException("The 'patientId' parameter is requred and cannot be null");
1 ✔
358
                }
359
                return Context.getEncounterService()
1 ✔
360
                        .filterEncountersByViewPermissions(dao.getEncountersByPatientId(patientId), null);
1 ✔
361
        }
362
        
363
        /**
364
         * @see org.openmrs.api.EncounterService#getEncountersByPatientIdentifier(java.lang.String)
365
         */
366
        @Override
367
        @Transactional(readOnly = true)
368
        public List<Encounter> getEncountersByPatientIdentifier(String identifier) throws APIException {
369
                if (identifier == null) {
1 ✔
370
                        throw new IllegalArgumentException("The 'identifier' parameter is required and cannot be null");
1 ✔
371
                }
372
                
373
                List<Encounter> encs = new ArrayList<>();
1 ✔
374
                for (Patient p : Context.getPatientService().getPatients(identifier, null, null, false)) {
1 ✔
375
                        encs.addAll(Context.getEncounterService().getEncountersByPatientId(p.getPatientId()));
1 ✔
376
                }
1 ✔
377
                return Context.getEncounterService().filterEncountersByViewPermissions(encs, null);
1 ✔
378
        }
379
        
380
        /**
381
         * @see org.openmrs.api.EncounterService#getEncounters(org.openmrs.Patient,
382
         *      org.openmrs.Location, java.util.Date, java.util.Date, java.util.Collection,
383
         *      java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection,
384
         *      boolean)
385
         * @deprecated As of 2.0, replaced by {@link #getEncounters(EncounterSearchCriteria)}
386
         */
387
        @Deprecated
388
        @Override
389
        @Transactional(readOnly = true)
390
        public List<Encounter> getEncounters(Patient who, Location loc, Date fromDate, Date toDate,
391
                                             Collection<Form> enteredViaForms, Collection<EncounterType> encounterTypes,
392
                                             Collection<Provider> providers, Collection<VisitType> visitTypes,
393
                                             Collection<Visit> visits, boolean includeVoided) {
394
                EncounterSearchCriteriaBuilder encounterSearchCriteriaBuilder = new EncounterSearchCriteriaBuilder().setPatient(who)
×
395
                        .setLocation(loc).setFromDate(fromDate).setToDate(toDate).setEnteredViaForms(enteredViaForms)
×
396
                        .setEncounterTypes(encounterTypes).setProviders(providers).setVisitTypes(visitTypes).setVisits(visits)
×
397
                        .setIncludeVoided(includeVoided);
×
398
                
399
                return getEncounters(encounterSearchCriteriaBuilder.createEncounterSearchCriteria());
×
400
        }
401
        
402
        /**
403
         * @see org.openmrs.api.EncounterService#getEncounters(org.openmrs.parameter.EncounterSearchCriteria)
404
         */
405
        @Override
406
        public List<Encounter> getEncounters(EncounterSearchCriteria encounterSearchCriteria) {
407
                // the second search parameter is null as it defaults to authenticated user from context
408
                return Context.getEncounterService().filterEncountersByViewPermissions(dao.getEncounters(encounterSearchCriteria),
1 ✔
409
                    null);
410
        }
411
        
412
        /**
413
         * @see org.openmrs.api.EncounterService#voidEncounter(org.openmrs.Encounter, java.lang.String)
414
         */
415
        @Override
416
        public Encounter voidEncounter(Encounter encounter, String reason) {
417
                
418
                // if authenticated user is not supposed to edit encounter of certain type
419
                Privilege missingPrivilege = getMissingEditPrivilege(encounter, null);
1 ✔
420
                if (missingPrivilege != null) {
1 ✔
421
                        throw new APIException("Encounter.error.privilege.required.void", new Object[] { missingPrivilege });
1 ✔
422
                }
423
                
424
                if (reason == null) {
1 ✔
425
                        throw new IllegalArgumentException("The argument 'reason' is required and so cannot be null");
×
426
                }
427
                
428
                ObsService os = Context.getObsService();
1 ✔
429
                for (Obs o : encounter.getObsAtTopLevel(false)) {
1 ✔
430
                        if (!o.getVoided()) {
1 ✔
431
                                os.voidObs(o, reason);
1 ✔
432
                        }
433
                }
1 ✔
434
                
435
                OrderService orderService = Context.getOrderService();
1 ✔
436
                for (Order o : encounter.getOrders()) {
1 ✔
437
                        // There is intentionally no voided check around this method call.  See TRUNK-5996.
438
                        orderService.voidOrder(o, reason);
1 ✔
439
                }
1 ✔
440
                
441
                encounter.setVoided(true);
1 ✔
442
                encounter.setVoidedBy(Context.getAuthenticatedUser());
1 ✔
443
                //we expect the dateVoided to be already set by AOP logic at this point unless this method was called within the API,
444
                //this ensures that original ParentVoidedDate and the dateVoided of associated objects will always match for the
445
                //unvoid handler to work
446
                if (encounter.getDateVoided() == null) {
1 ✔
447
                        encounter.setDateVoided(new Date());
1 ✔
448
                }
449
                encounter.setVoidReason(reason);
1 ✔
450

451
                for (EncounterProvider encounterProvider : encounter.getEncounterProviders()) {
1 ✔
452
                        if (!encounterProvider.getVoided()) {
1 ✔
453
                                encounterProvider.setVoided(true);
1 ✔
454
                                encounterProvider.setVoidedBy(encounter.getVoidedBy());
1 ✔
455
                                encounterProvider.setDateVoided(encounter.getDateVoided());
1 ✔
456
                                encounterProvider.setVoidReason(reason);
1 ✔
457
                        }
458
                }
1 ✔
459

460
                Context.getEncounterService().saveEncounter(encounter);
1 ✔
461
                return encounter;
1 ✔
462
        }
463
        
464
        /**
465
         * @see org.openmrs.api.EncounterService#unvoidEncounter(org.openmrs.Encounter)
466
         */
467
        @Override
468
        public Encounter unvoidEncounter(Encounter encounter) throws APIException {
469
                
470
                // if authenticated user is not supposed to edit encounter of certain type
471
                Privilege missingPrivilege = getMissingEditPrivilege(encounter, null);
1 ✔
472
                if (missingPrivilege != null) {
1 ✔
473
                        throw new APIException("Encounter.error.privilege.required.unvoid", new Object[] { missingPrivilege });
1 ✔
474
                }
475
                
476
                String voidReason = encounter.getVoidReason();
1 ✔
477
                if (voidReason == null) {
1 ✔
478
                        voidReason = "";
1 ✔
479
                }
480
                
481
                ObsService os = Context.getObsService();
1 ✔
482
                for (Obs o : encounter.getObsAtTopLevel(true)) {
1 ✔
483
                        if (voidReason.equals(o.getVoidReason())) {
1 ✔
484
                                os.unvoidObs(o);
×
485
                        }
486
                }
1 ✔
487
                
488
                OrderService orderService = Context.getOrderService();
1 ✔
489
                for (Order o : encounter.getOrders()) {
1 ✔
490
                        if (voidReason.equals(o.getVoidReason())) {
1 ✔
491
                                orderService.unvoidOrder(o);
×
492
                        }
493
                }
1 ✔
494
                
495
                encounter.setVoided(false);
1 ✔
496
                encounter.setVoidedBy(null);
1 ✔
497
                encounter.setDateVoided(null);
1 ✔
498
                encounter.setVoidReason(null);
1 ✔
499
                Context.getEncounterService().saveEncounter(encounter);
1 ✔
500
                return encounter;
1 ✔
501
        }
502
        
503
        /**
504
         * @see org.openmrs.api.EncounterService#purgeEncounter(org.openmrs.Encounter)
505
         */
506
        @Override
507
        public void purgeEncounter(Encounter encounter) throws APIException {
508
                // if authenticated user is not supposed to edit encounter of certain type
509
                Privilege missingPrivilege = getMissingEditPrivilege(encounter, null);
1 ✔
510
                if (missingPrivilege != null) {
1 ✔
511
                        throw new APIException("Encounter.error.privilege.required.purge", new Object[] { missingPrivilege });
1 ✔
512
                }
513
                dao.deleteEncounter(encounter);
1 ✔
514
        }
1 ✔
515
        
516
        /**
517
         * @see org.openmrs.api.EncounterService#purgeEncounter(Encounter, boolean)
518
         */
519
        @Override
520
        public void purgeEncounter(Encounter encounter, boolean cascade) throws APIException {
521
                
522
                // if authenticated user is not supposed to edit encounter of certain type
523
                Privilege missingPrivilege = getMissingEditPrivilege(encounter, null);
1 ✔
524
                if (missingPrivilege != null) {
1 ✔
525
                        throw new APIException("Encounter.error.privilege.required.purge", new Object[] { missingPrivilege });
1 ✔
526
                }
527
                
528
                if (cascade) {
1 ✔
529
                        ObsService obsService = Context.getObsService();
1 ✔
530
                        List<Encounter> justThisEncounter = new ArrayList<>();
1 ✔
531
                        justThisEncounter.add(encounter);
1 ✔
532
                        List<Obs> observations = new ArrayList<>(
1 ✔
533
                                        obsService.getObservations(null, justThisEncounter, null, null, null, null, null, null,
1 ✔
534
                                                        null, null, null, true));
535
                        for (Obs o : observations) {
1 ✔
536
                                obsService.purgeObs(o);
1 ✔
537
                        }
1 ✔
538
                        Set<Order> orders = encounter.getOrders();
1 ✔
539
                        for (Order o : orders) {
1 ✔
540
                                Context.getOrderService().purgeOrder(o);
1 ✔
541
                        }
1 ✔
542
                }
543
                Context.getEncounterService().purgeEncounter(encounter);
1 ✔
544
        }
1 ✔
545
        
546
        /**
547
         * @see org.openmrs.api.EncounterService#saveEncounterType(org.openmrs.EncounterType)
548
         */
549
        @Override
550
        public EncounterType saveEncounterType(EncounterType encounterType) {
551
                //make sure the user has not turned off encounter types editing
552
                Context.getEncounterService().checkIfEncounterTypesAreLocked();
1 ✔
553
                
554
                dao.saveEncounterType(encounterType);
1 ✔
555
                return encounterType;
1 ✔
556
        }
557
        
558
        /**
559
         * @see org.openmrs.api.EncounterService#getEncounterType(java.lang.Integer)
560
         */
561
        @Override
562
        @Transactional(readOnly = true)
563
        public EncounterType getEncounterType(Integer encounterTypeId) throws APIException {
564
                return dao.getEncounterType(encounterTypeId);
1 ✔
565
        }
566
        
567
        /**
568
         * @see org.openmrs.api.EncounterService#getEncounterType(java.lang.String)
569
         */
570
        @Override
571
        @Transactional(readOnly = true)
572
        public EncounterType getEncounterType(String name) throws APIException {
573
                return dao.getEncounterType(name);
1 ✔
574
        }
575
        
576
        /**
577
         * @see org.openmrs.api.EncounterService#getAllEncounterTypes()
578
         */
579
        @Override
580
        @Transactional(readOnly = true)
581
        public List<EncounterType> getAllEncounterTypes() throws APIException {
582
                return dao.getAllEncounterTypes(true);
1 ✔
583
        }
584
        
585
        /**
586
         * @see org.openmrs.api.EncounterService#getAllEncounterTypes(boolean)
587
         */
588
        @Override
589
        @Transactional(readOnly = true)
590
        public List<EncounterType> getAllEncounterTypes(boolean includeRetired) throws APIException {
591
                return dao.getAllEncounterTypes(includeRetired);
1 ✔
592
        }
593
        
594
        /**
595
         * @see org.openmrs.api.EncounterService#findEncounterTypes(java.lang.String)
596
         */
597
        @Override
598
        @Transactional(readOnly = true)
599
        public List<EncounterType> findEncounterTypes(String name) throws APIException {
600
                return dao.findEncounterTypes(name);
1 ✔
601
        }
602
        
603
        /**
604
         * @see org.openmrs.api.EncounterService#retireEncounterType(EncounterType, String)
605
         */
606
        @Override
607
        public EncounterType retireEncounterType(EncounterType encounterType, String reason) throws APIException {
608
                if (reason == null) {
1 ✔
609
                        throw new IllegalArgumentException("The 'reason' for retiring is required");
1 ✔
610
                }
611
                
612
                //make sure the user has not turned off encounter types editing
613
                Context.getEncounterService().checkIfEncounterTypesAreLocked();
1 ✔
614
                
615
                encounterType.setRetired(true);
1 ✔
616
                encounterType.setRetireReason(reason);
1 ✔
617
                return Context.getEncounterService().saveEncounterType(encounterType);
1 ✔
618
        }
619
        
620
        /**
621
         * @see org.openmrs.api.EncounterService#unretireEncounterType(org.openmrs.EncounterType)
622
         */
623
        @Override
624
        public EncounterType unretireEncounterType(EncounterType encounterType) throws APIException {
625
                Context.getEncounterService().checkIfEncounterTypesAreLocked();
1 ✔
626
                
627
                encounterType.setRetired(false);
1 ✔
628
                return Context.getEncounterService().saveEncounterType(encounterType);
1 ✔
629
        }
630
        
631
        /**
632
         * @see org.openmrs.api.EncounterService#purgeEncounterType(org.openmrs.EncounterType)
633
         */
634
        @Override
635
        public void purgeEncounterType(EncounterType encounterType) throws APIException {
636
                //make sure the user has not turned off encounter types editing
637
                Context.getEncounterService().checkIfEncounterTypesAreLocked();
1 ✔
638
                
639
                dao.deleteEncounterType(encounterType);
1 ✔
640
        }
1 ✔
641
        
642
        /**
643
         * @see org.openmrs.api.EncounterService#getEncounterByUuid(java.lang.String)
644
         */
645
        @Override
646
        @Transactional(readOnly = true)
647
        public Encounter getEncounterByUuid(String uuid) throws APIException {
648
                Encounter encounter = dao.getEncounterByUuid(uuid);
1 ✔
649
                if (encounter == null) {
1 ✔
650
                        return null;
1 ✔
651
                }
652
                EncounterType encounterType = encounter.getEncounterType();
1 ✔
653
                if (encounterType != null && !canViewEncounter(encounter, null)) {
1 ✔
654
                        throw new APIException("Encounter.error.privilege.required.view",
1 ✔
655
                                new Object[] { encounterType.getViewPrivilege() });
1 ✔
656
                }
657
                return encounter;
1 ✔
658
        }
659
        
660
        /**
661
         * @see org.openmrs.api.EncounterService#getEncounterTypeByUuid(java.lang.String)
662
         */
663
        @Override
664
        @Transactional(readOnly = true)
665
        public EncounterType getEncounterTypeByUuid(String uuid) throws APIException {
666
                return dao.getEncounterTypeByUuid(uuid);
1 ✔
667
        }
668
        
669
        /**
670
         * @see org.openmrs.api.EncounterService#getAllEncounters(org.openmrs.Cohort)
671
         */
672
        @Override
673
        @Transactional(readOnly = true)
674
        public Map<Integer, List<Encounter>> getAllEncounters(Cohort patients) {
675
                Map<Integer, List<Encounter>> encountersByPatient = dao.getAllEncounters(patients);
1 ✔
676
                User user = Context.getAuthenticatedUser();
1 ✔
677
                for (List<Encounter> encounters : encountersByPatient.values()) {
1 ✔
678
                        removeEncountersUserCannotView(encounters, user);
1 ✔
679
                }
1 ✔
680
                encountersByPatient.values().removeIf(List::isEmpty);
1 ✔
681
                return encountersByPatient;
1 ✔
682
        }
683
        
684
        /**
685
         * @see org.openmrs.api.EncounterService#getEncounters(java.lang.String, java.lang.Integer,
686
         *      java.lang.Integer, boolean)
687
         */
688
        @Override
689
        @Transactional(readOnly = true)
690
        public List<Encounter> getEncounters(String query, Integer start, Integer length, boolean includeVoided)
691
            throws APIException {
692
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
693
                    dao.getEncounters(query, null, start, length, includeVoided), null);
1 ✔
694
        }
695
        
696
        /**
697
         * @see org.openmrs.api.EncounterService#getEncounters(java.lang.String, java.lang.Integer,
698
         *      java.lang.Integer, java.lang.Integer, boolean)
699
         */
700
        @Override
701
        @Transactional(readOnly = true)
702
        public List<Encounter> getEncounters(String query, Integer patientId, Integer start, Integer length,
703
                                             boolean includeVoided) throws APIException {
704
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
705
                    dao.getEncounters(query, patientId, start, length, includeVoided), null);
1 ✔
706
        }
707
        
708
        /**
709
         * @see org.openmrs.api.EncounterService#getCountOfEncounters(java.lang.String, boolean)
710
         */
711
        @Override
712
        @Transactional(readOnly = true)
713
        public Integer getCountOfEncounters(String query, boolean includeVoided) {
714
                return OpenmrsUtil.convertToInteger(dao.getCountOfEncounters(query, null, includeVoided));
1 ✔
715
        }
716
        
717
        /**
718
         * @see EncounterService#getEncountersByVisit(Visit, boolean)
719
         */
720
        @Override
721
        @Transactional(readOnly = true)
722
        public List<Encounter> getEncountersByVisit(Visit visit, boolean includeVoided) {
723
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
724
                    dao.getEncountersByVisit(visit, includeVoided), null);
1 ✔
725
        }
726
        
727
        @Override
728
        @Transactional(readOnly = true)
729
        public List<EncounterVisitHandler> getEncounterVisitHandlers() {
730
                // copied because HandlerUtil hands out the shared, unmodifiable list it caches, while this method
731
                // has promised callers an ordinary List since 1.9. The path is cold enough for the copy not to
732
                // matter
733
                return new ArrayList<>(HandlerUtil.getHandlersForType(EncounterVisitHandler.class, null));
1 ✔
734
        }
735
        
736
        /**
737
         * @see org.openmrs.api.EncounterService#getActiveEncounterVisitHandler()
738
         */
739
        @Override
740
        @Transactional(readOnly = true)
741
        public EncounterVisitHandler getActiveEncounterVisitHandler() throws APIException {
742
                
743
                String handlerGlobalValue = Context.getAdministrationService().getGlobalProperty(
1 ✔
744
                    OpenmrsConstants.GP_VISIT_ASSIGNMENT_HANDLER, null);
745
                
746
                if (StringUtils.isBlank(handlerGlobalValue)) {
1 ✔
747
                        return null;
1 ✔
748
                }
749
                
750
                EncounterVisitHandler handler;
751
                
752
                // convention = [NamePrefix:beanName] or [className]
753
                String namePrefix = OpenmrsConstants.REGISTERED_COMPONENT_NAME_PREFIX;
1 ✔
754
                
755
                if (handlerGlobalValue.startsWith(namePrefix)) {
1 ✔
756
                        String beanName = handlerGlobalValue.substring(namePrefix.length());
1 ✔
757
                        
758
                        handler = Context.getRegisteredComponent(beanName, EncounterVisitHandler.class);
1 ✔
759
                } else {
1 ✔
760
                        Object instance;
761
                        
762
                        try {
763
                                instance = OpenmrsClassLoader.getInstance().loadClass(handlerGlobalValue).newInstance();
1 ✔
764
                        }
765
                        catch (Exception ex) {
×
766
                                throw new APIException("failed.instantiate.assignment.handler", new Object[] { handlerGlobalValue }, ex);
×
767
                        }
1 ✔
768
                        
769
                        if (instance instanceof EncounterVisitHandler) {
1 ✔
770
                                handler = (EncounterVisitHandler) instance;
1 ✔
771
                        } else {
772
                                throw new APIException("assignment.handler.should.implement.EncounterVisitHandler", (Object[]) null);
×
773
                        }
774
                }
775
                
776
                return handler;
1 ✔
777
        }
778
        
779
        /**
780
         * @see org.openmrs.api.EncounterService#saveEncounterRole(org.openmrs.EncounterRole)
781
         */
782
        @Override
783
        public EncounterRole saveEncounterRole(EncounterRole encounterRole) throws APIException {
784
                dao.saveEncounterRole(encounterRole);
1 ✔
785
                return encounterRole;
1 ✔
786
        }
787
        
788
        /**
789
         * @see org.openmrs.api.EncounterService#getEncounterRole(Integer)
790
         */
791
        @Override
792
        @Transactional(readOnly = true)
793
        public EncounterRole getEncounterRole(Integer encounterRoleId) throws APIException {
794
                return dao.getEncounterRole(encounterRoleId);
1 ✔
795
        }
796
        
797
        /**
798
         * @see org.openmrs.api.EncounterService#purgeEncounterRole(org.openmrs.EncounterRole)
799
         */
800
        @Override
801
        public void purgeEncounterRole(EncounterRole encounterRole) throws APIException {
802
                dao.deleteEncounterRole(encounterRole);
1 ✔
803
        }
1 ✔
804
        
805
        /**
806
         * @see org.openmrs.api.EncounterService#getAllEncounterRoles(boolean)
807
         */
808
        @Override
809
        @Transactional(readOnly = true)
810
        public List<EncounterRole> getAllEncounterRoles(boolean includeRetired) {
811
                return dao.getAllEncounterRoles(includeRetired);
1 ✔
812
        }
813
        
814
        /**
815
         * @see org.openmrs.api.EncounterService#getEncounterRoleByUuid(String)
816
         */
817
        @Override
818
        @Transactional(readOnly = true)
819
        public EncounterRole getEncounterRoleByUuid(String uuid) throws APIException {
820
                return dao.getEncounterRoleByUuid(uuid);
1 ✔
821
        }
822
        
823
        /**
824
         * @see org.openmrs.api.EncounterService#getEncounterRoleByName(String)
825
         */
826
        @Override
827
        public EncounterRole getEncounterRoleByName(String name) {
828
                return dao.getEncounterRoleByName(name);
1 ✔
829
        }
830
        
831
        /**
832
         * @see org.openmrs.api.EncounterService#retireEncounterRole(org.openmrs.EncounterRole, String)
833
         */
834
        @Override
835
        public EncounterRole retireEncounterRole(EncounterRole encounterRole, String reason) throws APIException {
836
                if (reason == null) {
1 ✔
837
                        throw new IllegalArgumentException("The 'reason' for retiring is required");
1 ✔
838
                }
839
                return Context.getEncounterService().saveEncounterRole(encounterRole);
1 ✔
840
        }
841
        
842
        /**
843
         * @see org.openmrs.api.EncounterService#unretireEncounterRole(org.openmrs.EncounterRole)
844
         */
845
        @Override
846
        public EncounterRole unretireEncounterRole(EncounterRole encounterRole) throws APIException {
847
                return Context.getEncounterService().saveEncounterRole(encounterRole);
1 ✔
848
        }
849
        
850
        /**
851
         * @see org.openmrs.api.EncounterService#getEncountersNotAssignedToAnyVisit(org.openmrs.Patient)
852
         */
853
        @Override
854
        @Transactional(readOnly = true)
855
        public List<Encounter> getEncountersNotAssignedToAnyVisit(Patient patient) throws APIException {
856
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
857
                    dao.getEncountersNotAssignedToAnyVisit(patient), null);
1 ✔
858
        }
859
        
860
        /**
861
         * @see org.openmrs.api.EncounterService#getEncountersByVisitsAndPatient(org.openmrs.Patient,
862
         *      boolean, java.lang.String, java.lang.Integer, java.lang.Integer)
863
         */
864
        @Override
865
        @Transactional(readOnly = true)
866
        public List<Encounter> getEncountersByVisitsAndPatient(Patient patient, boolean includeVoided, String query,
867
                                                               Integer start, Integer length) throws APIException {
868
                return Context.getEncounterService().filterEncountersByViewPermissions(
1 ✔
869
                    dao.getEncountersByVisitsAndPatient(patient, includeVoided, query, start, length), null);
1 ✔
870
        }
871
        
872
        /**
873
         * @see org.openmrs.api.EncounterService#getEncountersByVisitsAndPatientCount(org.openmrs.Patient,
874
         *      boolean, java.lang.String)
875
         */
876
        @Override
877
        @Transactional(readOnly = true)
878
        public Integer getEncountersByVisitsAndPatientCount(Patient patient, boolean includeVoided, String query)
879
            throws APIException {
880
                return dao.getEncountersByVisitsAndPatientCount(patient, includeVoided, query);
1 ✔
881
        }
882
        
883
        /**
884
         * @see org.openmrs.api.EncounterService#filterEncountersByViewPermissions(java.util.List,
885
         *      org.openmrs.User)
886
         */
887
        @Override
888
        @Transactional(readOnly = true)
889
        public List<Encounter> filterEncountersByViewPermissions(List<Encounter> encounters, User user) {
890
                if (encounters != null) {
1 ✔
891
                        // if user is not specified then use authenticated user from context by default
892
                        if (user == null) {
1 ✔
893
                                user = Context.getAuthenticatedUser();
1 ✔
894
                        }
895
                        removeEncountersUserCannotView(encounters, user);
1 ✔
896
                }
897
                return encounters;
1 ✔
898
        }
899
        
900
        /**
901
         * Removes from the given list the encounters whose type has a view privilege the user lacks
902
         *
903
         * @param encounters the encounters to filter in place
904
         * @param user the user to check
905
         */
906
        private void removeEncountersUserCannotView(List<Encounter> encounters, User user) {
907
                for (Iterator<Encounter> iterator = encounters.iterator(); iterator.hasNext();) {
1 ✔
908
                        Encounter encounter = iterator.next();
1 ✔
909
                        // determine whether it's need to include this encounter into result or not
910
                        // as it can be not accessed by current user due to permissions lack
911
                        EncounterType et = encounter.getEncounterType();
1 ✔
912
                        if (et != null && !userHasEncounterPrivilege(et.getViewPrivilege(), user)) {
1 ✔
913
                                // exclude this encounter from result
914
                                iterator.remove();
1 ✔
915
                        }
916
                }
1 ✔
917
        }
1 ✔
918

919
        /**
920
         * @see org.openmrs.api.EncounterService#canViewAllEncounterTypes(org.openmrs.User)
921
         */
922
        @Override
923
        @Transactional(readOnly = true)
924
        public boolean canViewAllEncounterTypes(User subject) {
925
                boolean canView = Boolean.TRUE;
1 ✔
926
                for (EncounterType et : Context.getEncounterService().getAllEncounterTypes()) {
1 ✔
927
                        if (!userHasEncounterPrivilege(et.getViewPrivilege(), subject)) {
1 ✔
928
                                canView = Boolean.FALSE;
1 ✔
929
                                break;
1 ✔
930
                        }
931
                }
1 ✔
932
                return canView;
1 ✔
933
        }
934
        
935
        /**
936
         * @see org.openmrs.api.EncounterService#canEditAllEncounterTypes(org.openmrs.User)
937
         */
938
        @Override
939
        @Transactional(readOnly = true)
940
        public boolean canEditAllEncounterTypes(User subject) {
941
                boolean canEdit = Boolean.TRUE;
1 ✔
942
                for (EncounterType et : Context.getEncounterService().getAllEncounterTypes()) {
1 ✔
943
                        if (!userHasEncounterPrivilege(et.getEditPrivilege(), subject)) {
1 ✔
944
                                canEdit = Boolean.FALSE;
1 ✔
945
                                break;
1 ✔
946
                        }
947
                }
1 ✔
948
                return canEdit;
1 ✔
949
        }
950
        
951
        /**
952
         * @see org.openmrs.api.EncounterService#canEditEncounter(org.openmrs.Encounter,
953
         *      org.openmrs.User)
954
         */
955
        @Override
956
        public boolean canEditEncounter(Encounter encounter, User user) {
957
                // if passed in encounter is null raise an exception
958
                if (encounter == null) {
1 ✔
959
                        throw new IllegalArgumentException("The encounter argument can not be null");
1 ✔
960
                }
961
                // if user is not specified, then use authenticated user from context by default
962
                if (user == null) {
1 ✔
UNCOV
963
                        user = Context.getAuthenticatedUser();
×
964
                }
965

966
                return getMissingEditPrivilege(encounter, user) == null;
1 ✔
967
        }
968

969
        /**
970
         * Finds the edit privilege the user lacks for the given encounter, checking the encounter type
971
         * stored in the database as well as the one set on the given object
972
         *
973
         * @param encounter the encounter to be edited
974
         * @param user the user to check, or null for the authenticated user
975
         * @return the missing edit privilege, or null if the user is allowed to edit the encounter
976
         */
977
        private Privilege getMissingEditPrivilege(Encounter encounter, User user) {
978
                if (user == null) {
1 ✔
979
                        user = Context.getAuthenticatedUser();
1 ✔
980
                }
981
                if (encounter.getEncounterId() != null) {
1 ✔
982
                        Privilege missingPrivilege = getMissingEditPrivilege(dao.getSavedEncounterType(encounter), user);
1 ✔
983
                        if (missingPrivilege != null) {
1 ✔
984
                                return missingPrivilege;
1 ✔
985
                        }
986
                }
987
                return getMissingEditPrivilege(encounter.getEncounterType(), user);
1 ✔
988
        }
989

990
        /**
991
         * @param encounterType the encounter type whose edit privilege to check, may be null
992
         * @param user the user to check
993
         * @return the edit privilege of the type if the user lacks it, otherwise null
994
         */
995
        private Privilege getMissingEditPrivilege(EncounterType encounterType, User user) {
996
                // since we restrict by encounter type, if it does not exist, then anyone is allowed to edit the encounter
997
                if (encounterType == null || userHasEncounterPrivilege(encounterType.getEditPrivilege(), user)) {
1 ✔
998
                        return null;
1 ✔
999
                }
1000
                return encounterType.getEditPrivilege();
1 ✔
1001
        }
1002
        
1003
        /**
1004
         * @see org.openmrs.api.EncounterService#canViewEncounter(org.openmrs.Encounter,
1005
         *      org.openmrs.User)
1006
         */
1007
        @Override
1008
        public boolean canViewEncounter(Encounter encounter, User user) {
1009
                // if passed in encounter is null raise an exception
1010
                if (encounter == null) {
1 ✔
1011
                        throw new IllegalArgumentException("The encounter argument can not be null");
1 ✔
1012
                }
1013
                // since we restrict by encounter type, if it does not exist, then anyone is allowed to view the encounter
1014
                if (encounter.getEncounterType() == null) {
1 ✔
1015
                        return Boolean.TRUE;
×
1016
                }
1017
                // if user is not specified, then use authenticated user from context by default
1018
                if (user == null) {
1 ✔
1019
                        user = Context.getAuthenticatedUser();
1 ✔
1020
                }
1021
                
1022
                return userHasEncounterPrivilege(encounter.getEncounterType().getViewPrivilege(), user);
1 ✔
1023
        }
1024
        
1025
        /**
1026
         * Convenient method that safely checks if user has given encounter privilege
1027
         * 
1028
         * @param privilege the privilege to test
1029
         * @param user the user instance to check if it has given privilege
1030
         * @return true if given user has specified privilege
1031
         */
1032
        private boolean userHasEncounterPrivilege(Privilege privilege, User user) {
1033
                //If the encounter privilege is null, everyone can see and edit the encounter.
1034
                if (privilege == null) {
1 ✔
1035
                        return true;
1 ✔
1036
                }
1037
                if (user == null || user.equals(Context.getAuthenticatedUser())) {
1 ✔
1038
                        return Context.hasPrivilege(privilege.getPrivilege(), false);
1 ✔
1039
                }
1040
                return user.hasPrivilege(privilege.getPrivilege());
1 ✔
1041
        }
1042
        
1043
        /**
1044
         * @see org.openmrs.api.EncounterService#checkIfEncounterTypesAreLocked()
1045
         */
1046
        @Override
1047
        @Transactional(readOnly = true)
1048
        public void checkIfEncounterTypesAreLocked() {
1049
                String locked = Context.getAdministrationService().getGlobalProperty(
1 ✔
1050
                    OpenmrsConstants.GLOBAL_PROPERTY_ENCOUNTER_TYPES_LOCKED, "false");
1051
                if (Boolean.valueOf(locked)) {
1 ✔
1052
                        throw new EncounterTypeLockedException();
1 ✔
1053
                }
1054
        }
1 ✔
1055
        
1056
        /**
1057
         * @see org.openmrs.api.EncounterService#getEncounterRolesByName(String)
1058
         */
1059
        
1060
        @Override
1061
        public List<EncounterRole> getEncounterRolesByName(String name) {
1062
                return dao.getEncounterRolesByName(name);
1 ✔
1063
        }
1064
        
1065
        @Override
1066
        public Encounter transferEncounter(Encounter encounter, Patient patient) {
1067
                Encounter encounterCopy = encounter.copyAndAssignToAnotherPatient(patient);
1 ✔
1068
                
1069
                voidEncounter(encounter, "transfer to patient: id = " + patient.getId());
1 ✔
1070
                
1071
                //void visit if voided encounter is the only one
1072
                Visit visit = encounter.getVisit();
1 ✔
1073
                if (visit != null && visit.getEncounters().size() == 1) {
1 ✔
1074
                        Context.getVisitService().voidVisit(visit, "Visit does not contain non-voided encounters");
1 ✔
1075
                }
1076
                
1077
                return saveEncounter(encounterCopy);
1 ✔
1078
        }
1079
}
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