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

openmrs / openmrs-core / 15205088843

23 May 2025 07:43AM UTC coverage: 65.083% (+0.01%) from 65.069%
15205088843

push

github

rkorytkowski
TRUNK-6300: Adding Windows test, cleaning up logs, adjusting variable name

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

336 existing lines in 18 files now uncovered.

23379 of 35922 relevant lines covered (65.08%)

0.65 hits per line

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

75.0
/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

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

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

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

© 2026 Coveralls, Inc