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

openmrs / openmrs-core / 41717837886

03 Nov 2026 08:59PM UTC coverage: 68.971% (+0.05%) from 68.922%
41717837886

push

github

ibacher
TRUNK-6863: Remove the generation token before clearing a node's cache locally (#6791)

1 of 1 new or added line in 1 file covered. (100.0%)

413 existing lines in 30 files now uncovered.

26445 of 38342 relevant lines covered (68.97%)

0.69 hits per line

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

92.55
/api/src/main/java/org/openmrs/Encounter.java
1
/**
2
 * This Source Code Form is subject to the terms of the Mozilla Public License,
3
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
4
 * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5
 * the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6
 *
7
 * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8
 * graphic logo is a trademark of OpenMRS Inc.
9
 */
10
package org.openmrs;
11

12
import java.util.ArrayDeque;
13
import java.util.ArrayList;
14
import java.util.Date;
15
import java.util.Deque;
16
import java.util.HashMap;
17
import java.util.LinkedHashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Optional;
21
import java.util.Set;
22
import java.util.stream.Collectors;
23

24
import javax.persistence.Access;
25
import javax.persistence.AccessType;
26
import javax.persistence.CascadeType;
27
import javax.persistence.Column;
28
import javax.persistence.Entity;
29
import javax.persistence.GeneratedValue;
30
import javax.persistence.GenerationType;
31
import javax.persistence.Id;
32
import javax.persistence.JoinColumn;
33
import javax.persistence.ManyToOne;
34
import javax.persistence.OneToMany;
35
import javax.persistence.OrderBy;
36
import javax.persistence.Table;
37

38
import org.hibernate.annotations.BatchSize;
39
import org.hibernate.annotations.Cascade;
40
import org.hibernate.envers.Audited;
41
import org.openmrs.annotation.AllowDirectAccess;
42
import org.openmrs.api.context.Context;
43
import org.openmrs.util.CollectionUtil;
44

45
/**
46
 * An Encounter represents one visit or interaction of a patient with a healthcare worker. Every
47
 * encounter can have 0 to n Observations associated with it Every encounter can have 0 to n Orders
48
 * associated with it The patientId attribute should be equal to patient.patientId and is only
49
 * included this second time for performance increases on bulk calls.
50
 *
51
 * @see Obs
52
 * @see Order
53
 */
54
@Entity
55
@Table(name = "encounter")
56
@BatchSize(size = 25)
57
@Audited
58
public class Encounter extends BaseChangeableOpenmrsData {
59
        
60
        public static final long serialVersionUID = 2L;
61
        
62
        // Fields
63
        @Id
64
        @GeneratedValue(strategy = GenerationType.IDENTITY)
65
        @Column(name = "encounter_id")
66
        private Integer encounterId;
67
        
68
        @Column(name = "encounter_datetime", nullable = false, length = 19)
69
        private Date encounterDatetime;
70
        
71
        @ManyToOne(optional = false)
72
        @JoinColumn(name = "patient_id")
73
        private Patient patient;
74
        
75
        @ManyToOne
76
        @JoinColumn(name = "location_id")
77
        private Location location;
78
        
79
        @ManyToOne
80
        @JoinColumn(name = "form_id")
81
        private Form form;
82
        
83
        @ManyToOne(optional = false)
84
        @JoinColumn(name = "encounter_type")
85
        private EncounterType encounterType;
86
        
87
        @OneToMany(mappedBy = "encounter")
88
        // getOrderGroups() derives order group positions from iteration order
89
        @OrderBy("orderId")
90
        private Set<Order> orders;
91
        
92
        @OneToMany(mappedBy = "encounter")
93
        private Set<Diagnosis> diagnoses;
94
        
95
        @OneToMany(mappedBy = "encounter")
96
        private Set<Condition> conditions;
97
        
98
        @OneToMany(mappedBy = "encounter")
99
        @Access(AccessType.FIELD)
100
        @OrderBy("concept_id")
101
        @BatchSize(size = 25)
102
        @AllowDirectAccess
103
        private Set<Obs> obs;
104
        
105
        @ManyToOne
106
        @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
107
        @JoinColumn(name = "visit_id")
108
        private Visit visit;
109
        
110
        @OneToMany(mappedBy = "encounter", cascade = CascadeType.ALL)
1 ✔
111
        @OrderBy("provider_id")
112
        private Set<EncounterProvider> encounterProviders = new LinkedHashSet<>();
113
        
114
        @OneToMany(mappedBy = "encounter")
115
        private Set<Allergy> allergies;
116
        
117
        // Constructors
118
        
119
        /** default constructor */
120
        public Encounter() {
1 ✔
121
        }
1 ✔
122
        
123
        /**
124
         * @param encounterId
125
         * <strong>Should</strong> set encounter id
126
         */
127
        public Encounter(Integer encounterId) {
1 ✔
128
                this.encounterId = encounterId;
1 ✔
129
        }
1 ✔
130
        
131
        // Property accessors
132
        
133
        /**
134
         * @return Returns the encounterDatetime.
135
         */
136
        public Date getEncounterDatetime() {
137
                return encounterDatetime;
1 ✔
138
        }
139
        
140
        /**
141
         * @param encounterDatetime The encounterDatetime to set.
142
         */
143
        public void setEncounterDatetime(Date encounterDatetime) {
144
                this.encounterDatetime = encounterDatetime;
1 ✔
145
        }
1 ✔
146
        
147
        /**
148
         * @return Returns the encounterId.
149
         */
150
        public Integer getEncounterId() {
151
                return encounterId;
1 ✔
152
        }
153
        
154
        /**
155
         * @param encounterId The encounterId to set.
156
         */
157
        public void setEncounterId(Integer encounterId) {
UNCOV
158
                this.encounterId = encounterId;
×
UNCOV
159
        }
×
160
        
161
        /**
162
         * @return Returns the encounterType.
163
         */
164
        public EncounterType getEncounterType() {
165
                return encounterType;
1 ✔
166
        }
167
        
168
        /**
169
         * @param encounterType The encounterType to set.
170
         */
171
        public void setEncounterType(EncounterType encounterType) {
172
                this.encounterType = encounterType;
1 ✔
173
        }
1 ✔
174
        
175
        /**
176
         * @return Returns the location.
177
         */
178
        public Location getLocation() {
179
                return location;
1 ✔
180
        }
181
        
182
        /**
183
         * @param location The location to set.
184
         */
185
        public void setLocation(Location location) {
186
                this.location = location;
1 ✔
187
        }
1 ✔
188
        
189
        /**
190
         * @return Returns a Set&lt;Obs&gt; of all non-voided, non-obsGroup children Obs of this
191
         *         Encounter
192
         * <strong>Should</strong> not return null with null obs set
193
         * <strong>Should</strong> get obs
194
         * <strong>Should</strong> not get voided obs
195
         * <strong>Should</strong> only get child obs
196
         * <strong>Should</strong> not get child obs if child also on encounter
197
         * <strong>Should</strong> get both child and parent obs after removing child from parent grouping
198
         * <strong>Should</strong> get obs with two levels of hierarchy
199
         * <strong>Should</strong> get obs with three levels of hierarchy
200
         * <strong>Should</strong> not get voided obs with three layers of hierarchy
201
         */
202
        public Set<Obs> getObs() {
203
                Set<Obs> ret = new LinkedHashSet<>();
1 ✔
204
                
205
                if (this.obs != null) {
1 ✔
206
                        for (Obs o : this.obs) {
1 ✔
207
                                ret.addAll(getObsLeaves(o));
1 ✔
208
                        }
1 ✔
209
                }
210
                
211
                return ret;
1 ✔
212
        }
213
        
214
        /**
215
         * Convenience method to recursively get all leaf obs of this encounter. This method goes down
216
         * into each obs and adds all non-grouping obs to the return list
217
         *
218
         * @param obsParent current obs to loop over
219
         * @return list of leaf obs
220
         */
221
        private List<Obs> getObsLeaves(Obs obsParent) {
222
                List<Obs> leaves = new ArrayList<>();
1 ✔
223
                
224
                if (obsParent.hasGroupMembers()) {
1 ✔
225
                        for (Obs child : obsParent.getGroupMembers()) {
1 ✔
226
                                if (!child.getVoided()) {
1 ✔
227
                                        if (!child.isObsGrouping()) {
1 ✔
228
                                                leaves.add(child);
1 ✔
229
                                        } else {
230
                                                // recurse if this is a grouping obs
231
                                                leaves.addAll(getObsLeaves(child));
1 ✔
232
                                        }
233
                                }
234
                        }
1 ✔
235
                } else if (!obsParent.getVoided()) {
1 ✔
236
                        leaves.add(obsParent);
1 ✔
237
                }
238
                
239
                return leaves;
1 ✔
240
        }
241

242
        /**
243
         * Convenience method to recursively get all leaf obs for a given parent obs.
244
         * @param obsParent indicates the parent Obs
245
         * @param includedVoided indicates whether or not to include voided Obs
246
         * @return a Set of all leaves Obs including the parent Obs
247
         * @since 2.2.1
248
         */
249
        private Set<Obs> getFlattenedObsLeaves(Obs obsParent, boolean includedVoided) {
250
                Set<Obs> leaves = new LinkedHashSet<>();
1 ✔
251

252
                if (includedVoided || (!obsParent.getVoided())) {
1 ✔
253
                        leaves.add(obsParent);
1 ✔
254
                        if (obsParent.hasGroupMembers()) {
1 ✔
255
                                for (Obs child : obsParent.getGroupMembers(includedVoided)) {
1 ✔
256
                                        leaves.addAll(getFlattenedObsLeaves(child, includedVoided));
1 ✔
257
                                }
1 ✔
258
                        }
259
                }
260
                return leaves;
1 ✔
261
        }
262

263
        /**
264
         * Returns all Obs where Obs.encounterId = Encounter.encounterId In practice, this method should
265
         * not be used very often...
266
         *
267
         * @param includeVoided specifies whether or not to include voided Obs
268
         * @return Returns the all Obs.
269
         * <strong>Should</strong> not return null with null obs set
270
         * <strong>Should</strong> get obs
271
         * <strong>Should</strong> get both parent and child obs
272
         * <strong>Should</strong> get both parent and child with child directly on encounter
273
         * <strong>Should</strong> get both child and parent obs after removing child from parent grouping
274
         */
275
        public Set<Obs> getAllObs(boolean includeVoided) {
276
                if (includeVoided && obs != null) {
1 ✔
277
                        return obs;
1 ✔
278
                }
279
                
280
                Set<Obs> ret = new LinkedHashSet<>();
1 ✔
281
                
282
                if (this.obs != null) {
1 ✔
283
                        ret = this.obs.stream().filter(o -> includeVoided || !o.getVoided()).collect(Collectors.toSet());
1 ✔
284
                }
285
                return ret;
1 ✔
286
        }
287
        
288
        /**
289
         * Convenience method to call {@link #getAllObs(boolean)} with a false parameter
290
         *
291
         * @return all non-voided obs
292
         * <strong>Should</strong> not get voided obs
293
         */
294
        public Set<Obs> getAllObs() {
295
                return getAllObs(false);
1 ✔
296
        }
297

298
        /**
299
         * Returns all encounter obs as a flatten list(Set) of obs.
300
         * @param includeVoided indicates whether or not to include voided obs
301
         * @return a Set of all encounter' Obs
302
         */
303
        public Set<Obs> getAllFlattenedObs(boolean includeVoided) {
304

305
                Set<Obs> ret = new LinkedHashSet<>();
1 ✔
306

307
                if (this.obs != null) {
1 ✔
308
                        for (Obs o : this.obs) {
1 ✔
309
                                if (includeVoided || (!o.getVoided())) {
1 ✔
310
                                        ret.addAll(getFlattenedObsLeaves(o, includeVoided));
1 ✔
311
                                }
312
                        }
1 ✔
313
                }
314
                return ret;
1 ✔
315
        }
316
        
317
        /**
318
         * Returns a Set&lt;Obs&gt; of all root-level Obs of an Encounter, including obsGroups
319
         *
320
         * @param includeVoided specifies whether or not to include voided Obs
321
         * @return Returns all obs at top level -- will not be null
322
         * <strong>Should</strong> not return null with null obs set
323
         * <strong>Should</strong> get obs
324
         * <strong>Should</strong> not get voided obs
325
         * <strong>Should</strong> only get parents obs
326
         * <strong>Should</strong> only return the grouped top level obs
327
         * <strong>Should</strong> get both child and parent obs after removing child from parent grouping
328
         */
329
        public Set<Obs> getObsAtTopLevel(boolean includeVoided) {
330
                
331
                return getAllObs(includeVoided).stream().filter(o -> o.getObsGroup() == null)
1 ✔
332
                        .collect(Collectors.toCollection(LinkedHashSet::new));
1 ✔
333
        }
334
        
335
        /**
336
         * @param obs The obs to set.
337
         */
338
        public void setObs(Set<Obs> obs) {
339
                this.obs = CollectionUtil.setCollection(this.obs, obs);
1 ✔
340
        }
1 ✔
341
        
342
        /**
343
         * Add the given Obs to the list of obs for this Encounter.
344
         *
345
         * @param observation the Obs to add to this encounter
346
         * <strong>Should</strong> add obs with null values
347
         * <strong>Should</strong> not fail with null obs
348
         * <strong>Should</strong> set encounter attribute on obs
349
         * <strong>Should</strong> add obs to non null initial obs set
350
         * <strong>Should</strong> add encounter attrs to obs if attributes are null
351
         * <strong>Should</strong> add encounter attrs to obs groupMembers if attributes are null
352
         */
353
        public void addObs(Obs observation) {
354
                if (obs == null) {
1 ✔
355
                        obs = new LinkedHashSet<>();
1 ✔
356
                }
357
                
358
                if (observation != null) {
1 ✔
359
                        obs.add(observation);
1 ✔
360
                        
361
                        //Propagate some attributes to the obs and any groupMembers
362
                        
363
                        // a Deque is a two-ended queue, that lets us add to the end, and fetch from the beginning
364
                        Deque<Obs> obsToUpdate = new ArrayDeque<>();
1 ✔
365
                        obsToUpdate.add(observation);
1 ✔
366
                        
367
                        //prevent infinite recursion if an obs is its own group member
368
                        Set<Obs> seenIt = new LinkedHashSet<>();
1 ✔
369
                        
370
                        while (!obsToUpdate.isEmpty()) {
1 ✔
371
                                Obs o = obsToUpdate.removeFirst();
1 ✔
372
                                
373
                                //has this obs already been processed?
374
                                if (o == null || seenIt.contains(o)) {
1 ✔
375
                                        continue;
1 ✔
376
                                }
377
                                seenIt.add(o);
1 ✔
378
                                
379
                                o.setEncounter(this);
1 ✔
380
                                
381
                                //if the attribute was already set, preserve it
382
                                //if not, inherit the values from the encounter
383
                                if (o.getObsDatetime() == null) {
1 ✔
384
                                        o.setObsDatetime(getEncounterDatetime());
1 ✔
385
                                }
386
                                if (o.getPerson() == null) {
1 ✔
387
                                        o.setPerson(getPatient());
1 ✔
388
                                }
389
                                if (o.getLocation() == null) {
1 ✔
390
                                        o.setLocation(getLocation());
1 ✔
391
                                }
392
                                
393
                                //propagate attributes to  all group members as well
394
                                if (o.getGroupMembers(true) != null) {
1 ✔
395
                                        obsToUpdate.addAll(o.getGroupMembers());
1 ✔
396
                                }
397
                        }
1 ✔
398
                        
399
                }
400
        }
1 ✔
401
        
402
        /**
403
         * Remove the given observation from the list of obs for this Encounter
404
         *
405
         * @param observation
406
         * <strong>Should</strong> remove obs successfully
407
         * <strong>Should</strong> not throw error when removing null obs from empty set
408
         * <strong>Should</strong> not throw error when removing null obs from non empty set
409
         */
410
        public void removeObs(Obs observation) {
411
                if (obs != null) {
1 ✔
412
                        obs.remove(observation);
1 ✔
413
                }
414
        }
1 ✔
415
        
416
        /**
417
         * @return Returns the orders
418
         */
419
        public Set<Order> getOrders() {
420
                if (orders == null) {
1 ✔
421
                        orders = new LinkedHashSet<>();
1 ✔
422
                }
423
                return orders;
1 ✔
424
        }
425
        
426
        /**
427
         * @param orders The orders to set.
428
         */
429
        public void setOrders(Set<Order> orders) {
430
                this.orders = CollectionUtil.setCollection(this.orders, orders);
1 ✔
431
        }
1 ✔
432
        
433
        /**
434
         * Add the given Order to the list of orders for this Encounter
435
         *
436
         * @param order
437
         * <strong>Should</strong> add order with null values
438
         * <strong>Should</strong> not fail with null obs passed to add order
439
         * <strong>Should</strong> set encounter attribute
440
         * <strong>Should</strong> add order to non null initial order set
441
         * <strong>Should</strong> add order to encounter when adding order to set returned from getOrders
442
         */
443
        public void addOrder(Order order) {
444
                if (order != null) {
1 ✔
445
                        order.setEncounter(this);
1 ✔
446
                        getOrders().add(order);
1 ✔
447
                }
448
        }
1 ✔
449
        
450
        /**
451
         * Remove the given order from the list of orders for this Encounter
452
         *
453
         * @param order
454
         * <strong>Should</strong> remove order from encounter
455
         * <strong>Should</strong> not fail when removing null order
456
         * <strong>Should</strong> not fail when removing non existent order
457
         */
458
        public void removeOrder(Order order) {
459
                if (orders != null) {
1 ✔
460
                        orders.remove(order);
1 ✔
461
                }
462
        }
1 ✔
463
        
464
        /**
465
         * @return Returns the patient.
466
         */
467
        public Patient getPatient() {
468
                return patient;
1 ✔
469
        }
470
        
471
        /**
472
         * @param patient The patient to set.
473
         */
474
        public void setPatient(Patient patient) {
475
                this.patient = patient;
1 ✔
476
        }
1 ✔
477
        
478
        /**
479
         * Gets the set of diagnoses
480
         * 
481
         * @return diagnoses - the set of diagnoses.
482
         * @since 2.2
483
         */
484
        public Set<Diagnosis> getDiagnoses() {
485
                if (diagnoses == null) {
1 ✔
486
                        diagnoses = new LinkedHashSet<>();
1 ✔
487
                }
488
                return diagnoses;
1 ✔
489
        }
490
        
491
        /**
492
         * Sets a set of diagnoses for the current Encounter
493
         * 
494
         * @param diagnoses the set of Diagnosis to set.
495
         * @since 2.2
496
         */
497
        public void setDiagnoses(Set<Diagnosis> diagnoses) {
498
                this.diagnoses = CollectionUtil.setCollection(this.diagnoses, diagnoses);
1 ✔
499
        }
1 ✔
500
        
501
        /**
502
         * Basic property getter for the encounter's non-voided conditions.
503
         * 
504
         * @return all non-voided conditions
505
         * @since 2.4.0, 2.3.1
506
         */
507
        public Set<Condition> getConditions() {
508
                return getConditions(false);
1 ✔
509
        }
510

511
        /**
512
         * Returns all conditions where 'Condition.encounterId = Encounter.encounterId'.
513
         *
514
         * @param includeVoided - Specifies whether or not to include voided conditions.
515
         * @return The set of conditions, or an empty set if there are no conditions to return.
516
         * @since 2.3.3, 2.4.0, 2.5.0
517
         */
518
        public Set<Condition> getConditions(boolean includeVoided) {
519
                return Optional.ofNullable(conditions).orElse(new LinkedHashSet<>())
1 ✔
520
                        .stream().filter(c -> includeVoided || !c.getVoided()).collect(Collectors.toSet());
1 ✔
521
        }
522
                
523
        /**
524
         * Basic property setter for conditions
525
         *  
526
         * @param conditions - set of conditions
527
         * @since 2.4.0, 2.3.1
528
         */
529
        public void setConditions(Set<Condition> conditions) {
UNCOV
530
                this.conditions = CollectionUtil.setCollection(this.conditions, conditions);
×
UNCOV
531
        }
×
532

533
        /**
534
         * Add the given condition to the set of conditions for this encounter.
535
         *
536
         * @param condition - the condition to add
537
         */
538
        public void addCondition(Condition condition) {
539
                if (conditions == null) {
1 ✔
540
                        conditions = new LinkedHashSet<>();
1 ✔
541
                }
542

543
                if (condition != null) {
1 ✔
544
                        condition.setEncounter(this);
1 ✔
545
                        conditions.add(condition);
1 ✔
546
                }
547
        }
1 ✔
548

549
        /**
550
         * Remove the given condition from the set of conditions for this encounter.
551
         * In practise the condition is not removed but rather voided.
552
         *
553
         * @param condition - the condition to remove
554
         */
555
        public void removeCondition(Condition condition) {
556
                Optional.ofNullable(conditions).orElse(new LinkedHashSet<>()).stream().filter(c -> !c.getVoided() && c.equals(condition)).forEach(c -> {
1 ✔
557
                        c.setVoided(true);
1 ✔
558
                        c.setDateVoided(new Date());
1 ✔
559
                        c.setVoidReason("Voided by the API");
1 ✔
560
                        c.setVoidedBy(Context.getAuthenticatedUser());
1 ✔
561
                });
1 ✔
562
        }
1 ✔
563
        
564
        /**
565
         * Basic property accessor for encounterProviders. The convenience methods getProvidersByRoles
566
         * and getProvidersByRole are the preferred methods for getting providers. This getter is
567
         * provided as a convenience for treating this like a DTO
568
         *
569
         * @return list of all existing providers on this encounter
570
         * @see #getProvidersByRole(EncounterRole)
571
         * @see #getProvidersByRoles()
572
         * @since 1.9.1
573
         */
574
        public Set<EncounterProvider> getEncounterProviders() {
575
                return encounterProviders;
1 ✔
576
        }
577
        
578
        /**
579
         * Basic property setter for encounterProviders. The convenience methods addProvider,
580
         * removeProvider, and setProvider are the preferred methods for adding/removing providers. This
581
         * setter is provided as a convenience for treating this like a DTO
582
         *
583
         * @param encounterProviders the list of EncounterProvider objects to set. Overwrites list as
584
         *            normal setter is inclined to do
585
         * @see #addProvider(EncounterRole, Provider)
586
         * @see #removeProvider(EncounterRole, Provider)
587
         * @see #setProvider(EncounterRole, Provider)
588
         * @since 1.9.1
589
         */
590
        public void setEncounterProviders(Set<EncounterProvider> encounterProviders) {
UNCOV
591
                this.encounterProviders = CollectionUtil.setCollection(this.encounterProviders, encounterProviders);
×
UNCOV
592
        }
×
593
        
594
        /**
595
         * Returns only the non-voided encounter providers for this encounter. If you want <u>all</u>
596
         * encounter providers, use {@link #getEncounterProviders()}
597
         *
598
         * @return list of non-voided encounter providers for this encounter
599
         * @see #getEncounterProviders()
600
         */
601
        public Set<EncounterProvider> getActiveEncounterProviders() {
602
                Set<EncounterProvider> activeProviders = new LinkedHashSet<>();
1 ✔
603
                Set<EncounterProvider> providers = getEncounterProviders();
1 ✔
604
                if (providers != null && !providers.isEmpty()) {
1 ✔
605
                        activeProviders = providers.stream().filter(p -> !p.getVoided()).collect(Collectors.toSet());
1 ✔
606
                }
607
                return activeProviders;
1 ✔
608
        }
609
        
610
        /**
611
         * @return Returns the form.
612
         */
613
        public Form getForm() {
614
                return form;
1 ✔
615
        }
616
        
617
        /**
618
         * @param form The form to set.
619
         */
620
        public void setForm(Form form) {
621
                this.form = form;
1 ✔
622
        }
1 ✔
623
        
624
        /**
625
         * @see java.lang.Object#toString()
626
         * <strong>Should</strong> not fail with empty object
627
         */
628
        @Override
629
        public String toString() {
630
                String ret = "";
1 ✔
631
                ret += encounterId == null ? "(no ID) " : encounterId.toString() + " ";
1 ✔
632
                ret += this.getEncounterDatetime() == null ? "(no Date) " : this.getEncounterDatetime().toString() + " ";
1 ✔
633
                ret += this.getEncounterType() == null ? "(no Type) " : this.getEncounterType().getName() + " ";
1 ✔
634
                ret += this.getLocation() == null ? "(no Location) " : this.getLocation().getName() + " ";
1 ✔
635
                ret += this.getPatient() == null ? "(no Patient) " : this.getPatient().getPatientId().toString() + " ";
1 ✔
636
                ret += this.getForm() == null ? "(no Form) " : this.getForm().getName() + " ";
1 ✔
637
                ret += "num Obs: " + this.getObsAtTopLevel(false) + " ";
1 ✔
638
                ret += "num Orders: " + this.getOrders().size() + " ";
1 ✔
639
                return "Encounter: [" + ret + "]";
1 ✔
640
        }
641
        
642
        /**
643
         * @since 1.5
644
         * @see org.openmrs.OpenmrsObject#getId()
645
         */
646
        @Override
647
        public Integer getId() {
648
                
649
                return getEncounterId();
1 ✔
650
        }
651
        
652
        /**
653
         * @since 1.5
654
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
655
         */
656
        @Override
657
        public void setId(Integer id) {
UNCOV
658
                setEncounterId(id);
×
659
                
UNCOV
660
        }
×
661
        
662
        /**
663
         * Gets the visit.
664
         *
665
         * @return the visit.
666
         * @since 1.9
667
         */
668
        public Visit getVisit() {
669
                return visit;
1 ✔
670
        }
671
        
672
        /**
673
         * Sets the visit
674
         *
675
         * @param visit the visit to set.
676
         * @since 1.9
677
         */
678
        public void setVisit(Visit visit) {
679
                this.visit = visit;
1 ✔
680
        }
1 ✔
681
        
682
        /**
683
         * Gets all unvoided providers, grouped by role.
684
         *
685
         * @return map of unvoided providers keyed by roles
686
         * @since 1.9
687
         * <strong>Should</strong> return empty map if no unvoided providers
688
         * <strong>Should</strong> return all roles and unvoided providers
689
         */
690
        public Map<EncounterRole, Set<Provider>> getProvidersByRoles() {
691
                return getProvidersByRoles(false);
1 ✔
692
        }
693
        
694
        /**
695
         * Gets all providers, grouped by role.
696
         *
697
         * @param includeVoided set to true to include voided providers, else set to false
698
         * @return map of providers keyed by roles
699
         * @since 1.9
700
         * <strong>Should</strong> return empty map if no providers
701
         * <strong>Should</strong> return all roles and providers
702
         */
703
        public Map<EncounterRole, Set<Provider>> getProvidersByRoles(boolean includeVoided) {
704
                
705
                return encounterProviders
1 ✔
706
                        .stream()
1 ✔
707
                        .filter(ep -> includeVoided || !ep.getVoided())
1 ✔
708
                        .collect(
1 ✔
709
                            Collectors.groupingBy(EncounterProvider::getEncounterRole,
1 ✔
710
                                Collectors.mapping(EncounterProvider::getProvider, Collectors.toSet())));
1 ✔
711
                
712
        }
713
        
714
        /**
715
         * Gets unvoided providers who had the given role in this encounter.
716
         *
717
         * @param role
718
         * @return unvoided providers or empty set if none was found
719
         * @since 1.9
720
         * <strong>Should</strong> return unvoided providers for role
721
         * <strong>Should</strong> return empty set for no role
722
         * <strong>Should</strong> return empty set for null role
723
         */
724
        public Set<Provider> getProvidersByRole(EncounterRole role) {
725
                return getProvidersByRole(role, false);
1 ✔
726
        }
727
        
728
        /**
729
         * Gets providers who had the given role in this encounter.
730
         *
731
         * @param role
732
         * @param includeVoided set to true to include voided providers, else set to false
733
         * @return providers or empty set if none was found
734
         * @since 1.9
735
         * <strong>Should</strong> return providers for role
736
         * <strong>Should</strong> return empty set for no role
737
         * <strong>Should</strong> return empty set for null role
738
         */
739
        public Set<Provider> getProvidersByRole(EncounterRole role, boolean includeVoided) {
740
                
741
                return encounterProviders.stream()
1 ✔
742
                        .filter(ep -> ep.getEncounterRole().equals(role) && (includeVoided || !ep.getVoided()))
1 ✔
743
                        .map(EncounterProvider::getProvider).collect(Collectors.toSet());
1 ✔
744
        }
745
        
746
        /**
747
         * Adds a new provider for the encounter, with the given role.
748
         *
749
         * @param role
750
         * @param provider
751
         * @since 1.9
752
         * <strong>Should</strong> add provider for new role
753
         * <strong>Should</strong> add second provider for role
754
         * <strong>Should</strong> not add same provider twice for role
755
         */
756
        public void addProvider(EncounterRole role, Provider provider) {
757
                // first, make sure the provider isn't already there
758
                for (EncounterProvider ep : encounterProviders) {
1 ✔
759
                        if (ep.getEncounterRole().equals(role) && ep.getProvider().equals(provider) && !ep.getVoided()) {
1 ✔
760
                                return;
1 ✔
761
                        }
762
                }
1 ✔
763
                EncounterProvider encounterProvider = new EncounterProvider();
1 ✔
764
                encounterProvider.setEncounter(this);
1 ✔
765
                encounterProvider.setEncounterRole(role);
1 ✔
766
                encounterProvider.setProvider(provider);
1 ✔
767
                encounterProvider.setDateCreated(new Date());
1 ✔
768
                encounterProvider.setCreator(Context.getAuthenticatedUser());
1 ✔
769
                encounterProviders.add(encounterProvider);
1 ✔
770
        }
1 ✔
771
        
772
        /**
773
         * Sets the provider for the given role.
774
         * <p>
775
         * If the encounter already had any providers for the given role, those are removed.
776
         *
777
         * @param role
778
         * @param provider
779
         * @since 1.9
780
         * <strong>Should</strong> set provider for new role
781
         * <strong>Should</strong> clear providers and set provider for role
782
         * <strong>Should</strong> void existing EncounterProvider
783
         */
784
        public void setProvider(EncounterRole role, Provider provider) {
785
                boolean hasProvider = false;
1 ✔
786
                for (EncounterProvider encounterProvider : encounterProviders) {
1 ✔
787
                        if (encounterProvider.getEncounterRole().equals(role)) {
1 ✔
788
                                if (!encounterProvider.getProvider().equals(provider)) {
1 ✔
789
                                        encounterProvider.setVoided(true);
1 ✔
790
                                        encounterProvider.setDateVoided(new Date());
1 ✔
791
                                        encounterProvider.setVoidedBy(Context.getAuthenticatedUser());
1 ✔
792
                                } else if (!encounterProvider.getVoided()) {
1 ✔
793
                                        hasProvider = true;
1 ✔
794
                                }
795
                        }
796
                }
1 ✔
797
                
798
                if (!hasProvider) {
1 ✔
799
                        addProvider(role, provider);
1 ✔
800
                }
801
        }
1 ✔
802
        
803
        /**
804
         * Removes the provider for a given role.
805
         *
806
         * @param role the role.
807
         * @param provider the provider.
808
         * @since 1.9
809
         * <strong>Should</strong> void existing EncounterProvider
810
         */
811
        public void removeProvider(EncounterRole role, Provider provider) {
812
                for (EncounterProvider encounterProvider : encounterProviders) {
1 ✔
813
                        if (encounterProvider.getEncounterRole().equals(role) && encounterProvider.getProvider().equals(provider)
1 ✔
814
                                && !encounterProvider.getVoided()) {
1 ✔
815
                                encounterProvider.setVoided(true);
1 ✔
816
                                encounterProvider.setDateVoided(new Date());
1 ✔
817
                                encounterProvider.setVoidedBy(Context.getAuthenticatedUser());
1 ✔
818
                                return;
1 ✔
819
                        }
820
                }
1 ✔
UNCOV
821
        }
×
822
        
823
        /**
824
         * Copied encounter will not have visit field copied.
825
         *
826
         * @param patient the Patient that will be assign to copied Encounter
827
         * @return copied encounter
828
         * <strong>Should</strong> copy all Encounter data except visit and assign copied Encounter to given Patient
829
         */
830
        public Encounter copyAndAssignToAnotherPatient(Patient patient) {
831
                Encounter target = new Encounter();
1 ✔
832
                
833
                target.setChangedBy(getChangedBy());
1 ✔
834
                target.setCreator(getCreator());
1 ✔
835
                target.setDateChanged(getDateChanged());
1 ✔
836
                target.setDateCreated(getDateCreated());
1 ✔
837
                target.setDateVoided(getDateVoided());
1 ✔
838
                target.setVoided(getVoided());
1 ✔
839
                target.setVoidedBy(getVoidedBy());
1 ✔
840
                target.setVoidReason(getVoidReason());
1 ✔
841
                
842
                // Encounter specific data
843
                target.setEncounterDatetime(getEncounterDatetime());
1 ✔
844
                target.setEncounterType(getEncounterType());
1 ✔
845
                target.setForm(getForm());
1 ✔
846
                target.setLocation(getLocation());
1 ✔
847
                target.setPatient(patient);
1 ✔
848
                
849
                //encounter providers
850
                for (EncounterProvider encounterProvider : getEncounterProviders()) {
1 ✔
851
                        EncounterProvider encounterProviderCopy = encounterProvider.copy();
1 ✔
852
                        encounterProviderCopy.setEncounter(target);
1 ✔
853
                        target.getEncounterProviders().add(encounterProviderCopy);
1 ✔
854
                }
1 ✔
855
                
856
                Context.getEncounterService().saveEncounter(target);
1 ✔
857
                
858
                //obs
859
                for (Obs obs : getAllObs()) {
1 ✔
860
                        Obs obsCopy = Obs.newInstance(obs);
1 ✔
861
                        obsCopy.setEncounter(target);
1 ✔
862
                        obsCopy.setPerson(patient);
1 ✔
863
                        target.addObs(obsCopy);
1 ✔
864
                }
1 ✔
865
                
866
                return target;
1 ✔
867
        }
868
        
869
        /**
870
         * Takes in a list of orders and pulls out the orderGroups within them
871
         *
872
         * @since 1.12
873
         * @return list of orderGroups
874
         */
875
        public List<OrderGroup> getOrderGroups() {
876
                Map<String, OrderGroup> orderGroups = new HashMap<>();
1 ✔
877
                for (Order order : orders) {
1 ✔
878
                        if (order.getOrderGroup() != null) {
1 ✔
879
                                orderGroups.computeIfAbsent(order.getOrderGroup().getUuid(), k -> order.getOrderGroup());
1 ✔
880
                                order.getOrderGroup().addOrder(order, null);
1 ✔
881
                        }
882
                }
1 ✔
883
                return new ArrayList<>(orderGroups.values());
1 ✔
884
        }
885
        
886
        /**
887
         * Takes in a list of orders and filters out the orders which have orderGroups
888
         * 
889
         * @since 1.12
890
         * @return list of orders not having orderGroups
891
         */
892
        public List<Order> getOrdersWithoutOrderGroups() {
893
                return orders.stream().filter(o -> o.getOrderGroup() == null).collect(Collectors.toList());
1 ✔
894
        }
895
        
896
        /**
897
         * Check if encounter has a particular diagnosis
898
         *
899
         * @since 2.2
900
         * @param diagnosis the diagnosis to check if it belongs to this given encounter
901
         * @return true if this encounter has the given diagnosis, else false
902
         */
903
        public Boolean hasDiagnosis(Diagnosis diagnosis) {
904
                for (Diagnosis diagnosis1 : getDiagnoses()) {
1 ✔
905
                        if (diagnosis.equals(diagnosis1)) {
1 ✔
906
                                return true;
1 ✔
907
                        }
UNCOV
908
                }
×
909
                return false;
1 ✔
910
        }
911
        
912
        /**
913
         * Basic property getter for the encounter's non-voided allergies.
914
         * 
915
         * @return all non-voided allergies
916
         * @since 2.5.0
917
         */
918
        public Set<Allergy> getAllergies() {
919
                return getAllergies(false);
1 ✔
920
        }
921

922
        /**
923
         * Returns all allergies where 'Allergy.encounterId = Encounter.encounterId'.
924
         *
925
         * @param includeVoided - Specifies whether or not to include voided allergies.
926
         * @return The set of allergies, or an empty set if there are no allergies to return.
927
         * @since 2.5.0
928
         */
929
        public Set<Allergy> getAllergies(boolean includeVoided) {
930
                return Optional.ofNullable(allergies).orElse(new LinkedHashSet<>())
1 ✔
931
                        .stream().filter(c -> includeVoided || !c.getVoided()).collect(Collectors.toSet());
1 ✔
932
        }
933
                
934
        /**
935
         * Basic property setter for allergies
936
         *  
937
         * @param allergies - set of allergies
938
         * @since 2.5.0
939
         */
940
        public void setAllergies(Set<Allergy> allergies) {
UNCOV
941
                this.allergies = CollectionUtil.setCollection(this.allergies, allergies);
×
UNCOV
942
        }
×
943

944
        /**
945
         * Add the given allergy to the set of allergies for this encounter.
946
         *
947
         * @param allergy - the allergy to add
948
         */
949
        public void addAllergy(Allergy allergy) {
950
                if (allergies == null) {
1 ✔
951
                        allergies = new LinkedHashSet<>();
1 ✔
952
                }
953

954
                if (allergy != null) {
1 ✔
955
                        allergy.setEncounter(this);
1 ✔
956
                        allergies.add(allergy);
1 ✔
957
                }
958
        }
1 ✔
959

960
        /**
961
         * Remove the given allergy from the set of allergies for this encounter.
962
         * In practice the allergy is not removed but rather voided.
963
         *
964
         * @param allergy - the allergy to remove
965
         */
966
        public void removeAllergy(Allergy allergy) {
967
                Optional.ofNullable(allergies).orElse(new LinkedHashSet<>()).stream().filter(c -> !c.getVoided() && c.equals(allergy)).forEach(c -> {
×
968
                        c.setVoided(true);
×
969
                        c.setDateVoided(new Date());
×
970
                        c.setVoidReason("Voided by the API");
×
UNCOV
971
                        c.setVoidedBy(Context.getAuthenticatedUser());
×
UNCOV
972
                });
×
UNCOV
973
        }
×
974
}
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