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

openmrs / openmrs-core / 16833034264

08 Aug 2025 02:32PM UTC coverage: 63.45% (+0.1%) from 63.332%
16833034264

push

github

ibacher
Switch to using ${project.version} instead of a property

Hopefully this makes the release plugin happy.

21443 of 33795 relevant lines covered (63.45%)

0.63 hits per line

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

92.55
/api/src/main/java/org/openmrs/validator/PatientProgramValidator.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.validator;
11

12
import java.util.Date;
13
import java.util.HashSet;
14
import java.util.Set;
15

16
import org.openmrs.PatientProgram;
17
import org.openmrs.PatientState;
18
import org.openmrs.ProgramWorkflow;
19
import org.openmrs.annotation.Handler;
20
import org.openmrs.api.context.Context;
21
import org.openmrs.messagesource.MessageSourceService;
22
import org.openmrs.util.OpenmrsUtil;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25
import org.springframework.validation.Errors;
26
import org.springframework.validation.ValidationUtils;
27
import org.springframework.validation.Validator;
28

29
/**
30
 * This class validates a {@link PatientProgram} object
31
 *
32
 * @since 1.9
33
 */
34
@Handler(supports = { PatientProgram.class }, order = 50)
35
public class PatientProgramValidator implements Validator {
1✔
36
        
37
        private static final Logger log = LoggerFactory.getLogger(PatientProgramValidator.class);
1✔
38
        
39
        /**
40
         * @see org.springframework.validation.Validator#supports(java.lang.Class)
41
         */
42
        @Override
43
        public boolean supports(Class<?> c) {
44
                return PatientProgram.class.isAssignableFrom(c);
1✔
45
        }
46
        
47
        /**
48
         * Validates the given PatientProgram.
49
         *
50
         * @param obj The patient program to validate.
51
         * @param errors Errors
52
         * @see org.springframework.validation.Validator#validate(java.lang.Object,
53
         *      org.springframework.validation.Errors)
54
         * <strong>Should</strong> fail validation if obj is null
55
         * <strong>Should</strong> fail if the patient field is blank
56
         * <strong>Should</strong> fail if there is more than one patientState with the same states and startDates
57
         * <strong>Should</strong> fail if there is more than one state with a null start date in the same workflow
58
         * <strong>Should</strong> pass if the start date of the first patient state in the work flow is null
59
         * <strong>Should</strong> fail if any patient state has an end date before its start date
60
         * <strong>Should</strong> fail if the program property is null
61
         * <strong>Should</strong> fail if any patient states overlap each other in the same work flow
62
         * <strong>Should</strong> fail if a patientState has an invalid work flow state
63
         * <strong>Should</strong> fail if a patient program has duplicate states in the same work flow
64
         * <strong>Should</strong> fail if a patient is in multiple states in the same work flow
65
         * <strong>Should</strong> fail if a enrolled date is in future at the date it set
66
         * <strong>Should</strong> fail if a completion date is in future at the date it set
67
         * <strong>Should</strong> fail if a patient program has an enroll date after its completion
68
         * <strong>Should</strong> pass if a patient is in multiple states in different work flows
69
         * <strong>Should</strong> pass for a valid program
70
         * <strong>Should</strong> pass for patient states that have the same start dates in the same work flow
71
         * <strong>Should</strong> pass validation if field lengths are correct
72
         * <strong>Should</strong> fail validation if field lengths are not correct
73
         */
74
        @Override
75
        public void validate(Object obj, Errors errors) {
76
                if (log.isDebugEnabled()) {
1✔
77
                        log.debug(this.getClass().getName() + ".validate...");
×
78
                }
79
                
80
                if (obj == null) {
1✔
81
                        throw new IllegalArgumentException("The parameter obj should not be null");
1✔
82
                }
83
                MessageSourceService mss = Context.getMessageSourceService();
1✔
84
                PatientProgram patientProgram = (PatientProgram) obj;
1✔
85
                ValidationUtils.rejectIfEmpty(errors, "patient", "error.required",
1✔
86
                    new Object[] { mss.getMessage("general.patient") });
1✔
87
                ValidationUtils.rejectIfEmpty(errors, "program", "error.required",
1✔
88
                    new Object[] { mss.getMessage("Program.program") });
1✔
89
                
90
                if (errors.hasErrors()) {
1✔
91
                        return;
1✔
92
                }
93
                
94
                ValidationUtils.rejectIfEmpty(errors, "dateEnrolled", "error.patientProgram.enrolledDateEmpty");
1✔
95
                
96
                Date today = new Date();
1✔
97
                if (patientProgram.getDateEnrolled() != null && today.before(patientProgram.getDateEnrolled())) {
1✔
98
                        errors.rejectValue("dateEnrolled", "error.patientProgram.enrolledDateDateCannotBeInFuture");
1✔
99
                }
100
                
101
                if (patientProgram.getDateCompleted() != null && today.before(patientProgram.getDateCompleted())) {
1✔
102
                        errors.rejectValue("dateCompleted", "error.patientProgram.completionDateCannotBeInFuture");
1✔
103
                }
104
                
105
                // if enrollment or complete date of program is in future or complete date has come before enroll date we should throw error
106
                if (patientProgram.getDateEnrolled() != null
1✔
107
                        && OpenmrsUtil.compareWithNullAsLatest(patientProgram.getDateCompleted(), patientProgram.getDateEnrolled()) < 0) {
1✔
108
                        errors.rejectValue("dateCompleted", "error.patientProgram.enrolledDateShouldBeBeforecompletionDate");
1✔
109
                }
110
                
111
                Set<ProgramWorkflow> workFlows = patientProgram.getProgram().getWorkflows();
1✔
112
                //Patient state validation is specific to a work flow
113
                for (ProgramWorkflow workFlow : workFlows) {
1✔
114
                        Set<PatientState> patientStates = patientProgram.getStates();
1✔
115
                        if (patientStates != null) {
1✔
116
                                //Set to store to keep track of unique valid state and start date combinations
117
                                Set<String> statesAndStartDates = new HashSet<>();
1✔
118
                                PatientState latestState = null;
1✔
119
                                boolean foundCurrentPatientState = false;
1✔
120
                                boolean foundStateWithNullStartDate = false;
1✔
121
                                for (PatientState patientState : patientStates) {
1✔
122
                                        if (patientState.getVoided()) {
1✔
123
                                                continue;
1✔
124
                                        }
125
                                        
126
                                        String missingRequiredFieldCode = null;
1✔
127
                                        //only the initial state can have a null start date
128
                                        if (patientState.getStartDate() == null) {
1✔
129
                                                if (foundStateWithNullStartDate) {
1✔
130
                                                        missingRequiredFieldCode = "general.dateStart";
1✔
131
                                                } else {
132
                                                        foundStateWithNullStartDate = true;
1✔
133
                                                }
134
                                        } else if (patientState.getState() == null) {
1✔
135
                                                missingRequiredFieldCode = "State.state";
1✔
136
                                        }
137
                                        
138
                                        if (missingRequiredFieldCode != null) {
1✔
139
                                                errors.rejectValue("states", "PatientState.error.requiredField", new Object[] { mss
1✔
140
                                                        .getMessage(missingRequiredFieldCode) }, null);
1✔
141
                                                return;
1✔
142
                                        }
143
                                        
144
                                        //state should belong to one of the workflows in the program
145
                                        // note that we are iterating over getAllWorkflows() here because we want to include
146
                                        // retired workflows, and the workflows variable does not include retired workflows
147
                                        boolean isValidPatientState = false;
1✔
148
                                        for (ProgramWorkflow wf : patientProgram.getProgram().getAllWorkflows()) {
1✔
149
                                                if (wf.getStates().contains(patientState.getState())) {
1✔
150
                                                        isValidPatientState = true;
1✔
151
                                                        break;
1✔
152
                                                }
153
                                        }
1✔
154
                                        
155
                                        if (!isValidPatientState) {
1✔
156
                                                errors.rejectValue("states", "PatientState.error.invalidPatientState",
1✔
157
                                                    new Object[] { patientState }, null);
158
                                                return;
1✔
159
                                        }
160
                                        
161
                                        //will validate it with other states in its workflow
162
                                        if (!patientState.getState().getProgramWorkflow().equals(workFlow)) {
1✔
163
                                                continue;
1✔
164
                                        }
165
                                        
166
                                        if (OpenmrsUtil.compareWithNullAsLatest(patientState.getEndDate(), patientState.getStartDate()) < 0) {
1✔
167
                                                errors.rejectValue("states", "PatientState.error.endDateCannotBeBeforeStartDate");
1✔
168
                                                return;
1✔
169
                                        } else if (statesAndStartDates.contains(patientState.getState().getUuid() + ""
1✔
170
                                                + patientState.getStartDate())) {
1✔
171
                                                // we already have a patient state with the same work flow state and start date
172
                                                errors.rejectValue("states", "PatientState.error.duplicatePatientStates");
1✔
173
                                                return;
1✔
174
                                        }
175
                                        
176
                                        //Ensure that the patient is only in one state at a given time
177
                                        if (!foundCurrentPatientState && patientState.getEndDate() == null) {
1✔
178
                                                foundCurrentPatientState = true;
1✔
179
                                        } else if (foundCurrentPatientState && patientState.getEndDate() == null) {
1✔
180
                                                errors.rejectValue("states", "PatientProgram.error.cannotBeInMultipleStates");
1✔
181
                                                return;
1✔
182
                                        }
183
                                        
184
                                        if (latestState == null) {
1✔
185
                                                latestState = patientState;
1✔
186
                                        } else {
187
                                                if (patientState.compareTo(latestState) > 0) {
1✔
188
                                                        //patient should have already left this state since it is older
189
                                                        if (latestState.getEndDate() == null) {
1✔
190
                                                                errors.rejectValue("states", "PatientProgram.error.cannotBeInMultipleStates");
×
191
                                                                return;
×
192
                                                        } else if (OpenmrsUtil.compareWithNullAsEarliest(patientState.getStartDate(), latestState
1✔
193
                                                                .getEndDate()) < 0) {
1✔
194
                                                                //current state was started before a previous state was ended
195
                                                                errors.rejectValue("states", "PatientProgram.error.foundOverlappingStates", new Object[] {
1✔
196
                                                                        patientState.getStartDate(), latestState.getEndDate() }, null);
1✔
197
                                                                return;
1✔
198
                                                        }
199
                                                        latestState = patientState;
1✔
200
                                                } else if (patientState.compareTo(latestState) < 0) {
1✔
201
                                                        //patient should have already left this state since it is older
202
                                                        if (patientState.getEndDate() == null) {
1✔
203
                                                                errors.rejectValue("states", "PatientProgram.error.cannotBeInMultipleStates");
×
204
                                                                return;
×
205
                                                        } else if (OpenmrsUtil.compareWithNullAsEarliest(latestState.getStartDate(), patientState
1✔
206
                                                                .getEndDate()) < 0) {
1✔
207
                                                                //latest state was started before a previous state was ended
208
                                                                errors.rejectValue("states", "PatientProgram.error.foundOverlappingStates");
×
209
                                                                return;
×
210
                                                        }
211
                                                }
212
                                        }
213
                                        
214
                                        statesAndStartDates.add(patientState.getState().getUuid() + "" + patientState.getStartDate());
1✔
215
                                }
1✔
216
                        }
217
                }
1✔
218
                ValidateUtil.validateFieldLengths(errors, obj.getClass(), "voidReason");
1✔
219
                //
220
        }
1✔
221
}
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