|
20 | 20 | import org.openmrs.PatientState; |
21 | 21 | import org.openmrs.api.PatientService; |
22 | 22 | import org.openmrs.api.context.Context; |
| 23 | +import org.openmrs.module.webservices.rest.SimpleObject; |
| 24 | +import org.openmrs.module.webservices.rest.web.ConversionUtil; |
23 | 25 | import org.openmrs.module.webservices.rest.web.RequestContext; |
24 | 26 | import org.openmrs.module.webservices.rest.web.RestConstants; |
25 | 27 | import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; |
|
32 | 34 | import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; |
33 | 35 | import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult; |
34 | 36 | import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; |
| 37 | +import org.openmrs.module.webservices.rest.web.response.IllegalRequestException; |
35 | 38 | import org.openmrs.module.webservices.rest.web.response.ResponseException; |
36 | 39 |
|
| 40 | +import java.util.Date; |
37 | 41 | import java.util.List; |
38 | 42 | import java.util.stream.Collectors; |
39 | 43 |
|
@@ -86,7 +90,64 @@ public PatientProgram newDelegate() { |
86 | 90 | public PatientProgram save(PatientProgram delegate) { |
87 | 91 | return Context.getProgramWorkflowService().savePatientProgram(delegate); |
88 | 92 | } |
89 | | - |
| 93 | + |
| 94 | + @Override |
| 95 | + public Object create(SimpleObject propertiesToCreate, RequestContext context) throws ResponseException { |
| 96 | + Object patientRef = propertiesToCreate.get("patient"); |
| 97 | + if (patientRef != null) { |
| 98 | + Patient patient = Context.getPatientService().getPatientByUuid(patientRef.toString()); |
| 99 | + if (patient != null) { |
| 100 | + Date birthdate = patient.getBirthdate(); |
| 101 | + if (birthdate != null) { |
| 102 | + Object dateEnrolledRaw = propertiesToCreate.get("dateEnrolled"); |
| 103 | + if (dateEnrolledRaw != null) { |
| 104 | + Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class); |
| 105 | + if (dateEnrolled != null && dateEnrolled.before(birthdate)) { |
| 106 | + throw new IllegalRequestException( |
| 107 | + "Enrollment date cannot be before the patient's date of birth"); |
| 108 | + } |
| 109 | + } |
| 110 | + Object dateCompletedRaw = propertiesToCreate.get("dateCompleted"); |
| 111 | + if (dateCompletedRaw != null) { |
| 112 | + Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class); |
| 113 | + if (dateCompleted != null && dateCompleted.before(birthdate)) { |
| 114 | + throw new IllegalRequestException( |
| 115 | + "Completion date cannot be before the patient's date of birth"); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return super.create(propertiesToCreate, context); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Object update(String uuid, SimpleObject propertiesToUpdate, RequestContext context) throws ResponseException { |
| 126 | + PatientProgram patientProgram = getByUniqueId(uuid); |
| 127 | + if (patientProgram != null) { |
| 128 | + Date birthdate = patientProgram.getPatient().getBirthdate(); |
| 129 | + if (birthdate != null) { |
| 130 | + Object dateEnrolledRaw = propertiesToUpdate.get("dateEnrolled"); |
| 131 | + if (dateEnrolledRaw != null) { |
| 132 | + Date dateEnrolled = (Date) ConversionUtil.convert(dateEnrolledRaw, Date.class); |
| 133 | + if (dateEnrolled != null && dateEnrolled.before(birthdate)) { |
| 134 | + throw new IllegalRequestException( |
| 135 | + "Enrollment date cannot be before the patient's date of birth"); |
| 136 | + } |
| 137 | + } |
| 138 | + Object dateCompletedRaw = propertiesToUpdate.get("dateCompleted"); |
| 139 | + if (dateCompletedRaw != null) { |
| 140 | + Date dateCompleted = (Date) ConversionUtil.convert(dateCompletedRaw, Date.class); |
| 141 | + if (dateCompleted != null && dateCompleted.before(birthdate)) { |
| 142 | + throw new IllegalRequestException( |
| 143 | + "Completion date cannot be before the patient's date of birth"); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return super.update(uuid, propertiesToUpdate, context); |
| 149 | + } |
| 150 | + |
90 | 151 | @Override |
91 | 152 | public DelegatingResourceDescription getRepresentationDescription(Representation rep) { |
92 | 153 | if (rep instanceof DefaultRepresentation) { |
|
0 commit comments