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

openmrs / openmrs-core / 41717832711

30 Oct 2026 04:16PM UTC coverage: 66.75% (+0.003%) from 66.747%
41717832711

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%)

419 existing lines in 31 files now uncovered.

26279 of 39369 relevant lines covered (66.75%)

0.67 hits per line

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

80.65
/api/src/main/java/org/openmrs/Patient.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 javax.persistence.Cacheable;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.LinkedList;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.TreeSet;
19

20
import org.hibernate.annotations.Cache;
21
import org.hibernate.annotations.CacheConcurrencyStrategy;
22
import org.hibernate.envers.Audited;
23
import org.openmrs.util.CollectionUtil;
24

25
/**
26
 * Defines a Patient in the system. A patient is simply an extension of a person and all that that
27
 * implies.
28
 * 
29
 * @version 2.0
30
 */
31
@Audited
32
@Cacheable
33
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
34
public class Patient extends Person {
35
        
36
        public static final long serialVersionUID = 93123L;
37
        
38
        private Integer patientId;
39
        
40
        private String allergyStatus = Allergies.UNKNOWN;
1 ✔
41
        
42
        private Set<PatientIdentifier> identifiers;
43

44
        
45
        // Constructors
46
        
47
        /** default constructor */
48
        public Patient() {
1 ✔
49
                setPatient(true);
1 ✔
50
        }
1 ✔
51
        
52
        /**
53
         * This constructor creates a new Patient object from the given {@link Person} object. All
54
         * attributes are copied over to the new object. NOTE! All child collection objects are copied
55
         * as pointers, each individual element is not copied. <br>
56
         * <br>
57
         *
58
         * @param person the person object to copy onto a new Patient
59
         * @see Person#Person(Person)
60
         */
61
        public Patient(Person person) {
62
                super(person);
1 ✔
63
                if (person != null) {
1 ✔
64
                        this.patientId = person.getPersonId();
1 ✔
65
                        if (person.getUuid() != null) {
1 ✔
66
                                this.setUuid(person.getUuid());
1 ✔
67
                        }
68
                }
69
                setPatient(true);
1 ✔
70
        }
1 ✔
71
        
72
        /**
73
         * Constructor with default patient id
74
         * 
75
         * @param patientId
76
         */
77
        public Patient(Integer patientId) {
78
                super(patientId);
1 ✔
79
                this.patientId = patientId;
1 ✔
80
                setPatient(true);
1 ✔
81
        }
1 ✔
82
        
83
        /**
84
         * This constructor creates a new Patient object from the given {@link Patient} object. All
85
         * attributes are copied over to the new object. In effect creating a clone/duplicate. <br>
86
         *
87
         * @param patient the person object to copy onto a new Patient
88
         * @since 2.2.0
89
         */
90
        public Patient(Patient patient) {
91
                super(patient);
1 ✔
92
                this.patientId = patient.getPatientId();
1 ✔
93
                this.allergyStatus = patient.getAllergyStatus();
1 ✔
94
                Set<PatientIdentifier> newIdentifiers = new TreeSet<>();
1 ✔
95
                for (PatientIdentifier pid : patient.getIdentifiers()) {
1 ✔
96
                        PatientIdentifier identifierClone = (PatientIdentifier) pid.clone();
1 ✔
97
                        identifierClone.setPatient(this);
1 ✔
98
                        newIdentifiers.add(identifierClone);
1 ✔
99
                }
1 ✔
100
                this.identifiers = newIdentifiers;
1 ✔
101
        }
1 ✔
102
        
103
        // Property accessors
104
        
105
        /**
106
         * @return internal identifier for patient
107
         */
108
        public Integer getPatientId() {
109
                if (this.patientId == null) {
1 ✔
110
                        this.patientId = getPersonId();
1 ✔
111
                }
112
                return this.patientId;
1 ✔
113
        }
114
        
115
        /**
116
         * Sets the internal identifier for a patient. <b>This should never be called directly</b>. It
117
         * exists only for the use of the supporting infrastructure.
118
         * 
119
         * @param patientId
120
         */
121
        public void setPatientId(Integer patientId) {
122
                super.setPersonId(patientId);
1 ✔
123
                this.patientId = patientId;
1 ✔
124
        }
1 ✔
125
        
126
        /**
127
         * Returns allergy status maintained by the supporting infrastructure.
128
         * 
129
         * @return current allargy status for patient
130
         * @since 2.0
131
         * <strong>Should</strong> return allergy status maintained by the supporting infrastructure
132
         */
133
        public String getAllergyStatus() {
134
                return this.allergyStatus;
1 ✔
135
        }
136
        
137
        /**
138
         * Sets the allergy status for a patient. <b>This should never be called directly</b>. It should
139
         * reflect allergy status maintained by the supporting infrastructure.
140
         * 
141
         * @param allergyStatus
142
         * @since 2.0
143
         * <strong>Should</strong> not be called by service client
144
         */
145
        public void setAllergyStatus(String allergyStatus) {
146
                this.allergyStatus = allergyStatus;
1 ✔
147
        }
1 ✔
148
        
149
        /**
150
         * Overrides the parent setPersonId(Integer) so that we can be sure patient id is also set
151
         * correctly.
152
         * 
153
         * @see org.openmrs.Person#setPersonId(java.lang.Integer)
154
         */
155
        @Override
156
        public void setPersonId(Integer personId) {
157
                super.setPersonId(personId);
1 ✔
158
                this.patientId = personId;
1 ✔
159
        }
1 ✔
160
        
161
        /**
162
         * Get all of this patients identifiers -- both voided and non-voided ones. If you want only
163
         * non-voided identifiers, use {@link #getActiveIdentifiers()}
164
         * 
165
         * @return Set of all known identifiers for this patient
166
         * @see org.openmrs.PatientIdentifier
167
         * @see #getActiveIdentifiers()
168
         * <strong>Should</strong> not return null
169
         */
170
        public Set<PatientIdentifier> getIdentifiers() {
171
                if (identifiers == null) {
1 ✔
172
                        identifiers = new TreeSet<>();
1 ✔
173
                }
174
                return this.identifiers;
1 ✔
175
        }
176
        
177
        /**
178
         * Update all identifiers for patient
179
         * 
180
         * @param identifiers Set&lt;PatientIdentifier&gt; to set as update all known identifiers for
181
         *            patient
182
         * @see org.openmrs.PatientIdentifier
183
         */
184
        public void setIdentifiers(Set<PatientIdentifier> identifiers) {
185
                this.identifiers = CollectionUtil.setCollection(getIdentifiers(), identifiers);
1 ✔
186
        }
1 ✔
187

188
        /**
189
         * Adds this PatientIdentifier if the patient doesn't contain it already
190
         * 
191
         * @param patientIdentifier
192
         */
193
        /**
194
         * Will only add PatientIdentifiers in this list that this patient does not have already
195
         * 
196
         * @param patientIdentifiers
197
         */
198
        public void addIdentifiers(Collection<PatientIdentifier> patientIdentifiers) {
199
                for (PatientIdentifier identifier : patientIdentifiers) {
1 ✔
200
                        addIdentifier(identifier);
1 ✔
201
                }
1 ✔
202
        }
1 ✔
203
        
204
        /**
205
         * Will add this PatientIdentifier if the patient doesn't contain it already
206
         * 
207
         * @param patientIdentifier
208
         * <strong>Should</strong> not fail with null identifiers list
209
         * <strong>Should</strong> add identifier to current list
210
         * <strong>Should</strong> not add identifier that is in list already
211
         */
212
        public void addIdentifier(PatientIdentifier patientIdentifier) {
213
                if (patientIdentifier != null) {
1 ✔
214
                        patientIdentifier.setPatient(this);
1 ✔
215
                        // make sure the set doesn't already contain an identifier with the same
216
                        // identifier, identifierType
217
                        for (PatientIdentifier currentId : getActiveIdentifiers()) {
1 ✔
218
                                if (currentId.equalsContent(patientIdentifier)) {
1 ✔
219
                                        // fail silently if someone tries to add a duplicate
220
                                        return;
1 ✔
221
                                }
222
                        }
1 ✔
223
                }
224
                
225
                getIdentifiers().add(patientIdentifier);
1 ✔
226
        }
1 ✔
227
        
228
        /**
229
         * Convenience method to remove the given identifier from this patient's list of identifiers. If
230
         * <code>patientIdentifier</code> is null, nothing is done.
231
         * 
232
         * @param patientIdentifier the identifier to remove
233
         * <strong>Should</strong> remove identifier if exists
234
         */
235
        public void removeIdentifier(PatientIdentifier patientIdentifier) {
236
                if (patientIdentifier != null) {
1 ✔
237
                        getIdentifiers().remove(patientIdentifier);
1 ✔
238
                }
239
        }
1 ✔
240
        
241
        /**
242
         * Convenience method to get the first "preferred" identifier for a patient. Otherwise, returns
243
         * the first non-voided identifier Otherwise, null
244
         * 
245
         * @return Returns the "preferred" patient identifier.
246
         */
247
        public PatientIdentifier getPatientIdentifier() {
248
                // normally the DAO layer returns these in the correct order, i.e. preferred and non-voided first, but it's possible that someone
249
                // has fetched a Patient, changed their identifiers around, and then calls this method, so we have to be careful.
250
                if (!getIdentifiers().isEmpty()) {
1 ✔
251
                        for (PatientIdentifier id : getIdentifiers()) {
1 ✔
252
                                if (id.getPreferred() && !id.getVoided()) {
1 ✔
253
                                        return id;
1 ✔
254
                                }
255
                        }
1 ✔
256
                        for (PatientIdentifier id : getIdentifiers()) {
1 ✔
257
                                if (!id.getVoided()) {
1 ✔
258
                                        return id;
1 ✔
259
                                }
260
                        }
1 ✔
261
                        return null;
1 ✔
262
                }
263
                return null;
1 ✔
264
        }
265
        
266
        /**
267
         * Returns the first (preferred) patient identifier matching a
268
         * <code>PatientIdentifierType</code> Otherwise, returns the first non-voided identifier
269
         * Otherwise, null
270
         * 
271
         * @param pit The PatientIdentifierType of which to return the PatientIdentifier
272
         * @return Returns a PatientIdentifier of the specified type.
273
         */
274
        public PatientIdentifier getPatientIdentifier(PatientIdentifierType pit) {
275
                if (!getIdentifiers().isEmpty()) {
×
276
                        for (PatientIdentifier id : getIdentifiers()) {
×
277
                                if (id.getPreferred() && !id.getVoided() && pit.equals(id.getIdentifierType())) {
×
UNCOV
278
                                        return id;
×
279
                                }
280
                        }
×
281
                        for (PatientIdentifier id : getIdentifiers()) {
×
282
                                if (!id.getVoided() && pit.equals(id.getIdentifierType())) {
×
UNCOV
283
                                        return id;
×
284
                                }
285
                        }
×
UNCOV
286
                        return null;
×
287
                }
UNCOV
288
                return null;
×
289
        }
290
        
291
        /**
292
         * Returns the first (preferred) patient identifier matching <code>identifierTypeId</code>
293
         * 
294
         * @param identifierTypeId
295
         * @return preferred patient identifier
296
         */
297
        public PatientIdentifier getPatientIdentifier(Integer identifierTypeId) {
298
                if (!getIdentifiers().isEmpty()) {
1 ✔
299
                        for (PatientIdentifier id : getIdentifiers()) {
1 ✔
300
                                if (id.getPreferred() && !id.getVoided()
1 ✔
301
                                        && identifierTypeId.equals(id.getIdentifierType().getPatientIdentifierTypeId())) {
1 ✔
302
                                        return id;
1 ✔
303
                                }
304
                        }
1 ✔
305
                        for (PatientIdentifier id : getIdentifiers()) {
1 ✔
306
                                if (!id.getVoided() && identifierTypeId.equals(id.getIdentifierType().getPatientIdentifierTypeId())) {
1 ✔
307
                                        return id;
1 ✔
308
                                }
309
                        }
1 ✔
UNCOV
310
                        return null;
×
311
                }
UNCOV
312
                return null;
×
313
        }
314
        
315
        /**
316
         * Returns the (preferred) patient identifier matching <code>identifierTypeName</code> Otherwise
317
         * returns that last <code>PatientIdenitifer</code>
318
         * 
319
         * @param identifierTypeName
320
         * @return preferred patient identifier
321
         */
322
        public PatientIdentifier getPatientIdentifier(String identifierTypeName) {
323
                if (!getIdentifiers().isEmpty()) {
×
324
                        for (PatientIdentifier id : getIdentifiers()) {
×
325
                                if (id.getPreferred() && !id.getVoided() && identifierTypeName.equals(id.getIdentifierType().getName())) {
×
UNCOV
326
                                        return id;
×
327
                                }
328
                        }
×
329
                        for (PatientIdentifier id : getIdentifiers()) {
×
330
                                if (!id.getVoided() && identifierTypeName.equals(id.getIdentifierType().getName())) {
×
UNCOV
331
                                        return id;
×
332
                                }
333
                        }
×
UNCOV
334
                        return null;
×
335
                }
UNCOV
336
                return null;
×
337
        }
338
        
339
        /**
340
         * Returns only the non-voided identifiers for this patient. If you want <u>all</u> identifiers,
341
         * use {@link #getIdentifiers()}
342
         * 
343
         * @return list of non-voided identifiers for this patient
344
         * @see #getIdentifiers()
345
         * <strong>Should</strong> return preferred identifiers first in the list
346
         */
347
        public List<PatientIdentifier> getActiveIdentifiers() {
348
                List<PatientIdentifier> ids = new ArrayList<>();
1 ✔
349
                List<PatientIdentifier> nonPreferred = new LinkedList<>();
1 ✔
350
                for (PatientIdentifier pi : getIdentifiers()) {
1 ✔
351
                        if (!pi.getVoided()) {
1 ✔
352
                                if (pi.getPreferred()) {
1 ✔
353
                                        ids.add(pi);
1 ✔
354
                                } else {
355
                                        nonPreferred.add(pi);
1 ✔
356
                                }
357
                        }
358
                }
1 ✔
359
                ids.addAll(nonPreferred);
1 ✔
360
                return ids;
1 ✔
361
        }
362
        
363
        /**
364
         * Returns only the non-voided identifiers for this patient. If you want <u>all</u> identifiers,
365
         * use {@link #getIdentifiers()}
366
         * 
367
         * @return list of non-voided identifiers for this patient
368
         * @param pit PatientIdentifierType
369
         * @see #getIdentifiers()
370
         */
371
        public List<PatientIdentifier> getPatientIdentifiers(PatientIdentifierType pit) {
372
                List<PatientIdentifier> ids = new ArrayList<>();
1 ✔
373
                for (PatientIdentifier pi : getIdentifiers()) {
1 ✔
374
                        if (!pi.getVoided() && pit.equals(pi.getIdentifierType())) {
1 ✔
375
                                ids.add(pi);
1 ✔
376
                        }
377
                }
1 ✔
378
                return ids;
1 ✔
379
        }
380
        
381
        @Override
382
        public String toString() {
383
                return "Patient#" + patientId;
1 ✔
384
        }
385
        
386
        /**
387
         * @since 1.5
388
         * @see org.openmrs.OpenmrsObject#getId()
389
         */
390
        @Override
391
        public Integer getId() {
392
                return getPatientId();
1 ✔
393
        }
394
        
395
        /**
396
         * @since 1.5
397
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
398
         */
399
        @Override
400
        public void setId(Integer id) {
401
                setPatientId(id);
1 ✔
402
        }
1 ✔
403
        
404
        /**
405
         * Returns the person represented
406
         * 
407
         * @return the person represented by this object
408
         * @since 1.10.0
409
         */
410
        public Person getPerson() {
411
                return this;
1 ✔
412
        }
413
        
414
}
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