• 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

89.09
/api/src/main/java/org/openmrs/Person.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 javax.persistence.Transient;
14
import java.text.ParseException;
15
import java.text.SimpleDateFormat;
16
import java.time.LocalDate;
17
import java.time.temporal.ChronoUnit;
18
import java.util.ArrayList;
19
import java.util.Calendar;
20
import java.util.Date;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Set;
25
import java.util.TreeSet;
26

27
import org.codehaus.jackson.annotate.JsonIgnore;
28
import org.hibernate.annotations.Cache;
29
import org.hibernate.annotations.CacheConcurrencyStrategy;
30
import org.hibernate.envers.Audited;
31
import org.hibernate.envers.NotAudited;
32
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId;
33
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
34
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexingDependency;
35
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.ObjectPath;
36
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.PropertyValue;
37
import org.openmrs.util.CollectionUtil;
38
import org.openmrs.util.OpenmrsUtil;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41
import org.springframework.util.StringUtils;
42

43
/**
44
 * A Person in the system. This can be either a small person stub, or indicative of an actual
45
 * Patient in the system. This class holds the generic person things that both the stubs and
46
 * patients share. Things like birthdate, names, addresses, and attributes are all generified into
47
 * the person table (and hence this super class)
48
 * 
49
 * @see org.openmrs.Patient
50
 */
51
@Audited
52
@Cacheable
53
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
54
public class Person extends BaseChangeableOpenmrsData {
55
        
56
        public static final long serialVersionUID = 2L;
57
        
58
        private static final Logger log = LoggerFactory.getLogger(Person.class);
1 ✔
59
        
60
        @DocumentId
61
        protected Integer personId;
62
        
63
        private Set<PersonAddress> addresses = null;
1 ✔
64
        
65
        private Set<PersonName> names = null;
1 ✔
66
        
67
        private Set<PersonAttribute> attributes = null;
1 ✔
68
        
69
        @GenericField
70
        private String gender;
71
        
72
        @GenericField
73
        private Date birthdate;
74
        
75
        private Date birthtime;
76
        
77
        private Boolean birthdateEstimated = false;
1 ✔
78
        
79
        private Boolean deathdateEstimated = false;
1 ✔
80
        
81
        @GenericField
1 ✔
82
        private Boolean dead = false;
1 ✔
83
        
84
        private Date deathDate;
85
        
86
        private Concept causeOfDeath;
87
        
88
        private String causeOfDeathNonCoded;
89

90
        private User personCreator;
91
        
92
        private Date personDateCreated;
93

94
        private User personChangedBy;
95
        
96
        private Date personDateChanged;
97
        
98
        private Boolean personVoided = false;
1 ✔
99

100
        private User personVoidedBy;
101
        
102
        private Date personDateVoided;
103
        
104
        private String personVoidReason;
105
        
106
        @GenericField
107
        @NotAudited
108
        @IndexingDependency(derivedFrom = @ObjectPath(@PropertyValue(propertyName = "patient")))
109
        private boolean isPatient;
110
        
111
        /**
112
         * Convenience map from PersonAttributeType.name to PersonAttribute.<br>
113
         * <br>
114
         * This is "cached" for each user upon first load. When an attribute is changed, the cache is
115
         * cleared and rebuilt on next access.
116
         */
117
        @Transient
1 ✔
118
        Map<String, PersonAttribute> attributeMap = null;
119
        
120
        @Transient
1 ✔
121
        private Map<String, PersonAttribute> allAttributeMap = null;
122
        
123
        /**
124
         * default empty constructor
125
         */
126
        public Person() {
1 ✔
127
        }
1 ✔
128
        
129
        /**
130
         * This constructor is used to build a new Person object copy from another person object
131
         * (usually a patient or a user subobject). All attributes are copied over to the new object.
132
         * NOTE! All child collection objects are copied as pointers, each individual element is not
133
         * copied. <br>
134
         *
135
         * @param person Person to create this person object from
136
         */
137
        public Person(Person person) {
1 ✔
138
                if (person == null) {
1 ✔
UNCOV
139
                        return;
×
140
                }
141
                
142
                personId = person.getPersonId();
1 ✔
143
                setUuid(person.getUuid());
1 ✔
144
                addresses = person.getAddresses();
1 ✔
145
                names = person.getNames();
1 ✔
146
                attributes = person.getAttributes();
1 ✔
147
                
148
                gender = person.getGender();
1 ✔
149
                birthdate = person.getBirthdate();
1 ✔
150
                birthtime = person.getBirthDateTime();
1 ✔
151
                birthdateEstimated = person.getBirthdateEstimated();
1 ✔
152
                deathdateEstimated = person.getDeathdateEstimated();
1 ✔
153
                dead = person.getDead();
1 ✔
154
                deathDate = person.getDeathDate();
1 ✔
155
                causeOfDeath = person.getCauseOfDeath();
1 ✔
156
                causeOfDeathNonCoded = person.getCauseOfDeathNonCoded();
1 ✔
157
                // base creator/voidedBy/changedBy info is not copied here
158
                // because that is specific to and will be recreated
159
                // by the subobject upon save
160
                
161
                setPersonCreator(person.getPersonCreator());
1 ✔
162
                setPersonDateCreated(person.getPersonDateCreated());
1 ✔
163
                setPersonChangedBy(person.getPersonChangedBy());
1 ✔
164
                setPersonDateChanged(person.getPersonDateChanged());
1 ✔
165
                setPersonVoided(person.getPersonVoided());
1 ✔
166
                setPersonVoidedBy(person.getPersonVoidedBy());
1 ✔
167
                setPersonDateVoided(person.getPersonDateVoided());
1 ✔
168
                setPersonVoidReason(person.getPersonVoidReason());
1 ✔
169
                
170
                setPatient(person.getIsPatient());
1 ✔
171
        }
1 ✔
172
        
173
        /**
174
         * Default constructor taking in the primary key personId value
175
         * 
176
         * @param personId Integer internal id for this person
177
         * <strong>Should</strong> set person id
178
         */
179
        public Person(Integer personId) {
1 ✔
180
                this.personId = personId;
1 ✔
181
        }
1 ✔
182
        
183
        // Property accessors
184
        
185
        /**
186
         * @return Returns the personId.
187
         */
188
        public Integer getPersonId() {
189
                return personId;
1 ✔
190
        }
191
        
192
        /**
193
         * @param personId The personId to set.
194
         */
195
        public void setPersonId(Integer personId) {
196
                this.personId = personId;
1 ✔
197
        }
1 ✔
198

199
        @IndexingDependency(derivedFrom = @ObjectPath(@PropertyValue(propertyName = "personVoided")))
200
        @Override
201
        public Boolean getVoided() {
202
                return super.getVoided();
1 ✔
203
        }
204

205
        /**
206
         * @return person's gender
207
         */
208
        public String getGender() {
209
                return this.gender;
1 ✔
210
        }
211
        
212
        /**
213
         * @param gender person's gender
214
         */
215
        public void setGender(String gender) {
216
                this.gender = gender;
1 ✔
217
        }
1 ✔
218
        
219
        /**
220
         * @return person's date of birth
221
         */
222
        public Date getBirthdate() {
223
                return this.birthdate;
1 ✔
224
        }
225
        
226
        /**
227
         * @param birthdate person's date of birth
228
         */
229
        public void setBirthdate(Date birthdate) {
230
                this.birthdate = birthdate;
1 ✔
231
        }
1 ✔
232
        
233
        /**
234
         * @return true if person's birthdate is estimated
235
         * @deprecated as of 2.0, use {@link #getBirthdateEstimated()}
236
         */
237
        @Deprecated
238
        @JsonIgnore
239
        public Boolean isBirthdateEstimated() {
240
                return getBirthdateEstimated();
1 ✔
241
        }
242
        
243
        public Boolean getBirthdateEstimated() {
244
                return birthdateEstimated;
1 ✔
245
        }
246
        
247
        /**
248
         * @param birthdateEstimated true if person's birthdate is estimated
249
         */
250
        public void setBirthdateEstimated(Boolean birthdateEstimated) {
251
                this.birthdateEstimated = birthdateEstimated;
1 ✔
252
        }
1 ✔
253
        
254
        public Boolean getDeathdateEstimated() {
255
                return this.deathdateEstimated;
1 ✔
256
        }
257
        
258
        /**
259
         * @param deathdateEstimated true if person's deathdate is estimated
260
         */
261
        public void setDeathdateEstimated(Boolean deathdateEstimated) {
262
                this.deathdateEstimated = deathdateEstimated;
1 ✔
263
        }
1 ✔
264
        
265
        /**
266
         * @param birthtime person's time of birth
267
         */
268
        public void setBirthtime(Date birthtime) {
269
                this.birthtime = birthtime;
1 ✔
270
        }
1 ✔
271
        
272
        /**
273
         * @return person's time of birth with the date portion set to the date from person's birthdate
274
         */
275
        public Date getBirthDateTime() {
276
                if (birthdate != null && birthtime != null) {
1 ✔
277
                        String birthDateString = new SimpleDateFormat("yyyy-MM-dd").format(birthdate);
1 ✔
278
                        String birthTimeString = new SimpleDateFormat("HH:mm:ss").format(birthtime);
1 ✔
279
                        
280
                        try {
281
                                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(birthDateString + " " + birthTimeString);
1 ✔
282
                        }
283
                        catch (ParseException e) {
×
UNCOV
284
                                log.error("Failed to parse birth date string", e);
×
285
                        }
286
                }
287
                return null;
1 ✔
288
        }
289
        
290
        /**
291
         * @return person's time of birth.
292
         */
293
        public Date getBirthtime() {
294
                return this.birthtime;
1 ✔
295
        }
296
        
297
        /**
298
         * @return Returns the death status.
299
         * @deprecated as of 2.0, use {@link #getDead()}
300
         */
301
        @Deprecated
302
        @JsonIgnore
303
        public Boolean isDead() {
304
                return getDead();
1 ✔
305
        }
306
        
307
        /**
308
         * @return Returns the death status.
309
         */
310
        public Boolean getDead() {
311
                return dead;
1 ✔
312
        }
313
        
314
        /**
315
         * @param dead The dead to set.
316
         */
317
        public void setDead(Boolean dead) {
318
                this.dead = dead;
1 ✔
319
        }
1 ✔
320
        
321
        /**
322
         * @return date of person's death
323
         */
324
        public Date getDeathDate() {
325
                return this.deathDate;
1 ✔
326
        }
327
        
328
        /**
329
         * @param deathDate date of person's death
330
         */
331
        public void setDeathDate(Date deathDate) {
332
                this.deathDate = deathDate;
1 ✔
333
                if (deathDate != null) {
1 ✔
334
                        setDead(true);
1 ✔
335
                }
336
        }
1 ✔
337
        
338
        /**
339
         * @return cause of person's death
340
         */
341
        public Concept getCauseOfDeath() {
342
                return this.causeOfDeath;
1 ✔
343
        }
344
        
345
        /**
346
         * @param causeOfDeath cause of person's death
347
         */
348
        public void setCauseOfDeath(Concept causeOfDeath) {
349
                this.causeOfDeath = causeOfDeath;
1 ✔
350
        }
1 ✔
351
        
352
        /**
353
         * This method returns the non coded cause of death
354
         * 
355
         * @return non coded cause of death
356
         * @since 2.2.0
357
         */
358
        public String getCauseOfDeathNonCoded() {
359
                return this.causeOfDeathNonCoded;
1 ✔
360
        }
361
        
362
        /**
363
         * This method sets the non coded cause of death with the value given as parameter
364
         * 
365
         * @param causeOfDeathNonCoded is a String that describes as text the cause of death
366
         * @since 2.2.0
367
         * <strong>Should</strong> not fail with null causeOfDeathNonCoded
368
         * <strong>Should</strong> set the attribute causeOfDeathNonCoded with the given parameter
369
         */
370
        public void setCauseOfDeathNonCoded(String causeOfDeathNonCoded) {
371
                this.causeOfDeathNonCoded = causeOfDeathNonCoded;
1 ✔
372
        }
1 ✔
373
        
374
        /**
375
         * @return list of known addresses for person
376
         * @see org.openmrs.PersonAddress
377
         * <strong>Should</strong> not get voided addresses
378
         * <strong>Should</strong> not fail with null addresses
379
         */
380
        public Set<PersonAddress> getAddresses() {
381
                if (addresses == null) {
1 ✔
382
                        addresses = new TreeSet<>();
1 ✔
383
                }
384
                return this.addresses;
1 ✔
385
        }
386
        
387
        /**
388
         * @param addresses Set&lt;PersonAddress&gt; list of known addresses for person
389
         * @see org.openmrs.PersonAddress
390
         */
391
        public void setAddresses(Set<PersonAddress> addresses) {
392
                this.addresses = CollectionUtil.setCollection(getAddresses(), addresses);
1 ✔
393
        }
1 ✔
394
        
395
        /**
396
         * @return all known names for person
397
         * @see org.openmrs.PersonName
398
         * <strong>Should</strong> not get voided names
399
         * <strong>Should</strong> not fail with null names
400
         */
401
        public Set<PersonName> getNames() {
402
                if (names == null) {
1 ✔
403
                        names = new TreeSet<>();
1 ✔
404
                }
405
                return this.names;
1 ✔
406
        }
407
        
408
        /**
409
         * @param names update all known names for person
410
         * @see org.openmrs.PersonName
411
         */
412
        public void setNames(Set<PersonName> names) {
413
                this.names = CollectionUtil.setCollection(getNames(), names);
1 ✔
414
        }
1 ✔
415
        
416
        /**
417
         * @return all known attributes for person
418
         * @see org.openmrs.PersonAttribute
419
         * <strong>Should</strong> not get voided attributes
420
         * <strong>Should</strong> not fail with null attributes
421
         */
422
        public Set<PersonAttribute> getAttributes() {
423
                if (attributes == null) {
1 ✔
424
                        attributes = new TreeSet<>();
1 ✔
425
                }
426
                return this.attributes;
1 ✔
427
        }
428
        
429
        /**
430
         * Returns only the non-voided attributes for this person
431
         * 
432
         * @return list attributes
433
         * <strong>Should</strong> not get voided attributes
434
         * <strong>Should</strong> not fail with null attributes
435
         */
436
        public List<PersonAttribute> getActiveAttributes() {
437
                List<PersonAttribute> attrs = new ArrayList<>();
1 ✔
438
                for (PersonAttribute attr : getAttributes()) {
1 ✔
439
                        if (!attr.getVoided()) {
1 ✔
440
                                attrs.add(attr);
1 ✔
441
                        }
442
                }
1 ✔
443
                return attrs;
1 ✔
444
        }
445
        
446
        /**
447
         * @param attributes update all known attributes for person
448
         * @see org.openmrs.PersonAttribute
449
         */
450
        public void setAttributes(Set<PersonAttribute> attributes) {
451
                this.attributes = CollectionUtil.setCollection(getAttributes(), attributes);
1 ✔
452
                attributeMap = null;
1 ✔
453
                allAttributeMap = null;
1 ✔
454
        }
1 ✔
455
        
456
        // Convenience methods
457
        
458
        /**
459
         * Convenience method to add the <code>attribute</code> to this person's attribute list if the
460
         * attribute doesn't exist already.<br>
461
         * <br>
462
         * Voids any current attribute with type = <code>newAttribute.getAttributeType()</code><br>
463
         * <br>
464
         * NOTE: This effectively limits persons to only one attribute of any given type **
465
         * 
466
         * @param newAttribute PersonAttribute to add to the Person
467
         * <strong>Should</strong> fail when new attribute exist
468
         * <strong>Should</strong> fail when new atribute are the same type with same value
469
         * <strong>Should</strong> void old attribute when new attribute are the same type with different value
470
         * <strong>Should</strong> remove attribute when old attribute are temporary
471
         * <strong>Should</strong> not save an attribute with a null value
472
         * <strong>Should</strong> not save an attribute with a blank string value
473
         * <strong>Should</strong> void old attribute when a null or blank string value is added
474
         */
475
        public void addAttribute(PersonAttribute newAttribute) {
476
                newAttribute.setPerson(this);
1 ✔
477
                boolean newIsNull = !StringUtils.hasText(newAttribute.getValue());
1 ✔
478
                
479
                for (PersonAttribute currentAttribute : getActiveAttributes()) {
1 ✔
480
                        if (currentAttribute.equals(newAttribute)) {
1 ✔
481
                                // if we have the same PersonAttributeId, don't add the new attribute
482
                                return;
1 ✔
483
                        } else if (currentAttribute.getAttributeType().equals(newAttribute.getAttributeType())) {
1 ✔
484
                                if (currentAttribute.getValue() != null && currentAttribute.getValue().equals(newAttribute.getValue())) {
1 ✔
485
                                        // this person already has this attribute
486
                                        return;
1 ✔
487
                                }
488
                                
489
                                // if the to-be-added attribute isn't already voided itself
490
                                // and if we have the same type, different value
491
                                if (!newAttribute.getVoided() || newIsNull) {
1 ✔
492
                                        if (currentAttribute.getCreator() != null) {
1 ✔
493
                                                currentAttribute.voidAttribute("New value: " + newAttribute.getValue());
1 ✔
494
                                        } else {
495
                                                // remove the attribute if it was just temporary (didn't have a creator
496
                                                // attached to it yet)
UNCOV
497
                                                removeAttribute(currentAttribute);
×
498
                                        }
499
                                }
500
                        }
501
                }
1 ✔
502
                attributeMap = null;
1 ✔
503
                allAttributeMap = null;
1 ✔
504
                if (!OpenmrsUtil.collectionContains(attributes, newAttribute) && !newIsNull) {
1 ✔
505
                        attributes.add(newAttribute);
1 ✔
506
                }
507
        }
1 ✔
508
        
509
        /**
510
         * Convenience method to get the <code>attribute</code> from this person's attribute list if the
511
         * attribute exists already.
512
         * 
513
         * @param attribute
514
         * <strong>Should</strong> not fail when person attribute is null
515
         * <strong>Should</strong> not fail when person attribute is not exist
516
         * <strong>Should</strong> remove attribute when exist
517
         */
518
        public void removeAttribute(PersonAttribute attribute) {
519
                if (attributes != null && attributes.remove(attribute)) {
1 ✔
520
                        attributeMap = null;
1 ✔
521
                        allAttributeMap = null;
1 ✔
522
                }
523
        }
1 ✔
524
        
525
        /**
526
         * Convenience Method to return the first non-voided person attribute matching a person
527
         * attribute type. <br>
528
         * <br>
529
         * Returns null if this person has no non-voided {@link PersonAttribute} with the given
530
         * {@link PersonAttributeType}, the given {@link PersonAttributeType} is null, or this person
531
         * has no attributes.
532
         * 
533
         * @param pat the PersonAttributeType to look for (can be a stub, see
534
         *            {@link PersonAttributeType#equals(Object)} for how its compared)
535
         * @return PersonAttribute that matches the given type
536
         * <strong>Should</strong> not fail when attribute type is null
537
         * <strong>Should</strong> not return voided attribute
538
         * <strong>Should</strong> return null when existing PersonAttributeType is voided
539
         */
540
        public PersonAttribute getAttribute(PersonAttributeType pat) {
541
                if (pat != null) {
1 ✔
542
                        for (PersonAttribute attribute : getAttributes()) {
1 ✔
543
                                if (pat.equals(attribute.getAttributeType()) && !attribute.getVoided()) {
1 ✔
UNCOV
544
                                        return attribute;
×
545
                                }
546
                        }
1 ✔
547
                }
548
                return null;
1 ✔
549
        }
550
        
551
        /**
552
         * Convenience method to get this person's first attribute that has a PersonAttributeType.name
553
         * equal to <code>attributeName</code>.<br>
554
         * <br>
555
         * Returns null if this person has no non-voided {@link PersonAttribute} with the given type
556
         * name, the given name is null, or this person has no attributes.
557
         * 
558
         * @param attributeName the name string to match on
559
         * @return PersonAttribute whose {@link PersonAttributeType#getName()} matchs the given name
560
         *         string
561
         * <strong>Should</strong> return person attribute based on attributeName
562
         * <strong>Should</strong> return null if AttributeName is voided
563
         */
564
        public PersonAttribute getAttribute(String attributeName) {
565
                if (attributeName != null) {
1 ✔
566
                        for (PersonAttribute attribute : getAttributes()) {
1 ✔
567
                                PersonAttributeType type = attribute.getAttributeType();
1 ✔
568
                                if (type != null && attributeName.equals(type.getName()) && !attribute.getVoided()) {
1 ✔
569
                                        return attribute;
1 ✔
570
                                }
571
                        }
1 ✔
572
                }
573
                
574
                return null;
1 ✔
575
        }
576
        
577
        /**
578
         * Convenience method to get this person's first attribute that has a PersonAttributeTypeId
579
         * equal to <code>attributeTypeId</code>.<br>
580
         * <br>
581
         * Returns null if this person has no non-voided {@link PersonAttribute} with the given type id
582
         * or this person has no attributes.<br>
583
         * <br>
584
         * The given id cannot be null.
585
         * 
586
         * @param attributeTypeId the id of the {@link PersonAttributeType} to look for
587
         * @return PersonAttribute whose {@link PersonAttributeType#getId()} equals the given Integer id
588
         * <strong>Should</strong> return PersonAttribute based on attributeTypeId
589
         * <strong>Should</strong> return null when existing personAttribute with matching attribute type id is voided
590
         */
591
        public PersonAttribute getAttribute(Integer attributeTypeId) {
592
                for (PersonAttribute attribute : getActiveAttributes()) {
1 ✔
593
                        if (attributeTypeId.equals(attribute.getAttributeType().getPersonAttributeTypeId())) {
1 ✔
594
                                return attribute;
1 ✔
595
                        }
596
                }
1 ✔
597
                return null;
1 ✔
598
        }
599
        
600
        /**
601
         * Convenience method to get all of this person's attributes that have a
602
         * PersonAttributeType.name equal to <code>attributeName</code>.
603
         * 
604
         * @param attributeName
605
         * <strong>Should</strong> return all PersonAttributes with matching attributeType names
606
         */
607
        public List<PersonAttribute> getAttributes(String attributeName) {
608
                List<PersonAttribute> ret = new ArrayList<>();
1 ✔
609
                
610
                for (PersonAttribute attribute : getActiveAttributes()) {
1 ✔
611
                        PersonAttributeType type = attribute.getAttributeType();
1 ✔
612
                        if (type != null && attributeName.equals(type.getName())) {
1 ✔
613
                                ret.add(attribute);
1 ✔
614
                        }
615
                }
1 ✔
616
                
617
                return ret;
1 ✔
618
        }
619
        
620
        /**
621
         * Convenience method to get all of this person's attributes that have a PersonAttributeType.id
622
         * equal to <code>attributeTypeId</code>.
623
         * 
624
         * @param attributeTypeId
625
         * <strong>Should</strong> return empty list when matching personAttribute by id is voided
626
         * <strong>Should</strong> return list of person attributes based on AttributeTypeId
627
         */
628
        public List<PersonAttribute> getAttributes(Integer attributeTypeId) {
629
                List<PersonAttribute> ret = new ArrayList<>();
1 ✔
630
                
631
                for (PersonAttribute attribute : getActiveAttributes()) {
1 ✔
632
                        if (attributeTypeId.equals(attribute.getAttributeType().getPersonAttributeTypeId())) {
1 ✔
633
                                ret.add(attribute);
1 ✔
634
                        }
635
                }
1 ✔
636
                
637
                return ret;
1 ✔
638
        }
639
        
640
        /**
641
         * Convenience method to get all of this person's attributes that have a PersonAttributeType
642
         * equal to <code>personAttributeType</code>.
643
         * 
644
         * @param personAttributeType
645
         */
646
        public List<PersonAttribute> getAttributes(PersonAttributeType personAttributeType) {
647
                List<PersonAttribute> ret = new ArrayList<>();
×
648
                for (PersonAttribute attribute : getAttributes()) {
×
649
                        if (personAttributeType.equals(attribute.getAttributeType()) && !attribute.getVoided()) {
×
UNCOV
650
                                ret.add(attribute);
×
651
                        }
652
                }
×
UNCOV
653
                return ret;
×
654
        }
655
        
656
        /**
657
         * Convenience method to get this person's active attributes in map form: &lt;String,
658
         * PersonAttribute&gt;.
659
         */
660
        public Map<String, PersonAttribute> getAttributeMap() {
661
                if (attributeMap != null) {
×
UNCOV
662
                        return attributeMap;
×
663
                }
664
                
UNCOV
665
                log.debug("Current Person Attributes: \n{}", printAttributes());
×
666
                
667
                attributeMap = new HashMap<>();
×
668
                for (PersonAttribute attribute : getActiveAttributes()) {
×
669
                        attributeMap.put(attribute.getAttributeType().getName(), attribute);
×
UNCOV
670
                }
×
671
                
UNCOV
672
                return attributeMap;
×
673
        }
674
        
675
        /**
676
         * Convenience method to get all of this person's attributes (including voided ones) in map
677
         * form: &lt;String, PersonAttribute&gt;.
678
         * 
679
         * @return All person's attributes in map form
680
         * @since 1.12
681
         */
682
        public Map<String, PersonAttribute> getAllAttributeMap() {
683
                if (allAttributeMap != null) {
×
UNCOV
684
                        return allAttributeMap;
×
685
                }
686
                
UNCOV
687
                log.debug("Current Person Attributes: \n{}", printAttributes());
×
688
                
689
                allAttributeMap = new HashMap<>();
×
690
                for (PersonAttribute attribute : getAttributes()) {
×
691
                        allAttributeMap.put(attribute.getAttributeType().getName(), attribute);
×
UNCOV
692
                }
×
693
                
UNCOV
694
                return allAttributeMap;
×
695
        }
696
        
697
        /**
698
         * Convenience method for viewing all of the person's current attributes
699
         * 
700
         * @return Returns a string with all the attributes
701
         */
702
        public String printAttributes() {
UNCOV
703
                StringBuilder s = new StringBuilder("");
×
704
                
705
                for (PersonAttribute attribute : getAttributes()) {
×
706
                        s.append(attribute.getAttributeType()).append(" : ").append(attribute.getValue()).append(" : voided? ")
×
707
                                .append(attribute.getVoided()).append("\n");
×
UNCOV
708
                }
×
709
                
UNCOV
710
                return s.toString();
×
711
        }
712
        
713
        /**
714
         * Convenience method to add the <code>name</code> to this person's name list if the name
715
         * doesn't exist already.
716
         * 
717
         * @param name
718
         */
719
        public void addName(PersonName name) {
720
                if (name != null) {
1 ✔
721
                        name.setPerson(this);
1 ✔
722
                        if (names == null) {
1 ✔
723
                                names = new TreeSet<>();
1 ✔
724
                        }
725
                        if (!OpenmrsUtil.collectionContains(names, name)) {
1 ✔
726
                                names.add(name);
1 ✔
727
                        }
728
                }
729
        }
1 ✔
730
        
731
        /**
732
         * Convenience method remove the <code>name</code> from this person's name list if the name
733
         * exists already.
734
         * 
735
         * @param name
736
         */
737
        public void removeName(PersonName name) {
738
                if (names != null) {
1 ✔
739
                        names.remove(name);
1 ✔
740
                }
741
        }
1 ✔
742
        
743
        /**
744
         * Convenience method to add the <code>address</code> to this person's address list if the
745
         * address doesn't exist already.
746
         * 
747
         * @param address
748
         * <strong>Should</strong> not add a person address with blank fields
749
         */
750
        public void addAddress(PersonAddress address) {
751
                if (address != null) {
1 ✔
752
                        address.setPerson(this);
1 ✔
753
                        if (addresses == null) {
1 ✔
754
                                addresses = new TreeSet<>();
1 ✔
755
                        }
756
                        if (!OpenmrsUtil.collectionContains(addresses, address) && !address.isBlank()) {
1 ✔
757
                                addresses.add(address);
1 ✔
758
                        }
759
                }
760
        }
1 ✔
761
        
762
        /**
763
         * Convenience method to remove the <code>address</code> from this person's address list if the
764
         * address exists already.
765
         * 
766
         * @param address
767
         */
768
        public void removeAddress(PersonAddress address) {
769
                if (addresses != null) {
1 ✔
770
                        addresses.remove(address);
1 ✔
771
                }
772
        }
1 ✔
773
        
774
        /**
775
         * Convenience method to get the {@link PersonName} object that is marked as "preferred". <br>
776
         * <br>
777
         * If two names are marked as preferred (or no names), the database ordering comes into effect
778
         * and the one that was created most recently will be returned. <br>
779
         * <br>
780
         * This method will never return a voided name, even if it is marked as preferred. <br>
781
         * <br>
782
         * Null is returned if this person has no names or all voided names.
783
         * 
784
         * @return the "preferred" person name.
785
         * @see #getNames()
786
         * @see PersonName#getPreferred()
787
         * <strong>Should</strong> get preferred and not-voided person name if exist
788
         * <strong>Should</strong> get not-voided person name if preferred address does not exist
789
         * <strong>Should</strong> get voided person address if person is voided and not-voided address does not exist
790
         * <strong>Should</strong> return null if person is not-voided and have voided names
791
         */
792
        public PersonName getPersonName() {
793
                // normally the DAO layer returns these in the correct order, i.e. preferred and non-voided first, but it's possible that someone
794
                // has fetched a Person, changed their names around, and then calls this method, so we have to be careful.
795
                if (getNames() != null && !getNames().isEmpty()) {
1 ✔
796
                        for (PersonName name : getNames()) {
1 ✔
797
                                if (name.getPreferred() && !name.getVoided()) {
1 ✔
798
                                        return name;
1 ✔
799
                                }
800
                        }
1 ✔
801
                        for (PersonName name : getNames()) {
1 ✔
802
                                if (!name.getVoided()) {
1 ✔
803
                                        return name;
1 ✔
804
                                }
805
                        }
1 ✔
806
                        
807
                        if (getVoided()) {
1 ✔
808
                                return getNames().iterator().next();
1 ✔
809
                        }
810
                }
811
                return null;
1 ✔
812
        }
813
        
814
        /**
815
         * Convenience method to get the given name attribute on this person's preferred PersonName
816
         * 
817
         * @return String given name of the person
818
         */
819
        public String getGivenName() {
820
                PersonName personName = getPersonName();
1 ✔
821
                if (personName == null) {
1 ✔
822
                        return "";
1 ✔
823
                } else {
824
                        return personName.getGivenName();
1 ✔
825
                }
826
        }
827
        
828
        /**
829
         * Convenience method to get the middle name attribute on this person's preferred PersonName
830
         * 
831
         * @return String middle name of the person
832
         */
833
        public String getMiddleName() {
834
                PersonName personName = getPersonName();
1 ✔
835
                if (personName == null) {
1 ✔
836
                        return "";
1 ✔
837
                } else {
838
                        return personName.getMiddleName();
1 ✔
839
                }
840
        }
841
        
842
        /**
843
         * Convenience method to get the family name attribute on this person's preferred PersonName
844
         * 
845
         * @return String family name of the person
846
         */
847
        public String getFamilyName() {
848
                PersonName personName = getPersonName();
1 ✔
849
                if (personName == null) {
1 ✔
850
                        return "";
1 ✔
851
                } else {
852
                        return personName.getFamilyName();
1 ✔
853
                }
854
        }
855
        
856
        /**
857
         * Convenience method to get the {@link PersonAddress} object that is marked as "preferred". <br>
858
         * <br>
859
         * If two addresses are marked as preferred (or no addresses), the database ordering comes into
860
         * effect and the one that was created most recently will be returned. <br>
861
         * <br>
862
         * This method will never return a voided address, even if it is marked as preferred. <br>
863
         * <br>
864
         * Null is returned if this person has no addresses or all voided addresses.
865
         * 
866
         * @return the "preferred" person address.
867
         * @see #getAddresses()
868
         * @see PersonAddress#getPreferred()
869
         * <strong>Should</strong> get preferred and not-voided person address if exist
870
         * <strong>Should</strong> get not-voided person address if preferred address does not exist
871
         * <strong>Should</strong> get voided person address if person is voided and not-voided address does not exist
872
         * <strong>Should</strong> return null if person is not-voided and have voided address
873
         */
874
        public PersonAddress getPersonAddress() {
875
                // normally the DAO layer returns these in the correct order, i.e. preferred and non-voided first, but it's possible that someone
876
                // has fetched a Person, changed their addresses around, and then calls this method, so we have to be careful.
877
                if (getAddresses() != null && !getAddresses().isEmpty()) {
1 ✔
878
                        for (PersonAddress addr : getAddresses()) {
1 ✔
879
                                if (addr.getPreferred() && !addr.getVoided()) {
1 ✔
880
                                        return addr;
1 ✔
881
                                }
882
                        }
1 ✔
883
                        for (PersonAddress addr : getAddresses()) {
1 ✔
884
                                if (!addr.getVoided()) {
1 ✔
885
                                        return addr;
1 ✔
886
                                }
887
                        }
1 ✔
888
                        
889
                        if (getVoided()) {
1 ✔
890
                                return getAddresses().iterator().next();
1 ✔
891
                        }
892
                }
893
                return null;
1 ✔
894
        }
895
        
896
        /**
897
         * Convenience method to calculate this person's age based on the birthdate For a person who
898
         * lived 1990 to 2000, age would be -5 in 1985, 5 in 1995, 10 in 2000, and 10 2010.
899
         * 
900
         * @return Returns age as an Integer.
901
         * <strong>Should</strong> get correct age after death
902
         */
903
        public Integer getAge() {
904
                return getAge(null);
1 ✔
905
        }
906
        
907
        /**
908
         * Convenience method: calculates the person's age on a given date based on the birthdate
909
         * 
910
         * @param onDate (null defaults to today)
911
         * @return int value of the person's age
912
         * <strong>Should</strong> get age before birthday
913
         * <strong>Should</strong> get age on birthday with no minutes defined
914
         * <strong>Should</strong> get age on birthday with minutes defined
915
         * <strong>Should</strong> get age after birthday
916
         * <strong>Should</strong> get age after death
917
         * <strong>Should</strong> get age with given date after death
918
         * <strong>Should</strong> get age with given date before death
919
         * <strong>Should</strong> get age with given date before birth
920
         */
921
        public Integer getAge(Date onDate) {
922
                if (birthdate == null) {
1 ✔
923
                        return null;
1 ✔
924
                }
925
                
926
                // Use default end date as today.
927
                Calendar today = Calendar.getInstance();
1 ✔
928
                // But if given, use the given date.
929
                if (onDate != null) {
1 ✔
930
                        today.setTime(onDate);
1 ✔
931
                }
932
                
933
                // If date given is after date of death then use date of death as end date
934
                if (getDeathDate() != null && today.getTime().after(getDeathDate())) {
1 ✔
935
                        today.setTime(getDeathDate());
1 ✔
936
                }
937
                
938
                Calendar bday = Calendar.getInstance();
1 ✔
939
                bday.setTime(birthdate);
1 ✔
940
                
941
                int age = today.get(Calendar.YEAR) - bday.get(Calendar.YEAR);
1 ✔
942
                
943
                // Adjust age when today's date is before the person's birthday
944
                int todaysMonth = today.get(Calendar.MONTH);
1 ✔
945
                int bdayMonth = bday.get(Calendar.MONTH);
1 ✔
946
                int todaysDay = today.get(Calendar.DAY_OF_MONTH);
1 ✔
947
                int bdayDay = bday.get(Calendar.DAY_OF_MONTH);
1 ✔
948
                
949
                if (todaysMonth < bdayMonth) {
1 ✔
UNCOV
950
                        age--;
×
951
                } else if (todaysMonth == bdayMonth && todaysDay < bdayDay) {
1 ✔
952
                        // we're only comparing on month and day, not minutes, etc
953
                        age--;
1 ✔
954
                }
955
                
956
                return age;
1 ✔
957
        }
958

959
        /**
960
         * Method to get the age of a person in months.
961
         *
962
         * @return the age in months as an Integer e.g. 20 (to mean 20 months)
963
         *
964
         * @since 2.7.0
965
         */
966
        public Integer getAgeInMonths() {
967
                return getAgeInChronoUnit(ChronoUnit.MONTHS);
1 ✔
968
        }
969

970
        /**
971
         * Method to get the age of a person in weeks.
972
         *
973
         * @return the age in weeks as an Integer e.g. 20 (to mean 20 weeks)
974
         *
975
         * @since 2.7.0
976
         */
977
        public Integer getAgeInWeeks() {
978
                return getAgeInChronoUnit(ChronoUnit.WEEKS);
1 ✔
979
        }
980

981
        /**
982
         * Method to get the age of a person in days.
983
         *
984
         * @return the age in days as an Integer e.g. 20 (to mean 20 days)
985
         *
986
         * @since 2.7.0
987
         */
988
        public Integer getAgeInDays() {
989
                return getAgeInChronoUnit(ChronoUnit.DAYS);
1 ✔
990
        }
991

992
        /**
993
         * Gets the age of a person with the specified ChronoUnit.
994
         *
995
         * @param chronoUnit the unit of precision for the age calculation (e.g. WEEKS, MONTHS, YEARS)
996
         * @return the age in the specified unit as an Integer
997
         *
998
         * @since 2.7.0
999
         */
1000
        private Integer getAgeInChronoUnit(ChronoUnit chronoUnit) {
1001
                if (this.birthdate == null) {
1 ✔
1002
                        return null;
1 ✔
1003
                }
1004

1005
                LocalDate birthDate = new java.sql.Date(this.birthdate.getTime()).toLocalDate();
1 ✔
1006
                LocalDate endDate = LocalDate.now();
1 ✔
1007

1008
                // If date given is after date of death then use date of death as end date
1009
                if (this.deathDate != null) {
1 ✔
1010
                        LocalDate deathDate = new java.sql.Date(this.deathDate.getTime()).toLocalDate();
1 ✔
1011

1012
                        if (endDate.isAfter(deathDate)) {
1 ✔
1013
                                endDate = deathDate;
1 ✔
1014
                        }
1015
                }
1016

1017
                switch (chronoUnit) {
1 ✔
1018
                        case DAYS:
1019
                                return (int) ChronoUnit.DAYS.between(birthDate, endDate);
1 ✔
1020
                        case WEEKS:
1021
                                return (int) ChronoUnit.WEEKS.between(birthDate, endDate);
1 ✔
1022
                        case MONTHS:
1023
                                return (int) ChronoUnit.MONTHS.between(birthDate, endDate);
1 ✔
1024
                        default:
UNCOV
1025
                                throw new IllegalArgumentException("Unsupported ChronoUnit: " + chronoUnit);
×
1026
                }
1027
        }
1028
        
1029
        /**
1030
         * Convenience method: sets a person's birth date from an age as of the given date Also sets
1031
         * flag indicating that the birth date is inexact. This sets the person's birth date to January
1032
         * 1 of the year that matches this age and date
1033
         * 
1034
         * @param age (the age to set)
1035
         * @param ageOnDate (null defaults to today)
1036
         */
1037
        public void setBirthdateFromAge(int age, Date ageOnDate) {
1038
                Calendar c = Calendar.getInstance();
1 ✔
1039
                c.setTime(ageOnDate == null ? new Date() : ageOnDate);
1 ✔
1040
                c.set(Calendar.DATE, 1);
1 ✔
1041
                c.set(Calendar.MONTH, Calendar.JANUARY);
1 ✔
1042
                c.add(Calendar.YEAR, -1 * age);
1 ✔
1043
                setBirthdate(c.getTime());
1 ✔
1044
                setBirthdateEstimated(true);
1 ✔
1045
                
1046
        }
1 ✔
1047
        
1048
        public User getPersonChangedBy() {
1049
                return personChangedBy;
1 ✔
1050
        }
1051
        
1052
        public void setPersonChangedBy(User changedBy) {
1053
                this.personChangedBy = changedBy;
1 ✔
1054
                this.setChangedBy(changedBy);
1 ✔
1055
        }
1 ✔
1056
        
1057
        public Date getPersonDateChanged() {
1058
                return personDateChanged;
1 ✔
1059
        }
1060
        
1061
        public void setPersonDateChanged(Date dateChanged) {
1062
                this.personDateChanged = dateChanged;
1 ✔
1063
                this.setDateChanged(dateChanged);
1 ✔
1064
        }
1 ✔
1065
        
1066
        public User getPersonCreator() {
1067
                return personCreator;
1 ✔
1068
        }
1069
        
1070
        public void setPersonCreator(User creator) {
1071
                this.personCreator = creator;
1 ✔
1072
                this.setCreator(creator);
1 ✔
1073
        }
1 ✔
1074
        
1075
        public Date getPersonDateCreated() {
1076
                return personDateCreated;
1 ✔
1077
        }
1078
        
1079
        public void setPersonDateCreated(Date dateCreated) {
1080
                this.personDateCreated = dateCreated;
1 ✔
1081
                this.setDateCreated(dateCreated);
1 ✔
1082
        }
1 ✔
1083
        
1084
        public Date getPersonDateVoided() {
1085
                return personDateVoided;
1 ✔
1086
        }
1087
        
1088
        public void setPersonDateVoided(Date dateVoided) {
1089
                this.personDateVoided = dateVoided;
1 ✔
1090
                this.setDateVoided(dateVoided);
1 ✔
1091
        }
1 ✔
1092
        
1093
        public void setPersonVoided(Boolean voided) {
1094
                this.personVoided = voided;
1 ✔
1095
                this.setVoided(voided);
1 ✔
1096
        }
1 ✔
1097
        
1098
        public Boolean getPersonVoided() {
1099
                return personVoided;
1 ✔
1100
        }
1101
        
1102
        /**
1103
         * @deprecated as of 2.0, use {@link #getPersonVoided()}
1104
         */
1105
        @Deprecated
1106
        @JsonIgnore
1107
        public Boolean isPersonVoided() {
UNCOV
1108
                return getPersonVoided();
×
1109
        }
1110
        
1111
        public User getPersonVoidedBy() {
1112
                return personVoidedBy;
1 ✔
1113
        }
1114
        
1115
        public void setPersonVoidedBy(User voidedBy) {
1116
                this.personVoidedBy = voidedBy;
1 ✔
1117
                this.setVoidedBy(voidedBy);
1 ✔
1118
        }
1 ✔
1119
        
1120
        public String getPersonVoidReason() {
1121
                return personVoidReason;
1 ✔
1122
        }
1123
        
1124
        public void setPersonVoidReason(String voidReason) {
1125
                this.personVoidReason = voidReason;
1 ✔
1126
                this.setVoidReason(voidReason);
1 ✔
1127
        }
1 ✔
1128
        
1129
        /**
1130
         * @return true/false whether this person is a patient or not
1131
         * @deprecated as of 2.0, use {@link #getIsPatient()}
1132
         */
1133
        @Deprecated
1134
        @JsonIgnore
1135
        @NotAudited
1136
        public boolean isPatient() {
1137
                return getIsPatient();
1 ✔
1138
        }
1139
        
1140
        @NotAudited
1141
        public boolean getIsPatient() {
1142
                return isPatient;
1 ✔
1143
        }
1144
        
1145
        /**
1146
         * This should only be set by the database layer by looking at whether a row exists in the
1147
         * patient table
1148
         * 
1149
         * @param isPatient whether this person is a patient or not
1150
         */
1151
        protected void setPatient(boolean isPatient) {
1152
                this.isPatient = isPatient;
1 ✔
1153
        }
1 ✔
1154
        
1155
        /**
1156
         * @see java.lang.Object#toString()
1157
         */
1158
        @Override
1159
        public String toString() {
1160
                return "Person(personId=" + personId + ")";
1 ✔
1161
        }
1162
        
1163
        /**
1164
         * @since 1.5
1165
         * @see org.openmrs.OpenmrsObject#getId()
1166
         */
1167
        @Override
1168
        public Integer getId() {
1169
                
1170
                return getPersonId();
1 ✔
1171
        }
1172
        
1173
        /**
1174
         * @since 1.5
1175
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
1176
         */
1177
        @Override
1178
        public void setId(Integer id) {
1179
                setPersonId(id);
1 ✔
1180
                
1181
        }
1 ✔
1182
}
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