/
OrderResource1_8.java
312 lines (288 loc) · 13.2 KB
/
OrderResource1_8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;
import java.util.List;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.api.OrderService.ORDER_STATUS;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.api.RestService;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
import org.openmrs.module.webservices.rest.web.resource.impl.DataDelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.openmrs.module.webservices.rest.web.response.ResponseException;
/**
* Resource for {@link Order} and all of its subclasses
*/
@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "1.8.*",
"1.9.*" }, order = 1)
public class OrderResource1_8 extends DataDelegatingCrudResource<Order> {
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#hasTypesDefined()
*/
@Override
public boolean hasTypesDefined() {
return true;
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getByUniqueId(java.lang.String)
*/
@Override
public Order getByUniqueId(String uniqueId) {
return Context.getOrderService().getOrderByUuid(uniqueId);
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#newDelegate()
*/
@Override
public Order newDelegate() {
return new Order();
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceHandler#save(java.lang.Object)
*/
@Override
public Order save(Order delegate) {
return Context.getOrderService().saveOrder(delegate);
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#delete(java.lang.Object,
* java.lang.String, org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
protected void delete(Order delegate, String reason, RequestContext context) throws ResponseException {
if (delegate.isVoided()) {
// DELETE is idempotent, so we return success here
return;
}
Context.getOrderService().voidOrder(delegate, reason);
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#purge(java.lang.Object,
* org.openmrs.module.webservices.rest.web.RequestContext)
*/
@Override
public void purge(Order delegate, RequestContext context) throws ResponseException {
Context.getOrderService().purgeOrder(delegate);
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext)
* @should return all Orders (including retired) if context.includeAll is set
*/
@Override
protected PageableResult doGetAll(RequestContext context) throws ResponseException {
//ORDER_STATUS.ANY is used to specify that all orders, including voided ones should be retrieved
ORDER_STATUS orderStatus = context.getIncludeAll() ? ORDER_STATUS.ANY : null;
return new NeedsPaging<Order>(Context.getOrderService().getOrders(Order.class, null, null, orderStatus, null, null,
null), context);
}
/**
* Display string for {@link Order}
*
* @param order
* @return ConceptName
*/
@PropertyGetter("display")
public String getDisplayString(Order order) {
if (order.getConcept() == null)
return "[No Concept]";
return order.getConcept().getName().getName();
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation)
*/
@Override
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
if (rep instanceof DefaultRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("display");
description.addProperty("orderType", Representation.REF);
description.addProperty("patient", Representation.REF);
description.addProperty("concept", Representation.REF);
description.addProperty("instructions");
description.addProperty("startDate");
description.addProperty("autoExpireDate");
description.addProperty("encounter", Representation.REF);
description.addProperty("orderer", Representation.REF);
description.addProperty("accessionNumber");
description.addProperty("discontinuedBy", Representation.REF);
description.addProperty("discontinuedDate");
description.addProperty("discontinuedReason");
description.addProperty("discontinuedReasonNonCoded");
description.addProperty("voided");
description.addSelfLink();
description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
return description;
} else if (rep instanceof FullRepresentation) {
DelegatingResourceDescription description = new DelegatingResourceDescription();
description.addProperty("uuid");
description.addProperty("display");
description.addProperty("orderType");
description.addProperty("patient");
description.addProperty("concept");
description.addProperty("instructions");
description.addProperty("startDate");
description.addProperty("autoExpireDate");
description.addProperty("encounter");
description.addProperty("orderer");
description.addProperty("accessionNumber");
description.addProperty("discontinuedBy");
description.addProperty("discontinuedDate");
description.addProperty("discontinuedReason");
description.addProperty("discontinuedReasonNonCoded");
description.addProperty("voided");
description.addProperty("auditInfo");
description.addSelfLink();
return description;
} else {
return null;
}
}
/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
*/
@Override
public DelegatingResourceDescription getCreatableProperties() {
DelegatingResourceDescription d = new DelegatingResourceDescription();
d.addRequiredProperty("orderType");
d.addRequiredProperty("patient");
d.addRequiredProperty("concept");
d.addProperty("instructions");
d.addProperty("startDate");
d.addProperty("autoExpireDate");
d.addProperty("encounter");
d.addProperty("orderer");
d.addProperty("discontinuedBy");
d.addProperty("discontinuedDate");
d.addProperty("discontinuedReason");
d.addProperty("discontinuedReasonNonCoded");
d.addProperty("accessionNumber");
return d;
}
@Override
public Model getGETModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getGETModel(rep);
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
model
.property("uuid", new StringProperty())
.property("display", new StringProperty())
.property("instructions", new StringProperty())
.property("startDate", new DateProperty())
.property("autoExpireDate", new DateProperty())
.property("accessionNumber", new StringProperty())
.property("discontinuedDate", new DateProperty())
.property("discontinuedReasonNonCoded", new StringProperty())
.property("voided", new BooleanProperty());
}
if (rep instanceof DefaultRepresentation) {
model
.property("orderType", new RefProperty("#/definitions/OrdertypeGetRef"))
.property("patient", new RefProperty("#/definitions/PatientGetRef"))
.property("concept", new RefProperty("#/definitions/ConceptGetRef"))
.property("encounter", new RefProperty("#/definitions/EncounterGetRef"))
.property("orderer", new RefProperty("#/definitions/UserGetRef"))
.property("discontinuedBy", new RefProperty("#/definitions/UserGetRef"))
.property("discontinuedReason", new RefProperty("#/definitions/ConceptGetRef"));
} else if (rep instanceof FullRepresentation) {
model
.property("orderType", new RefProperty("#/definitions/OrdertypeGet"))
.property("patient", new RefProperty("#/definitions/PatientGet"))
.property("concept", new RefProperty("#/definitions/ConceptGet"))
.property("encounter", new RefProperty("#/definitions/EncounterGet"))
.property("orderer", new RefProperty("#/definitions/UserGet"))
.property("discontinuedBy", new RefProperty("#/definitions/UserGet"))
.property("discontinuedReason", new RefProperty("#/definitions/ConceptGet"));
}
return model;
}
@Override
public Model getCREATEModel(Representation rep) {
ModelImpl model = new ModelImpl()
.property("orderType", new StringProperty().example("uuid"))
.property("patient", new StringProperty().example("uuid"))
.property("concept", new StringProperty().example("uuid"))
.property("instructions", new StringProperty())
.property("startDate", new DateProperty())
.property("autoExpireDate", new DateProperty())
.property("encounter", new StringProperty().example("uuid"))
.property("orderer", new StringProperty().example("uuid"))
.property("discontinuedBy", new StringProperty().example("uuid"))
.property("discontinuedDate", new DateProperty())
.property("discontinuedReason", new RefProperty("#/definitions/ConceptCreate"))
.property("discontinuedReasonNonCoded", new StringProperty())
.property("accessionNumber", new StringProperty())
.required("orderType").required("patient").required("concept");
if (rep instanceof FullRepresentation) {
model
.property("orderType", new RefProperty("#/definitions/OrdertypeCreate"))
.property("patient", new RefProperty("#/definitions/PatientCreate"))
.property("concept", new RefProperty("#/definitions/ConceptCreate"))
.property("encounter", new RefProperty("#/definitions/EncounterCreate"))
.property("orderer", new RefProperty("#/definitions/UserCreate"))
.property("discontinuedBy", new RefProperty("#/definitions/UserCreate"))
.property("discontinuedReason", new RefProperty("#/definitions/ConceptCreate"));
}
//FIXME missing prop: type
return model;
}
@Override
public Model getUPDATEModel(Representation rep) {
return new ModelImpl(); //FIXME missing props
}
/**
* Gets orders by given patient (paged according to context if necessary) only if a patient
* parameter exists in the request set on the {@link RequestContext} otherwise
*
* @param context
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext)
* @return all orders for a given patient (possibly filtered by context.type)
*/
@Override
protected PageableResult doSearch(RequestContext context) {
String patientUuid = context.getRequest().getParameter("patient");
if (patientUuid != null) {
Patient patient = ((PatientResource1_8) Context.getService(RestService.class).getResourceBySupportedClass(
Patient.class)).getByUniqueId(patientUuid);
if (patient == null)
return new EmptySearchResult();
// if the user indicated a specific type, try to delegate to the appropriate subclass handler
if (context.getType() != null) {
PageableResult ret = (PageableResult) findAndInvokeSubclassHandlerMethod(context.getType(),
"getOrdersByPatient", patient, context);
if (ret != null)
return ret;
}
List<Order> orders = Context.getOrderService().getOrdersByPatient(patient);
// if the user indicated a specific type, and we couldn't delegate to a subclass handler above, filter here
if (context.getType() != null) {
filterByType(orders, context.getType());
}
return new NeedsPaging<Order>(orders, context);
}
//currently this is not supported since the superclass throws an exception
return super.doSearch(context);
}
}