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

openmrs / openmrs-core / 43293528472

12 Nov 2026 05:19PM UTC coverage: 66.969% (+0.2%) from 66.721%
43293528472

push

github

ibacher
Validate module ids and contain module paths (#6813)

38 of 42 new or added lines in 4 files covered. (90.48%)

391 existing lines in 8 files now uncovered.

26387 of 39402 relevant lines covered (66.97%)

0.67 hits per line

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

93.36
/api/src/main/java/org/openmrs/api/impl/OrderServiceImpl.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.api.impl;
11

12
import org.apache.commons.lang3.time.DateUtils;
13
import org.hibernate.proxy.HibernateProxy;
14
import org.openmrs.CareSetting;
15
import org.openmrs.Concept;
16
import org.openmrs.ConceptClass;
17
import org.openmrs.Encounter;
18
import org.openmrs.DrugOrder;
19
import org.openmrs.Order;
20
import org.openmrs.OrderAttribute;
21
import org.openmrs.OrderAttributeType;
22
import org.openmrs.OrderFrequency;
23
import org.openmrs.OrderGroup;
24
import org.openmrs.OrderType;
25
import org.openmrs.GlobalProperty;
26
import org.openmrs.Patient;
27
import org.openmrs.Provider;
28
import org.openmrs.ReferralOrder;
29
import org.openmrs.Order.FulfillerStatus;
30
import org.openmrs.OrderGroupAttribute;
31
import org.openmrs.OrderGroupAttributeType;
32
import org.openmrs.TestOrder;
33
import org.openmrs.Visit;
34
import org.openmrs.api.APIException;
35
import org.openmrs.api.AmbiguousOrderException;
36
import org.openmrs.api.CannotDeleteObjectInUseException;
37
import org.openmrs.api.CannotStopDiscontinuationOrderException;
38
import org.openmrs.api.CannotStopInactiveOrderException;
39
import org.openmrs.api.CannotUnvoidOrderException;
40
import org.openmrs.api.EditedOrderDoesNotMatchPreviousException;
41
import org.openmrs.api.GlobalPropertyListener;
42
import org.openmrs.api.MissingRequiredPropertyException;
43
import org.openmrs.api.OrderContext;
44
import org.openmrs.api.OrderEntryException;
45
import org.openmrs.api.OrderNumberGenerator;
46
import org.openmrs.api.OrderService;
47
import org.openmrs.api.UnchangeableObjectException;
48
import org.openmrs.api.context.Context;
49
import org.openmrs.api.db.OrderDAO;
50
import org.openmrs.customdatatype.CustomDatatypeUtil;
51
import org.openmrs.order.OrderUtil;
52
import org.openmrs.parameter.OrderSearchCriteria;
53
import org.openmrs.util.ConfigUtil;
54
import org.openmrs.util.OpenmrsConstants;
55
import org.openmrs.util.OpenmrsUtil;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58
import org.springframework.transaction.annotation.Propagation;
59
import org.springframework.transaction.annotation.Transactional;
60
import org.springframework.util.StringUtils;
61

62
import java.lang.reflect.Field;
63
import java.util.ArrayList;
64
import java.util.Arrays;
65
import java.util.Calendar;
66
import java.util.Collections;
67
import java.util.Date;
68
import java.util.List;
69
import java.util.Locale;
70
import java.util.Set;
71
import java.util.stream.Collectors;
72

73
import static org.openmrs.Order.Action.DISCONTINUE;
74
import static org.openmrs.Order.Action.NEW;
75
import static org.openmrs.Order.Action.REVISE;
76

77
/**
78
 * Default implementation of the Order-related services class. This method should not be invoked by
79
 * itself. Spring injection is used to inject this implementation into the ServiceContext. Which
80
 * implementation is injected is determined by the spring application context file:
81
 * /metadata/api/spring/applicationContext.xml
82
 * 
83
 * @see org.openmrs.api.OrderService
84
 */
85
@Transactional
86
public class OrderServiceImpl extends BaseOpenmrsService implements OrderService, OrderNumberGenerator, GlobalPropertyListener {
87
        
88
        private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
1 ✔
89
        
90
        private static final String ORDER_NUMBER_PREFIX = "ORD-";
91
        
92
        protected OrderDAO dao;
93
        
94
        private static OrderNumberGenerator orderNumberGenerator = null;
1 ✔
95

96
        public OrderServiceImpl() {
1 ✔
97
        }
1 ✔
98
        
99
        /**
100
         * @see org.openmrs.api.OrderService#setOrderDAO(org.openmrs.api.db.OrderDAO)
101
         */
102
        @Override
103
        public void setOrderDAO(OrderDAO dao) {
104
                this.dao = dao;
1 ✔
105
        }
1 ✔
106
        
107
        /**
108
         * @see org.openmrs.api.OrderService#saveOrder(org.openmrs.Order, org.openmrs.api.OrderContext)
109
         */
110
        @Override
111
        public synchronized Order saveOrder(Order order, OrderContext orderContext) throws APIException {
112
                return saveOrder(order, orderContext, false);
1 ✔
113
        }
114
        
115
        /**
116
         * @see org.openmrs.api.OrderService#saveOrderGroup(org.openmrs.OrderGroup)
117
         */
118
        @Override
119
        public OrderGroup saveOrderGroup(OrderGroup orderGroup) throws APIException {
120
                return saveOrderGroup(orderGroup, null);
1 ✔
121
        }
122

123
        /**
124
         * @see org.openmrs.api.OrderService#saveOrderGroup(org.openmrs.OrderGroup, org.openmrs.api.OrderContext)
125
         */
126
        @Override
127
        public OrderGroup saveOrderGroup(OrderGroup orderGroup, OrderContext orderContext) throws APIException {
128
                if (orderGroup.getId() == null) {
1 ✔
129
                        // an OrderGroup requires an encounter, which has a patient, so it
130
                        // is odd that OrderGroup has a patient field. There is no obvious
131
                        // reason why they should ever be different.
132
                        orderGroup.setPatient(orderGroup.getEncounter().getPatient());
1 ✔
133
                        CustomDatatypeUtil.saveAttributesIfNecessary(orderGroup);
1 ✔
134
                        dao.saveOrderGroup(orderGroup);
1 ✔
135
                }
136
                List<Order> orders = orderGroup.getOrders();
1 ✔
137
                for (Order order : orders) {
1 ✔
138
                        if (order.getId() == null) {
1 ✔
139
                                order.setEncounter(orderGroup.getEncounter());
1 ✔
140
                                Context.getOrderService().saveOrder(order, orderContext);
1 ✔
141
                        }
142
                }
1 ✔
143
                Set<OrderGroup> nestedGroups = orderGroup.getNestedOrderGroups();
1 ✔
144
                if (nestedGroups != null) {
1 ✔
145
                        for (OrderGroup nestedGroup : nestedGroups) {
1 ✔
146
                                Context.getOrderService().saveOrderGroup(nestedGroup, orderContext);
×
UNCOV
147
                        }
×
148
                }
149
                return orderGroup;
1 ✔
150
        }
151
        
152
        /**
153
         * @see org.openmrs.api.OrderService#saveOrder(org.openmrs.Order, org.openmrs.api.OrderContext)
154
         */
155
        @Override
156
        public synchronized Order saveRetrospectiveOrder(Order order, OrderContext orderContext) {
157
                return saveOrder(order, orderContext, true);
1 ✔
158
        }
159

160
        private Order saveOrder(Order order, OrderContext orderContext, boolean isRetrospective) {
161

162
                failOnExistingOrder(order);
1 ✔
163
                ensureDateActivatedIsSet(order);
1 ✔
164
                ensureConceptIsSet(order);
1 ✔
165
                ensureDrugOrderAutoExpirationDateIsSet(order);
1 ✔
166
                ensureOrderTypeIsSet(order,orderContext);
1 ✔
167
                ensureCareSettingIsSet(order,orderContext);
1 ✔
168
                failOnOrderTypeMismatch(order);
1 ✔
169
                
170
                // If isRetrospective is false, but the dateActivated is prior to the current date, set isRetrospective to true
171
                if (!isRetrospective) {
1 ✔
172
                        Date dateActivated = order.getDateActivated();
1 ✔
173
                        Date currentDate = new Date();
1 ✔
174
                        isRetrospective = !dateActivated.after(currentDate) && !DateUtils.isSameDay(dateActivated, currentDate);
1 ✔
175
                }
176
                
177
                Order previousOrder = order.getPreviousOrder();
1 ✔
178
                if (REVISE == order.getAction()) {
1 ✔
179
                        if (previousOrder == null) {
1 ✔
180
                                throw new MissingRequiredPropertyException("Order.previous.required", (Object[]) null);
1 ✔
181
                        }
182
                        stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
1 ✔
183
                } else if (DISCONTINUE == order.getAction()) {
1 ✔
184
                        discontinueExistingOrdersIfNecessary(order, isRetrospective);
1 ✔
185
                }
186
                
187
                if (previousOrder != null) {
1 ✔
188
                        //concept should be the same as on previous order, same applies to drug for drug orders
189
                        if (NEW != order.getAction() && !order.hasSameOrderableAs(previousOrder)) {
1 ✔
190
                                throw new EditedOrderDoesNotMatchPreviousException("Order.orderable.doesnot.match");
1 ✔
191
                        } else if (!order.getOrderType().equals(previousOrder.getOrderType())) {
1 ✔
192
                                throw new EditedOrderDoesNotMatchPreviousException("Order.type.doesnot.match");
1 ✔
193
                        } else if (!order.getCareSetting().equals(previousOrder.getCareSetting())) {
1 ✔
194
                                throw new EditedOrderDoesNotMatchPreviousException("Order.care.setting.doesnot.match");
1 ✔
195
                        } else if (!getActualType(order).equals(getActualType(previousOrder))) {
1 ✔
196
                                throw new EditedOrderDoesNotMatchPreviousException("Order.class.doesnot.match");
1 ✔
197
                        }
198
                }
199
                
200
                if (DISCONTINUE != order.getAction()) {
1 ✔
201
                        Date asOfDate = new Date();
1 ✔
202
                        if (isRetrospective) {
1 ✔
203
                                asOfDate = order.getDateActivated();
1 ✔
204
                        }
205
                        List<Order> activeOrders = getActiveOrders(order.getPatient(), null, order.getCareSetting(), asOfDate);
1 ✔
206
                        List<String> parallelOrders = Collections.emptyList();
1 ✔
207
                        if (orderContext != null && orderContext.getAttribute(PARALLEL_ORDERS) != null) {
1 ✔
208
                                parallelOrders = Arrays.asList((String[]) orderContext.getAttribute(PARALLEL_ORDERS));
1 ✔
209
                        }
210
                        for (Order activeOrder : activeOrders) {
1 ✔
211
                                //Reject if there is an active drug order for the same orderable with overlapping schedule
212
                                if (!parallelOrders.contains(activeOrder.getUuid())
1 ✔
213
                                        && areDrugOrdersOfSameOrderableAndOverlappingSchedule(order, activeOrder)) {
1 ✔
214
                                        throw new AmbiguousOrderException("Order.cannot.have.more.than.one");
1 ✔
215
                                }
216
                        }
1 ✔
217
                }
218
                return saveOrderInternal(order, orderContext);
1 ✔
219
        }
220

221
        private void failOnExistingOrder(Order order) {
222
                if (order.getOrderId() != null) {
1 ✔
223
                        throw new UnchangeableObjectException("Order.cannot.edit.existing");
1 ✔
224
                }
225
        }
1 ✔
226

227
        private void ensureDateActivatedIsSet(Order order) {
228
                if (order.getDateActivated() == null) {
1 ✔
229
                        order.setDateActivated(new Date());
1 ✔
230
                }
231
        }
1 ✔
232

233
        private void ensureConceptIsSet(Order order) {
234
                Concept concept = order.getConcept();
1 ✔
235
                if (concept == null && isDrugOrder(order)) {
1 ✔
236
                        DrugOrder drugOrder = (DrugOrder) order;
1 ✔
237
                        if (drugOrder.getDrug() != null) {
1 ✔
238
                                concept = drugOrder.getDrug().getConcept();
×
UNCOV
239
                                drugOrder.setConcept(concept);
×
240
                        } else {
241
                                if (drugOrder.isNonCodedDrug()) {
1 ✔
242
                                        concept = getNonCodedDrugConcept();
1 ✔
243
                                        drugOrder.setConcept(concept);
1 ✔
244
                                }
245
                        }
246
                }
247
                if (concept == null) {
1 ✔
UNCOV
248
                        throw new MissingRequiredPropertyException("Order.concept.required");
×
249
                }
250
        }
1 ✔
251

252
        private void ensureDrugOrderAutoExpirationDateIsSet(Order order) {
253
                if (isDrugOrder(order)) {
1 ✔
254
                        ((DrugOrder) order).setAutoExpireDateBasedOnDuration();
1 ✔
255
                }
256
        }
1 ✔
257

258
        private void ensureOrderTypeIsSet(Order order, OrderContext orderContext) {
259
                if (order.getOrderType() != null) {
1 ✔
260
                        return;
1 ✔
261
                }
262
                OrderType orderType = null;
1 ✔
263
                if (orderContext != null) {
1 ✔
264
                        orderType = orderContext.getOrderType();
1 ✔
265
                }
266
                if (orderType == null) {
1 ✔
267
                        orderType = getOrderTypeByConcept(order.getConcept());
1 ✔
268
                }
269
                if (orderType == null && order instanceof DrugOrder) {
1 ✔
270
                        orderType = getDefaultOrderType(DrugOrder.class, OrderType.DRUG_ORDER_TYPE_UUID);
1 ✔
271
                }
272
                if (orderType == null && order instanceof TestOrder) {
1 ✔
273
                        orderType = getDefaultOrderType(TestOrder.class, OrderType.TEST_ORDER_TYPE_UUID);
1 ✔
274
                }
275
                if (orderType == null && order instanceof ReferralOrder) {
1 ✔
UNCOV
276
                        orderType = getDefaultOrderType(ReferralOrder.class, OrderType.REFERRAL_ORDER_TYPE_UUID);
×
277
                }
278
                if (orderType == null) {
1 ✔
279
                        throw new OrderEntryException("Order.type.cannot.determine");
1 ✔
280
                }
281
                Order previousOrder = order.getPreviousOrder();
1 ✔
282
                if (previousOrder != null && !orderType.equals(previousOrder.getOrderType())) {
1 ✔
UNCOV
283
                        throw new OrderEntryException("Order.type.does.not.match");
×
284
                }
285
                order.setOrderType(orderType);
1 ✔
286
        }
1 ✔
287

288
        private void ensureCareSettingIsSet(Order order, OrderContext orderContext) {
289
                if (order.getCareSetting() != null) {
1 ✔
290
                        return;
1 ✔
291
                }
292
                CareSetting careSetting = null;
1 ✔
293
                if (orderContext != null) {
1 ✔
294
                        careSetting = orderContext.getCareSetting();
1 ✔
295
                }
296
                Order previousOrder = order.getPreviousOrder();
1 ✔
297
                if (careSetting == null || (previousOrder != null && !careSetting.equals(previousOrder.getCareSetting()))) {
1 ✔
UNCOV
298
                        throw new OrderEntryException("Order.care.cannot.determine");
×
299
                }
300
                order.setCareSetting(careSetting);
1 ✔
301
        }
1 ✔
302

303
        private void failOnOrderTypeMismatch(Order order) {
304
                if (!order.getOrderType().getJavaClass().isAssignableFrom(order.getClass())) {
1 ✔
305
                        throw new OrderEntryException("Order.type.class.does.not.match", new Object[] {
×
UNCOV
306
                                        order.getOrderType().getJavaClass(), order.getClass().getName() });
×
307
                }
308
        }
1 ✔
309

310
        private boolean areDrugOrdersOfSameOrderableAndOverlappingSchedule(Order firstOrder, Order secondOrder) {
311
                return firstOrder.hasSameOrderableAs(secondOrder)
1 ✔
312
                        && !OpenmrsUtil.nullSafeEquals(firstOrder.getPreviousOrder(), secondOrder)
1 ✔
313
                        && OrderUtil.checkScheduleOverlap(firstOrder, secondOrder)
1 ✔
314
                        && firstOrder.getOrderType().equals(getDefaultOrderType(DrugOrder.class, OrderType.DRUG_ORDER_TYPE_UUID));
1 ✔
315
        }
316

317
        private OrderType getDefaultOrderType(Class<? extends Order> orderSubclass, String fallbackUuid) {
318
                OrderType type = getOrderTypeByUuid(fallbackUuid);
1 ✔
319
                
320
                if (type == null) {
1 ✔
UNCOV
321
                        List<OrderType> types = getOrderTypesByClassName(orderSubclass.getName(), true, false);
×
UNCOV
322
                        if (types.size() == 1) {
×
UNCOV
323
                                type = types.get(0);
×
324
                        }
325
                }
326
                return type;
1 ✔
327
        }
328

329
        private boolean isDrugOrder(Order order) {
330
                return DrugOrder.class.isAssignableFrom(getActualType(order));
1 ✔
331
        }
332

333
        
334
        /**
335
         * To support MySQL datetime values (which are only precise to the second) we subtract one
336
         * second. Eventually we may move this method and enhance it to subtract the smallest moment the
337
         * underlying database will represent.
338
         * 
339
         * @param date
340
         * @return one moment before date
341
         */
342
        private Date aMomentBefore(Date date) {
343
                return DateUtils.addSeconds(date, -1);
1 ✔
344
        }
345
        
346
        private Order saveOrderInternal(Order order, OrderContext orderContext) {
347
                if (order.getOrderId() == null) {
1 ✔
348
                        if (order.getOrderNumber() == null) {
1 ✔
349
                                setProperty(order, "orderNumber", getOrderNumberGenerator().getNewOrderNumber(orderContext));
1 ✔
350
                        }
351
                                
352
                        //DC orders should auto expire upon creating them
353
                        if (DISCONTINUE == order.getAction()) {
1 ✔
354
                                order.setAutoExpireDate(order.getDateActivated());
1 ✔
355
                        } else if (order.getAutoExpireDate() != null) {
1 ✔
356
                                Calendar cal = Calendar.getInstance();
1 ✔
357
                                cal.setTime(order.getAutoExpireDate());
1 ✔
358
                                int hours = cal.get(Calendar.HOUR_OF_DAY);
1 ✔
359
                                int minutes = cal.get(Calendar.MINUTE);
1 ✔
360
                                int seconds = cal.get(Calendar.SECOND);
1 ✔
361
                                cal.get(Calendar.MILLISECOND);
1 ✔
362
                                //roll autoExpireDate to end of day (23:59:59) if no time portion is specified
363
                                if (hours == 0 && minutes == 0 && seconds == 0) {
1 ✔
364
                                        cal.set(Calendar.HOUR_OF_DAY, 23);
1 ✔
365
                                        cal.set(Calendar.MINUTE, 59);
1 ✔
366
                                        cal.set(Calendar.SECOND, 59);
1 ✔
367
                                        // the OpenMRS database is only precise to the second
368
                                        cal.set(Calendar.MILLISECOND, 0);
1 ✔
369
                                        order.setAutoExpireDate(cal.getTime());
1 ✔
370
                                }
371
                        }
372
                }
373
                
374
                return dao.saveOrder(order);
1 ✔
375
        }
376
        
377
        private void setProperty(Order order, String propertyName, Object value) {
378
                Boolean isAccessible = null;
1 ✔
379
                Field field = null;
1 ✔
380
                try {
381
                        field = Order.class.getDeclaredField(propertyName);
1 ✔
382
                        field.setAccessible(true);
1 ✔
383
                        field.set(order, value);
1 ✔
384
                }
UNCOV
385
                catch (Exception e) {
×
UNCOV
386
                        throw new APIException("Order.failed.set.property", new Object[] { propertyName, order }, e);
×
387
                }
388
                finally {
389
                        if (field != null && isAccessible != null) {
1 ✔
UNCOV
390
                                field.setAccessible(isAccessible);
×
391
                        }
392
                }
393
        }
1 ✔
394
        
395
        /**
396
         * Gets the configured order number generator, if none is specified, it defaults to an instance
397
         * if this class
398
         * 
399
         * @return
400
         */
401
        private OrderNumberGenerator getOrderNumberGenerator() {
402
                if (orderNumberGenerator == null) {
1 ✔
403
                        String generatorBeanId = Context.getAdministrationService().getGlobalProperty(
1 ✔
404
                            OpenmrsConstants.GP_ORDER_NUMBER_GENERATOR_BEAN_ID);
405
                        if (StringUtils.hasText(generatorBeanId)) {
1 ✔
406
                                orderNumberGenerator = Context.getRegisteredComponent(generatorBeanId, OrderNumberGenerator.class);
1 ✔
407
                                log.info("Successfully set the configured order number generator");
1 ✔
408
                        } else {
409
                                orderNumberGenerator = this;
1 ✔
410
                                log.info("Setting default order number generator");
1 ✔
411
                        }
412
                }
413
                
414
                return orderNumberGenerator;
1 ✔
415
        }
416
        
417
        /**
418
         * If this is a discontinue order, ensure that the previous order is discontinued. If a
419
         * previousOrder is present, then ensure this is discontinued. If no previousOrder is present,
420
         * then try to find a previousOrder and discontinue it. If cannot find a previousOrder, throw
421
         * exception
422
         * 
423
         * @param order
424
         * @param isRetrospective
425
         */
426
        //Ignore and return if this is not an order to discontinue
427
        private void discontinueExistingOrdersIfNecessary(Order order, Boolean isRetrospective) {
428
                if (DISCONTINUE != order.getAction()) {
1 ✔
UNCOV
429
                        return;
×
430
                }
431
                
432
                //Mark previousOrder as discontinued if it is not already
433
                Order previousOrder = order.getPreviousOrder();
1 ✔
434
                if (previousOrder != null) {
1 ✔
435
                        stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), isRetrospective);
1 ✔
436
                        return;
1 ✔
437
                }
438
                
439
                //Mark first order found corresponding to this DC order as discontinued.
440
                Date asOfDate = null;
1 ✔
441
                if (isRetrospective) {
1 ✔
442
                        asOfDate = order.getDateActivated();
1 ✔
443
                }
444
                List<? extends Order> orders = getActiveOrders(order.getPatient(), order.getOrderType(), order.getCareSetting(),
1 ✔
445
                    asOfDate);
446
                boolean isDrugOrderAndHasADrug = isDrugOrder(order)
1 ✔
447
                        && (((DrugOrder) order).getDrug() != null || ((DrugOrder) order).isNonCodedDrug());
1 ✔
448
                Order orderToBeDiscontinued = null;
1 ✔
449
                for (Order activeOrder : orders) {
1 ✔
450
                        if (!getActualType(order).equals(getActualType(activeOrder))) {
1 ✔
UNCOV
451
                                continue;
×
452
                        }
453
                        //For drug orders, the drug must match if the order has a drug
454
                        if (isDrugOrderAndHasADrug) {
1 ✔
455
                                Order existing = order.hasSameOrderableAs(activeOrder) ? activeOrder : null;
1 ✔
456
                                if (existing != null) {
1 ✔
457
                                        if (orderToBeDiscontinued == null) {
1 ✔
458
                                                orderToBeDiscontinued = existing;
1 ✔
459
                                        } else {
460
                                                throw new AmbiguousOrderException("Order.discontinuing.ambiguous.orders");
1 ✔
461
                                        }
462
                                }
463
                        } else if (activeOrder.getConcept().equals(order.getConcept())) {
1 ✔
464
                                if (orderToBeDiscontinued == null) {
1 ✔
465
                                        orderToBeDiscontinued = activeOrder;
1 ✔
466
                                } else {
467
                                        throw new AmbiguousOrderException("Order.discontinuing.ambiguous.orders");
1 ✔
468
                                }
469
                        }
470
                }
1 ✔
471
                if (orderToBeDiscontinued != null) {
1 ✔
472
                        order.setPreviousOrder(orderToBeDiscontinued);
1 ✔
473
                        stopOrder(orderToBeDiscontinued, aMomentBefore(order.getDateActivated()), isRetrospective);
1 ✔
474
                }
475
        }
1 ✔
476
        
477
        /**
478
         * Returns the class object of the specified persistent object returning the actual persistent
479
         * class in case it is a hibernate proxy
480
         *
481
         * @param persistentObject
482
         * @return the Class object
483
         */
484
        private Class<?> getActualType(Object persistentObject) {
485
                Class<?> type = persistentObject.getClass();
1 ✔
486
                if (persistentObject instanceof HibernateProxy) {
1 ✔
UNCOV
487
                        type = ((HibernateProxy) persistentObject).getHibernateLazyInitializer().getPersistentClass();
×
488
                }
489
                return type;
1 ✔
490
        }
491
        
492
        /**
493
         * @see org.openmrs.api.OrderService#purgeOrder(org.openmrs.Order)
494
         */
495
        @Override
496
        public void purgeOrder(Order order) throws APIException {
497
                purgeOrder(order, false);
1 ✔
498
        }
1 ✔
499
        
500
        /**
501
         * @see org.openmrs.api.OrderService#purgeOrder(Order)
502
         */
503
        @Override
504
        public void purgeOrder(Order order, boolean cascade) throws APIException {
505
                if (cascade) {
1 ✔
506
                        dao.deleteObsThatReference(order);
1 ✔
507
                }
508
                
509
                dao.deleteOrder(order);
1 ✔
510
        }
1 ✔
511
        
512
        /**
513
         * @see org.openmrs.api.OrderService#voidOrder(org.openmrs.Order, java.lang.String)
514
         */
515
        @Override
516
        public Order voidOrder(Order order, String voidReason) throws APIException {
517
                if (!StringUtils.hasLength(voidReason)) {
1 ✔
UNCOV
518
                        throw new IllegalArgumentException("voidReason cannot be empty or null");
×
519
                }
520
                
521
                Order previousOrder = order.getPreviousOrder();
1 ✔
522
                if (previousOrder != null && isDiscontinueOrReviseOrder(order)) {
1 ✔
523
                        setProperty(previousOrder, "dateStopped", null);
1 ✔
524
                }
525
                
526
                return saveOrderInternal(order, null);
1 ✔
527
        }
528
        
529
        /**
530
         * @see org.openmrs.api.OrderService#unvoidOrder(org.openmrs.Order)
531
         */
532
        @Override
533
        public Order unvoidOrder(Order order) throws APIException {
534
                Order previousOrder = order.getPreviousOrder();
1 ✔
535
                if (previousOrder != null && isDiscontinueOrReviseOrder(order)) {
1 ✔
536
                        if (!previousOrder.isActive()) {
1 ✔
537
                                final String action = DISCONTINUE == order.getAction() ? "discontinuation" : "revision";
1 ✔
538
                                throw new CannotUnvoidOrderException(action);
1 ✔
539
                        }
540
                        stopOrder(previousOrder, aMomentBefore(order.getDateActivated()), false);
1 ✔
541
                }
542
                
543
                return saveOrderInternal(order, null);
1 ✔
544
        }
545
        
546
        @Override
547
        public Order updateOrderFulfillerStatus(Order order, Order.FulfillerStatus orderFulfillerStatus, String fullFillerComment) {
548
                return updateOrderFulfillerStatus(order, orderFulfillerStatus, fullFillerComment, null);
1 ✔
549
        }
550

551
        @Override
552
        public Order updateOrderFulfillerStatus(Order order, FulfillerStatus orderFulfillerStatus, String fullFillerComment,
553
                                                                                        String accessionNumber) {
554

555
                if (orderFulfillerStatus != null) {
1 ✔
556
                        order.setFulfillerStatus(orderFulfillerStatus);
1 ✔
557
                }
558

559
                if (fullFillerComment != null) {
1 ✔
560
                        order.setFulfillerComment(fullFillerComment);
1 ✔
561
                }
562
        
563
                if (accessionNumber != null) {
1 ✔
564
                        order.setAccessionNumber(accessionNumber);
1 ✔
565
                }
566
                
567
                return saveOrderInternal(order, null);
1 ✔
568
        }
569
        
570
        /**
571
         * @see org.openmrs.api.OrderService#getOrder(java.lang.Integer)
572
         */
573
        @Override
574
        @Transactional(readOnly = true)
575
        public Order getOrder(Integer orderId) throws APIException {
576
                return dao.getOrder(orderId);
1 ✔
577
        }
578
        
579
        /**
580
         * @see OrderService#getOrders(org.openmrs.Patient, org.openmrs.CareSetting,
581
         *      org.openmrs.OrderType, boolean)
582
         */
583
        @Override
584
        public List<Order> getOrders(Patient patient, CareSetting careSetting, OrderType orderType, boolean includeVoided) {
585
                return this.getOrders(patient, null, careSetting, orderType, includeVoided);
1 ✔
586
        }
587
        
588
        /**
589
         * @see OrderService#getOrders(org.openmrs.Patient, org.openmrs.Visit, org.openmrs.CareSetting,
590
         *      org.openmrs.OrderType, boolean)
591
         */
592
        @Override
593
        public List<Order> getOrders(Patient patient, Visit visit, CareSetting careSetting, OrderType orderType, boolean includeVoided) {
594
                if (patient == null) {
1 ✔
595
                        throw new IllegalArgumentException("Patient is required");
1 ✔
596
                }
597
                if (careSetting == null) {
1 ✔
598
                        throw new IllegalArgumentException("CareSetting is required");
1 ✔
599
                }
600
                List<OrderType> orderTypes = null;
1 ✔
601
                if (orderType != null) {
1 ✔
602
                        orderTypes = new ArrayList<>();
1 ✔
603
                        orderTypes.add(orderType);
1 ✔
604
                        orderTypes.addAll(getSubtypes(orderType, true));
1 ✔
605
                }
606
                return dao.getOrders(patient, visit, careSetting, orderTypes, includeVoided, false);
1 ✔
607
        }
608
        
609
        /**
610
         * @see OrderService#getAllOrdersByPatient(org.openmrs.Patient)
611
         */
612
        @Override
613
        public List<Order> getAllOrdersByPatient(Patient patient) {
614
                if (patient == null) {
1 ✔
615
                        throw new IllegalArgumentException("Patient is required");
1 ✔
616
                }
617
                return dao.getOrders(patient, null, null, true, true);
1 ✔
618
        }
619
        
620
        @Override
621
        public List<Order> getOrders(OrderSearchCriteria orderSearchCriteria) {
622
                return dao.getOrders(orderSearchCriteria);
1 ✔
623
        }
624
        
625
        /**
626
         * @see org.openmrs.api.OrderService#getOrderByUuid(java.lang.String)
627
         */
628
        @Override
629
        @Transactional(readOnly = true)
630
        public Order getOrderByUuid(String uuid) throws APIException {
631
                return dao.getOrderByUuid(uuid);
1 ✔
632
        }
633
        
634
        /**
635
         * @see org.openmrs.api.OrderService#getDiscontinuationOrder(Order)
636
         */
637
        @Transactional(readOnly = true)
638
        @Override
639
        public Order getDiscontinuationOrder(Order order) throws APIException {
640
                return dao.getDiscontinuationOrder(order);
1 ✔
641
        }
642
        
643
        /**
644
         * @see org.openmrs.api.OrderService#getRevisionOrder(Order)
645
         */
646
        @Override
647
        public Order getRevisionOrder(Order order) throws APIException {
648
                return dao.getRevisionOrder(order);
1 ✔
649
        }
650
        
651
        /**
652
         * @see org.openmrs.api.OrderNumberGenerator#getNewOrderNumber(org.openmrs.api.OrderContext)
653
         * @param orderContext
654
         */
655
        @Override
656
        public String getNewOrderNumber(OrderContext orderContext) throws APIException {
657
                return ORDER_NUMBER_PREFIX + Context.getOrderService().getNextOrderNumberSeedSequenceValue();
1 ✔
658
        }
659
        
660
        /**
661
         * @see org.openmrs.api.OrderService#getOrderByOrderNumber(java.lang.String)
662
         */
663
        @Override
664
        @Transactional(readOnly = true)
665
        public Order getOrderByOrderNumber(String orderNumber) {
666
                return dao.getOrderByOrderNumber(orderNumber);
1 ✔
667
        }
668
        
669
        /**
670
         * @see org.openmrs.api.OrderService#getOrderHistoryByConcept(org.openmrs.Patient,
671
         *      org.openmrs.Concept)
672
         */
673
        @Override
674
        @Transactional(readOnly = true)
675
        public List<Order> getOrderHistoryByConcept(Patient patient, Concept concept) {
676
                if (patient == null || concept == null) {
1 ✔
677
                        throw new IllegalArgumentException("patient and concept are required");
1 ✔
678
                }
679
                List<Concept> concepts = new ArrayList<>();
1 ✔
680
                concepts.add(concept);
1 ✔
681
                
682
                List<Patient> patients = new ArrayList<>();
1 ✔
683
                patients.add(patient);
1 ✔
684
                
685
                return dao.getOrders(null, patients, concepts, new ArrayList<>(), new ArrayList<>());
1 ✔
686
        }
687
        
688
        /**
689
         * @see org.openmrs.api.OrderService#getNextOrderNumberSeedSequenceValue()
690
         */
691
        @Override
692
        @Transactional(propagation = Propagation.REQUIRES_NEW)
693
        public synchronized Long getNextOrderNumberSeedSequenceValue() {
694
                return dao.getNextOrderNumberSeedSequenceValue();
1 ✔
695
        }
696
        
697
        /**
698
         * @see org.openmrs.api.OrderService#getOrderHistoryByOrderNumber(java.lang.String)
699
         */
700
        @Override
701
        @Transactional(readOnly = true)
702
        public List<Order> getOrderHistoryByOrderNumber(String orderNumber) {
703
                List<Order> orders = new ArrayList<>();
1 ✔
704
                Order order = dao.getOrderByOrderNumber(orderNumber);
1 ✔
705
                while (order != null) {
1 ✔
706
                        orders.add(order);
1 ✔
707
                        order = order.getPreviousOrder();
1 ✔
708
                }
709
                return orders;
1 ✔
710
        }
711
        
712
        /**
713
         * @see org.openmrs.api.OrderService#getActiveOrders(org.openmrs.Patient, org.openmrs.OrderType,
714
         *      org.openmrs.CareSetting, java.util.Date)
715
         */
716
        @Override
717
        @Transactional(readOnly = true)
718
        public List<Order> getActiveOrders(Patient patient, OrderType orderType, CareSetting careSetting, Date asOfDate) {
719
                return this.getActiveOrders(patient, null, orderType, careSetting, asOfDate);
1 ✔
720
        }
721
        
722
        /**
723
         * @see org.openmrs.api.OrderService#getActiveOrders(org.openmrs.Patient, org.openmrs.Visit, org.openmrs.OrderType,
724
         *      org.openmrs.CareSetting, java.util.Date)
725
         */
726
        @Override
727
        @Transactional(readOnly = true)
728
        public List<Order> getActiveOrders(Patient patient, Visit visit, OrderType orderType, CareSetting careSetting, Date asOfDate) {
729
                if (patient == null) {
1 ✔
730
                        throw new IllegalArgumentException("Patient is required when fetching active orders");
1 ✔
731
                }
732
                if (asOfDate == null) {
1 ✔
733
                        asOfDate = new Date();
1 ✔
734
                }
735
                List<OrderType> orderTypes = null;
1 ✔
736
                if (orderType != null) {
1 ✔
737
                        orderTypes = new ArrayList<>();
1 ✔
738
                        orderTypes.add(orderType);
1 ✔
739
                        orderTypes.addAll(getSubtypes(orderType, true));
1 ✔
740
                }
741
                return dao.getActiveOrders(patient, visit, orderTypes, careSetting, asOfDate);
1 ✔
742
        }
743
        
744
        /**
745
         * @see org.openmrs.api.OrderService#getCareSetting(Integer)
746
         */
747
        @Override
748
        public CareSetting getCareSetting(Integer careSettingId) {
749
                return dao.getCareSetting(careSettingId);
1 ✔
750
        }
751
        
752
        /**
753
         * @see org.openmrs.api.OrderService#getCareSettingByUuid(String)
754
         */
755
        @Override
756
        public CareSetting getCareSettingByUuid(String uuid) {
757
                return dao.getCareSettingByUuid(uuid);
1 ✔
758
        }
759
        
760
        /**
761
         * @see org.openmrs.api.OrderService#getCareSettingByName(String)
762
         */
763
        @Override
764
        public CareSetting getCareSettingByName(String name) {
765
                return dao.getCareSettingByName(name);
1 ✔
766
        }
767
        
768
        /**
769
         * @see org.openmrs.api.OrderService#getCareSettings(boolean)
770
         */
771
        @Override
772
        public List<CareSetting> getCareSettings(boolean includeRetired) {
773
                return dao.getCareSettings(includeRetired);
1 ✔
774
        }
775
        
776
        /**
777
         * @see OrderService#getOrderTypeByName(String)
778
         */
779
        @Override
780
        public OrderType getOrderTypeByName(String orderTypeName) {
781
                return dao.getOrderTypeByName(orderTypeName);
1 ✔
782
        }
783
        
784
        /**
785
         * @see OrderService#getOrderFrequency(Integer)
786
         */
787
        @Override
788
        public OrderFrequency getOrderFrequency(Integer orderFrequencyId) {
789
                return dao.getOrderFrequency(orderFrequencyId);
1 ✔
790
        }
791
        
792
        /**
793
         * @see OrderService#getOrderFrequencyByUuid(String)
794
         */
795
        @Override
796
        public OrderFrequency getOrderFrequencyByUuid(String uuid) {
797
                return dao.getOrderFrequencyByUuid(uuid);
1 ✔
798
        }
799
        
800
        /**
801
         * @see OrderService#getOrderFrequencies(boolean)
802
         */
803
        @Override
804
        public List<OrderFrequency> getOrderFrequencies(boolean includeRetired) {
805
                return dao.getOrderFrequencies(includeRetired);
1 ✔
806
        }
807
        
808
        /**
809
         * @see OrderService#getOrderFrequencies(String, java.util.Locale, boolean, boolean)
810
         */
811
        @Override
812
        public List<OrderFrequency> getOrderFrequencies(String searchPhrase, Locale locale, boolean exactLocale,
813
                                                        boolean includeRetired) {
814
                if (searchPhrase == null) {
1 ✔
815
                        throw new IllegalArgumentException("searchPhrase is required");
1 ✔
816
                }
817
                return dao.getOrderFrequencies(searchPhrase, locale, exactLocale, includeRetired);
1 ✔
818
        }
819
        
820
        /**
821
         * @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, org.openmrs.Concept,
822
         *      java.util.Date, org.openmrs.Provider, org.openmrs.Encounter)
823
         */
824
        @Override
825
        public Order discontinueOrder(Order orderToDiscontinue, Concept reasonCoded, Date discontinueDate, Provider orderer,
826
                                      Encounter encounter) {
827
                if (discontinueDate == null) {
1 ✔
828
                        discontinueDate = aMomentBefore(new Date());
1 ✔
829
                }
830
                stopOrder(orderToDiscontinue, discontinueDate, false);
1 ✔
831
                Order newOrder = orderToDiscontinue.cloneForDiscontinuing();
1 ✔
832
                newOrder.setOrderReason(reasonCoded);
1 ✔
833
                newOrder.setOrderer(orderer);
1 ✔
834
                newOrder.setEncounter(encounter);
1 ✔
835
                newOrder.setDateActivated(discontinueDate);
1 ✔
836
                return saveOrderInternal(newOrder, null);
1 ✔
837
        }
838
        
839
        /**
840
         * @see org.openmrs.api.OrderService#discontinueOrder(org.openmrs.Order, String, java.util.Date,
841
         *      org.openmrs.Provider, org.openmrs.Encounter)
842
         */
843
        @Override
844
        public Order discontinueOrder(Order orderToDiscontinue, String reasonNonCoded, Date discontinueDate, Provider orderer,
845
                                      Encounter encounter) {
846
                if (discontinueDate == null) {
1 ✔
847
                        discontinueDate = aMomentBefore(new Date());
1 ✔
848
                }
849
                stopOrder(orderToDiscontinue, discontinueDate, false);
1 ✔
850
                Order newOrder = orderToDiscontinue.cloneForDiscontinuing();
1 ✔
851
                newOrder.setOrderReasonNonCoded(reasonNonCoded);
1 ✔
852
                newOrder.setOrderer(orderer);
1 ✔
853
                newOrder.setEncounter(encounter);
1 ✔
854
                newOrder.setDateActivated(discontinueDate);
1 ✔
855
                return saveOrderInternal(newOrder, null);
1 ✔
856
        }
857
        
858
        private boolean isDiscontinueOrReviseOrder(Order order) {
859
                return DISCONTINUE == order.getAction() || REVISE == order.getAction();
1 ✔
860
        }
861
        
862
        /**
863
         * Make necessary checks, set necessary fields for discontinuing <code>orderToDiscontinue</code>
864
         * and save.
865
         *
866
         * @param orderToStop
867
         * @param discontinueDate
868
         */
869
        private void stopOrder(Order orderToStop, Date discontinueDate, boolean isRetrospective) {
870
                if (discontinueDate == null) {
1 ✔
UNCOV
871
                        discontinueDate = new Date();
×
872
                }
873
                if (discontinueDate.after(new Date())) {
1 ✔
874
                        throw new IllegalArgumentException("Discontinue date cannot be in the future");
1 ✔
875
                }
876
                if (DISCONTINUE == orderToStop.getAction()) {
1 ✔
877
                        throw new CannotStopDiscontinuationOrderException();
1 ✔
878
                }
879

880
                if (!ConfigUtil.getProperty(OpenmrsConstants.GP_ALLOW_SETTING_STOP_DATE_ON_INACTIVE_ORDERS, false)) {
1 ✔
881
                        if (isRetrospective && orderToStop.getDateStopped() != null) {
1 ✔
882
                                throw new CannotStopInactiveOrderException();
1 ✔
883
                        }
884
                        if (!isRetrospective && !orderToStop.isActive()) {
1 ✔
885
                                throw new CannotStopInactiveOrderException();
1 ✔
886
                        } else if (isRetrospective && !orderToStop.isActive(discontinueDate)) {
1 ✔
UNCOV
887
                                throw new CannotStopInactiveOrderException();
×
888
                        }
889
                }
890
                
891
                setProperty(orderToStop, "dateStopped", discontinueDate);
1 ✔
892
                saveOrderInternal(orderToStop, null);
1 ✔
893
        }
1 ✔
894
        
895
        /**
896
         * @see org.openmrs.api.OrderService#saveOrderFrequency(org.openmrs.OrderFrequency)
897
         */
898
        @Override
899
        public OrderFrequency saveOrderFrequency(OrderFrequency orderFrequency) throws APIException {
900
                return dao.saveOrderFrequency(orderFrequency);
1 ✔
901
        }
902
        
903
        /**
904
         * @see org.openmrs.api.OrderService#retireOrderFrequency(org.openmrs.OrderFrequency,
905
         *      java.lang.String)
906
         */
907
        @Override
908
        public OrderFrequency retireOrderFrequency(OrderFrequency orderFrequency, String reason) {
909
                return dao.saveOrderFrequency(orderFrequency);
1 ✔
910
        }
911
        
912
        /**
913
         * @see org.openmrs.api.OrderService#unretireOrderFrequency(org.openmrs.OrderFrequency)
914
         */
915
        @Override
916
        public OrderFrequency unretireOrderFrequency(OrderFrequency orderFrequency) {
917
                return Context.getOrderService().saveOrderFrequency(orderFrequency);
1 ✔
918
        }
919
        
920
        /**
921
         * @see org.openmrs.api.OrderService#purgeOrderFrequency(org.openmrs.OrderFrequency)
922
         */
923
        @Override
924
        public void purgeOrderFrequency(OrderFrequency orderFrequency) {
925
                
926
                if (dao.isOrderFrequencyInUse(orderFrequency)) {
1 ✔
927
                        throw new CannotDeleteObjectInUseException("Order.frequency.cannot.delete", (Object[]) null);
1 ✔
928
                }
929
                
930
                dao.purgeOrderFrequency(orderFrequency);
1 ✔
931
        }
1 ✔
932
        
933
        /**
934
         * @see org.openmrs.api.OrderService#getOrderFrequencyByConcept(org.openmrs.Concept)
935
         */
936
        @Override
937
        @Transactional(readOnly = true)
938
        public OrderFrequency getOrderFrequencyByConcept(Concept concept) {
939
                return dao.getOrderFrequencyByConcept(concept);
1 ✔
940
        }
941
        
942
        /**
943
         * @see GlobalPropertyListener#supportsPropertyName(String)
944
         */
945
        @Override
946
        public boolean supportsPropertyName(String propertyName) {
947
                return OpenmrsConstants.GP_ORDER_NUMBER_GENERATOR_BEAN_ID.equals(propertyName);
1 ✔
948
        }
949
        
950
        /**
951
         * @see GlobalPropertyListener#globalPropertyChanged(org.openmrs.GlobalProperty)
952
         */
953
        @Override
954
        public void globalPropertyChanged(GlobalProperty newValue) {
955
                setOrderNumberGenerator(null);
1 ✔
956
        }
1 ✔
957
        
958
        /**
959
         * @see GlobalPropertyListener#globalPropertyDeleted(String)
960
         */
961
        @Override
962
        public void globalPropertyDeleted(String propertyName) {
UNCOV
963
                setOrderNumberGenerator(null);
×
UNCOV
964
        }
×
965
        
966
        /**
967
         * Helper method to deter instance methods from setting static fields
968
         */
969
        private static void setOrderNumberGenerator(OrderNumberGenerator orderNumberGenerator) {
970
                OrderServiceImpl.orderNumberGenerator = orderNumberGenerator;
1 ✔
971
        }
1 ✔
972
        
973
        /**
974
         * @see org.openmrs.api.OrderService#getOrderType(Integer)
975
         */
976
        
977
        @Override
978
        @Transactional(readOnly = true)
979
        public OrderType getOrderType(Integer orderTypeId) {
980
                return dao.getOrderType(orderTypeId);
1 ✔
981
        }
982
        
983
        /**
984
         * @see org.openmrs.api.OrderService#getOrderTypeByUuid(String)
985
         */
986
        @Override
987
        @Transactional(readOnly = true)
988
        public OrderType getOrderTypeByUuid(String uuid) {
989
                return dao.getOrderTypeByUuid(uuid);
1 ✔
990
        }
991
        
992
        /**
993
         * @see org.openmrs.api.OrderService#getOrderTypes(boolean)
994
         */
995
        @Override
996
        @Transactional(readOnly = true)
997
        public List<OrderType> getOrderTypes(boolean includeRetired) {
998
                return dao.getOrderTypes(includeRetired);
1 ✔
999
        }
1000
        
1001
        /**
1002
         * @see org.openmrs.api.OrderService#saveOrderType(org.openmrs.OrderType)
1003
         */
1004
        @Override
1005
        public OrderType saveOrderType(OrderType orderType) {
1006
                return dao.saveOrderType(orderType);
1 ✔
1007
        }
1008
        
1009
        /**
1010
         * @see org.openmrs.api.OrderService#purgeOrderType(org.openmrs.OrderType)
1011
         */
1012
        @Override
1013
        public void purgeOrderType(OrderType orderType) {
1014
                if (dao.isOrderTypeInUse(orderType)) {
1 ✔
1015
                        throw new CannotDeleteObjectInUseException("Order.type.cannot.delete", (Object[]) null);
1 ✔
1016
                }
1017
                dao.purgeOrderType(orderType);
1 ✔
1018
        }
1 ✔
1019
        
1020
        /**
1021
         * @see org.openmrs.api.OrderService#retireOrderType(org.openmrs.OrderType, String)
1022
         */
1023
        @Override
1024
        public OrderType retireOrderType(OrderType orderType, String reason) {
1025
                return saveOrderType(orderType);
1 ✔
1026
        }
1027
        
1028
        /**
1029
         * @see org.openmrs.api.OrderService#unretireOrderType(org.openmrs.OrderType)
1030
         */
1031
        @Override
1032
        public OrderType unretireOrderType(OrderType orderType) {
1033
                return saveOrderType(orderType);
1 ✔
1034
        }
1035
        
1036
        /**
1037
         * @see org.openmrs.api.OrderService#getSubtypes(org.openmrs.OrderType, boolean)
1038
         */
1039
        @Override
1040
        @Transactional(readOnly = true)
1041
        public List<OrderType> getSubtypes(OrderType orderType, boolean includeRetired) {
1042
                List<OrderType> allSubtypes = new ArrayList<>();
1 ✔
1043
                List<OrderType> immediateAncestors = dao.getOrderSubtypes(orderType, includeRetired);
1 ✔
1044
                while (!immediateAncestors.isEmpty()) {
1 ✔
1045
                        List<OrderType> ancestorsAtNextLevel = new ArrayList<>();
1 ✔
1046
                        for (OrderType type : immediateAncestors) {
1 ✔
1047
                                allSubtypes.add(type);
1 ✔
1048
                                ancestorsAtNextLevel.addAll(dao.getOrderSubtypes(type, includeRetired));
1 ✔
1049
                        }
1 ✔
1050
                        immediateAncestors = ancestorsAtNextLevel;
1 ✔
1051
                }
1 ✔
1052
                return allSubtypes;
1 ✔
1053
        }
1054
        
1055
        /**
1056
         * @see org.openmrs.api.OrderService#getOrderTypeByConceptClass(org.openmrs.ConceptClass)
1057
         */
1058
        @Override
1059
        @Transactional(readOnly = true)
1060
        public OrderType getOrderTypeByConceptClass(ConceptClass conceptClass) {
1061
                return dao.getOrderTypeByConceptClass(conceptClass);
1 ✔
1062
        }
1063
        
1064
        /**
1065
         * @see org.openmrs.api.OrderService#getOrderTypeByConcept(org.openmrs.Concept)
1066
         */
1067
        @Override
1068
        @Transactional(readOnly = true)
1069
        public OrderType getOrderTypeByConcept(Concept concept) {
1070
                return Context.getOrderService().getOrderTypeByConceptClass(concept.getConceptClass());
1 ✔
1071
        }
1072

1073
        /**
1074
         * @see org.openmrs.api.OrderService#getOrderTypesByClassName(String, boolean)
1075
         */
1076
        @Override
1077
        @Transactional(readOnly = true)
1078
        public List<OrderType> getOrderTypesByClassName(String javaClassName, boolean includeRetired) throws APIException {
1079
                return dao.getOrderTypesByClassName(javaClassName, includeRetired);
1 ✔
1080
        }
1081
        
1082
        /**
1083
         * @see org.openmrs.api.OrderService#getOrderTypesByClassName(String, boolean, boolean)
1084
         */
1085
        @Override
1086
        @Transactional(readOnly = true)
1087
        public List<OrderType> getOrderTypesByClassName(String javaClassName, boolean includeSubclasses, boolean includeRetired) throws APIException {
1088
                if (!StringUtils.hasText(javaClassName)) {
1 ✔
UNCOV
1089
                        throw new APIException("javaClassName cannot be null");
×
1090
                }
1091
                
1092
                if (!includeSubclasses) {
1 ✔
UNCOV
1093
                        return getOrderTypesByClassName(javaClassName, includeRetired);
×
1094
                }
1095

1096
                Class<?> superClass;
1097
                try {
1098
                        superClass = Context.loadClass(javaClassName);
1 ✔
UNCOV
1099
                } catch (ClassNotFoundException e) {
×
UNCOV
1100
                        throw new APIException("Invalid javaClassName: " + javaClassName, e);
×
1101
                }
1 ✔
1102

1103
                return getOrderTypes(includeRetired).stream()
1 ✔
1104
                        .filter(ot -> {
1 ✔
1105
                                try {
1106
                                        Class<?> c = Context.loadClass(ot.getJavaClassName());
1 ✔
1107
                                        return superClass.isAssignableFrom(c);
1 ✔
1108
                                } catch (Exception ignore) {
1 ✔
1109
                                        return false;
1 ✔
1110
                                }
1111
                        }).collect(Collectors.toList());
1 ✔
1112
        }
1113
        
1114
        /**
1115
         * @see org.openmrs.api.OrderService#getDrugRoutes()
1116
         */
1117
        @Override
1118
        @Transactional(readOnly = true)
1119
        public List<Concept> getDrugRoutes() {
1120
                return getSetMembersOfConceptSetFromGP(OpenmrsConstants.GP_DRUG_ROUTES_CONCEPT_UUID);
1 ✔
1121
        }
1122
        
1123
        @Override
1124
        @Transactional(readOnly = true)
1125
        public List<Concept> getDrugDosingUnits() {
1126
                return getSetMembersOfConceptSetFromGP(OpenmrsConstants.GP_DRUG_DOSING_UNITS_CONCEPT_UUID);
1 ✔
1127
        }
1128
        
1129
        @Override
1130
        @Transactional(readOnly = true)
1131
        public List<Concept> getDrugDispensingUnits() {
1132
                List<Concept> dispensingUnits = new ArrayList<>(
1 ✔
1133
                                getSetMembersOfConceptSetFromGP(OpenmrsConstants.GP_DRUG_DISPENSING_UNITS_CONCEPT_UUID));
1 ✔
1134
                for (Concept concept : getDrugDosingUnits()) {
1 ✔
1135
                        if (!dispensingUnits.contains(concept)) {
1 ✔
1136
                                dispensingUnits.add(concept);
1 ✔
1137
                        }
1138
                }
1 ✔
1139
                return dispensingUnits;
1 ✔
1140
        }
1141
        
1142
        @Override
1143
        @Transactional(readOnly = true)
1144
        public List<Concept> getDurationUnits() {
1145
                return getSetMembersOfConceptSetFromGP(OpenmrsConstants.GP_DURATION_UNITS_CONCEPT_UUID);
1 ✔
1146
        }
1147
        
1148
        /**
1149
         * @see org.openmrs.api.OrderService#getTestSpecimenSources()
1150
         */
1151
        @Override
1152
        public List<Concept> getTestSpecimenSources() {
1153
                return getSetMembersOfConceptSetFromGP(OpenmrsConstants.GP_TEST_SPECIMEN_SOURCES_CONCEPT_UUID);
1 ✔
1154
        }
1155
        
1156
        @Override
1157
        public Concept getNonCodedDrugConcept() {
1158
                String conceptUuid = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_DRUG_ORDER_DRUG_OTHER);
1 ✔
1159
                if (StringUtils.hasText(conceptUuid)) {
1 ✔
1160
                        return Context.getConceptService().getConceptByUuid(conceptUuid);
1 ✔
1161
                }
1162
                return null;
1 ✔
1163
        }
1164
        
1165
        @Override
1166
        @Transactional(readOnly = true)
1167
        public OrderGroup getOrderGroupByUuid(String uuid) throws APIException {
1168
                return dao.getOrderGroupByUuid(uuid);
1 ✔
1169
        }
1170
        
1171
        @Override
1172
        @Transactional(readOnly = true)
1173
        public OrderGroup getOrderGroup(Integer orderGroupId) throws APIException {
1174
                return dao.getOrderGroupById(orderGroupId);
1 ✔
1175
        }
1176
        
1177
        private List<Concept> getSetMembersOfConceptSetFromGP(String globalProperty) {
1178
                String conceptUuid = Context.getAdministrationService().getGlobalProperty(globalProperty);
1 ✔
1179
                Concept concept = Context.getConceptService().getConceptByUuid(conceptUuid);
1 ✔
1180
                if (concept != null && concept.getSet()) {
1 ✔
1181
                        return concept.getSetMembers();
1 ✔
1182
                }
1183
                return Collections.emptyList();
1 ✔
1184
        }
1185
        @Override
1186
        public List<OrderGroup> getOrderGroupsByPatient(Patient patient) throws APIException {
1187
                return dao.getOrderGroupsByPatient(patient);
1 ✔
1188
        }
1189

1190
        @Override
1191
        public List<OrderGroup> getOrderGroupsByEncounter(Encounter encounter) throws APIException {
1192
                return dao.getOrderGroupsByEncounter(encounter);
1 ✔
1193
        }
1194
        
1195
        /**
1196
         * @see org.openmrs.api.OrderService#getOrderGroupAttributeTypes()
1197
         */
1198
        @Override
1199
        @Transactional(readOnly = true)
1200
        public List<OrderGroupAttributeType> getAllOrderGroupAttributeTypes() throws APIException {
1201
                return dao.getAllOrderGroupAttributeTypes();
1 ✔
1202
        }
1203
        
1204
        /**
1205
         * @see org.openmrs.api.OrderService#getOrderGroupAttributeTypeById()
1206
         */
1207
        @Override
1208
        @Transactional(readOnly = true)
1209
        public OrderGroupAttributeType getOrderGroupAttributeType(Integer id) throws APIException {
1210
                return dao.getOrderGroupAttributeType(id);
1 ✔
1211
        }
1212
        
1213
        /**
1214
         * @see org.openmrs.api.OrderService#getOrderGroupAttributeTypeByUuid()
1215
         */
1216
        @Override
1217
        @Transactional(readOnly = true)
1218
        public OrderGroupAttributeType getOrderGroupAttributeTypeByUuid(String uuid)throws APIException {
1219
                return dao.getOrderGroupAttributeTypeByUuid(uuid);
1 ✔
1220
        }
1221
        
1222
        /**
1223
         * @see org.openmrs.api.OrderService#saveOrderGroupAttributeType()
1224
         */
1225
        @Override
1226
        public OrderGroupAttributeType saveOrderGroupAttributeType(OrderGroupAttributeType orderGroupAttributeType) throws APIException{
1227
                return dao.saveOrderGroupAttributeType(orderGroupAttributeType);
1 ✔
1228
        }
1229

1230
        /**
1231
         * @see org.openmrs.api.OrderService#retireOrderGroupAttributeType()
1232
         */
1233
        @Override
1234
        public OrderGroupAttributeType retireOrderGroupAttributeType(OrderGroupAttributeType orderGroupAttributeType, String reason)throws APIException {
1235
                return Context.getOrderService().saveOrderGroupAttributeType(orderGroupAttributeType);
1 ✔
1236
        }
1237

1238
        /**
1239
         * @see org.openmrs.api.OrderService#unretireOrderGroupAttributeType()
1240
         */
1241
        @Override
1242
        public OrderGroupAttributeType unretireOrderGroupAttributeType(OrderGroupAttributeType orderGroupAttributeType)throws APIException {
1243
                return Context.getOrderService().saveOrderGroupAttributeType(orderGroupAttributeType);
1 ✔
1244
        }
1245

1246
        /**
1247
         * @see org.openmrs.api.OrderService#purgeOrderGroupAttributeType()
1248
         */
1249
        @Override
1250
        public void purgeOrderGroupAttributeType(OrderGroupAttributeType orderGroupAttributeType) throws APIException{
1251
                dao.deleteOrderGroupAttributeType(orderGroupAttributeType);
1 ✔
1252
        }
1 ✔
1253

1254
        /**
1255
         * @see org.openmrs.api.OrderService#getOrderGroupAttributeTypeByName()
1256
         */
1257
        @Override
1258
        @Transactional(readOnly = true)
1259
        public OrderGroupAttributeType getOrderGroupAttributeTypeByName(String orderGroupAttributeTypeName)throws APIException {
1260
                return dao.getOrderGroupAttributeTypeByName(orderGroupAttributeTypeName);
1 ✔
1261
        }
1262

1263
        /**
1264
         * @see org.openmrs.api.OrderService#getOrderGroupAttributeByUuid()
1265
         */
1266
        @Override
1267
        @Transactional(readOnly = true)
1268
        public OrderGroupAttribute getOrderGroupAttributeByUuid(String uuid)throws APIException {
1269
                return dao.getOrderGroupAttributeByUuid(uuid);
1 ✔
1270
        }
1271

1272
        /**
1273
         * @see org.openmrs.api.OrderService#getAllOrderAttributeTypes()
1274
         */
1275
        @Override
1276
        @Transactional(readOnly = true)
1277
        public List<OrderAttributeType> getAllOrderAttributeTypes() throws APIException {
1278
                return dao.getAllOrderAttributeTypes();
1 ✔
1279
        }
1280

1281
        /**
1282
         * @see org.openmrs.api.OrderService#getOrderAttributeTypeById(Integer)
1283
         */
1284
        @Override
1285
        @Transactional(readOnly = true)
1286
        public OrderAttributeType getOrderAttributeTypeById(Integer id) throws APIException {
1287
                return dao.getOrderAttributeTypeById(id);
1 ✔
1288
        }
1289

1290
        /**
1291
         * @see org.openmrs.api.OrderService#getOrderAttributeTypeByUuid(String)
1292
         */
1293
        @Override
1294
        @Transactional(readOnly = true)
1295
        public OrderAttributeType getOrderAttributeTypeByUuid(String uuid)throws APIException {
1296
                return dao.getOrderAttributeTypeByUuid(uuid);
1 ✔
1297
        }
1298

1299
        /**
1300
         * @see org.openmrs.api.OrderService#saveOrderAttributeType(OrderAttributeType)
1301
         */
1302
        @Override
1303
        public OrderAttributeType saveOrderAttributeType(OrderAttributeType orderAttributeType) throws APIException{
1304
                return dao.saveOrderAttributeType(orderAttributeType);
1 ✔
1305
        }
1306

1307
        /**
1308
         * @see org.openmrs.api.OrderService#retireOrderAttributeType(OrderAttributeType)
1309
         */
1310
        @Override
1311
        public OrderAttributeType retireOrderAttributeType(OrderAttributeType orderAttributeType, String reason)throws APIException {
1312
                return Context.getOrderService().saveOrderAttributeType(orderAttributeType);
1 ✔
1313
        }
1314

1315
        /**
1316
         * @see org.openmrs.api.OrderService#unretireOrderAttributeType(OrderAttributeType)
1317
         */
1318
        @Override
1319
        public OrderAttributeType unretireOrderAttributeType(OrderAttributeType orderAttributeType)throws APIException {
1320
                return Context.getOrderService().saveOrderAttributeType(orderAttributeType);
1 ✔
1321
        }
1322

1323
        /**
1324
         * @see org.openmrs.api.OrderService#purgeOrderAttributeType(OrderAttributeType)
1325
         */
1326
        @Override
1327
        public void purgeOrderAttributeType(OrderAttributeType orderAttributeType) throws APIException{
1328
                dao.deleteOrderAttributeType(orderAttributeType);
1 ✔
1329
        }
1 ✔
1330

1331
        /**
1332
         * @see org.openmrs.api.OrderService#getOrderAttributeTypeByName(String)
1333
         */
1334
        @Override
1335
        @Transactional(readOnly = true)
1336
        public OrderAttributeType getOrderAttributeTypeByName(String orderAttributeTypeName)throws APIException {
1337
                return dao.getOrderAttributeTypeByName(orderAttributeTypeName);
1 ✔
1338
        }
1339

1340
        /**
1341
         * @see org.openmrs.api.OrderService#getOrderAttributeByUuid(String)
1342
         */
1343
        @Override
1344
        @Transactional(readOnly = true)
1345
        public OrderAttribute getOrderAttributeByUuid(String uuid)throws APIException {
1346
                return dao.getOrderAttributeByUuid(uuid);
1 ✔
1347
        }
1348
}
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