Guest User

Untitled

a guest
Mar 7th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.91 KB | None | 0 0
  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.module.webservices.rest.web.v1_0.resource.openmrs1_10;
  11.  
  12. import io.swagger.models.Model;
  13. import io.swagger.models.ModelImpl;
  14. import io.swagger.models.properties.DateProperty;
  15. import io.swagger.models.properties.RefProperty;
  16. import io.swagger.models.properties.StringProperty;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.openmrs.*;
  19. import org.openmrs.api.OrderContext;
  20. import org.openmrs.api.context.Context;
  21. import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty;
  22. import org.openmrs.module.webservices.rest.web.ConversionUtil;
  23. import org.openmrs.module.webservices.rest.web.RequestContext;
  24. import org.openmrs.module.webservices.rest.web.RestConstants;
  25. import org.openmrs.module.webservices.rest.web.annotation.Resource;
  26. import org.openmrs.module.webservices.rest.web.api.RestService;
  27. import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
  28. import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
  29. import org.openmrs.module.webservices.rest.web.representation.Representation;
  30. import org.openmrs.module.webservices.rest.web.resource.api.PageableResult;
  31. import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
  32. import org.openmrs.module.webservices.rest.web.resource.impl.EmptySearchResult;
  33. import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
  34. import org.openmrs.module.webservices.rest.web.response.ObjectNotFoundException;
  35. import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
  36. import org.openmrs.module.webservices.rest.web.response.ResponseException;
  37. import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.OrderResource1_8;
  38. import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8;
  39.  
  40. import java.util.*;
  41.  
  42. /**
  43. * {@link org.openmrs.module.webservices.rest.web.annotation.Resource} for {@link org.openmrs.Order}
  44. * , supporting standard CRUD operations
  45. */
  46. @Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "1.10.*",
  47. "1.11.*", "1.12.*", "2.0.*", "2.1.*" })
  48. public class OrderResource1_10 extends OrderResource1_8 {
  49.  
  50. /**
  51. * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation)
  52. */
  53. @Override
  54. public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
  55. if (rep instanceof DefaultRepresentation) {
  56. DelegatingResourceDescription description = new DelegatingResourceDescription();
  57. description.addProperty("uuid");
  58. description.addProperty("orderNumber");
  59. description.addProperty("accessionNumber");
  60. description.addProperty("patient", Representation.REF);
  61. description.addProperty("concept", Representation.REF);
  62. description.addProperty("action");
  63. description.addProperty("careSetting", Representation.REF);
  64. description.addProperty("previousOrder", Representation.REF);
  65. description.addProperty("dateActivated");
  66. description.addProperty("scheduledDate");
  67. description.addProperty("dateStopped");
  68. description.addProperty("autoExpireDate");
  69. description.addProperty("encounter", Representation.REF);
  70. description.addProperty("orderer", Representation.REF);
  71. description.addProperty("orderReason", Representation.REF);
  72. description.addProperty("orderReasonNonCoded");
  73. description.addProperty("orderType");
  74. description.addProperty("urgency");
  75. description.addProperty("instructions");
  76. description.addProperty("commentToFulfiller");
  77. description.addProperty("display");
  78. description.addSelfLink();
  79. description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL);
  80. return description;
  81. } else if (rep instanceof FullRepresentation) {
  82. DelegatingResourceDescription description = new DelegatingResourceDescription();
  83. description.addProperty("uuid");
  84. description.addProperty("orderNumber");
  85. description.addProperty("accessionNumber");
  86. description.addProperty("patient", Representation.REF);
  87. description.addProperty("concept", Representation.REF);
  88. description.addProperty("action");
  89. description.addProperty("careSetting", Representation.DEFAULT);
  90. description.addProperty("previousOrder", Representation.REF);
  91. description.addProperty("dateActivated");
  92. description.addProperty("scheduledDate");
  93. description.addProperty("dateStopped");
  94. description.addProperty("autoExpireDate");
  95. description.addProperty("encounter", Representation.REF);
  96. description.addProperty("orderer", Representation.REF);
  97. description.addProperty("orderReason", Representation.REF);
  98. description.addProperty("orderReasonNonCoded");
  99. description.addProperty("orderType");
  100. description.addProperty("urgency");
  101. description.addProperty("instructions");
  102. description.addProperty("commentToFulfiller");
  103. description.addProperty("display");
  104. description.addProperty("auditInfo");
  105. description.addSelfLink();
  106. return description;
  107. } else {
  108. return null;
  109. }
  110. }
  111.  
  112. @Override
  113. public Model getCREATEModel(Representation rep) {
  114. ModelImpl model = new ModelImpl()
  115. .property("encounter", new StringProperty().example("uuid"))
  116. .property("orderType", new StringProperty().example("uuid"))
  117. .property("action", new EnumProperty(Order.Action.class))
  118. .property("accessionNumber", new StringProperty())
  119. .property("dateActivated", new DateProperty())
  120. .property("scheduledDate", new DateProperty())
  121. .property("patient", new StringProperty().example("uuid"))
  122. .property("concept", new StringProperty().example("uuid"))
  123. .property("careSetting", new StringProperty().example("uuid"))
  124. .property("dateStopped", new DateProperty())
  125. .property("autoExpireDate", new DateProperty())
  126. .property("orderer", new StringProperty().example("uuid"))
  127. .property("previousOrder", new StringProperty().example("uuid"))
  128. .property("urgency", new EnumProperty(Order.Urgency.class))
  129. .property("orderReason", new StringProperty().example("uuid"))
  130. .property("orderReasonNonCoded", new StringProperty())
  131. .property("instructions", new StringProperty())
  132. .property("commentToFulfiller", new StringProperty())
  133.  
  134. .required("orderType").required("patient").required("concept");
  135. if (rep instanceof FullRepresentation) {
  136. model
  137. .property("encounter", new RefProperty("#/definitions/EncounterCreate"))
  138. .property("patient", new RefProperty("#/definitions/PatientCreate"))
  139. .property("concept", new RefProperty("#/definitions/ConceptCreate"))
  140. .property("orderer", new RefProperty("#/definitions/UserCreate"))
  141. .property("previousOrder", new RefProperty("#/definitions/OrderCreate"))
  142. .property("orderReason", new RefProperty("#/definitions/ConceptCreate"));
  143. }
  144. //FIXME missing prop: type
  145. return model;
  146. }
  147.  
  148. /**
  149. * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
  150. */
  151. @Override
  152. public DelegatingResourceDescription getCreatableProperties() {
  153. DelegatingResourceDescription d = new DelegatingResourceDescription();
  154. d.addRequiredProperty("encounter");
  155. d.addProperty("action");
  156. d.addProperty("accessionNumber");
  157. d.addProperty("dateActivated");
  158. d.addProperty("scheduledDate");
  159. d.addProperty("patient");
  160. d.addProperty("concept");
  161. d.addProperty("careSetting");
  162. d.addProperty("dateStopped");
  163. d.addProperty("autoExpireDate");
  164. d.addProperty("orderer");
  165. d.addProperty("previousOrder");
  166. d.addProperty("urgency");
  167. d.addProperty("orderType");
  168. d.addProperty("orderReason");
  169. d.addProperty("orderReasonNonCoded");
  170. d.addProperty("instructions");
  171. d.addProperty("commentToFulfiller");
  172. return d;
  173. }
  174.  
  175. /**
  176. * Fetches an order by uuid or order number
  177. *
  178. * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getByUniqueId(String)
  179. */
  180. @Override
  181. public Order getByUniqueId(String uniqueId) {
  182. Order order = super.getByUniqueId(uniqueId);
  183. if (order == null) {
  184. order = Context.getOrderService().getOrderByOrderNumber(uniqueId);
  185. }
  186. return order;
  187. }
  188.  
  189. /**
  190. * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext)
  191. */
  192. @Override
  193. protected PageableResult doGetAll(RequestContext context) throws ResponseException {
  194. throw new ResourceDoesNotSupportOperationException();
  195. }
  196.  
  197. @Override
  198. public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
  199. throw new ResourceDoesNotSupportOperationException();
  200. }
  201.  
  202. /**
  203. * Gets orders by given patient (paged according to context if necessary) only if a patient
  204. * parameter exists in the request set on the {@link RequestContext}, optional careSetting,
  205. * orderType, asOfDate request parameters can be specified to filter on
  206. *
  207. * @param context
  208. * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext)
  209. * @return all orders for a given patient (possibly filtered by context.type)
  210. */
  211. @Override
  212. protected PageableResult doSearch(RequestContext context) throws ResponseException {
  213. String patientUuid = context.getRequest().getParameter("patient");
  214. if (patientUuid != null) {
  215. Patient patient = ((PatientResource1_8) Context.getService(RestService.class).getResourceBySupportedClass(
  216. Patient.class)).getByUniqueId(patientUuid);
  217. if (patient == null) {
  218. throw new ObjectNotFoundException();
  219. }
  220.  
  221. // if the user indicated a specific type, try to delegate to the appropriate subclass handler
  222. if (context.getType() != null) {
  223. PageableResult ret = (PageableResult) findAndInvokeSubclassHandlerMethod(context.getType(),
  224. "getActiveOrders", patient, context);
  225. if (ret != null) {
  226. return ret;
  227. }
  228. }
  229.  
  230. String careSettingUuid = context.getRequest().getParameter("careSetting");
  231. String asOfDateString = context.getRequest().getParameter("asOfDate");
  232. String orderTypeUuid = context.getRequest().getParameter("orderType");
  233. String sortParam = context.getRequest().getParameter("sort");
  234.  
  235. CareSetting careSetting = null;
  236. Date asOfDate = null;
  237. OrderType orderType = null;
  238. if (StringUtils.isNotBlank(asOfDateString)) {
  239. asOfDate = (Date) ConversionUtil.convert(asOfDateString, Date.class);
  240. }
  241. if (StringUtils.isNotBlank(careSettingUuid)) {
  242. careSetting = ((CareSettingResource1_10) Context.getService(RestService.class).getResourceBySupportedClass(
  243. CareSetting.class)).getByUniqueId(careSettingUuid);
  244. if (careSetting == null) {
  245. throw new ObjectNotFoundException();
  246. }
  247. }
  248. if (StringUtils.isNotBlank(orderTypeUuid)) {
  249. orderType = ((OrderTypeResource1_10) Context.getService(RestService.class).getResourceBySupportedClass(
  250. OrderType.class)).getByUniqueId(orderTypeUuid);
  251. if (orderType == null) {
  252. throw new ObjectNotFoundException();
  253. }
  254. }
  255.  
  256. String status = context.getRequest().getParameter("status");
  257. List<Order> orders = OrderUtil.getOrders(patient, careSetting, orderType, status, asOfDate,
  258. context.getIncludeAll());
  259. // if the user indicated a specific type, and we couldn't delegate to a subclass handler above, filter here
  260. if (context.getType() != null) {
  261. filterByType(orders, context.getType());
  262. }
  263.  
  264. if (StringUtils.isNotBlank(sortParam)) {
  265. List<Order> sortedOrder = sortOrdersBasedOnDateActivatedOrDateStopped(orders, sortParam, status);
  266. return new NeedsPaging<Order>(sortedOrder, context);
  267. } else {
  268. return new NeedsPaging<Order>(orders, context);
  269. }
  270. }
  271.  
  272. return new EmptySearchResult();
  273. }
  274.  
  275. private static Date getUsableDate(Order order) {
  276. return order.getDateStopped() != null ? order.getDateStopped() : (order.getAutoExpireDate() != null ? order
  277. .getAutoExpireDate() : order.getDateCreated());
  278. }
  279.  
  280. private static Date getActiveOrderSortDate(Order order) {
  281. return order.getDateActivated() != null ? order.getDateActivated() : order.getDateCreated();
  282. }
  283.  
  284. public List<Order> sortOrdersBasedOnDateActivatedOrDateStopped(List<Order> orders, final String sortOrder,
  285. final String status) {
  286. List<Order> sortedList = new ArrayList<Order>(orders);
  287. Collections.sort(sortedList, new Comparator<Order>() {
  288.  
  289. @Override
  290. public int compare(Order o1, Order o2) {
  291. if (status.equalsIgnoreCase("inactive")) {
  292. return sortOrder.equalsIgnoreCase("ASC") ?
  293. getUsableDate(o1).compareTo(getUsableDate(o2)) : getUsableDate(o2).compareTo(getUsableDate(o1));
  294. } else {
  295.  
  296. return sortOrder.equalsIgnoreCase("asc") ? getActiveOrderSortDate(o1).compareTo(
  297. getActiveOrderSortDate(o2)) : getActiveOrderSortDate(o2).compareTo(getActiveOrderSortDate(o1));
  298. }
  299. }
  300. });
  301. return sortedList;
  302. }
  303.  
  304. /**
  305. * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion()
  306. */
  307. @Override
  308. public String getResourceVersion() {
  309. return RestConstants1_10.RESOURCE_VERSION;
  310. }
  311.  
  312. /**
  313. * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceHandler#save(java.lang.Object)
  314. */
  315.  
  316. @Override
  317. public Order save(Order delegate) {
  318. return Context.getOrderService().saveOrder(delegate, setOrderContext(delegate));
  319. }
  320.  
  321. private OrderContext setOrderContext(Order order) {
  322. OrderContext orderContext = new OrderContext();
  323.  
  324. OrderType orderType = null;
  325.  
  326. if (orderType == null) {
  327. orderType = Context.getOrderService().getOrderTypeByConcept(order.getConcept());
  328. }
  329. if (orderType == null && order instanceof DrugOrder) {
  330. orderType = Context.getOrderService().getOrderTypeByUuid(OrderType.DRUG_ORDER_TYPE_UUID);
  331. } else if (orderType == null && order instanceof TestOrder) {
  332. orderType = Context.getOrderService().getOrderTypeByUuid(OrderType.TEST_ORDER_TYPE_UUID);
  333. } else {
  334. ////this is for checking if the order is an instance of procedure order
  335. orderType = Context.getOrderService().getOrderTypeByUuid("4237a01f-29c5-4167-9d8e-96d6e590aa33");
  336. }
  337.  
  338. orderContext.setCareSetting(null);
  339. orderContext.setOrderType(orderType);
  340. return orderContext;
  341. }
  342. }
Add Comment
Please, Sign In to add comment