• 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

55.56
/api/src/main/java/org/openmrs/PatientProgram.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 org.hibernate.envers.Audited;
13
import org.openmrs.util.CollectionUtil;
14
import org.openmrs.customdatatype.CustomValueDescriptor;
15
import org.openmrs.customdatatype.Customizable;
16
import org.openmrs.util.OpenmrsUtil;
17

18
import java.util.ArrayList;
19
import java.util.Collection;
20
import java.util.Collections;
21
import java.util.Date;
22
import java.util.HashMap;
23
import java.util.HashSet;
24
import java.util.LinkedHashSet;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Set;
28

29
/**
30
 * PatientProgram
31
 */
32
@Audited
33
public class PatientProgram extends BaseChangeableOpenmrsData implements Customizable<PatientProgramAttribute>{
34
        
35
        public static final long serialVersionUID = 0L;
36
        
37
        // ******************
38
        // Properties
39
        // ******************
40
        
41
        private Integer patientProgramId;
42
        
43
        private Patient patient;
44
        
45
        private Program program;
46
        
47
        private Location location;
48
        
49
        private Date dateEnrolled;
50
        
51
        private Date dateCompleted;
52
        
53
        private Concept outcome;
54
        
55
        private Set<PatientState> states = new HashSet<>();
1 ✔
56
         
57
        private Set<PatientProgramAttribute> attributes = new LinkedHashSet<>();
1 ✔
58
        
59
        // ******************
60
        // Constructors
61
        // ******************
62
        
63
        /** Default Constructor */
64
        public PatientProgram() {
1 ✔
65
        }
1 ✔
66
        
67
        /** Constructor with id */
68
        public PatientProgram(Integer patientProgramId) {
1 ✔
69
                setPatientProgramId(patientProgramId);
1 ✔
70
        }
1 ✔
71

72
        /**
73
         * Does a mostly-shallow copy of this PatientProgram. Does not copy patientProgramId. The
74
         * 'states' property will be deep-copied.
75
         * 
76
         * @return a shallow copy of this PatientProgram
77
         */
78
        public PatientProgram copy() {
UNCOV
79
                return copyHelper(new PatientProgram());
×
80
        }
81
        
82
        /**
83
         * The purpose of this method is to allow subclasses of PatientProgram to delegate a portion of
84
         * their copy() method back to the superclass, in case the base class implementation changes.
85
         * 
86
         * @param target a PatientProgram that will have the state of <code>this</code> copied into it
87
         * @return the PatientProgram that was passed in, with state copied into it
88
         */
89
        protected PatientProgram copyHelper(PatientProgram target) {
90
                target.setPatient(this.getPatient());
×
91
                target.setProgram(this.getProgram());
×
92
                target.setLocation(this.getLocation());
×
93
                target.setDateEnrolled(this.getDateEnrolled());
×
94
                target.setDateCompleted(target.getDateCompleted());
×
95
                Set<PatientState> statesCopy = new HashSet<>();
×
96
                if (this.getStates() != null) {
×
97
                        for (PatientState s : this.getStates()) {
×
98
                                PatientState stateCopy = s.copy();
×
99
                                stateCopy.setPatientProgram(target);
×
100
                                statesCopy.add(stateCopy);
×
UNCOV
101
                        }
×
102
                }
103
                target.setStates(statesCopy);
×
104
                target.setCreator(this.getCreator());
×
105
                target.setDateCreated(this.getDateCreated());
×
106
                target.setChangedBy(this.getChangedBy());
×
107
                target.setDateChanged(this.getDateChanged());
×
108
                target.setVoided(this.getVoided());
×
109
                target.setVoidedBy(this.getVoidedBy());
×
110
                target.setDateVoided(this.getDateVoided());
×
111
                target.setVoidReason(this.getVoidReason());
×
UNCOV
112
                return target;
×
113
        }
114
        
115
        // ******************
116
        // Instance methods
117
        // ******************
118
        
119
        /**
120
         * Returns true if the associated {@link Patient} is enrolled in the associated {@link Program}
121
         * on the passed {@link Date}
122
         * 
123
         * @param onDate - Date to check for PatientProgram enrollment
124
         * @return boolean - true if the associated {@link Patient} is enrolled in the associated
125
         *         {@link Program} on the passed {@link Date}
126
         */
127
        public boolean getActive(Date onDate) {
128
                if (onDate == null) {
×
UNCOV
129
                        onDate = new Date();
×
130
                }
131
                return !getVoided() && (getDateEnrolled() == null || OpenmrsUtil.compare(getDateEnrolled(), onDate) <= 0)
×
UNCOV
132
                        && (getDateCompleted() == null || OpenmrsUtil.compare(getDateCompleted(), onDate) > 0);
×
133
        }
134
        
135
        /**
136
         * Returns true if the associated {@link Patient} is currently enrolled in the associated
137
         * {@link Program}
138
         * 
139
         * @return boolean - true if the associated {@link Patient} is currently enrolled in the
140
         *         associated {@link Program}
141
         */
142
        public boolean getActive() {
UNCOV
143
                return getActive(null);
×
144
        }
145
        
146
        /**
147
         * Returns the {@link PatientState} associated with this PatientProgram that has an id that
148
         * matches the passed <code>patientStateId</code>
149
         * 
150
         * @param patientStateId - The identifier to use to lookup a {@link PatientState}
151
         * @return PatientState that has an id that matches the passed <code>patientStateId</code>
152
         */
153
        public PatientState getPatientState(Integer patientStateId) {
154
                for (PatientState s : getStates()) {
×
155
                        if (s.getPatientStateId() != null && s.getPatientStateId().equals(patientStateId)) {
×
UNCOV
156
                                return s;
×
157
                        }
158
                }
×
UNCOV
159
                return null;
×
160
        }
161
        
162
        /**
163
         * Attempts to transition the PatientProgram to the passed {@link ProgramWorkflowState} on the
164
         * passed {@link Date} by ending the most recent {@link PatientState} in the
165
         * {@link PatientProgram} and creating a new one with the passed {@link ProgramWorkflowState}
166
         * This will throw an IllegalArgumentException if the transition is invalid
167
         * 
168
         * @param programWorkflowState - The {@link ProgramWorkflowState} to transition to
169
         * @param onDate - The {@link Date} of the transition
170
         * @throws IllegalArgumentException
171
         */
172
        public void transitionToState(ProgramWorkflowState programWorkflowState, Date onDate) {
173
                PatientState lastState = getCurrentState(programWorkflowState.getProgramWorkflow());
1 ✔
174
                if (lastState != null && onDate == null) {
1 ✔
UNCOV
175
                        throw new IllegalArgumentException("You can't change from a non-null state without giving a change date");
×
176
                }
177
                if (lastState != null && lastState.getEndDate() != null) {
1 ✔
UNCOV
178
                        throw new IllegalArgumentException("You can't change out of a state that has an end date already");
×
179
                }
180
                if (lastState != null && lastState.getStartDate() != null
1 ✔
181
                        && OpenmrsUtil.compare(lastState.getStartDate(), onDate) > 0) {
1 ✔
UNCOV
182
                        throw new IllegalArgumentException("You can't change out of a state before that state started");
×
183
                }
184
                if (lastState != null
1 ✔
185
                        && !programWorkflowState.getProgramWorkflow().isLegalTransition(lastState.getState(), programWorkflowState)) {
1 ✔
UNCOV
186
                        throw new IllegalArgumentException("You can't change from state " + lastState.getState() + " to "
×
187
                                + programWorkflowState);
188
                }
189
                if (lastState != null) {
1 ✔
190
                        lastState.setEndDate(onDate);
1 ✔
191
                }
192
                
193
                PatientState newState = new PatientState();
1 ✔
194
                newState.setPatientProgram(this);
1 ✔
195
                newState.setState(programWorkflowState);
1 ✔
196
                newState.setStartDate(onDate);
1 ✔
197

198
                if (newState.getPatientProgram() != null && newState.getPatientProgram().getDateCompleted() != null) {
1 ✔
199
                        newState.setEndDate(newState.getPatientProgram().getDateCompleted());
1 ✔
200
                }
201
                
202
                if (programWorkflowState.getTerminal()) {
1 ✔
203
                        setDateCompleted(onDate);
1 ✔
204
                }
205
                
206
                getStates().add(newState);
1 ✔
207
        }
1 ✔
208
        
209
        /**
210
         * Attempts to void the latest {@link PatientState} in the {@link PatientProgram} If earlier
211
         * PatientStates exist, it will try to reset the endDate to null so that the next latest state
212
         * becomes the current {@link PatientState}
213
         * 
214
         * @param workflow - The {@link ProgramWorkflow} whose last {@link PatientState} within the
215
         *            current {@link PatientProgram} we want to void
216
         * @param voidBy - The user who is voiding the {@link PatientState}
217
         * @param voidDate - The date to void the {@link PatientState}
218
         * @param voidReason - The reason for voiding the {@link PatientState}
219
         * <strong>Should</strong> void state with endDate null if startDates equal
220
         */
221
        public void voidLastState(ProgramWorkflow workflow, User voidBy, Date voidDate, String voidReason) {
222
                List<PatientState> states = statesInWorkflow(workflow, false);
1 ✔
223
                if (voidDate == null) {
1 ✔
UNCOV
224
                        voidDate = new Date();
×
225
                }
226
                PatientState last = null;
1 ✔
227
                PatientState nextToLast = null;
1 ✔
228
                if (!states.isEmpty()) {
1 ✔
229
                        last = states.get(states.size() - 1);
1 ✔
230
                }
231
                if (states.size() > 1) {
1 ✔
232
                        nextToLast = states.get(states.size() - 2);
1 ✔
233
                }
234
                if (last != null) {
1 ✔
235
                        last.setVoided(true);
1 ✔
236
                        last.setVoidedBy(voidBy);
1 ✔
237
                        last.setDateVoided(voidDate);
1 ✔
238
                        last.setVoidReason(voidReason);
1 ✔
239
                }
240
                if (nextToLast != null && nextToLast.getEndDate() != null) {
1 ✔
241
                        nextToLast.setEndDate(nextToLast.getPatientProgram() != null
1 ✔
242
                                && nextToLast.getPatientProgram().getDateCompleted() != null ? nextToLast.getPatientProgram()
1 ✔
243
                                .getDateCompleted() : null);
1 ✔
244
                        nextToLast.setDateChanged(voidDate);
1 ✔
245
                        nextToLast.setChangedBy(voidBy);
1 ✔
246
                }
247
        }
1 ✔
248
        
249
        /**
250
         * Returns the current {@link PatientState} for the passed {@link ProgramWorkflow} within this
251
         * {@link PatientProgram}.
252
         * 
253
         * @param programWorkflow The ProgramWorkflow whose current {@link PatientState} we want to
254
         *            retrieve
255
         * @return PatientState The current {@link PatientState} for the passed {@link ProgramWorkflow}
256
         *         within this {@link PatientProgram}
257
         */
258
        public PatientState getCurrentState(ProgramWorkflow programWorkflow) {
259
                Date now = new Date();
1 ✔
260
                PatientState currentState = null;
1 ✔
261
                
262
                for (PatientState state : getSortedStates()) {
1 ✔
263
                        //states are sorted with the most current state at the last position
264
                        if ((programWorkflow == null || state.getState().getProgramWorkflow().equals(programWorkflow))
1 ✔
265
                                && state.getActive(now)) {
1 ✔
266
                                currentState = state;
1 ✔
267
                        }
268
                }
1 ✔
269
                return currentState;
1 ✔
270
        }
271
        
272
        /**
273
         * Returns a Set&lt;PatientState&gt; of all current {@link PatientState}s for the
274
         * {@link PatientProgram}
275
         * 
276
         * @return Set&lt;PatientState&gt; of all current {@link PatientState}s for the {@link PatientProgram}
277
         */
278
        public Set<PatientState> getCurrentStates() {
279
                Set<PatientState> ret = new HashSet<>();
×
280
                Date now = new Date();
×
281
                for (PatientState state : getStates()) {
×
282
                        if (state.getActive(now)) {
×
UNCOV
283
                                ret.add(state);
×
284
                        }
285
                }
×
UNCOV
286
                return ret;
×
287
        }
288
        
289
        /**
290
         * Returns a Set&lt;PatientState&gt; of all recent {@link PatientState}s for each workflow of the
291
         * {@link PatientProgram}
292
         *
293
         * @return Set&lt;PatientState&gt; of all recent {@link PatientState}s for the {@link PatientProgram}
294
         */
295
        public Set<PatientState> getMostRecentStateInEachWorkflow() {
296
                HashMap<ProgramWorkflow,PatientState> map = new HashMap<>();
1 ✔
297

298
                for (PatientState state : getSortedStates()) {
1 ✔
299
                        if (!state.isVoided()) {
1 ✔
300
                                ProgramWorkflow workflow = state.getState().getProgramWorkflow();
1 ✔
301
                                map.put(workflow,state);
1 ✔
302
                        }
303
                }
1 ✔
304

305
                Set<PatientState> ret = new HashSet<>();
1 ✔
306
                for (Map.Entry<ProgramWorkflow, PatientState> entry : map.entrySet()) {
1 ✔
307
                        ret.add(entry.getValue());
1 ✔
308
                }
1 ✔
309

310
                return ret;
1 ✔
311
        }
312

313
        /**
314
         * Returns a List&lt;PatientState&gt; of all {@link PatientState}s in the passed
315
         * {@link ProgramWorkflow} for the {@link PatientProgram}
316
         * 
317
         * @param programWorkflow - The {@link ProgramWorkflow} to check
318
         * @param includeVoided - If true, return voided {@link PatientState}s in the returned
319
         *            {@link List}
320
         * @return List&lt;PatientState&gt; of all {@link PatientState}s in the passed {@link ProgramWorkflow}
321
         *         for the {@link PatientProgram}
322
         */
323
        public List<PatientState> statesInWorkflow(ProgramWorkflow programWorkflow, boolean includeVoided) {
324
                List<PatientState> ret = new ArrayList<>();
1 ✔
325
                for (PatientState st : getSortedStates()) {
1 ✔
326
                        if (st.getState().getProgramWorkflow().equals(programWorkflow) && (includeVoided || !st.getVoided())) {
1 ✔
327
                                ret.add(st);
1 ✔
328
                        }
329
                }
1 ✔
330
                return ret;
1 ✔
331
        }
332
        
333
        /** @see Object#toString() */
334
        @Override
335
        public String toString() {
336
                return "PatientProgram(id=" + getPatientProgramId() + ", patient=" + getPatient() + ", program=" + getProgram()
1 ✔
337
                        + ")";
338
        }
339
        
340
        // ******************
341
        // Property Access
342
        // ******************
343
        
344
        public Concept getOutcome() {
345
                return outcome;
1 ✔
346
        }
347
        
348
        public void setOutcome(Concept concept) {
349
                this.outcome = concept;
1 ✔
350
        }
1 ✔
351
        
352
        public Date getDateCompleted() {
353
                return dateCompleted;
1 ✔
354
        }
355
        
356
        public void setDateCompleted(Date dateCompleted) {
357
                this.dateCompleted = dateCompleted;
1 ✔
358
        }
1 ✔
359
        
360
        public Date getDateEnrolled() {
361
                return dateEnrolled;
1 ✔
362
        }
363
        
364
        public void setDateEnrolled(Date dateEnrolled) {
365
                this.dateEnrolled = dateEnrolled;
1 ✔
366
        }
1 ✔
367
        
368
        public Patient getPatient() {
369
                return patient;
1 ✔
370
        }
371
        
372
        public void setPatient(Patient patient) {
373
                this.patient = patient;
1 ✔
374
        }
1 ✔
375
        
376
        public Integer getPatientProgramId() {
377
                return patientProgramId;
1 ✔
378
        }
379
        
380
        public void setPatientProgramId(Integer patientProgramId) {
381
                this.patientProgramId = patientProgramId;
1 ✔
382
        }
1 ✔
383
        
384
        public Program getProgram() {
385
                return program;
1 ✔
386
        }
387
        
388
        public void setProgram(Program program) {
389
                this.program = program;
1 ✔
390
        }
1 ✔
391
        
392
        public Set<PatientState> getStates() {
393
                return states;
1 ✔
394
        }
395
        
396
        public void setStates(Set<PatientState> states) {
397
                this.states = CollectionUtil.setCollection(this.states, states);
1 ✔
398
        }
1 ✔
399
        
400
        /**
401
         * @since 1.5
402
         * @see org.openmrs.OpenmrsObject#getId()
403
         */
404
        @Override
405
        public Integer getId() {
406
                return getPatientProgramId();
1 ✔
407
        }
408
        
409
        /**
410
         * @since 1.5
411
         * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
412
         */
413
        @Override
414
        public void setId(Integer id) {
415
                setPatientProgramId(id);
×
UNCOV
416
        }
×
417
        
418
        /**
419
         * @since 1.8
420
         * @return the location
421
         */
422
        public Location getLocation() {
423
                return location;
1 ✔
424
        }
425
        
426
        /**
427
         * @since 1.8
428
         * @param location the location to set
429
         */
430
        public void setLocation(Location location) {
431
                this.location = location;
1 ✔
432
        }
1 ✔
433
        
434
        /**
435
         * @return states sorted by {@link PatientState#compareTo(PatientState)}
436
         */
437
        private List<PatientState> getSortedStates() {
438
                List<PatientState> sortedStates = new ArrayList<>(getStates());
1 ✔
439
                Collections.sort(sortedStates);
1 ✔
440
                return sortedStates;
1 ✔
441
        }
442

443
        @Override
444
        public Set<PatientProgramAttribute> getAttributes() {
445
            return attributes;
1 ✔
446
        }
447

448
        @Override
449
        public Collection<PatientProgramAttribute> getActiveAttributes() {
UNCOV
450
            ArrayList<PatientProgramAttribute> ret = new ArrayList<>();
×
451

452
            if (this.getAttributes() != null) {
×
453
                for (PatientProgramAttribute attr : this.getAttributes()) {
×
454
                    if (!attr.isVoided()) {
×
UNCOV
455
                        ret.add(attr);
×
456
                    }
UNCOV
457
                }
×
458
            }
459

UNCOV
460
            return ret;
×
461
        }
462

463
        @Override
464
        public List<PatientProgramAttribute> getActiveAttributes(CustomValueDescriptor ofType) {
UNCOV
465
            ArrayList<PatientProgramAttribute> ret = new ArrayList<>();
×
466

467
            if (this.getAttributes() != null) {
×
468
                for (PatientProgramAttribute attr : this.getAttributes()) {
×
469
                    if (attr.getAttributeType().equals(ofType) && !attr.isVoided()) {
×
UNCOV
470
                        ret.add(attr);
×
471
                    }
UNCOV
472
                }
×
473
            }
474

UNCOV
475
            return ret;
×
476
        }
477

478
        @Override
479
        public void addAttribute(PatientProgramAttribute attribute) {
480
            if (this.getAttributes() == null) {
1 ✔
UNCOV
481
                this.setAttributes(new LinkedHashSet<>());
×
482
            }
483

484
            this.getAttributes().add(attribute);
1 ✔
485
            attribute.setOwner(this);
1 ✔
486
        }
1 ✔
487

488
        public void setAttributes(Set<PatientProgramAttribute> attributes) {
UNCOV
489
            this.attributes = CollectionUtil.setCollection(this.attributes, attributes);
×
UNCOV
490
        }
×
491

492
        public void setAttribute(PatientProgramAttribute attribute) {
493
            if (this.getAttributes() == null) {
×
UNCOV
494
                this.addAttribute(attribute);
×
495
            } else {
496
                if (this.getActiveAttributes(attribute.getAttributeType()).size() == 1) {
×
497
                    PatientProgramAttribute patientProgramAttribute = this.getActiveAttributes(attribute.getAttributeType()).get(0);
×
498
                    if (!patientProgramAttribute.getValue().equals(attribute.getValue())) {
×
499
                        if (patientProgramAttribute.getId() != null) {
×
UNCOV
500
                            patientProgramAttribute.setVoided(Boolean.TRUE);
×
501
                        } else {
UNCOV
502
                            this.getAttributes().remove(patientProgramAttribute);
×
503
                        }
504

505
                        this.getAttributes().add(attribute);
×
UNCOV
506
                        attribute.setOwner(this);
×
507
                    }
508
                } else {
×
509
                    for (PatientProgramAttribute existing : this.getActiveAttributes(attribute.getAttributeType())) {
×
510
                        if (existing.getAttributeType().equals(attribute.getAttributeType())) {
×
511
                            if (existing.getId() != null) {
×
UNCOV
512
                                existing.setVoided(Boolean.TRUE);
×
513
                            } else {
UNCOV
514
                                this.getAttributes().remove(existing);
×
515
                            }
516
                        }
UNCOV
517
                    }
×
518

519
                    this.getAttributes().add(attribute);
×
UNCOV
520
                    attribute.setOwner(this);
×
521
                }
522
            }
UNCOV
523
        }
×
524
}
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